Skip to main content

TxnView

Struct TxnView 

Source
pub struct TxnView<'a> { /* private fields */ }
Expand description

A read-only transaction (consistent view).

Implementations§

Source§

impl<'a> TxnView<'a>

Source

pub fn get<K: AsRef<[u8]>>(&self, k: K) -> Result<ValRef, OpCode>

Gets the value associated with a key in this view.

Examples found in repository?
examples/demo.rs (line 36)
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 seek<K>(&self, prefix: K) -> Iter<'_>
where K: AsRef<[u8]>,

Seeks an iterator to a key prefix in this view. prefix can’t be empty and the Iter::Item is only valid in current iteration.

NOTE: Iter will save a clone of the resource, so do not save Iter to avoid resource shortage.

Examples found in repository?
examples/demo.rs (line 44)
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 range<K, R>(&self, range: R) -> Iter<'_>
where K: AsRef<[u8]>, R: RangeBounds<K>,

Trait Implementations§

Source§

impl Drop for TxnView<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for TxnView<'a>

§

impl<'a> !UnwindSafe for TxnView<'a>

§

impl<'a> Freeze for TxnView<'a>

§

impl<'a> Send for TxnView<'a>

§

impl<'a> Sync for TxnView<'a>

§

impl<'a> Unpin for TxnView<'a>

§

impl<'a> UnsafeUnpin for TxnView<'a>

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> 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<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.