Skip to main content

BfTree

Struct BfTree 

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

The bf-tree instance

Implementations§

Source§

impl BfTree

Source

pub fn recovery( config_file: impl AsRef<Path>, wal_file: impl AsRef<Path>, buffer_ptr: Option<*mut u8>, )

Recovery a Bf-Tree from snapshot and WAL files. Incomplete function, internal use only

Source

pub fn new_from_snapshot( bf_tree_config: Config, buffer_ptr: Option<*mut u8>, ) -> Result<Self, ConfigError>

Instead of creating a new Bf-Tree instance, it loads a Bf-Tree snapshot file and resume from there.

Source

pub fn snapshot(&self) -> PathBuf

Stop the world and take a snapshot of the current state.

Returns the snapshot file path

Source§

impl BfTree

Source

pub fn new( file_path: impl AsRef<Path>, cache_size_byte: usize, ) -> Result<Self, ConfigError>

Create a new bf-tree instance with customized storage backend and circular buffer size

For in-memory tree, use :memory: as file path. For cache-only tree, use :cache: as file path For disk tree, file_path is the path to the index file

Mini page cache must be at least 8192 bytes for practical workloads.

use bf_tree::BfTree;
let tree = BfTree::new(":memory:", 8192).unwrap();
Source

pub fn new_with_config_file<P: AsRef<Path>>( config_file_path: P, ) -> Result<Self, ConfigError>

Create a new bf-tree instance with customized configuration based on a config file

Source

pub fn with_config( config: Config, buffer_ptr: Option<*mut u8>, ) -> Result<Self, ConfigError>

Initialize the bf-tree with provided config. For advanced user only. An optional pre-allocated buffer pointer can be provided to use as the buffer pool memory.

Source

pub fn config(&self) -> &Config

Source

pub fn get_buffer_metrics(&self) -> CircularBufferMetrics

Get the buffer metrics of the circular buffer. This is a blocking call, will stop all other buffer operations, use it wisely.

Source

pub fn update_read_promotion_rate(&self, new_rate: usize)

Chance% to promote a base read record to mini page.

Source

pub fn insert(&self, key: &[u8], value: &[u8]) -> LeafInsertResult

Insert a key-value pair to the system, overrides existing value if present.

use bf_tree::BfTree;
use bf_tree::LeafReadResult;

let mut config = bf_tree::Config::default();
config.cb_min_record_size(4);
let tree = BfTree::with_config(config, None).unwrap();
tree.insert(b"key", b"value");
let mut buffer = [0u8; 1024];
let read_size = tree.read(b"key", &mut buffer);

assert_eq!(read_size, LeafReadResult::Found(5));
assert_eq!(&buffer[..5], b"value");
Source

pub fn read(&self, key: &[u8], out_buffer: &mut [u8]) -> LeafReadResult

Read a record from the tree. Returns the number of bytes read.

TODO: don’t panic if the out_buffer is too small, instead returns a error.

use bf_tree::BfTree;
use bf_tree::LeafReadResult;

let mut config = bf_tree::Config::default();
config.cb_min_record_size(4);

let tree = BfTree::with_config(config, None).unwrap();
tree.insert(b"key", b"value");
let mut buffer = [0u8; 1024];
let read_size = tree.read(b"key", &mut buffer);
assert_eq!(read_size, LeafReadResult::Found(5));
assert_eq!(&buffer[..5], b"value");
Source

pub fn delete(&self, key: &[u8])

Delete a record from the tree.

use bf_tree::BfTree;
use bf_tree::LeafReadResult;

let tree = BfTree::default();
tree.insert(b"key", b"value");
tree.delete(b"key");
let mut buffer = [0u8; 1024];
let rt = tree.read(b"key", &mut buffer);
assert_eq!(rt, LeafReadResult::Deleted);
Source

pub fn scan_with_count<'a>( &'a self, key: &[u8], cnt: usize, return_field: ScanReturnField, ) -> Result<ScanIter<'a, 'a>, ScanIterError>

Scan records in the tree, with starting key and desired scan count. Returns a iterator that yields key-value pairs.

Source

pub fn scan_with_end_key<'a>( &'a self, start_key: &[u8], end_key: &[u8], return_field: ScanReturnField, ) -> Result<ScanIter<'a, 'a>, ScanIterError>

Source

pub fn get_metrics(&mut self) -> Option<Value>

Collect all metrics and reset the metric recorder The caller needs to ensure there are no references to the bf-tree’s metrics recorder anymore.

Trait Implementations§

Source§

impl Default for BfTree

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for BfTree

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for BfTree

Source§

impl Sync for BfTree

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> 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, 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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V