Database

Struct Database 

Source
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

Source

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")?;
Source

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 stored
  • config - Storage configuration options
Source

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 scope
Source

pub fn new() -> Result<Self>

👎Deprecated since 0.2.0: Use Database::open() for persistent storage or Database::in_memory() for temporary storage

Creates a new in-memory database (alias for in_memory()).

For backward compatibility with v0.1.0.

Source

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 insert
  • value - 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 value
Source

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"),
}
Source

pub fn delete(&self, key: &[u8]) -> Result<bool>

Deletes a key-value pair.

Returns true if the key existed and was deleted, false otherwise.

§Arguments
  • key - The key to delete
§Examples
use rustlite::Database;

let db = Database::open("./data")?;
db.put(b"temp", b"value")?;
db.delete(b"temp")?;
assert_eq!(db.get(b"temp")?, None);
Source

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 disk
Source

pub fn is_persistent(&self) -> bool

Returns whether this is a persistent database.

Trait Implementations§

Source§

impl Clone for Database

Source§

fn clone(&self) -> Database

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.