Skip to content

Commit

Permalink
Merge pull request #85 from carlsverre/main
Browse files Browse the repository at this point in the history
better zero copy support from types that implement Into<Slice>
  • Loading branch information
marvin-j97 authored Dec 14, 2024
2 parents 122c77c + ec3e1b9 commit 296e64f
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 21 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ quick_cache = { version = "0.6.5", default-features = false, features = [] }
rustc-hash = "2.0.0"
self_cell = "1.0.4"
tempfile = "3.12.0"
value-log = "1.3.0"
value-log = "1.4.0"
varint-rs = "2.2.0"
xxhash-rust = { version = "0.8.12", features = ["xxh3"] }

Expand Down
13 changes: 9 additions & 4 deletions src/abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -559,10 +559,15 @@ pub trait AbstractTree {
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn insert<K: AsRef<[u8]>, V: AsRef<[u8]>>(&self, key: K, value: V, seqno: SeqNo) -> (u32, u32);
fn insert<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
value: V,
seqno: SeqNo,
) -> (u32, u32);

/// Inserts a key-value pair.
fn raw_insert_with_lock<K: AsRef<[u8]>, V: AsRef<[u8]>>(
fn raw_insert_with_lock<K: Into<UserKey>, V: Into<UserValue>>(
&self,
lock: &RwLockWriteGuard<'_, Memtable>,
key: K,
Expand Down Expand Up @@ -598,7 +603,7 @@ pub trait AbstractTree {
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn remove<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> (u32, u32);
fn remove<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u32, u32);

/// Removes an item from the tree.
///
Expand Down Expand Up @@ -632,5 +637,5 @@ pub trait AbstractTree {
/// # Errors
///
/// Will return `Err` if an IO error occurs.
fn remove_weak<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> (u32, u32);
fn remove_weak<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u32, u32);
}
19 changes: 12 additions & 7 deletions src/blob_tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ impl AbstractTree for BlobTree {
)
}

fn raw_insert_with_lock<K: AsRef<[u8]>, V: AsRef<[u8]>>(
fn raw_insert_with_lock<K: Into<UserKey>, V: Into<UserValue>>(
&self,
lock: &RwLockWriteGuard<'_, Memtable>,
key: K,
Expand All @@ -609,21 +609,26 @@ impl AbstractTree for BlobTree {
// NOTE: Initially, we always write an inline value
// On memtable flush, depending on the values' sizes, they will be separated
// into inline or indirect values
let item = MaybeInlineValue::Inline(value.as_ref().into());
let item = MaybeInlineValue::Inline(value.into());

let value = item.encode_into_vec();

let value = InternalValue::from_components(key.as_ref(), value, seqno, r#type);
let value = InternalValue::from_components(key, value, seqno, r#type);
lock.insert(value)
}

fn insert<K: AsRef<[u8]>, V: AsRef<[u8]>>(&self, key: K, value: V, seqno: SeqNo) -> (u32, u32) {
fn insert<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
value: V,
seqno: SeqNo,
) -> (u32, u32) {
use value::MaybeInlineValue;

// NOTE: Initially, we always write an inline value
// On memtable flush, depending on the values' sizes, they will be separated
// into inline or indirect values
let item = MaybeInlineValue::Inline(value.as_ref().into());
let item = MaybeInlineValue::Inline(value.into());

let value = item.encode_into_vec();

Expand Down Expand Up @@ -680,11 +685,11 @@ impl AbstractTree for BlobTree {
}
}

fn remove<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
fn remove<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
self.index.remove(key, seqno)
}

fn remove_weak<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
fn remove_weak<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
self.index.remove_weak(key, seqno)
}
}
22 changes: 13 additions & 9 deletions src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,31 +404,35 @@ impl AbstractTree for Tree {
Box::new(self.create_prefix(prefix, None, None))
}

fn insert<K: AsRef<[u8]>, V: AsRef<[u8]>>(&self, key: K, value: V, seqno: SeqNo) -> (u32, u32) {
let value =
InternalValue::from_components(key.as_ref(), value.as_ref(), seqno, ValueType::Value);
fn insert<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
value: V,
seqno: SeqNo,
) -> (u32, u32) {
let value = InternalValue::from_components(key, value, seqno, ValueType::Value);
self.append_entry(value)
}

fn raw_insert_with_lock<K: AsRef<[u8]>, V: AsRef<[u8]>>(
fn raw_insert_with_lock<K: Into<UserKey>, V: Into<UserValue>>(
&self,
lock: &RwLockWriteGuard<'_, Memtable>,
key: K,
value: V,
seqno: SeqNo,
r#type: ValueType,
) -> (u32, u32) {
let value = InternalValue::from_components(key.as_ref(), value.as_ref(), seqno, r#type);
let value = InternalValue::from_components(key, value, seqno, r#type);
lock.insert(value)
}

fn remove<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
let value = InternalValue::new_tombstone(key.as_ref(), seqno);
fn remove<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
let value = InternalValue::new_tombstone(key, seqno);
self.append_entry(value)
}

fn remove_weak<K: AsRef<[u8]>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
let value = InternalValue::new_weak_tombstone(key.as_ref(), seqno);
fn remove_weak<K: Into<UserKey>>(&self, key: K, seqno: SeqNo) -> (u32, u32) {
let value = InternalValue::new_weak_tombstone(key, seqno);
self.append_entry(value)
}
}
Expand Down

0 comments on commit 296e64f

Please sign in to comment.