aboutsummaryrefslogtreecommitdiff
path: root/src/db/lib.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2022-06-07 17:05:17 +0200
committerAlex Auvolat <alex@adnab.me>2022-06-07 17:05:17 +0200
commita3a01141ec8f8eefe1a31162bd53fbaa2d37e601 (patch)
tree9363dc84501de391bb59732e721710f47af6974e /src/db/lib.rs
parenta9e79f848b704347601e082c8065c3ce042b283b (diff)
downloadgarage-a3a01141ec8f8eefe1a31162bd53fbaa2d37e601.tar.gz
garage-a3a01141ec8f8eefe1a31162bd53fbaa2d37e601.zip
db abstraction: make .insert() and .remove() return the old value
Diffstat (limited to 'src/db/lib.rs')
-rw-r--r--src/db/lib.rs30
1 files changed, 16 insertions, 14 deletions
diff --git a/src/db/lib.rs b/src/db/lib.rs
index 4543e53c..ed1bdb75 100644
--- a/src/db/lib.rs
+++ b/src/db/lib.rs
@@ -166,15 +166,18 @@ impl Tree {
.transpose()
}
- /// True if item didn't exist before, false if item already existed
- /// and was replaced.
+ /// Returns the old value if there was one
#[inline]
- pub fn insert<T: AsRef<[u8]>, U: AsRef<[u8]>>(&self, key: T, value: U) -> Result<bool> {
+ pub fn insert<T: AsRef<[u8]>, U: AsRef<[u8]>>(
+ &self,
+ key: T,
+ value: U,
+ ) -> Result<Option<Value>> {
self.0.insert(self.1, key.as_ref(), value.as_ref())
}
- /// True if item was removed, false if item already didn't exist
+ /// Returns the old value if there was one
#[inline]
- pub fn remove<T: AsRef<[u8]>>(&self, key: T) -> Result<bool> {
+ pub fn remove<T: AsRef<[u8]>>(&self, key: T) -> Result<Option<Value>> {
self.0.remove(self.1, key.as_ref())
}
@@ -220,20 +223,19 @@ impl<'a> Transaction<'a> {
self.0.len(tree.1)
}
- /// True if item didn't exist before, false if item already existed
- /// and was replaced.
+ /// Returns the old value if there was one
#[inline]
pub fn insert<T: AsRef<[u8]>, U: AsRef<[u8]>>(
&mut self,
tree: &Tree,
key: T,
value: U,
- ) -> Result<bool> {
+ ) -> Result<Option<Value>> {
self.0.insert(tree.1, key.as_ref(), value.as_ref())
}
- /// True if item was removed, false if item already didn't exist
+ /// Returns the old value if there was one
#[inline]
- pub fn remove<T: AsRef<[u8]>>(&mut self, tree: &Tree, key: T) -> Result<bool> {
+ pub fn remove<T: AsRef<[u8]>>(&mut self, tree: &Tree, key: T) -> Result<Option<Value>> {
self.0.remove(tree.1, key.as_ref())
}
@@ -289,8 +291,8 @@ pub(crate) trait IDb: Send + Sync {
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
fn len(&self, tree: usize) -> Result<usize>;
- fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result<bool>;
- fn remove(&self, tree: usize, key: &[u8]) -> Result<bool>;
+ fn insert(&self, tree: usize, key: &[u8], value: &[u8]) -> Result<Option<Value>>;
+ fn remove(&self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
fn iter(&self, tree: usize) -> Result<ValueIter<'_>>;
fn iter_rev(&self, tree: usize) -> Result<ValueIter<'_>>;
@@ -315,8 +317,8 @@ pub(crate) trait ITx {
fn get(&self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
fn len(&self, tree: usize) -> Result<usize>;
- fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result<bool>;
- fn remove(&mut self, tree: usize, key: &[u8]) -> Result<bool>;
+ fn insert(&mut self, tree: usize, key: &[u8], value: &[u8]) -> Result<Option<Value>>;
+ fn remove(&mut self, tree: usize, key: &[u8]) -> Result<Option<Value>>;
fn iter(&self, tree: usize) -> Result<ValueIter<'_>>;
fn iter_rev(&self, tree: usize) -> Result<ValueIter<'_>>;