| Crates.io | herosal-tst |
| lib.rs | herosal-tst |
| version | 0.1.0 |
| created_at | 2025-12-13 11:18:43.829134+00 |
| updated_at | 2025-12-13 11:18:43.829134+00 |
| description | A persistent ternary search tree implementation using OurDB for storage |
| homepage | |
| repository | https://github.com/threefoldtech/sal |
| max_upload_size | |
| id | 1982822 |
| size | 61,837 |
A persistent ternary search tree implementation in Rust using OurDB for storage.
TST is a space-optimized tree data structure that enables efficient string key operations with persistent storage. 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.
A ternary search tree is a type of trie where each node has three children: left, middle, and right. Unlike a radix tree which compresses common prefixes, a TST stores one character per node and uses a binary search tree-like structure for efficient traversal.
Key characteristics:
Add the dependency to your Cargo.toml:
[dependencies]
tst = { path = "../tst" }
use tst::TST;
fn main() -> Result<(), tst::Error> {
// Create a new ternary search tree
let mut tree = TST::new("/tmp/tst", false)?;
// Set key-value pairs
tree.set("hello", b"world".to_vec())?;
tree.set("help", b"me".to_vec())?;
// Get values by key
let value = tree.get("hello")?;
println!("hello: {}", String::from_utf8_lossy(&value)); // Prints: world
// List keys by prefix
let keys = tree.list("hel")?; // Returns ["hello", "help"]
println!("Keys with prefix 'hel': {:?}", keys);
// Get all values by prefix
let values = tree.getall("hel")?; // Returns [b"world", b"me"]
// Delete keys
tree.delete("help")?;
Ok(())
}
// Create a new ternary search tree
let mut tree = TST::new("/tmp/tst", false)?;
// Create a new ternary search tree and reset if it exists
let mut tree = TST::new("/tmp/tst", true)?;
// Set a key-value pair
tree.set("key", b"value".to_vec())?;
// Get a value by key
let value = tree.get("key")?;
// Delete a key
tree.delete("key")?;
// List all keys with a given prefix
let keys = tree.list("prefix")?;
// Get all values for keys with a given prefix
let values = tree.getall("prefix")?;
TST is particularly useful for:
The TST implementation uses OurDB for persistent storage:
The project includes a comprehensive test suite that verifies all functionality:
cd ~/code/git.threefold.info/herocode/db/tst
# Run all tests
cargo test
# Run specific test file
cargo test --test basic_test
cargo test --test prefix_test
The project includes example applications that demonstrate how to use the TST:
# Run the basic usage example
cargo run --example basic_usage
# Run the prefix operations example
cargo run --example prefix_ops
# Run the performance test
cargo run --example performance
While both TST and RadixTree provide efficient string key operations, they have different characteristics:
Choose TST when:
Choose RadixTree when:
This project is licensed under the same license as the HeroCode project.