pub struct ConstMap<K: Key + Send + Sync + Hash + Eq, const V: usize, D: Durability = Bitcask, H: WriteHook<K> = NoHook> { /* private fields */ }Expand description
A map with fixed-size keys and values. All values are stored inline in a per-shard HashMap.
Reads never touch disk — zero I/O reads. O(1) lookup instead of O(log n) SkipList.
No ordered iteration — use ConstTree if you need prefix/range scans.
Generic over D: Durability (default: Bitcask). Use Fixed backend for
frequent updates without compaction (FixedMap is a type alias for ConstMap<K, V, Fixed>).
Each ConstMap owns its storage engine — one map = one database directory.
§Write hooks
Uses WriteHook<K>. on_write fires on put/insert/delete/cas/update.
Does not fire inside atomic(). on_init fires once per live entry
during migrate() or replay_init(). Old value is always provided in
on_write — NEEDS_OLD_VALUE is ignored (value is inline, zero cost).
Implementations§
Source§impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ConstMap<K, V, Bitcask, H>
impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ConstMap<K, V, Bitcask, H>
Sourcepub fn open_hooked(
path: impl AsRef<Path>,
config: Config,
hook: H,
) -> DbResult<Self>
pub fn open_hooked( path: impl AsRef<Path>, config: Config, hook: H, ) -> DbResult<Self>
Open or create a ConstMap with a write hook for secondary index maintenance.
Sourcepub fn close(self) -> DbResult<()>
pub fn close(self) -> DbResult<()>
Graceful shutdown: write hint files (if enabled), flush write buffers + fsync.
Sourcepub fn flush_buffers(&self) -> DbResult<()>
pub fn flush_buffers(&self) -> DbResult<()>
Flush all shard write buffers to disk (without fsync).
Sourcepub fn sync_hints(&self) -> DbResult<()>
pub fn sync_hints(&self) -> DbResult<()>
Write hint files for all active shard files. Call during graceful shutdown.
Source§impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ConstMap<K, V, Fixed, H>
impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ConstMap<K, V, Fixed, H>
Sourcepub fn open_with_hook(
path: impl AsRef<Path>,
config: FixedConfig,
hook: H,
) -> DbResult<Self>
pub fn open_with_hook( path: impl AsRef<Path>, config: FixedConfig, hook: H, ) -> DbResult<Self>
Open or create a ConstMap with a write hook, using Fixed (fixed-slot) backend.
Source§impl<K: Key + Send + Sync + Hash + Eq, const V: usize, D: Durability, H: WriteHook<K>> ConstMap<K, V, D, H>
impl<K: Key + Send + Sync + Hash + Eq, const V: usize, D: Durability, H: WriteHook<K>> ConstMap<K, V, D, H>
Sourcepub fn get(&self, key: &K) -> Option<[u8; V]>
pub fn get(&self, key: &K) -> Option<[u8; V]>
Get a value by key. O(1) HashMap lookup, zero disk I/O.
Sourcepub fn get_or_err(&self, key: &K) -> DbResult<[u8; V]>
pub fn get_or_err(&self, key: &K) -> DbResult<[u8; V]>
Get a value by key, returning Err(KeyNotFound) if absent.
Sourcepub fn put(&self, key: &K, value: &[u8; V]) -> DbResult<Option<[u8; V]>>
pub fn put(&self, key: &K, value: &[u8; V]) -> DbResult<Option<[u8; V]>>
Insert or update a key-value pair. Returns the old value if the key existed.
Sourcepub fn insert(&self, key: &K, value: &[u8; V]) -> DbResult<()>
pub fn insert(&self, key: &K, value: &[u8; V]) -> DbResult<()>
Insert a key-value pair only if the key does not exist.
Returns Err(KeyExists) if the key is already present.
Sourcepub fn delete(&self, key: &K) -> DbResult<Option<[u8; V]>>
pub fn delete(&self, key: &K) -> DbResult<Option<[u8; V]>>
Delete a key. Returns the old value if the key existed.
Sourcepub fn cas(
&self,
key: &K,
expected: &[u8; V],
new_value: &[u8; V],
) -> DbResult<()>
pub fn cas( &self, key: &K, expected: &[u8; V], new_value: &[u8; V], ) -> DbResult<()>
Compare-and-swap: if current value == expected, replace with new_value.
Returns Ok(()) on success, Err(CasMismatch) if current != expected,
Err(KeyNotFound) if key doesn’t exist.
Sourcepub fn update(
&self,
key: &K,
f: impl FnOnce(&[u8; V]) -> [u8; V],
) -> DbResult<Option<[u8; V]>>
pub fn update( &self, key: &K, f: impl FnOnce(&[u8; V]) -> [u8; V], ) -> DbResult<Option<[u8; V]>>
Atomically read-modify-write. Returns Some(new_value) if key existed, None otherwise.
The closure must not be heavy (shard lock is held).
Sourcepub fn fetch_update(
&self,
key: &K,
f: impl FnOnce(&[u8; V]) -> [u8; V],
) -> DbResult<Option<[u8; V]>>
pub fn fetch_update( &self, key: &K, f: impl FnOnce(&[u8; V]) -> [u8; V], ) -> DbResult<Option<[u8; V]>>
Like update(), but returns Some(old_value) instead of the new one.
pub fn is_empty(&self) -> bool
Sourcepub fn atomic<R>(
&self,
shard_key: &K,
f: impl FnOnce(&mut ConstMapShard<'_, K, V, D, H>) -> DbResult<R>,
) -> DbResult<R>
pub fn atomic<R>( &self, shard_key: &K, f: impl FnOnce(&mut ConstMapShard<'_, K, V, D, H>) -> DbResult<R>, ) -> DbResult<R>
Atomically execute multiple operations on a single shard.
All keys must route to the same shard as shard_key.
The closure must be short — shard lock is held for its duration.
pub fn shard_for(&self, key: &K) -> usize
Trait Implementations§
Source§impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> CompactionIndex<K> for ConstMap<K, V, Bitcask, H>
impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> CompactionIndex<K> for ConstMap<K, V, Bitcask, H>
Source§fn update_if_match(&self, key: &K, old_loc: DiskLoc, new_loc: DiskLoc) -> bool
fn update_if_match(&self, key: &K, old_loc: DiskLoc, new_loc: DiskLoc) -> bool
old_loc, it is updated to new_loc and returns true.Source§fn contains_key(&self, key: &K) -> bool
fn contains_key(&self, key: &K) -> bool
Source§impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ReplicationTarget for ConstMap<K, V, Bitcask, H>
Available on crate feature replication only.
impl<K: Key + Send + Sync + Hash + Eq, const V: usize, H: WriteHook<K>> ReplicationTarget for ConstMap<K, V, Bitcask, H>
replication only.Source§fn apply_entry(
&self,
shard_id: u8,
file_id: u32,
entry_offset: u64,
header: &EntryHeader,
key: &[u8],
value: &[u8],
) -> DbResult<()>
fn apply_entry( &self, shard_id: u8, file_id: u32, entry_offset: u64, header: &EntryHeader, key: &[u8], value: &[u8], ) -> DbResult<()>
Source§fn try_apply_entry(
&self,
shard_id: u8,
file_id: u32,
entry_offset: u64,
header: &EntryHeader,
raw_after_header: &[u8],
) -> DbResult<bool>
fn try_apply_entry( &self, shard_id: u8, file_id: u32, entry_offset: u64, header: &EntryHeader, raw_after_header: &[u8], ) -> DbResult<bool>
Auto Trait Implementations§
impl<K, const V: usize, D, H> Freeze for ConstMap<K, V, D, H>
impl<K, const V: usize, D = Bitcask, H = NoHook> !RefUnwindSafe for ConstMap<K, V, D, H>
impl<K, const V: usize, D, H> Send for ConstMap<K, V, D, H>
impl<K, const V: usize, D, H> Sync for ConstMap<K, V, D, H>
impl<K, const V: usize, D, H> Unpin for ConstMap<K, V, D, H>
impl<K, const V: usize, D, H> UnsafeUnpin for ConstMap<K, V, D, H>where
D: UnsafeUnpin,
H: UnsafeUnpin,
impl<K, const V: usize, D, H> UnwindSafe for ConstMap<K, V, D, H>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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