The DatabaseManager trait defines the interface for database backends in TAPLE Core. It provides the foundation for creating and managing database collections.
Trait Definition
pub trait DatabaseManager < C > : Sync + Send
where
C : DatabaseCollection ,
{
fn default () -> Self ;
fn create_collection ( & self , identifier : & str ) -> C ;
}
Type Parameters
The type of collection this manager creates. Must implement the DatabaseCollection trait.
Required Trait Bounds
The DatabaseManager trait requires implementations to be:
Sync - Safe to share between threads
Send - Safe to transfer between threads
Methods
default
Creates a default instance of the database manager. This constructor is primarily used for battery tests.
Returns: A new instance of the database manager
create_collection
fn create_collection ( & self , identifier : & str ) -> C
Creates a new database collection with the specified identifier.
The identifier/name for the collection to create
Returns: A new collection instance of type C
Implementation Example
Here’s how to implement a custom database backend:
use taple_core :: database :: { DatabaseManager , DatabaseCollection };
use std :: sync :: RwLock ;
use std :: collections :: HashMap ;
pub struct MyDatabaseManager {
collections : RwLock < HashMap < String , MyCollection >>,
}
impl DatabaseManager < MyCollection > for MyDatabaseManager {
fn default () -> Self {
Self {
collections : RwLock :: new ( HashMap :: new ()),
}
}
fn create_collection ( & self , identifier : & str ) -> MyCollection {
let mut lock = self . collections . write () . unwrap ();
let collection = MyCollection :: new ( identifier );
lock . insert ( identifier . to_string (), collection . clone ());
collection
}
}
Testing Your Implementation
TAPLE provides a test macro to validate your database implementation:
use taple_core :: test_database_manager_trait;
test_database_manager_trait! {
unit_test_my_manager : MyDatabaseManager : MyCollection
}
This macro runs a comprehensive battery of tests including:
Basic CRUD operations (put, get, delete)
Collection isolation
Iterator functionality
Reverse iteration
Built-in Implementations
MemoryManager In-memory database implementation for testing and development