### Get Key Start Offset in Redb Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Calculates the starting byte offset for a specific key at a given index. Returns None if the index is out of bounds. ```rust fn key_start(&self, n: usize) -> Option { if n == 0 { Some(self.key_section_start()) } else { self.key_end(n - 1) } } ``` -------------------------------- ### Calculate Key Section Start in RawBranchBuilder Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Calculates the starting offset for the key section within the branch page. This accounts for variable key sizes. ```rust fn key_section_start(&self) -> usize { let mut offset = 8 + (PageNumber::serialized_size() + size_of::()) * (self.num_keys + 1); if self.fixed_key_size.is_none() { offset += size_of::() * self.num_keys; } offset } ``` -------------------------------- ### BranchAccessor Key Section Start Calculation Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Determines the starting byte offset of the key section within a branch page. The calculation differs based on whether keys have a fixed size. ```rust fn key_section_start(&self) -> usize { if self.fixed_key_size.is_none() { 8 + (PageNumber::serialized_size() + size_of::()) * self.count_children() + size_of::() * self.num_keys() } else { 8 + (PageNumber::serialized_size() + size_of::()) * self.count_children() } } ``` -------------------------------- ### TransactionTracker::new Source: https://docs.rs/redb/latest/src/redb/transaction_tracker.rs.html Initializes a new TransactionTracker with a given starting transaction ID. ```rust pub(crate) fn new(next_transaction_id: TransactionId) -> Self { Self { state: Mutex::new(State { next_savepoint_id: SavepointId(0), live_read_transactions: Default::default(), next_transaction_id, live_write_transaction: None, valid_savepoints: Default::default(), pending_non_durable_commits: Default::default(), unprocessed_freed_non_durable_commits: Default::default(), }), live_write_transaction_available: Condvar::new(), } } ``` -------------------------------- ### Get Value Start Offset in Redb Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Calculates the starting byte offset for a specific value at a given index. Returns None if the index is out of bounds. ```rust fn value_start(&self, n: usize) -> Option { if n == 0 { self.key_end(self.num_pairs() - 1) } else { self.value_end(n - 1) } } ``` -------------------------------- ### Basic redb Database Operations Example Source: https://docs.rs/redb/latest/src/redb/lib.rs.html Demonstrates creating a database, inserting a key-value pair, and retrieving it within read and write transactions. Requires the `tempfile` crate for temporary file creation. ```rust #![deny(clippy::all, clippy::pedantic, clippy::disallowed_methods)] // TODO: revisit this list and see if we can enable some #![allow( clippy::default_trait_access, clippy::if_not_else, clippy::iter_not_returning_iterator, clippy::missing_errors_doc, clippy::missing_panics_doc, clippy::module_name_repetitions, clippy::must_use_candidate, clippy::needless_pass_by_value, clippy::redundant_closure_for_method_calls, clippy::similar_names, clippy::too_many_lines, clippy::unnecessary_wraps, clippy::unreadable_literal )] //! # redb //! //! A simple, portable, high-performance, ACID, embedded key-value store. //! //! redb is written in pure Rust and is loosely inspired by [lmdb][lmdb]. Data is stored in a collection //! of copy-on-write B-trees. For more details, see the [design doc][design]. //! //! # Features //! //! - Zero-copy, thread-safe, `BTreeMap` based API //! - Fully ACID-compliant transactions //! - MVCC support for concurrent readers & writer, without blocking //! - Crash-safe by default //! - Savepoints and rollbacks //! //! # Example //! //! ``` //! use redb::{Database, Error, ReadableDatabase, ReadableTable, TableDefinition}; //! //! const TABLE: TableDefinition<&str, u64> = TableDefinition::new("my_data"); //! //! fn main() -> Result<(), Error> { //! # #[cfg(not(target_os = "wasi"))] //! let file = tempfile::NamedTempFile::new().unwrap(); //! # #[cfg(target_os = "wasi")] //! # let file = tempfile::NamedTempFile::new_in("/tmp").unwrap(); //! let db = Database::create(file.path())?; //! let write_txn = db.begin_write()?; //! { //! let mut table = write_txn.open_table(TABLE)?; //! table.insert("my_key", &123)?; //! } //! write_txn.commit()?; //! //! let read_txn = db.begin_read()?; //! let table = read_txn.open_table(TABLE)?; //! assert_eq!(table.get("my_key")?.unwrap().value(), 123); //! //! Ok(()) //! } //! ``` //! //! [lmdb]: https://www.lmdb.tech/doc/ //! [design]: https://github.com/cberner/redb/blob/master/docs/design.md ``` -------------------------------- ### Basic redb Usage Example Source: https://docs.rs/redb/latest/index.html Demonstrates the basic usage of redb for creating a database, inserting a key-value pair, and retrieving it within transactions. Requires the `tempfile` crate for temporary file creation. ```rust use redb::{Database, Error, ReadableDatabase, ReadableTable, TableDefinition}; const TABLE: TableDefinition<&str, u64> = TableDefinition::new("my_data"); fn main() -> Result<(), Error> { let file = tempfile::NamedTempFile::new().unwrap(); let db = Database::create(file.path())?; let write_txn = db.begin_write()?; { let mut table = write_txn.open_table(TABLE)?; table.insert("my_key", &123)?; } write_txn.commit()?; let read_txn = db.begin_read()?; let table = read_txn.open_table(TABLE)?; assert_eq!(table.get("my_key")?.unwrap().value(), 123); Ok(()) } ``` -------------------------------- ### Basic Redb Database Usage Source: https://docs.rs/redb/latest/src/redb/db.rs.html Demonstrates the fundamental steps of creating a redb database, opening a write transaction, creating a table, inserting data, and committing the transaction. Ensure the necessary imports and error handling are in place. ```rust use redb::*; # use tempfile::NamedTempFile; const TABLE: TableDefinition = TableDefinition::new("my_data"); # fn main() -> Result<(), Error> { # #[cfg(not(target_os = "wasi"))] # let tmpfile = NamedTempFile::new().unwrap(); # #[cfg(target_os = "wasi")] # let tmpfile = NamedTempFile::new_in("/tmp").unwrap(); # let filename = tmpfile.path(); let db = Database::create(filename)?; let write_txn = db.begin_write()?; { let mut table = write_txn.open_table(TABLE)?; table.insert(&0, &0)?; } write_txn.commit()?; # Ok(()) # } ``` -------------------------------- ### Get Region Base Address Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/layout.rs.html Calculates the starting byte address for a given region index. ```rust pub(super) fn region_base_address(&self, region: u32) -> u64 { assert!(region < self.num_regions()); u64::from(self.full_region_layout.page_size()) + u64::from(region) * self.full_region_layout.len() } ``` -------------------------------- ### Create or Initialize Database Source: https://docs.rs/redb/latest/redb/struct.Builder.html Opens the specified file as a redb database. Initializes a new database if the file does not exist or is empty. Opens an existing database if the file is valid. Returns an error otherwise. ```rust pub fn create(&self, path: impl AsRef) -> Result ``` -------------------------------- ### Get Last Key-Value Pair Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Retrieves the last key-value pair in the B-tree. It starts the search from the cached root page if available. ```rust if let Some(ref root) = self.cached_root { self.last_helper(root.clone()) } else { Ok(None) } ``` -------------------------------- ### Database Initialization with Custom Page Size Source: https://docs.rs/redb/latest/src/redb/db.rs.html Demonstrates creating a Redb database with a specific page size. This is a fundamental step before performing any operations. ```rust let db = Database::builder() .set_page_size(1024) .create(tmpfile.path()) .unwrap(); ``` -------------------------------- ### Get First Key-Value Pair Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Retrieves the first key-value pair in the B-tree. It starts the search from the cached root page if available. ```rust if let Some(ref root) = self.cached_root { self.first_helper(root.clone()) } else { Ok(None) } ``` -------------------------------- ### Get Value Byte Range in Redb Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Returns the start and end byte offsets for a value at a given index. Returns None if the index is out of bounds. ```rust pub(super) fn value_range(&self, n: usize) -> Option<(usize, usize)> { Some((self.value_start(n)?, self.value_end(n)?)) } ``` -------------------------------- ### Basic Database Usage Source: https://docs.rs/redb/latest/redb/struct.Database.html Demonstrates the basic workflow of creating a database, beginning a write transaction, inserting data, and committing the transaction. Ensure proper error handling for file operations and transactions. ```rust use redb::*; const TABLE: TableDefinition = TableDefinition::new("my_data"); let db = Database::create(filename)?; let write_txn = db.begin_write()?; { let mut table = write_txn.open_table(TABLE)?; table.insert(&0, &0)?; } write_txn.commit()?; ``` -------------------------------- ### FileBackend Initialization Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/file_backend/optimized.rs.html Details on how to initialize the FileBackend, including handling file locking and potential errors. ```APIDOC ## FileBackend::new ### Description Creates a new `FileBackend` instance that stores database data to the provided `File`. ### Method `FileBackend::new(file: File) -> Result` ### Parameters - `file` (File) - The file object to use for storage. ### Request Example ```rust let file = File::create("my_database.redb")?; let backend = FileBackend::new(file)?; ``` ### Response #### Success Response (Ok) - `FileBackend` - An initialized `FileBackend` instance. #### Error Response - `DatabaseError` - If the database file is already open or another error occurs during initialization. ## FileBackend::new_internal ### Description An internal constructor for `FileBackend` that allows specifying if the backend is read-only and handles file locking. ### Method `FileBackend::new_internal(file: File, read_only: bool) -> Result` ### Parameters - `file` (File) - The file object to use for storage. - `read_only` (bool) - If true, the backend will be initialized in read-only mode. ### Request Example ```rust let file = File::open("my_database.redb")?; let backend = FileBackend::new_internal(file, true)?; ``` ### Response #### Success Response (Ok) - `FileBackend` - An initialized `FileBackend` instance. #### Error Response - `DatabaseError::DatabaseAlreadyOpen` - If the database file is already locked by another process. - `DatabaseError` - For other file system or locking errors. ``` -------------------------------- ### Getting File Metadata with and_then Source: https://docs.rs/redb/latest/redb/type.Result.html Demonstrates using `and_then` with `std::path::Path::metadata` to retrieve file metadata and then its modification time. This example shows error handling for non-existent paths. ```rust use std::{io::ErrorKind, path::Path}; // Note: on Windows "/" maps to "C:\\" let root_modified_time = Path::new("/").metadata().and_then(|md| md.modified()); assert!(root_modified_time.is_ok()); let should_fail = Path::new("/bad/path").metadata().and_then(|md| md.modified()); assert!(should_fail.is_err()); assert_eq!(should_fail.unwrap_err().kind(), ErrorKind::NotFound); ``` -------------------------------- ### Create Database with Custom Backend Source: https://docs.rs/redb/latest/redb/struct.Builder.html Opens an existing or creates a new database using a specified `StorageBackend`. ```rust pub fn create_with_backend( &self, backend: impl StorageBackend, ) -> Result ``` -------------------------------- ### Get Value using ReadableTable Trait Source: https://docs.rs/redb/latest/redb/struct.ReadOnlyTable.html Implements the get method from the ReadableTable trait for ReadOnlyTable, returning an AccessGuard for the value associated with the key. ```rust fn get<'a>( &self, key: impl Borrow>, ) -> Result>> ``` -------------------------------- ### Database Initialization with Cache Size and Page Size Source: https://docs.rs/redb/latest/src/redb/db.rs.html Shows how to initialize a Redb database with both a custom cache size and page size. This configuration can impact performance. ```rust let db = Database::builder() .set_cache_size(1024 * 1024) .set_page_size(1024) .create(tmpfile.path()) .unwrap(); ``` -------------------------------- ### Get Multimap Values by Key (ReadableMultimapTable Trait) Source: https://docs.rs/redb/latest/src/redb/multimap_table.rs.html Implements the `get` method for the `ReadableMultimapTable` trait. Returns an iterator over values for a given key, ensuring the transaction remains active. ```rust fn get<'a>(&self, key: impl Borrow>) -> Result> { let iter = if let Some(collection) = self.tree.get(key.borrow())? { DynamicCollection::iter(collection, self.transaction_guard.clone(), self.mem.clone())? } else { MultimapValue::new_subtree( BtreeRangeIter::new::>( &(..), None, self.mem.clone(), PageHint::None, )?, 0, self.transaction_guard.clone(), ) }; Ok(iter) } ``` -------------------------------- ### Initializing Database with Transaction Tracker Source: https://docs.rs/redb/latest/src/redb/db.rs.html This snippet initializes a `Database` instance, setting up the transactional memory and the transaction tracker. It restores the tracker's state based on persistent savepoints, registering each one to ensure correct transaction management. ```rust mem.begin_writable()?; let next_transaction_id = mem.get_last_committed_transaction_id()?.next(); let db = Database { mem, transaction_tracker: Arc::new(TransactionTracker::new(next_transaction_id)), }; // Restore the tracker state for any persistent savepoints let txn = db.begin_write().map_err(|e| e.into_storage_error())?; if let Some(next_id) = txn.next_persistent_savepoint_id()? { db.transaction_tracker .restore_savepoint_counter_state(next_id); } for id in txn.list_persistent_savepoints()? { let savepoint = match txn.get_persistent_savepoint(id) { Ok(savepoint) => savepoint, Err(err) => match err { SavepointError::InvalidSavepoint | SavepointError::ImmediateDurabilityRequired => unreachable!(), SavepointError::Storage(storage) => { return Err(storage.into()); } }, }; db.transaction_tracker .register_persistent_savepoint(&savepoint); } txn.abort()?; Ok(db) ``` -------------------------------- ### Create or Open Database Source: https://docs.rs/redb/latest/redb/struct.Database.html Opens a redb database file. If the file does not exist or is empty, a new database is initialized. Otherwise, an existing database is opened. ```rust pub fn create(path: impl AsRef) -> Result ``` -------------------------------- ### Get references to Result values Source: https://docs.rs/redb/latest/redb/type.Result.html Use `as_ref()` to get a `Result<&T, &E>` from a `&Result`, allowing inspection without consuming the original. `as_mut()` provides mutable references. ```rust let x: Result = Ok(2); assert_eq!(x.as_ref(), Ok(&2)); let x: Result = Err("Error"); assert_eq!(x.as_ref(), Err(&"Error")); ``` -------------------------------- ### Calculate Key Section Start Offset Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Determines the starting offset for the key data section within the page buffer. Accounts for header, and optional key/value offset entries. ```rust fn key_section_start(&self) -> usize { let mut offset = 4; if self.fixed_key_size.is_none() { offset += size_of::() * self.num_pairs; } if self.fixed_value_size.is_none() { offset += size_of::() * self.num_pairs; } offset } } ``` -------------------------------- ### FileBackend::new Source: https://docs.rs/redb/latest/redb/backends/struct.FileBackend.html Creates a new FileBackend instance. ```APIDOC ## impl FileBackend ### pub fn new(file: File) -> Result Creates a new backend which stores data to the given file. ``` -------------------------------- ### Create Database from File Source: https://docs.rs/redb/latest/redb/struct.Builder.html Opens an existing or creates a new database using the provided `File`. The file must be empty or contain a valid database. ```rust pub fn create_file(&self, file: File) -> Result ``` -------------------------------- ### Get a specific key-value pair from a table (generic ReadableTable trait) Source: https://docs.rs/redb/latest/src/redb/table.rs.html Implements the `get` method for the `ReadableTable` trait. Retrieves a value by key, returning an `AccessGuard` that holds a reference to the value. ```rust fn get<'a>(&self, key: impl Borrow>) -> Result>> { self.tree.get(key.borrow()) } ``` -------------------------------- ### Initialize FileBackend with File Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/file_backend/optimized.rs.html Creates a new FileBackend instance. It attempts to acquire a file lock to ensure exclusive access. If the file is already locked, it returns a DatabaseAlreadyOpen error. If file locking is not supported, it proceeds without locking but logs a warning. ```rust pub fn new(file: File) -> Result { Self::new_internal(file, false) } ``` ```rust pub(crate) fn new_internal(file: File, read_only: bool) -> Result { let result = if read_only { file.try_lock_shared() } else { file.try_lock() }; match result { Ok(()) => Ok(Self { file, lock_supported: true, }), Err(TryLockError::WouldBlock) => Err(DatabaseError::DatabaseAlreadyOpen), Err(TryLockError::Error(err)) if err.kind() == io::ErrorKind::Unsupported => { #[cfg(feature = "logging")] warn!( "File locks not supported on this platform. You must ensure that only a single process opens the database file, at a time" ); Ok(Self { file, lock_supported: false, }) } Err(TryLockError::Error(err)) => Err(err.into()), } } ``` -------------------------------- ### Calculate Key Section Start Offset in Redb Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Determines the starting byte offset for the key section of a leaf node. Considers fixed key size and presence of value offsets. ```rust fn key_section_start(&self) -> usize { let mut offset = 4; if self.fixed_key_size.is_none() { offset += size_of::() * self.num_pairs; } if self.fixed_value_size.is_none() { offset += size_of::() * self.num_pairs; } offset } ``` -------------------------------- ### Create New Redb Database in Empty File Source: https://docs.rs/redb/latest/src/redb/db.rs.html Demonstrates creating a new Redb database within an empty file using the builder pattern. ```rust let tmpfile = crate::create_tempfile(); let _db = Database::builder() .create_file(tmpfile.into_file()) .unwrap(); ``` -------------------------------- ### Get Root Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Retrieves the current root header of the B-Tree. ```APIDOC ## BtreeMut::get_root ### Description Returns the header of the B-Tree's root node. ### Method `&self` ### Returns - `Option` - The root header if it exists, otherwise None. ### Request Example ```rust let root_header = btree_mut.get_root(); ``` ### Response Example ```json { "success": true, "data": { "root_header": { "root": 10, "level": 2, "checksum": "some_checksum_value" } } } ``` ``` -------------------------------- ### Range Query Example Source: https://docs.rs/redb/latest/src/redb/table.rs.html Demonstrates how to perform a range query on a Redb table to retrieve elements within a specified key range. ```rust use redb::*; # use tempfile::NamedTempFile; const TABLE: TableDefinition<&str, u64> = TableDefinition::new("my_data"); # fn main() -> Result<(), Error> { # #[cfg(not(target_os = "wasi"))] # let tmpfile = NamedTempFile::new().unwrap(); # #[cfg(target_os = "wasi")] # let tmpfile = NamedTempFile::new_in("/tmp").unwrap(); # let filename = tmpfile.path(); let db = Database::create(filename)?; let write_txn = db.begin_write()?; { let mut table = write_txn.open_table(TABLE)?; table.insert("a", &0)?; table.insert("b", &1)?; table.insert("c", &2)?; } write_txn.commit()?; let read_txn = db.begin_read()?; let table = read_txn.open_table(TABLE)?; let mut iter = table.range("a".."c")?; ``` -------------------------------- ### Get Value from EntryGuard Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_iters.rs.html Deserializes and returns the value from the EntryGuard. ```rust pub(crate) fn value(&self) -> V::SelfType<'_> { V::from_bytes(&self.page.memory()[self.value_range.clone()]) } ``` -------------------------------- ### Initialize InMemoryBackend Source: https://docs.rs/redb/latest/redb/backends/struct.InMemoryBackend.html Creates a new, empty memory backend. This is the primary way to get an instance of the in-memory storage. ```rust pub fn new() -> Self ``` -------------------------------- ### Get Key from EntryGuard Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_iters.rs.html Deserializes and returns the key from the EntryGuard. ```rust pub(crate) fn key(&self) -> K::SelfType<'_> { K::from_bytes(&self.page.memory()[self.key_range.clone()]) } ``` -------------------------------- ### Open Existing Database Source: https://docs.rs/redb/latest/redb/struct.Builder.html Opens an existing redb database file. ```rust pub fn open(&self, path: impl AsRef) -> Result ``` -------------------------------- ### Create Redb Database Source: https://docs.rs/redb/latest/src/redb/db.rs.html Initializes a new redb database file at the specified path. If the file exists and is a valid redb database, it will be opened; otherwise, a new one is created. Handles potential database errors during creation. ```rust pub fn create(path: impl AsRef) -> Result { Self::builder().create(path) } ``` -------------------------------- ### Get TypeName String Source: https://docs.rs/redb/latest/redb/struct.TypeName.html Retrieves the string representation of the TypeName. ```rust pub fn name(&self) -> &str ``` -------------------------------- ### SystemNamespace Initialization Source: https://docs.rs/redb/latest/src/redb/transactions.rs.html Initializes a new SystemNamespace, setting up the table tree and freed pages tracking. Allocations within the system tree are ignored for savepoint restoration. ```rust fn new( root_page: Option, guard: Arc, mem: Arc, ) -> Self { let ignore = Arc::new(Mutex::new(PageTrackerPolicy::Ignore)); let freed_pages = Arc::new(Mutex::new(vec![])); Self { table_tree: TableTreeMut::new( root_page, guard.clone(), mem, freed_pages.clone(), ignore, ), freed_pages, transaction_guard: guard.clone(), } } ``` -------------------------------- ### SystemTable get method Source: https://docs.rs/redb/latest/src/redb/transactions.rs.html Retrieves a value from the system table by key. ```rust fn get<'a>(&self, key: impl Borrow>) -> Result>> where K: 'a, { self.tree.get(key.borrow()) } ``` -------------------------------- ### Get RawBtree Root Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Returns the BtreeHeader of the root node, if it exists. ```rust pub(crate) fn get_root(&self) -> Option { self.root } ``` -------------------------------- ### Create Redb Database with Custom Backend Source: https://docs.rs/redb/latest/src/redb/db.rs.html Creates a new redb database using a custom `StorageBackend`. This allows for flexible storage solutions. ```rust pub fn create_with_backend( &self, backend: impl StorageBackend, ) -> Result { Database::new( Box::new(backend), true, self.page_size, self.region_size, self.cache_size, &self.repair_callback, ) } ``` -------------------------------- ### Create New Redb Database with File Source: https://docs.rs/redb/latest/src/redb/db.rs.html Creates a new redb database using a provided `File` object. The file must be empty or contain a valid database. ```rust pub fn create_file(&self, file: File) -> Result { Database::new( Box::new(FileBackend::new(file)?), true, self.page_size, self.region_size, self.cache_size, &self.repair_callback, ) } ``` -------------------------------- ### Btree Utility Methods Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Utility methods for the Btree, such as getting its length. ```APIDOC ## GET /len ### Description Returns the number of key-value pairs in the Btree. ### Method GET ### Endpoint /len ### Parameters None ### Response #### Success Response (200) - **u64** - The total number of elements in the Btree. #### Response Example ```json { "count": 12345 } ``` ``` -------------------------------- ### DatabaseLayout::new Constructor Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/layout.rs.html Initializes a `DatabaseLayout` with the specified number of full regions, the layout for full regions, and an optional trailing region. ```rust pub(super) fn new( full_regions: u32, full_region: RegionLayout, trailing_region: Option, ) -> Self { Self { full_region_layout: full_region, num_full_regions: full_regions, trailing_partial_region: trailing_region, } } ``` -------------------------------- ### Get MultimapTableDefinition Name Source: https://docs.rs/redb/latest/redb/struct.MultimapTableDefinition.html Returns the name of the multimap table definition. ```rust fn name(&self) -> &str ``` -------------------------------- ### Initialize TableTree Source: https://docs.rs/redb/latest/src/redb/tree_store/table_tree.rs.html Constructs a new `TableTree` instance. Requires the master root page, a page hint, a transaction guard, and transactional memory. ```rust pub(crate) fn new( master_root: Option, page_hint: PageHint, guard: Arc, mem: Arc, ) -> Result { Ok(Self { tree: Btree::new(master_root, page_hint, guard, mem.clone())?, mem, }) } ``` -------------------------------- ### Get Multimap Length Source: https://docs.rs/redb/latest/src/redb/multimap_table.rs.html Returns the total number of values stored in the multimap. ```rust fn len(&self) -> Result { Ok(self.num_values) } ``` -------------------------------- ### Builder Initialization and Configuration Source: https://docs.rs/redb/latest/redb/struct.Builder.html Methods for creating a new Builder and setting configuration options like cache size and repair callbacks. ```APIDOC ## Builder ### Description Configuration builder of a redb Database. ### Methods #### `new()` Construct a new Builder with sensible defaults. **Defaults:** - `cache_size_bytes`: 1GiB #### `set_repair_callback()` Set a callback which will be invoked periodically in the event that the database file needs to be repaired. The `RepairSession` argument can be used to control the repair process. If the database file needs repair, the callback will be invoked at least once. There is no upper limit on the number of times it may be called. #### `set_cache_size()` Set the amount of memory (in bytes) used for caching data ``` -------------------------------- ### Get Table Length Source: https://docs.rs/redb/latest/src/redb/tree_store/table_tree_base.rs.html Retrieves the total number of elements stored in the table. ```APIDOC ## GET /get_length ### Description Retrieves the total number of elements stored in the table. ### Method GET ### Endpoint /get_length ### Parameters None ### Response #### Success Response (200) - **u64** - The number of elements in the table. #### Response Example ```json 1000 ``` ``` -------------------------------- ### Create a new InMemoryBackend Source: https://docs.rs/redb/latest/redb/backends/struct.InMemoryBackend.html Instantiate a new, empty in-memory database backend. This is useful for temporary data storage. ```rust pub struct InMemoryBackend(/* private fields */); ``` -------------------------------- ### Get Btree Page Hint Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Returns the page hint used by the Btree. ```rust pub(crate) fn hint(&self) -> PageHint { self.hint } ``` -------------------------------- ### UntypedBtreeMut Get Root Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Returns the current root header of the mutable B-tree. ```rust pub(crate) fn get_root(&self) -> Option { self.root } ``` -------------------------------- ### Initialize or Repair Redb Database Source: https://docs.rs/redb/latest/src/redb/db.rs.html This function handles the initialization and opening of a Redb database. It first checks for a valid allocator state. If found, it loads it directly. Otherwise, it performs a full repair process. This is the primary entry point for opening a database. ```rust fn new( file: Box, allow_initialize: bool, page_size: usize, region_size: Option, cache_size: usize, repair_callback: &(dyn Fn(&mut RepairSession) + 'static), ) -> Result { #[cfg(feature = "logging")] let file_path = format!("{:?}", &file); #[cfg(feature = "logging")] info!("Opening database {:?}", &file_path); let mem = TransactionalMemory::new( file, allow_initialize, page_size, region_size, cache_size, false, )?; let mut mem = Arc::new(mem); // If the last transaction used 2-phase commit and updated the allocator state table, then // we can just load the allocator state from there. Otherwise, we need a full repair if let Some(tree) = Self::get_allocator_state_table(&mem)? { #[cfg(feature = "logging")] info!("Found valid allocator state, full repair not needed"); mem.load_allocator_state(&tree)?; #[cfg(debug_assertions)] Self::mark_allocated_page_for_debug(&mut mem)?; } else { // Full repair is needed Self::repair_primary_corrupted(mem.clone(), repair_callback)?; } Ok(Self { mem }) } ``` -------------------------------- ### Get Full Region Layout Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/layout.rs.html Returns a reference to the layout of the full regions. ```rust pub(super) fn full_region_layout(&self) -> &RegionLayout { &self.full_region_layout } ``` -------------------------------- ### Builder Configuration and Creation Source: https://docs.rs/redb/latest/src/redb/db.rs.html This section covers the configuration options available through the Builder struct and the process of creating or opening a Redb database. ```APIDOC ## Builder Configuration builder of a redb [Database]. ### Methods #### `new()` Construct a new [Builder] with sensible defaults. **Defaults** - `cache_size_bytes`: 1GiB #### `set_repair_callback(callback: impl Fn(&mut RepairSession) + 'static)` Set a callback which will be invoked periodically in the event that the database file needs to be repaired. The [`RepairSession`] argument can be used to control the repair process. If the database file needs repair, the callback will be invoked at least once. There is no upper limit on the number of times it may be called. **Returns:** `&mut Self` #### `set_page_size(size: usize)` Set the internal page size of the database. Valid values are powers of two, greater than or equal to 512. **Defaults** Default to 4 Kib pages. **Returns:** `&mut Self` #### `set_cache_size(bytes: usize)` Set the amount of memory (in bytes) used for caching data. **Returns:** `&mut Self` #### `set_region_size(size: u64)` Set the region size of the database. **Returns:** `&mut Self` #### `create(path: impl AsRef)` Opens the specified file as a redb database. - if the file does not exist, or is an empty file, a new database will be initialized in it - if the file is a valid redb database, it will be opened - otherwise this function will return an error **Parameters** - **path** (`impl AsRef`) - The path to the database file. **Returns:** `Result` ### RepairSession Represents the state and control of a database repair process. #### Methods ##### `new(progress: f64)` Creates a new `RepairSession`. ##### `aborted()` Returns `true` if the repair process has been aborted, `false` otherwise. **Returns:** `bool` ##### `abort()` Aborts the repair process. The corresponding call to [`Builder::open`] or [`Builder::create`] will return an error. ##### `progress()` Returns an estimate of the repair progress in the range [0.0, 1.0). At 1.0 the repair is complete. **Returns:** `f64` ``` -------------------------------- ### Create New FileBackend Source: https://docs.rs/redb/latest/redb/backends/struct.FileBackend.html Creates a new FileBackend instance that will store data in the provided file. Returns a Result indicating success or a DatabaseError. ```rust pub fn new(file: File) -> Result ``` -------------------------------- ### Get Bitmap Length Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/bitmap.rs.html Returns the total number of bits managed by the bitmap. ```rust pub fn len(&self) -> u32 { self.len } ``` -------------------------------- ### Initialize Btree Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Constructor for the Btree. Initializes with optional root, page hint, transaction guard, and memory. Caches the root page if it exists. ```rust pub(crate) fn new( root: Option, hint: PageHint, guard: Arc, mem: Arc, ) -> Result { let cached_root = if let Some(header) = root { Some(mem.get_page(header.root, hint)?) } else { None }; Ok(Self { mem, transaction_guard: guard, cached_root, root, hint, _key_type: Default::default(), _value_type: Default::default(), }) } ``` -------------------------------- ### Get Length Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/buddy_allocator.rs.html Returns the total number of pages managed by this memory manager. ```rust pub(crate) fn len(&self) -> u32 { self.len } ``` -------------------------------- ### Iterator Information and State Source: https://docs.rs/redb/latest/redb/struct.ExtractIf.html Adapters for getting iteration count or peeking at elements. ```APIDOC ## POST /api/iterator/enumerate ### Description Creates an iterator which gives the current iteration count as well as the next value. ### Method POST ### Endpoint /api/iterator/enumerate ### Response #### Success Response (200) - **iterator** (Iterator) - The resulting iterator with counts. #### Response Example ```json { "iterator": "" } ``` ## POST /api/iterator/peekable ### Description Creates an iterator which can use `peek` and `peek_mut` methods to look at the next element without consuming it. ### Method POST ### Endpoint /api/iterator/peekable ### Response #### Success Response (200) - **iterator** (Iterator) - The resulting peekable iterator. #### Response Example ```json { "iterator": "" } ``` ``` -------------------------------- ### Database Creation and Opening Source: https://docs.rs/redb/latest/src/redb/db.rs.html Methods for creating new databases or opening existing ones with different configurations. ```APIDOC ## POST /api/databases/create_file ### Description Creates a new Redb database file or opens an existing one. ### Method POST ### Endpoint /api/databases/create_file ### Parameters #### Request Body - **file** (File) - Required - The file object to use for the database. ### Request Example ```json { "file": "path/to/your/database.redb" } ``` ### Response #### Success Response (200) - **Database** (Database) - A handle to the newly created or opened database. #### Response Example ```json { "database_handle": "" } ``` ``` ```APIDOC ## POST /api/databases/open ### Description Opens an existing Redb database file. ### Method POST ### Endpoint /api/databases/open ### Parameters #### Request Body - **path** (string) - Required - The path to the existing Redb database file. ### Request Example ```json { "path": "path/to/your/database.redb" } ``` ### Response #### Success Response (200) - **Database** (Database) - A handle to the opened database. #### Response Example ```json { "database_handle": "" } ``` ``` ```APIDOC ## POST /api/databases/open_read_only ### Description Opens an existing Redb database file in read-only mode. ### Method POST ### Endpoint /api/databases/open_read_only ### Parameters #### Request Body - **path** (string) - Required - The path to the existing Redb database file. ### Request Example ```json { "path": "path/to/your/database.redb" } ``` ### Response #### Success Response (200) - **ReadOnlyDatabase** (ReadOnlyDatabase) - A handle to the opened database in read-only mode. #### Response Example ```json { "read_only_database_handle": "" } ``` ``` ```APIDOC ## POST /api/databases/create_with_backend ### Description Creates a new Redb database using a provided storage backend. ### Method POST ### Endpoint /api/databases/create_with_backend ### Parameters #### Request Body - **backend** (StorageBackend) - Required - An implementation of the `StorageBackend` trait. ### Request Example ```json { "backend": "" } ``` ### Response #### Success Response (200) - **Database** (Database) - A handle to the newly created database. #### Response Example ```json { "database_handle": "" } ``` ``` -------------------------------- ### Any::type_id Implementation Source: https://docs.rs/redb/latest/redb/enum.CommitError.html Gets the TypeId of the current type. This is a blanket implementation for all types. ```rust fn type_id(&self) -> TypeId { // Implementation omitted for brevity } ``` -------------------------------- ### Create EntryGuard Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_iters.rs.html Constructs a new EntryGuard with the provided page, key range, and value range. ```rust fn new(page: PageImpl, key_range: Range, value_range: Range) -> Self { Self { page, key_range, value_range, _key_type: Default::default(), _value_type: Default::default(), } } ``` -------------------------------- ### Initialize RawBranchBuilder Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Creates a new RawBranchBuilder instance, setting up the mutable page slice, the expected number of keys, and the fixed key size. It also initializes the page type and number of keys in the buffer. ```rust pub(super) fn new(page: &'b mut [u8], num_keys: usize, fixed_key_size: Option) -> Self { assert!(num_keys > 0); page[0] = BRANCH; page[2..4].copy_from_slice(&u16::try_from(num_keys).unwrap().to_le_bytes()); #[cfg(debug_assertions)] { ``` -------------------------------- ### Database Creation and Opening Source: https://docs.rs/redb/latest/redb/struct.Database.html Functions for creating a new redb database file or opening an existing one. ```APIDOC ## POST /api/users ### Description Creates a new user in the system. ### Method POST ### Endpoint /api/users ### Parameters #### Query Parameters - **limit** (integer) - Optional - The maximum number of users to return. #### Request Body - **username** (string) - Required - The username for the new user. - **email** (string) - Required - The email address for the new user. ### Request Example ```json { "username": "johndoe", "email": "john.doe@example.com" } ``` ### Response #### Success Response (200) - **id** (integer) - The unique identifier for the newly created user. - **username** (string) - The username of the created user. - **email** (string) - The email address of the created user. #### Response Example ```json { "id": 123, "username": "johndoe", "email": "john.doe@example.com" } ``` ``` -------------------------------- ### Get Number of Keys in BranchAccessor Source: https://docs.rs/redb/latest/src/redb/tree_store/btree_base.rs.html Returns the number of keys stored in the branch node. ```rust fn num_keys(&self) -> usize { self.num_keys } ``` -------------------------------- ### Get Database Builder Source: https://docs.rs/redb/latest/redb/struct.Database.html Provides a convenient way to create a new database builder, which allows for more configuration options when creating or opening a database. ```rust pub fn builder() -> Builder ``` -------------------------------- ### Get Subtree Header Source: https://docs.rs/redb/latest/src/redb/multimap_table.rs.html Extracts the BtreeHeader from the data of an `UntypedDynamicCollection` assumed to be of type SubtreeV2. ```rust BtreeHeader::from_le_bytes( self.data[1..=BtreeHeader::serialized_size()] .try_into() .unwrap(), ) ``` -------------------------------- ### Get Primary Transaction Slot Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/header.rs.html Returns a reference to the current primary transaction header. ```rust pub(super) fn primary_slot(&self) -> &TransactionHeader { &self.transaction_slots[self.primary_slot] } ``` -------------------------------- ### Initialize RawBtree Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Constructor for RawBtree. Takes optional root, fixed key/value sizes, memory, and page hint. ```rust pub(crate) fn new( root: Option, fixed_key_size: Option, fixed_value_size: Option, mem: Arc, hint: PageHint, ) -> Self { Self { mem, root, hint, fixed_key_size, fixed_value_size, } } ``` -------------------------------- ### Buddy Allocator Initialization Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/buddy_allocator.rs.html Initializes a new BuddyAllocator with a given number of pages and maximum page capacity. It calculates the maximum order and lazily sizes bitmaps for each order, marking available pages starting from the highest order. ```rust pub(crate) fn new(num_pages: u32, max_page_capacity: u32) -> Self { let max_order = calculate_usable_order(max_page_capacity); let mut capacity_for_order = max_page_capacity; let mut pages_for_order = num_pages; let mut free = vec![]; for _ in 0..=max_order { // Lazily size each bitmap for the actual current pages, but pad the tree // height so resize() can grow to the full region capacity without needing // to insert new tree levels. free.push(BtreeBitmap::new_padded( pages_for_order, pages_for_order, capacity_for_order, )); pages_for_order = next_higher_order(pages_for_order); capacity_for_order = next_higher_order(capacity_for_order); } // Mark the available pages, starting with the highest order let mut accounted_pages = 0; for order in (0..=max_order).rev() { let order_size = 2u32.pow(order.into()); while accounted_pages + order_size <= num_pages { let page = accounted_pages / order_size; free[order as usize].clear(page); accounted_pages += order_size; } } assert_eq!(accounted_pages, num_pages); Self { free, len: num_pages, max_order, } } ``` -------------------------------- ### Get Btree Transaction Guard Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Returns a reference to the Btree's transaction guard. ```rust pub(crate) fn transaction_guard(&self) -> &Arc { &self.transaction_guard } ``` -------------------------------- ### Get BtreeMut Length Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Returns the total number of key-value pairs stored in the B-tree. ```rust pub(crate) fn len(&self) -> Result { self.read_tree()?.len() } ``` -------------------------------- ### Initialize Padded BtreeBitmap Source: https://docs.rs/redb/latest/src/redb/tree_store/page_store/bitmap.rs.html Creates a new BtreeBitmap, similar to `new()`, but ensures the tree height is sufficient for a `max_capacity`. This prevents the need to insert new levels during resize operations. ```rust pub(crate) fn new_padded(num_pages: u32, capacity: u32, max_capacity: u32) -> Self { let mut result = Self::new(num_pages, capacity); let max_height = Self::height_for_capacity(max_capacity); while result.heights.len() < max_height { let root_len = result.heights[0].len(); let parent_len = root_len.div_ceil(64); result .heights ``` -------------------------------- ### Get Value by Key Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Retrieves a read-only guard for the value associated with a given key. ```APIDOC ## GET /get ### Description Retrieves a read-only `AccessGuard` for the value associated with the specified key. If the key is not found, `None` is returned. ### Method GET ### Endpoint `/get` ### Parameters #### Path Parameters None #### Query Parameters - **key** (K::SelfType<'_>) - Required - The key to look up. #### Request Body None ### Request Example ```json { "key": "search_key" } ``` ### Response #### Success Response (200) - **Option>** - An optional `AccessGuard` to the value if the key is found. #### Response Example ```json { "value_guard": "" } ``` ``` -------------------------------- ### Get B-Tree Statistics Source: https://docs.rs/redb/latest/src/redb/tree_store/btree.rs.html Retrieves statistics about the B-Tree, such as depth, node counts, etc. ```APIDOC ## GET /stats ### Description Fetches statistical information about the B-Tree. This includes metrics like the number of nodes, depth, and memory usage. ### Method GET ### Endpoint `/stats` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```json {} ``` ### Response #### Success Response (200) - **BtreeStats** (BtreeStats) - An object containing various statistics about the B-Tree. #### Response Example ```json { "stats": { "depth": 5, "node_count": 1000, "total_pages": 1200 } } ``` ``` -------------------------------- ### InMemoryBackend Constructor Source: https://docs.rs/redb/latest/redb/backends/struct.InMemoryBackend.html Provides a way to create a new, empty in-memory backend instance. ```APIDOC ## pub fn new() -> Self ### Description Creates a new, empty memory backend. ### Method `new` ### Returns `Self` - A new instance of `InMemoryBackend`. ```