122 lines
3.0 KiB
Rust
122 lines
3.0 KiB
Rust
//! TST is a space-optimized tree data structure that enables efficient string key operations
|
|
//! with persistent storage using OurDB as a backend.
|
|
//!
|
|
//! This implementation provides a persistent ternary search tree that can be used for efficient
|
|
//! string key operations, such as auto-complete, routing tables, and more.
|
|
|
|
mod error;
|
|
mod node;
|
|
mod operations;
|
|
mod serialize;
|
|
|
|
pub use error::Error;
|
|
pub use node::TSTNode;
|
|
|
|
use ourdb::OurDB;
|
|
|
|
/// TST represents a ternary search tree data structure with persistent storage.
|
|
pub struct TST {
|
|
/// Database for persistent storage
|
|
db: OurDB,
|
|
|
|
/// Database ID of the root node
|
|
root_id: Option<u32>,
|
|
}
|
|
|
|
impl TST {
|
|
/// Creates a new TST with the specified database path.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `path` - The path to the database directory
|
|
/// * `reset` - Whether to reset the database if it exists
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// A new `TST` instance
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the database cannot be created or opened
|
|
pub fn new(path: &str, reset: bool) -> Result<Self, Error> {
|
|
operations::new_tst(path, reset)
|
|
}
|
|
|
|
/// Sets a key-value pair in the tree.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `key` - The key to set
|
|
/// * `value` - The value to set
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the operation fails
|
|
pub fn set(&mut self, key: &str, value: Vec<u8>) -> Result<(), Error> {
|
|
operations::set(self, key, value)
|
|
}
|
|
|
|
/// Gets a value by key from the tree.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `key` - The key to get
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// The value associated with the key
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the key is not found or the operation fails
|
|
pub fn get(&mut self, key: &str) -> Result<Vec<u8>, Error> {
|
|
operations::get(self, key)
|
|
}
|
|
|
|
/// Deletes a key from the tree.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `key` - The key to delete
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the key is not found or the operation fails
|
|
pub fn delete(&mut self, key: &str) -> Result<(), Error> {
|
|
operations::delete(self, key)
|
|
}
|
|
|
|
/// Lists all keys with a given prefix.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `prefix` - The prefix to search for
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// A list of keys that start with the given prefix
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the operation fails
|
|
pub fn list(&mut self, prefix: &str) -> Result<Vec<String>, Error> {
|
|
operations::list(self, prefix)
|
|
}
|
|
|
|
/// Gets all values for keys with a given prefix.
|
|
///
|
|
/// # Arguments
|
|
///
|
|
/// * `prefix` - The prefix to search for
|
|
///
|
|
/// # Returns
|
|
///
|
|
/// A list of values for keys that start with the given prefix
|
|
///
|
|
/// # Errors
|
|
///
|
|
/// Returns an error if the operation fails
|
|
pub fn getall(&mut self, prefix: &str) -> Result<Vec<Vec<u8>>, Error> {
|
|
operations::getall(self, prefix)
|
|
}
|
|
} |