Trait CursorApi

Source
pub trait CursorApi {
    // Required methods
    fn first(&mut self) -> Option<(&[u8], Option<&[u8]>)>;
    fn last(&mut self) -> Option<(&[u8], Option<&[u8]>)>;
    fn next(&mut self) -> Option<(&[u8], Option<&[u8]>)>;
    fn prev(&mut self) -> Option<(&[u8], Option<&[u8]>)>;
    fn seek<T: AsRef<[u8]>>(
        &mut self,
        seek: T,
    ) -> Option<(&[u8], Option<&[u8]>)>;
}
Expand description

Read-only Cursor API

Required Methods§

Source

fn first(&mut self) -> Option<(&[u8], Option<&[u8]>)>

Moves the cursor to the first item in the bucket and returns its key and value.

If the bucket is empty then None is returned.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key1", "value1")?;
    b.put("key2", "value2")?;
    b.put("key3", "value3")?;
    Ok(())
  })?;

  db.view(|tx| {
    let b = tx.bucket("test").unwrap();
    let mut c = b.cursor();
    let first = c.first();
    assert_eq!(Some((b"key1".as_slice(), Some(b"value1".as_slice()))), first);
    Ok(())
  })?;

  Ok(())
}
Source

fn last(&mut self) -> Option<(&[u8], Option<&[u8]>)>

Moves the cursor to the last item in the bucket and returns its key and value.

If the bucket is empty then None is returned.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key1", "value1")?;
    b.put("key2", "value2")?;
    b.put("key3", "value3")?;
    Ok(())
  })?;

  db.view(|tx| {
    let b = tx.bucket("test").unwrap();
    let mut c = b.cursor();
    let last = c.last();
    assert_eq!(Some((b"key3".as_slice(), Some(b"value3".as_slice()))), last);
    Ok(())
  })?;

  Ok(())
}
Source

fn next(&mut self) -> Option<(&[u8], Option<&[u8]>)>

Moves the cursor to the next item in the bucket and returns its key and value.

If the cursor is at the end of the bucket then None is returned.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key1", "value1")?;
    b.put("key2", "value2")?;
    b.put("key3", "value3")?;
    Ok(())
  })?;

  db.view(|tx| {
    let b = tx.bucket("test").unwrap();
    let mut c = b.cursor();
    c.first();
    let next = c.next();
    assert_eq!(Some((b"key2".as_slice(), Some(b"value2".as_slice()))), next);
    Ok(())
  })?;

  Ok(())
}
Source

fn prev(&mut self) -> Option<(&[u8], Option<&[u8]>)>

Moves the cursor to the previous item in the bucket and returns its key and value. If the cursor is at the beginning of the bucket then None is returned.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key1", "value1")?;
    b.put("key2", "value2")?;
    b.put("key3", "value3")?;
    Ok(())
  })?;

  db.view(|tx| {
    let b = tx.bucket("test").unwrap();
    let mut c = b.cursor();
    c.last();
    let prev = c.prev();
    assert_eq!(Some((b"key2".as_slice(), Some(b"value2".as_slice()))), prev);
    Ok(())
  })?;

  Ok(())
}
Source

fn seek<T: AsRef<[u8]>>(&mut self, seek: T) -> Option<(&[u8], Option<&[u8]>)>

Moves the cursor to a given key using a b-tree search and returns it.

If the key does not exist then the next key is used. If no keys follow, None is returned.

use bbolt_rs::*;

fn main() -> Result<()> {
  let mut db = Bolt::open_mem()?;

  db.update(|mut tx| {
    let mut b = tx.create_bucket_if_not_exists("test")?;
    b.put("key1", "value1")?;
    b.put("key2", "value2")?;
    b.put("key3", "value3")?;
    Ok(())
  })?;

  db.view(|tx| {
    let b = tx.bucket("test").unwrap();
    let mut c = b.cursor();
    let seek = c.seek("key2");
    assert_eq!(Some((b"key2".as_slice(), Some(b"value2".as_slice()))), seek);
    Ok(())
  })?;

  Ok(())
}

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'tx: 'a, 'a> CursorApi for CursorImpl<'tx, 'a>

Source§

impl<'tx: 'a, 'a> CursorApi for CursorRwImpl<'tx, 'a>