MemoryManager and MemoryCollection provide a simple, thread-safe in-memory database implementation. This is ideal for testing, development, and scenarios where persistence is not required.
MemoryManager
Structure
MemoryManager maintains a thread-safe map of data stores, allowing multiple collections to share the same underlying storage.
Implementation
Constructor
new
MemoryCollection
Structure
BTreeMap with thread-safe access through RwLock.
Methods
Implements allDatabaseCollection trait methods:
get
put
del
iter
Complete Usage Example
Thread Safety
The in-memory implementation uses:RwLockfor read-write synchronizationArcfor shared ownership across threadsBTreeMapfor ordered key storage
Internal Implementation Details
DataStore
The underlying storage structure:BTreeMap to maintain ordered keys, enabling efficient prefix-based iteration.
Iterator Types
Two specialized iterator types handle forward and reverse iteration:MemoryIterator- Forward iteration with prefix filteringRevMemoryIterator- Reverse iteration with prefix filtering
- Acquire a read lock on the data
- Filter entries by prefix
- Strip the prefix from returned keys
- Yield
(String, Vec<u8>)tuples
Testing
The implementation includes comprehensive tests:- Basic CRUD operations
- Collection behavior
- Iterator functionality (forward and reverse)
- Prefix filtering
Use Cases
Unit Testing
Perfect for testing TAPLE nodes without persistence overhead
Development
Quick prototyping and development without database setup
Ephemeral Nodes
Temporary nodes that don’t need to persist state
Integration Tests
Clean state for each test run
Limitations
For production deployments, implement a persistent database backend using theDatabaseManager and DatabaseCollection traits.
Related Types
- DatabaseManager - Manager trait interface
- DatabaseCollection - Collection trait interface
- Error - Database error types
