pub struct Keyspace(/* private fields */);Expand description
Handle to a keyspace
Each keyspace is backed by an LSM-tree to provide a disk-backed search tree, and can be configured individually.
A keyspace generally only takes a little bit of memory and disk space, but does not spawn its own background threads.
As long as a handle to a keyspace is held, its folder is not cleaned up from disk in case it is deleted from another thread.
Implementations§
Source§impl Keyspace
impl Keyspace
Sourcepub fn fragmented_blob_bytes(&self) -> u64
pub fn fragmented_blob_bytes(&self) -> u64
Returns the number of blob bytes on disk that are not referenced.
These will be reclaimed over time by blob garbage collection automatically.
Sourcepub fn start_ingestion(&self) -> Result<Ingestion<'_>>
pub fn start_ingestion(&self) -> Result<Ingestion<'_>>
Sourcepub fn disk_space(&self) -> u64
pub fn disk_space(&self) -> u64
Sourcepub fn iter(&self) -> Iter ⓘ
pub fn iter(&self) -> Iter ⓘ
Returns an iterator that scans through the entire keyspace.
Avoid using this function, or limit it as otherwise it may scan a lot of items.
§Examples
tree.insert("a", "abc")?;
tree.insert("f", "abc")?;
tree.insert("g", "abc")?;
assert_eq!(3, tree.iter().count());Sourcepub fn range<K: AsRef<[u8]>, R: RangeBounds<K>>(&self, range: R) -> Iter ⓘ
pub fn range<K: AsRef<[u8]>, R: RangeBounds<K>>(&self, range: R) -> Iter ⓘ
Returns an iterator over a range of items.
Avoid using full or unbounded ranges as they may scan a lot of items (unless limited).
§Examples
tree.insert("a", "abc")?;
tree.insert("f", "abc")?;
tree.insert("g", "abc")?;
assert_eq!(2, tree.range("a"..="f").count());Sourcepub fn prefix<K: AsRef<[u8]>>(&self, prefix: K) -> Iter ⓘ
pub fn prefix<K: AsRef<[u8]>>(&self, prefix: K) -> Iter ⓘ
Returns an iterator over a prefixed set of items.
Avoid using an empty prefix as it may scan a lot of items (unless limited).
§Examples
tree.insert("a", "abc")?;
tree.insert("ab", "abc")?;
tree.insert("abc", "abc")?;
assert_eq!(2, tree.prefix("ab").count());Sourcepub fn approximate_len(&self) -> usize
pub fn approximate_len(&self) -> usize
Approximates the amount of items in the keyspace.
For update- or delete-heavy workloads, this value will diverge from the real value, but is a O(1) operation.
For insert-only workloads (e.g. logs, time series) this value is reliable.
§Examples
assert_eq!(tree.approximate_len(), 0);
tree.insert("1", "abc")?;
assert_eq!(tree.approximate_len(), 1);
tree.remove("1")?;
// Oops! approximate_len will not be reliable here
assert_eq!(tree.approximate_len(), 2);Sourcepub fn len(&self) -> Result<usize>
pub fn len(&self) -> Result<usize>
Scans the entire keyspace, returning the amount of items.
§Caution
This operation scans the entire keyspace: O(n) complexity!
Never, under any circumstances, use .len() == 0 to check
if the keyspace is empty, use Keyspace::is_empty instead.
If you want an estimate, use Keyspace::approximate_len instead.
§Examples
assert_eq!(tree.len()?, 0);
tree.insert("1", "abc")?;
tree.insert("3", "abc")?;
tree.insert("5", "abc")?;
assert_eq!(tree.len()?, 3);§Errors
Will return Err if an IO error occurs.
Sourcepub fn first_key_value(&self) -> Option<Guard>
pub fn first_key_value(&self) -> Option<Guard>
Returns the first key-value pair in the keyspace. The key in this pair is the minimum key in the keyspace.
§Examples
tree.insert("1", "abc")?;
tree.insert("3", "abc")?;
tree.insert("5", "abc")?;
let key = tree.first_key_value().expect("item should exist").key()?;
assert_eq!(&*key, "1".as_bytes());§Errors
Will return Err if an IO error occurs.
Sourcepub fn last_key_value(&self) -> Option<Guard>
pub fn last_key_value(&self) -> Option<Guard>
Returns the last key-value pair in the keyspace. The key in this pair is the maximum key in the keyspace.
§Examples
tree.insert("1", "abc")?;
tree.insert("3", "abc")?;
tree.insert("5", "abc")?;
let key = tree.last_key_value().expect("item should exist").key()?;
assert_eq!(&*key, "5".as_bytes());§Errors
Will return Err if an IO error occurs.
Sourcepub fn is_kv_separated(&self) -> bool
pub fn is_kv_separated(&self) -> bool
Returns true if the underlying LSM-tree is key-value-separated.
Sourcepub fn insert<K: Into<UserKey>, V: Into<UserValue>>(
&self,
key: K,
value: V,
) -> Result<()>
pub fn insert<K: Into<UserKey>, V: Into<UserValue>>( &self, key: K, value: V, ) -> Result<()>
Inserts a key-value pair into the keyspace.
Keys may be up to 65536 bytes long, values up to 2^32 bytes. Shorter keys and values result in better performance.
If the key already exists, the item will be overwritten.
§Examples
tree.insert("a", "abc")?;
assert!(!tree.is_empty()?);§Errors
Will return Err if an IO error occurs.
Sourcepub fn remove<K: Into<UserKey>>(&self, key: K) -> Result<()>
pub fn remove<K: Into<UserKey>>(&self, key: K) -> Result<()>
Removes an item from the keyspace.
The key may be up to 65536 bytes long. Shorter keys result in better performance.
§Examples
tree.insert("a", "abc")?;
let item = tree.get("a")?.expect("should have item");
assert_eq!("abc".as_bytes(), &*item);
tree.remove("a")?;
let item = tree.get("a")?;
assert_eq!(None, item);§Errors
Will return Err if an IO error occurs.
Trait Implementations§
Source§impl AsRef<Keyspace> for OptimisticTxKeyspace
impl AsRef<Keyspace> for OptimisticTxKeyspace
Source§impl AsRef<Keyspace> for SingleWriterTxKeyspace
impl AsRef<Keyspace> for SingleWriterTxKeyspace
impl Eq for Keyspace
Auto Trait Implementations§
impl Freeze for Keyspace
impl !RefUnwindSafe for Keyspace
impl Send for Keyspace
impl Sync for Keyspace
impl Unpin for Keyspace
impl !UnwindSafe for Keyspace
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.