@@ -4,30 +4,45 @@ This document provides examples for the public APIs in the `vsdb_trie_db` crate.
44
55## MptStore
66
7- ` MptStore ` provides an out-of-the-box wrapper for the ` trie-db ` crate, backed by ` vsdb ` .
7+ ` MptStore ` provides a high-level wrapper for managing Merkle Patricia Tries with persistent storage .
88
99``` rust
1010use vsdb_trie_db :: MptStore ;
1111
1212// Create a new MptStore
13- let mut store = MptStore :: new ();
13+ let store = MptStore :: new ();
1414
15- // Initialize a new trie with a backend key
16- let mut trie = store . trie_init (b " my_trie " ) . unwrap ( );
15+ // Initialize a new trie (starts with an empty root)
16+ let mut trie = store . trie_init ();
1717
18- // Insert a key-value pair
18+ // Insert key-value pairs (automatically commits)
1919trie . insert (b " key1" , b " value1" ). unwrap ();
20+ trie . insert (b " key2" , b " value2" ). unwrap ();
2021
21- // Commit the changes to the trie
22- let mut trie = trie . commit (). unwrap ();
22+ // Get the current root hash
2323let root = trie . root ();
2424
25- // Retrieve the value
25+ // Retrieve values
2626let value = trie . get (b " key1" ). unwrap (). unwrap ();
2727assert_eq! (value , b " value1" );
2828
29- // Create a read-only handle to the trie at a specific root
30- let ro_trie = trie . ro_handle ( root ) . unwrap ( );
31- let value = ro_trie . get (b " key1" ). unwrap (). unwrap ();
29+ // Load an existing trie from a root hash
30+ let loaded_trie = store . trie_load ( & root );
31+ let value = loaded_trie . get (b " key1" ). unwrap (). unwrap ();
3232assert_eq! (value , b " value1" );
33+
34+ // Remove a key
35+ trie . remove (b " key2" ). unwrap ();
36+ assert! (trie . get (b " key2" ). unwrap (). is_none ());
37+
38+ // Batch update operations
39+ let ops = vec! [
40+ (b " key3" . as_ref (), Some (b " value3" . as_ref ())), // Insert
41+ (b " key1" . as_ref (), None ), // Remove
42+ ];
43+ trie . batch_update (& ops ). unwrap ();
44+
45+ // Verify changes
46+ assert! (trie . get (b " key1" ). unwrap (). is_none ());
47+ assert_eq! (trie . get (b " key3" ). unwrap (). unwrap (), b " value3" );
3348```
0 commit comments