pub struct Database { /* private fields */ }Expand description
The main database handle.
Provides a unified interface for both in-memory and persistent storage. Thread-safe and can be cloned to share across threads.
§Examples
use rustlite::Database;
// Open a persistent database
let db = Database::open("./my_data")?;
db.put(b"key", b"value")?;
// Data persists across restarts
drop(db);
let db = Database::open("./my_data")?;
assert_eq!(db.get(b"key")?, Some(b"value".to_vec()));Implementations§
Source§impl Database
impl Database
Sourcepub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
pub fn open<P: AsRef<Path>>(path: P) -> Result<Self>
Opens a persistent database at the specified path.
Creates the directory if it doesn’t exist. Data is persisted to disk using a Write-Ahead Log (WAL) and SSTable files.
§Arguments
path- Directory path where database files will be stored
§Examples
use rustlite::Database;
let db = Database::open("./my_database")?;
db.put(b"hello", b"world")?;Sourcepub fn open_with_config<P: AsRef<Path>>(
path: P,
config: StorageConfig,
) -> Result<Self>
pub fn open_with_config<P: AsRef<Path>>( path: P, config: StorageConfig, ) -> Result<Self>
Opens a persistent database with custom configuration.
§Arguments
path- Directory path where database files will be storedconfig- Storage configuration options
Sourcepub fn in_memory() -> Result<Self>
pub fn in_memory() -> Result<Self>
Creates an in-memory database.
Data is stored only in memory and will be lost when the database is dropped. Useful for testing or temporary data.
§Examples
use rustlite::Database;
let db = Database::in_memory()?;
db.put(b"temp", b"data")?;
// Data is lost when db goes out of scopeSourcepub fn new() -> Result<Self>
👎Deprecated since 0.2.0: Use Database::open() for persistent storage or Database::in_memory() for temporary storage
pub fn new() -> Result<Self>
Database::open() for persistent storage or Database::in_memory() for temporary storageCreates a new in-memory database (alias for in_memory()).
For backward compatibility with v0.1.0.
Sourcepub fn put(&self, key: &[u8], value: &[u8]) -> Result<()>
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()>
Inserts or updates a key-value pair.
If the key already exists, its value will be updated.
§Arguments
key- The key to insertvalue- The value to associate with the key
§Examples
use rustlite::Database;
let db = Database::open("./data")?;
db.put(b"name", b"Alice")?;
db.put(b"name", b"Bob")?; // Updates the valueSourcepub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>
pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>
Retrieves a value by key.
Returns None if the key doesn’t exist.
§Arguments
key- The key to look up
§Examples
use rustlite::Database;
let db = Database::open("./data")?;
db.put(b"greeting", b"Hello!")?;
match db.get(b"greeting")? {
Some(value) => println!("Found: {}", String::from_utf8_lossy(&value)),
None => println!("Key not found"),
}Sourcepub fn sync(&self) -> Result<()>
pub fn sync(&self) -> Result<()>
Forces all pending writes to disk.
For persistent databases, this flushes the memtable to SSTable and syncs the WAL. For in-memory databases, this is a no-op.
§Examples
use rustlite::Database;
let db = Database::open("./data")?;
db.put(b"important", b"data")?;
db.sync()?; // Ensure data is on diskSourcepub fn is_persistent(&self) -> bool
pub fn is_persistent(&self) -> bool
Returns whether this is a persistent database.