Struct DbCore

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

The main concurrent time-series database core struct.

Implementations§

Source§

impl DbCore

Source

pub fn with_config(config: DbConfig) -> Result<Self, DbError>

Creates a new DbCore instance with the provided configuration.

This sets up the in-memory storage, write buffer, and (if enabled) persistence mechanisms such as the Write-Ahead Log (WAL) and snapshotting. It also spawns the background flush thread, which periodically flushes staged writes to storage and handles snapshot creation.

§Arguments
  • config - The database configuration to use.
§Returns
  • Ok(DbCore) if initialization succeeds.
  • Err(DbError) if any component fails to initialize (e.g., WAL or snapshotter).
§Errors

Returns an error if persistence components cannot be initialized.

Source

pub fn new(flush_interval: Duration) -> Self

Creates a new DbCore instance with default configuration, but with a custom flush interval.

This is a convenience constructor for quickly creating a database with a specific flush interval. All other configuration options use their default values.

§Arguments
  • flush_interval - The interval between automatic buffer flushes.
§Panics

Panics if the database cannot be initialized with the default configuration.

Source

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

Recovers the database state from disk using the latest snapshot and any newer WAL entries.

This method should be called after constructing the database if you want to restore persisted data. It loads the most recent snapshot (if enabled), then applies any WAL entries that occurred after the snapshot.

§Returns
  • Ok(()) if recovery succeeds or if persistence is not enabled.
  • Err(DbError) if recovery fails.
§Errors

Returns an error if loading the snapshot or WAL fails.

Source

pub fn insert( &self, series: &str, timestamp: Timestamp, value: Value, tags: TagSet, ) -> Result<(), DbError>

Inserts a data point into the specified time series.

This method is thread-safe and can be called concurrently from multiple threads. The data point is first staged in the write buffer and will be flushed to storage either automatically (by the background thread) or manually (via flush()). If WAL is enabled, the insert is also logged for durability.

§Arguments
  • series - Name of the time series.
  • timestamp - Timestamp of the data point.
  • value - Value to insert.
  • tags - Associated tags for the data point.
§Returns
  • Ok(()) if the data point is staged successfully.
  • Err(DbError) if staging or logging fails.
§Errors

Returns an error if the WAL or write buffer cannot be accessed.

Source

pub fn query( &self, series: &str, time_range: Range<Timestamp>, tag_filter: Option<&TagSet>, ) -> Result<Vec<(Timestamp, Value)>, DbError>

Queries data points from a specific time series within a given time range, optionally filtering by a set of tags.

This method is thread-safe and allows concurrent queries. It acquires a read lock on the storage and the relevant series chunk, then executes the query in parallel.

§Arguments
  • series - The name of the time series to query.
  • time_range - The time range for the query (start inclusive, end exclusive).
  • tag_filter - An optional set of tags to filter by. Only points matching all tags are returned.
§Returns
  • Ok(Vec<(Timestamp, Value)>) with all matching data points.
  • Err(DbError) if the series does not exist or a lock cannot be acquired.
§Errors

Returns an error if the series is not found or if a lock is poisoned.

Source

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

Triggers an immediate flush of the write buffer to storage.

This sends a command to the background flush thread to flush all staged data points to the in-memory storage. Useful for testing or ensuring data is persisted before shutdown.

§Returns
  • Ok(()) if the flush command is sent successfully.
  • Err(DbError) if the command cannot be sent.
§Errors

Returns an error if the background thread cannot be reached.

Source

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

Triggers an immediate snapshot of the current database state.

This sends a command to the background flush thread to create a snapshot of all in-memory data. Snapshots are only available if enabled in the configuration.

§Returns
  • Ok(()) if the snapshot command is sent successfully.
  • Err(DbError) if snapshots are not enabled or the command cannot be sent.
§Errors

Returns an error if snapshots are disabled or if the background thread cannot be reached.

Source

pub fn get_config(&self) -> &DbConfig

Returns a reference to the current database configuration.

This allows inspection of the configuration used to initialize the database.

§Returns
  • A reference to the DbConfig struct.

Trait Implementations§

Source§

impl Debug for DbCore

Source§

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

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

impl Default for DbCore

Default implementation uses a 1-second flush interval.

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for DbCore

Implement Drop to gracefully shut down the background flush thread.

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl Freeze for DbCore

§

impl !RefUnwindSafe for DbCore

§

impl Send for DbCore

§

impl Sync for DbCore

§

impl Unpin for DbCore

§

impl !UnwindSafe for DbCore

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, 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
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.