pub struct DbWithTtl { /* private fields */ }Expand description
A Db wrapper that attaches a wall-clock TTL to every written
value. Entries whose embedded timestamp is older than ttl_seconds
read as None and are physically reclaimed at the next compaction.
ttl_seconds == 0 disables expiration entirely - the wrapper still
appends the TTL suffix for format consistency, but the filter
keeps every entry and reads never treat any value as expired.
Implementations§
Source§impl DbWithTtl
impl DbWithTtl
Sourcepub fn open<P: AsRef<Path>>(
path: P,
opts: Options,
ttl_seconds: u64,
) -> Result<Self>
pub fn open<P: AsRef<Path>>( path: P, opts: Options, ttl_seconds: u64, ) -> Result<Self>
Open or create a TTL-enabled database at path. The caller’s
opts are forwarded to Db::open with one override: the
compaction filter slot is replaced with a TtlCompactionFilter
bound to ttl_seconds. Any user-supplied filter is dropped and
logged via tracing::warn.
Sourcepub fn put(&self, key: &[u8], value: &[u8]) -> Result<()>
pub fn put(&self, key: &[u8], value: &[u8]) -> Result<()>
Write key → value with the current wall-clock timestamp
appended. The in-memory value is the stamped form until a
matching DbWithTtl::get strips it off.
Sourcepub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>
pub fn get(&self, key: &[u8]) -> Result<Option<Vec<u8>>>
Look up key. Returns Ok(None) if the key is absent or if
its embedded timestamp has aged beyond ttl_seconds. The
trailing TTL suffix is always stripped from the returned value.
Sourcepub fn get_slice(&self, key: &[u8]) -> Result<Option<DbSlice>>
pub fn get_slice(&self, key: &[u8]) -> Result<Option<DbSlice>>
DbWithTtl::get without copying the value.
The TTL suffix is dropped by narrowing the view, so the result
still borrows the bytes the database already owns. Same absence
semantics as DbWithTtl::get: an expired entry, and a value
with no decodable TTL suffix, both read as Ok(None).
Sourcepub fn delete(&self, key: &[u8]) -> Result<()>
pub fn delete(&self, key: &[u8]) -> Result<()>
Delete key. Range deletes are delegated to the inner
Db::delete - tombstones don’t carry timestamps.
Sourcepub fn delete_range(&self, start: &[u8], end: &[u8]) -> Result<()>
pub fn delete_range(&self, start: &[u8], end: &[u8]) -> Result<()>
Delete every key in [start, end). Same semantics as
Db::delete_range; TTL has no effect on range tombstones.
Sourcepub fn scan(
&self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Result<Vec<(Vec<u8>, Vec<u8>)>>
pub fn scan( &self, start: Option<&[u8]>, end: Option<&[u8]>, ) -> Result<Vec<(Vec<u8>, Vec<u8>)>>
Scan [start, end), returning (key, value) pairs where
every value has its trailing TTL suffix stripped and every
expired entry is omitted. Ordering matches Db::scan.
Sourcepub fn write(&self, batch: WriteBatch) -> Result<()>
pub fn write(&self, batch: WriteBatch) -> Result<()>
Apply a batch of point writes atomically. Each put in the
batch gets its value stamped with the current timestamp; each
delete is passed through unchanged.
The input batch’s original contents are consumed and the stamped/delegated version is applied.
Sourcepub fn inner(&self) -> &Db
pub fn inner(&self) -> &Db
Borrow the underlying Db for APIs that DbWithTtl does
not wrap (streaming iterator, snapshots, compact_range).
Values read through the inner Db still carry their trailing
TTL suffix - callers are responsible for stripping it via
strip_timestamp if they want the user payload.
Sourcepub fn compact_range(
&self,
start: Option<&[u8]>,
end: Option<&[u8]>,
) -> Result<()>
pub fn compact_range( &self, start: Option<&[u8]>, end: Option<&[u8]>, ) -> Result<()>
Synchronously compact every SSTable overlapping [start, end)
down the tree. Expired entries are physically removed via the
installed TtlCompactionFilter.