Skip to content

Commit

Permalink
fix account already existing and search engine using deckid for userid
Browse files Browse the repository at this point in the history
  • Loading branch information
andychenbruce committed Mar 13, 2024
1 parent d610181 commit 64c7544
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
2 changes: 1 addition & 1 deletion backend/crates/server_backend/src/andy_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub enum AndyError {
#[error("nonexistant user")]
UserDoesNotExist,
#[error("deck does not exist")]
DeckDoesNotExist,
DeckDoesNotExist((u32, u32)),
#[error("favorite does not exist")]
FavoriteDoesNotExist,
#[error("favorite already exist")]
Expand Down
44 changes: 34 additions & 10 deletions backend/crates/server_backend/src/server/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ impl Database {
let write_txn = self.db.begin_write()?;
{
let mut table = write_txn.open_table(Self::USERS_TABLE)?;
table.get(user_id)?.ok_or(AndyError::UserAlreadyExist)?;
if table.get(user_id)?.is_some() {
return Err(AndyError::UserAlreadyExist);
}
table.insert(
user_id,
UserEntry {
Expand Down Expand Up @@ -220,7 +222,7 @@ impl Database {
let table = read_txn.open_table(Self::DECKS_TABLE)?;
let deck = table
.get((user_id, deck_id))?
.ok_or(AndyError::DeckDoesNotExist)?
.ok_or(AndyError::DeckDoesNotExist((user_id, deck_id)))?
.value();

Ok(api_structs::CardDeck {
Expand Down Expand Up @@ -254,7 +256,10 @@ impl Database {

let read_txn = self.db.begin_read()?;
let table = read_txn.open_table(Self::DECKS_TABLE)?;
let mut deck = table.get(key)?.ok_or(AndyError::DeckDoesNotExist)?.value();
let mut deck = table
.get(key)?
.ok_or(AndyError::DeckDoesNotExist(key))?
.value();
deck.rating = ((deck.rating * (deck.num_ratings as f32)) + new_rating)
/ ((deck.num_ratings + 1) as f32);

Expand All @@ -276,7 +281,10 @@ impl Database {

let read_txn = self.db.begin_read()?;
let table = read_txn.open_table(Self::DECKS_TABLE)?;
let mut deck = table.get(key)?.ok_or(AndyError::DeckDoesNotExist)?.value();
let mut deck = table
.get(key)?
.ok_or(AndyError::DeckDoesNotExist(key))?
.value();
deck.cards.push(Card { question, answer });

self.insert_or_replace(key, deck, Self::DECKS_TABLE)?;
Expand All @@ -293,7 +301,10 @@ impl Database {

let read_txn = self.db.begin_read()?;
let table = read_txn.open_table(Self::DECKS_TABLE)?;
let mut deck = table.get(key)?.ok_or(AndyError::DeckDoesNotExist)?.value();
let mut deck = table
.get(key)?
.ok_or(AndyError::DeckDoesNotExist(key))?
.value();
if deck.cards.len() <= (card_index as usize) {
return Err(AndyError::CardIndexOutOfBounds);
}
Expand All @@ -315,7 +326,10 @@ impl Database {

let read_txn = self.db.begin_read()?;
let table = read_txn.open_table(Self::DECKS_TABLE)?;
let mut deck = table.get(key)?.ok_or(AndyError::DeckDoesNotExist)?.value();
let mut deck = table
.get(key)?
.ok_or(AndyError::DeckDoesNotExist(key))?
.value();
let card = deck
.cards
.get_mut(card_index as usize)
Expand Down Expand Up @@ -364,7 +378,10 @@ impl Database {

let read_txn = self.db.begin_read()?;
let table = read_txn.open_table(Self::DECKS_TABLE)?;
let deck = table.get(key)?.ok_or(AndyError::DeckDoesNotExist)?.value();
let deck = table
.get(key)?
.ok_or(AndyError::DeckDoesNotExist(key))?
.value();

Ok(api_structs::ListCardsResponse {
cards: deck
Expand Down Expand Up @@ -427,7 +444,10 @@ impl Database {

let read_txn = self.db.begin_read()?;
let table = read_txn.open_table(Self::DECKS_TABLE)?;
let mut deck = table.get(key)?.ok_or(AndyError::DeckDoesNotExist)?.value();
let mut deck = table
.get(key)?
.ok_or(AndyError::DeckDoesNotExist(key))?
.value();
deck.icon_num = icon_num;
self.insert_or_replace(key, deck, Self::DECKS_TABLE)?;

Expand Down Expand Up @@ -515,7 +535,9 @@ impl Database {
let read_txn = self.db.begin_read()?;
{
let table = read_txn.open_table(Self::DECKS_TABLE)?;
table.get(id_pair)?.ok_or(AndyError::DeckDoesNotExist)?;
table
.get(id_pair)?
.ok_or(AndyError::DeckDoesNotExist(id_pair))?;
}

let table = read_txn.open_table(Self::USERS_TABLE)?;
Expand All @@ -541,7 +563,9 @@ impl Database {
let read_txn = self.db.begin_read()?;
{
let table = read_txn.open_table(Self::DECKS_TABLE)?;
table.get(id_pair)?.ok_or(AndyError::DeckDoesNotExist)?;
table
.get(id_pair)?
.ok_or(AndyError::DeckDoesNotExist(id_pair))?;
}

let table = read_txn.open_table(Self::USERS_TABLE)?;
Expand Down
1 change: 0 additions & 1 deletion backend/crates/server_backend/src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ async fn search(
.into_iter()
.map(|(user_id, deck_id)| state.database.get_deck_info(user_id, deck_id))
.collect::<Result<_, AndyError>>()?;

Ok(api_structs::SearchDecksResponse { decks })
} else {
//send dummy data
Expand Down
8 changes: 6 additions & 2 deletions backend/crates/server_backend/src/server/search_engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ impl SearchEngine {
id: (super::database::UserId, super::database::DeckId),
cards: Vec<super::database::Card>,
) -> Result<(), SearchEngineError> {
if cards.is_empty() {
return Ok(());
}
let payload: Payload = json!({
Self::FIELD_USER_ID: id.0,
Self::FIELD_DECK_ID: id.1
Expand All @@ -89,7 +92,7 @@ impl SearchEngine {
let sentences: Vec<_> = cards.into_iter().map(format_card).collect();

let vectors = self.get_embedder()?.run(sentences)?;

println!("adding {} vectors", vectors.len());
let points: Vec<_> = vectors
.into_iter()
.enumerate()
Expand Down Expand Up @@ -279,7 +282,7 @@ impl SearchEngine {
) -> Result<u32, SearchEngineError> {
match point
.payload
.get(Self::FIELD_DECK_ID)
.get(field)
.ok_or(SearchEngineError::PayloadFieldMissing(field))?
.kind
.as_ref()
Expand Down Expand Up @@ -307,6 +310,7 @@ async fn loop_inside(resources: &super::SharedState) -> Result<(), crate::AndyEr
let decks = resources.database.get_all_decks()?;

for deck in decks {
println!("adding deck ids: {:?} info {:?}", deck.0, deck.1.name);
resources
.search_engine
.lock()
Expand Down

0 comments on commit 64c7544

Please sign in to comment.