Skip to main content

Engine

Struct Engine 

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

The core LSM-tree storage engine.

Engine provides a fully synchronous key-value API with WAL durability, Bloom filters, block cache, and background flush/compaction/GC.

§Opening

use flowdb::{Engine, Config};

let engine = Engine::open(Config::default()).unwrap();

§Writing

use flowdb::Record;

engine.write_batch_owned(vec![
    Record::new("sensor:temp", 1_700_000_000_000, b"22.5".to_vec()),
]).unwrap();

§Reading

use flowdb::Query;

for rec in engine.query(Query::prefix("sensor:")).unwrap() {
    println!("{}", rec.key_str());
}

§Shutdown

engine.shutdown().unwrap();

If the engine is behind an Arc, use Engine::close instead.

Implementations§

Source§

impl Engine

Source

pub fn spawn_background_maintenance(&self) -> Option<MaintenanceHandle>

Spawn a background maintenance thread that periodically flushes the memtable to SSTable, runs compaction, garbage-collects expired SST files, and syncs the WAL. The thread runs until the returned MaintenanceHandle is dropped or the engine is shut down.

Unlike an async runtime approach, this uses a plain OS thread and does not require a Tokio context, making FlowDB fully runtime-agnostic.

Source

pub fn open(config: Config) -> Result<Self>

Open (or create) an engine at the configured data directory.

On first call with create_if_missing: true (default), the data directory and its sub-directories (WAL/, SST/, INDEX/) are created automatically.

If the directory already contains valid data (WAL + SST files), the engine recovers by replaying the WAL from the last flushed sequence number.

Source

pub fn write_batch(&self, batch: &[Record]) -> Result<()>

Write a batch of records using the engine’s default TTL.

This is a borrowed-input variant — the caller retains ownership of batch.

Source

pub fn write_batch_owned(&self, batch: Vec<Record>) -> Result<()>

Write a batch of records (owned input, no TTL override).

Equivalent to write_batch but takes ownership of the Vec, which can avoid a clone in some call sites.

Source

pub fn write_batch_sync(&self, batch: Vec<Record>) -> Result<()>

Write a batch synchronously, bypassing the background write pipeline.

This method encodes, WAL-logs, and memtable-inserts on the calling thread. It is useful when the caller needs a synchronous durability guarantee without waiting for the background writer.

Source

pub fn write_batch_ttl( &self, batch: &[Record], ttl_secs: Option<u64>, ) -> Result<()>

Source

pub fn query(&self, query: Query) -> Result<Vec<Record>>

Source

pub fn query_by_prefix(&self, key: &str) -> Result<Vec<Record>>

Source

pub fn query_by_key_range(&self, start: &str, end: &str) -> Result<Vec<Record>>

Source

pub fn query_time_range(&self, start: i64, end: i64) -> Result<Vec<Record>>

Source

pub fn query_prefix_time_range( &self, key: &str, start: i64, end: i64, ) -> Result<Vec<Record>>

Source

pub fn query_key_time_range( &self, start_key: &str, end_key: &str, start: i64, end: i64, ) -> Result<Vec<Record>>

Source

pub fn get(&self, key: &str, ts: i64) -> Result<Option<Record>>

Source

pub fn get_sync(&self, key: &str, ts: i64) -> Option<Record>

Source

pub fn delete_batch(&self, keys_ts: &[(String, i64)]) -> Result<()>

Source

pub fn delete_range(&self, start_key: &str, end_key: &str) -> Result<()>

Source

pub fn patch_record( &self, key: &str, ts: i64, new_value: Option<Vec<u8>>, new_ttl_secs: Option<u64>, ) -> Result<Record>

Source

pub fn stats(&self) -> EngineStats

Source

pub fn metrics_text(&self) -> String

Source

pub fn flush(&self) -> Result<()>

Source

pub fn trigger_gc(&self) -> Result<u64>

Source

pub fn trigger_compaction(&self) -> Result<bool>

Source

pub fn shutdown(self) -> Result<()>

Shut down the engine, flushing the memtable and WAL. Consumes self — use Engine::close if the engine is behind an Arc.

Source

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

Flush the memtable and WAL without consuming self.

This is the Arc<Engine>-friendly alternative to shutdown. Use it when the engine is shared across threads. The background maintenance thread (if any) is NOT stopped — drop the MaintenanceHandle first, or simply let Engine::shutdown consume it.

Source

pub fn scan(&self, range: ScanRange) -> Result<ScanIterator>

Lazy iterator scan with default ReadOptions.

Source

pub fn scan_opt( &self, range: ScanRange, _opts: &ReadOptions, ) -> Result<ScanIterator>

Lazy iterator scan with caller-provided ReadOptions.

Source

pub fn scan_prefix(&self, prefix: &str) -> Result<ScanIterator>

Convenience: prefix scan returning a lazy iterator.

Source

pub fn scan_prefix_time_range( &self, prefix: &str, ts_start: i64, ts_end: i64, ) -> Result<ScanIterator>

Convenience: prefix + time-range scan returning a lazy iterator.

Source

pub fn get_latest(&self, key: &str) -> Result<Option<Record>>

Get the latest record for a given key (highest ts).

Unlike the old scan-then-last approach which is O(n) in the number of versions, this walks memtables and SST blocks via the block index, yielding O(log n) or O(k) where k is the number of blocks.

Auto Trait Implementations§

§

impl !Freeze for Engine

§

impl !RefUnwindSafe for Engine

§

impl !UnwindSafe for Engine

§

impl Send for Engine

§

impl Sync for Engine

§

impl Unpin for Engine

§

impl UnsafeUnpin for Engine

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, 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