Skip to main content

Db

Struct Db 

Source
pub struct Db { /* private fields */ }
Expand description

The main database handle.

Provides a high-level, thread-safe API for reading and writing key-value pairs with automatic background flushing and compaction.

§Thread safety

Db is Send + Sync — it can be shared across threads via Arc<Db>.

§Background compaction

When the write buffer fills, the active memtable is frozen and a background task is dispatched to:

  1. Flush the frozen memtable to a new SSTable.
  2. Run minor compaction if size-tiered thresholds are met.
  3. Run tombstone compaction if the tombstone ratio is high enough.

Major compaction must be triggered explicitly via Db::major_compact.

§Shutdown

Call Db::close for a graceful shutdown. If the handle is dropped without calling close, the destructor will attempt cleanup, but errors are silently ignored.

Implementations§

Source§

impl Db

Source

pub fn open(path: impl AsRef<Path>, config: DbConfig) -> Result<Self, DbError>

Opens (or creates) a database at the given directory.

On a fresh directory the required sub-directories are created automatically. On an existing directory, the manifest and WALs are replayed to recover the last durable state.

§Errors
  • DbError::InvalidConfig — a configuration parameter is out of its documented bounds.
  • DbError::Engine — the directory could not be created, the manifest/WAL could not be opened or replayed, or I/O failed during recovery.
Source

pub fn close(&self) -> Result<(), DbError>

Gracefully shuts down the database.

Waits for all in-flight background tasks to complete, flushes remaining frozen memtables, checkpoints the manifest, and fsyncs all directories.

Subsequent operations on this handle return DbError::Closed. Calling close more than once is harmless.

§Errors
Source

pub fn put(&self, key: &[u8], value: &[u8]) -> Result<(), DbError>

Inserts or updates a key-value pair.

The write is persisted to the WAL before being applied in memory. If the write buffer is full, the active memtable is frozen and a background flush is scheduled automatically.

§Errors
Source

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

Deletes a key by inserting a point tombstone.

Subsequent reads return None until a new value is written.

§Errors
Source

pub fn delete_range(&self, start: &[u8], end: &[u8]) -> Result<(), DbError>

Deletes all keys in the half-open range [start, end).

§Errors
Source

pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>, DbError>

Retrieves the value associated with a key.

Returns Ok(None) if the key does not exist or has been deleted.

§Errors
Source

pub fn scan(&self, start: &[u8], end: &[u8]) -> Result<Vec<KeyValue>, DbError>

Scans all live key-value pairs in the half-open range [start, end).

Returns pairs sorted by key in ascending order. Deleted keys are excluded.

Returns an empty Vec if the range contains no live keys.

§Errors
Source

pub fn major_compact(&self) -> Result<bool, DbError>

Runs a full major compaction, merging all SSTables into one.

This is a blocking operation. All range tombstones are applied and all spent tombstones are dropped from the output.

Returns true if compaction was performed, false if there were fewer than 2 SSTables.

§Errors

Trait Implementations§

Source§

impl Debug for Db

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Db

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl !Freeze for Db

§

impl RefUnwindSafe for Db

§

impl Send for Db

§

impl Sync for Db

§

impl Unpin for Db

§

impl UnsafeUnpin for Db

§

impl UnwindSafe for Db

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. 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.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more