Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update lookup.rs #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 19 additions & 5 deletions src/lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ pub struct LookupState
{
db: Database,
hasher: NickHasher,
sync_hash_creator: CacheDb<Box<str>, Option<hash>>
}

impl LookupState
{
pub fn new(db: Database, hasher: NickHasher) -> Self
{
Self { db, hasher }
let sync_hash_creator = CacheDb::new().with_constructor({
let hasher = hasher.clone();
move |nick| {Some(hasher.create_nick_hash(nick)) }
});
Self { db, hasher, sync_hash_creator }
}

pub async fn lookup(&self, nick: &str) -> Result<bool, LookupError>
Expand Down Expand Up @@ -50,9 +55,18 @@ impl LookupState
}
else
{
let hash = self.hasher.create_nick_hash(nick)?;
self.db.add_hash(index, &hash).await?;
Ok(false)
let mut hash = self.sync_hash_creator.get_mut(Blocking, nick)?;

if hash.is_some() {
// the cachedb created a hash for us, lets store it
self.db.add_hash(index, &hash).await?;
// and invalidate it!
hash = None;
Ok(false)
} else {
// we waited, and got an ivalidated hash, what now? (this means it is inserted in the db unless an error happend there..)
Ok(true) // ??
}
}
}
}
}