Skip to content

Commit

Permalink
DbWriteCache: do not cache items read from the database (the underlyi…
Browse files Browse the repository at this point in the history
…ng db already has a cache)
  • Loading branch information
sabledb-io committed Apr 11, 2024
1 parent a6657f7 commit 8790927
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 24 deletions.
2 changes: 1 addition & 1 deletion libsabledb/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ impl Client {
}
},
Err(e) => {
client_state.error(&format!(
client_state.warn(&format!(
"failed to process command: {:?} error: {:?}",
command, e
));
Expand Down
1 change: 0 additions & 1 deletion libsabledb/src/storage/hash_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ pub struct HashDb<'a> {
/// This class handles String command database access
store: &'a StorageAdapter,
db_id: u16,
#[allow(dead_code)]
cache: Box<DbWriteCache<'a>>,
}

Expand Down
28 changes: 6 additions & 22 deletions libsabledb/src/storage/write_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@ use std::rc::Rc;

struct CacheEntry {
data: BytesMut,
is_dirty: bool,
#[allow(dead_code)]
flags: PutFlags,
}

impl CacheEntry {
pub fn new(data: BytesMut, is_dirty: bool, flags: PutFlags) -> Self {
CacheEntry {
data,
is_dirty,
flags,
}
pub fn new(data: BytesMut, flags: PutFlags) -> Self {
CacheEntry { data, flags }
}
}

Expand Down Expand Up @@ -44,7 +39,7 @@ impl<'a> DbWriteCache<'a> {
}

pub fn put(&self, key: &BytesMut, value: BytesMut) -> Result<(), SableError> {
let entry = Rc::new(CacheEntry::new(value, true, PutFlags::Override));
let entry = Rc::new(CacheEntry::new(value, PutFlags::Override));
let _ = self.changes.insert(key.clone(), Some(entry));
Ok(())
}
Expand All @@ -55,7 +50,7 @@ impl<'a> DbWriteCache<'a> {
value: BytesMut,
flags: PutFlags,
) -> Result<(), SableError> {
let entry = Rc::new(CacheEntry::new(value, true, flags));
let entry = Rc::new(CacheEntry::new(value, flags));
let _ = self.changes.insert(key.clone(), Some(entry));
Ok(())
}
Expand All @@ -82,14 +77,7 @@ impl<'a> DbWriteCache<'a> {
/// this means that it was deleted, so return a `None` as well
pub fn get(&self, key: &BytesMut) -> Result<Option<BytesMut>, SableError> {
let Some(value) = self.changes.get(key) else {
// No such entry
if let Some(value) = self.store.get(key)? {
// update the cache
let entry = Rc::new(CacheEntry::new(value, false, PutFlags::Override));
self.changes.insert(key.clone(), Some(entry.clone()));
return Ok(Some(entry.data.clone()));
}
return Ok(None);
return self.store.get(key);
};

// found an match in cache
Expand All @@ -107,11 +95,7 @@ impl<'a> DbWriteCache<'a> {
for entry in &self.changes {
match entry.value() {
None => batch_update.delete(entry.key().clone()),
Some(value) => {
if value.is_dirty {
batch_update.put(entry.key().clone(), value.data.clone());
}
}
Some(value) => batch_update.put(entry.key().clone(), value.data.clone()),
}
}
batch_update
Expand Down

0 comments on commit 8790927

Please sign in to comment.