Skip to main content

Bucket

Struct Bucket 

Source
pub struct Bucket { /* private fields */ }
Expand description

A bucket is a named collection of key-value pairs.

Implementations§

Source§

impl Bucket

Source

pub fn begin(&self) -> Result<TxnKV<'_>, OpCode>

Begins a new read-write transaction.

Examples found in repository?
examples/observer.rs (line 19)
8fn main() -> Result<(), OpCode> {
9    let path = std::env::temp_dir().join(format!("mace_observer_{}", std::process::id()));
10    let _ = std::fs::remove_dir_all(&path);
11
12    let observer = Arc::new(InMemoryObserver::new(256));
13    let mut opt = Options::new(&path);
14    opt.observer = observer.clone();
15
16    let db = Mace::new(opt.validate()?)?;
17    let bucket = db.new_bucket("observe", BucketOptions::default())?;
18
19    let tx = bucket.begin()?;
20    tx.put("k1", "v1")?;
21
22    let r = tx.put("k1", "v2");
23    assert_eq!(r.err(), Some(OpCode::AbortTx));
24    drop(tx);
25
26    let tx = bucket.begin()?;
27    tx.put("k2", "v2")?;
28    tx.commit()?;
29
30    let snapshot = observer.snapshot();
31    print_snapshot(&snapshot);
32    Ok(())
33}
More examples
Hide additional examples
examples/demo.rs (line 11)
3fn main() -> Result<(), OpCode> {
4    let path = std::env::temp_dir().join("mace");
5    let _ = std::fs::remove_dir_all(&path);
6    let opt = Options::new(path).validate()?;
7    let db = Mace::new(opt)?;
8    let bucket = db.new_bucket("test", BucketOptions::default())?;
9
10    // start a read-write transaction
11    let kv = bucket.begin()?;
12    kv.put("foo", "bar")?;
13    kv.put("fool", "+1s")?;
14    kv.put("foolish", "elder")?;
15
16    // can't create two identical keys
17    let r = kv.put("foolish", "114514").err();
18    assert_eq!(r.unwrap(), OpCode::AbortTx);
19
20    // use `update` for exist key or use `upsert` when unsure
21    let r = kv.update("foolish", "114514");
22    assert!(r.is_ok());
23
24    let r = kv.get("foo")?;
25    assert_eq!(r.slice(), "bar".as_bytes());
26    kv.del("foolish")?;
27    kv.commit()?;
28
29    // rollback
30    let kv = bucket.begin()?;
31    kv.put("mo", "ha")?;
32    drop(kv);
33
34    // start a read-only transaction
35    let view = bucket.view()?;
36    let r = view.get("foo")?;
37    assert_eq!(r.slice(), "bar".as_bytes());
38    let r = view.get("mo");
39    assert_eq!(r.err().unwrap(), OpCode::NotFound);
40
41    // prefix scan
42    let r = view.get("foolish");
43    assert!(r.is_err() && r.err().unwrap() == OpCode::NotFound);
44    let iter = view.seek("foo");
45    assert_eq!(iter.count(), 2);
46
47    Ok(())
48}
Source

pub fn view(&self) -> Result<TxnView<'_>, OpCode>

Begins a new read-only transaction (view).

Examples found in repository?
examples/demo.rs (line 35)
3fn main() -> Result<(), OpCode> {
4    let path = std::env::temp_dir().join("mace");
5    let _ = std::fs::remove_dir_all(&path);
6    let opt = Options::new(path).validate()?;
7    let db = Mace::new(opt)?;
8    let bucket = db.new_bucket("test", BucketOptions::default())?;
9
10    // start a read-write transaction
11    let kv = bucket.begin()?;
12    kv.put("foo", "bar")?;
13    kv.put("fool", "+1s")?;
14    kv.put("foolish", "elder")?;
15
16    // can't create two identical keys
17    let r = kv.put("foolish", "114514").err();
18    assert_eq!(r.unwrap(), OpCode::AbortTx);
19
20    // use `update` for exist key or use `upsert` when unsure
21    let r = kv.update("foolish", "114514");
22    assert!(r.is_ok());
23
24    let r = kv.get("foo")?;
25    assert_eq!(r.slice(), "bar".as_bytes());
26    kv.del("foolish")?;
27    kv.commit()?;
28
29    // rollback
30    let kv = bucket.begin()?;
31    kv.put("mo", "ha")?;
32    drop(kv);
33
34    // start a read-only transaction
35    let view = bucket.view()?;
36    let r = view.get("foo")?;
37    assert_eq!(r.slice(), "bar".as_bytes());
38    let r = view.get("mo");
39    assert_eq!(r.err().unwrap(), OpCode::NotFound);
40
41    // prefix scan
42    let r = view.get("foolish");
43    assert!(r.is_err() && r.err().unwrap() == OpCode::NotFound);
44    let iter = view.seek("foo");
45    assert_eq!(iter.count(), 2);
46
47    Ok(())
48}
Source

pub fn checkpoint(&self)

Starts a manual checkpoint which will flush dirty pages to disk and may trigger WAL gc

Source

pub fn id(&self) -> u64

Returns the unique identifier of this bucket.

Source

pub fn options(&self) -> &Options

Returns the options used by this bucket.

Trait Implementations§

Source§

impl Clone for Bucket

Source§

fn clone(&self) -> Bucket

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Deref for Bucket

Source§

type Target = Inner

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.