52 lines
1.6 KiB
Rust
52 lines
1.6 KiB
Rust
use radixtree::RadixTree;
|
|
use std::path::PathBuf;
|
|
|
|
fn main() -> Result<(), radixtree::Error> {
|
|
// Create a temporary directory for the database
|
|
let db_path = std::env::temp_dir().join("radixtree_example");
|
|
std::fs::create_dir_all(&db_path)?;
|
|
|
|
println!("Creating radix tree at: {}", db_path.display());
|
|
|
|
// Create a new radix tree
|
|
let mut tree = RadixTree::new(db_path.to_str().unwrap(), true)?;
|
|
|
|
// Store some data
|
|
println!("Storing data...");
|
|
tree.set("hello", b"world".to_vec())?;
|
|
tree.set("help", b"me".to_vec())?;
|
|
tree.set("helicopter", b"flying".to_vec())?;
|
|
|
|
// Retrieve and print the data
|
|
let value = tree.get("hello")?;
|
|
println!("hello: {}", String::from_utf8_lossy(&value));
|
|
|
|
// Update a value
|
|
println!("Updating value...");
|
|
tree.update("hello", b"updated world".to_vec())?;
|
|
|
|
// Retrieve the updated value
|
|
let updated_value = tree.get("hello")?;
|
|
println!("hello (updated): {}", String::from_utf8_lossy(&updated_value));
|
|
|
|
// Delete a key
|
|
println!("Deleting 'help'...");
|
|
tree.delete("help")?;
|
|
|
|
// Try to retrieve the deleted key (should fail)
|
|
match tree.get("help") {
|
|
Ok(value) => println!("Unexpected: help still exists with value: {}", String::from_utf8_lossy(&value)),
|
|
Err(e) => println!("As expected, help was deleted: {}", e),
|
|
}
|
|
|
|
// Clean up (optional)
|
|
if std::env::var("KEEP_DB").is_err() {
|
|
std::fs::remove_dir_all(&db_path)?;
|
|
println!("Cleaned up database directory");
|
|
} else {
|
|
println!("Database kept at: {}", db_path.display());
|
|
}
|
|
|
|
Ok(())
|
|
}
|