pub struct StorageEngine { /* private fields */ }Expand description
The main storage engine for FlashKV.
This is the “brain” of the database - it stores all key-value pairs and handles concurrent access from multiple client connections.
§Thread Safety
This struct is designed to be wrapped in an Arc and shared across
all client handler tasks. All operations are thread-safe.
§Example
use flashkv::storage::StorageEngine;
use bytes::Bytes;
use std::time::Duration;
let engine = StorageEngine::new();
// Set a key
engine.set(Bytes::from("name"), Bytes::from("Ariz"));
// Get the value
let value = engine.get(&Bytes::from("name"));
assert_eq!(value, Some(Bytes::from("Ariz")));
// Set with expiry
engine.set_with_ttl(Bytes::from("session"), Bytes::from("abc123"), Duration::from_secs(60));Implementations§
Source§impl StorageEngine
impl StorageEngine
Sourcepub fn set(&self, key: Bytes, value: Bytes) -> bool
pub fn set(&self, key: Bytes, value: Bytes) -> bool
Sets a key-value pair without expiry.
If the key already exists, its value is overwritten.
§Returns
Returns true if a new key was created, false if an existing key was updated.
Sourcepub fn set_with_ttl(&self, key: Bytes, value: Bytes, ttl: Duration) -> bool
pub fn set_with_ttl(&self, key: Bytes, value: Bytes, ttl: Duration) -> bool
Sets a key-value pair with a TTL (Time-To-Live).
The key will automatically expire after the specified duration.
§Returns
Returns true if a new key was created, false if an existing key was updated.
Sourcepub fn get(&self, key: &Bytes) -> Option<Bytes>
pub fn get(&self, key: &Bytes) -> Option<Bytes>
Gets the value for a key.
Returns None if the key doesn’t exist or has expired.
This implements “lazy expiry” - expired keys are detected and removed on access.
Sourcepub fn get_entry(&self, key: &Bytes) -> Option<Entry>
pub fn get_entry(&self, key: &Bytes) -> Option<Entry>
Gets the full entry for a key (including metadata).
This is useful for commands like TTL that need access to expiry information.
Sourcepub fn delete(&self, key: &Bytes) -> bool
pub fn delete(&self, key: &Bytes) -> bool
Deletes a key from the database.
§Returns
Returns true if the key was deleted, false if it didn’t exist.
Sourcepub fn delete_many(&self, keys: &[Bytes]) -> u64
pub fn delete_many(&self, keys: &[Bytes]) -> u64
Sourcepub fn exists_many(&self, keys: &[Bytes]) -> u64
pub fn exists_many(&self, keys: &[Bytes]) -> u64
Counts how many of the given keys exist.
Sourcepub fn expire(&self, key: &Bytes, ttl: Duration) -> bool
pub fn expire(&self, key: &Bytes, ttl: Duration) -> bool
Sets an expiry time on an existing key.
§Returns
Returns true if the expiry was set, false if the key doesn’t exist.
Sourcepub fn persist(&self, key: &Bytes) -> bool
pub fn persist(&self, key: &Bytes) -> bool
Removes the expiry from a key (makes it persistent).
§Returns
Returns true if the expiry was removed, false if the key doesn’t exist
or didn’t have an expiry.
Sourcepub fn ttl(&self, key: &Bytes) -> Option<i64>
pub fn ttl(&self, key: &Bytes) -> Option<i64>
Gets the remaining TTL for a key in seconds.
§Returns
Some(seconds)if the key exists and has an expirySome(-1)if the key exists but has no expiryNoneif the key doesn’t exist
Sourcepub fn pttl(&self, key: &Bytes) -> Option<i64>
pub fn pttl(&self, key: &Bytes) -> Option<i64>
Gets the remaining TTL for a key in milliseconds.
Sourcepub fn incr(&self, key: &Bytes) -> Result<i64, &'static str>
pub fn incr(&self, key: &Bytes) -> Result<i64, &'static str>
Increments an integer value by 1.
If the key doesn’t exist, it’s set to 0 before the operation. Returns an error if the value is not a valid integer.
Sourcepub fn incr_by(&self, key: &Bytes, delta: i64) -> Result<i64, &'static str>
pub fn incr_by(&self, key: &Bytes, delta: i64) -> Result<i64, &'static str>
Increments an integer value by a specified amount.
Sourcepub fn decr_by(&self, key: &Bytes, delta: i64) -> Result<i64, &'static str>
pub fn decr_by(&self, key: &Bytes, delta: i64) -> Result<i64, &'static str>
Decrements an integer value by a specified amount.
Sourcepub fn append(&self, key: &Bytes, value: &Bytes) -> usize
pub fn append(&self, key: &Bytes, value: &Bytes) -> usize
Appends a value to an existing string.
If the key doesn’t exist, it’s created with the given value.
§Returns
Returns the length of the string after the append.
Sourcepub fn strlen(&self, key: &Bytes) -> usize
pub fn strlen(&self, key: &Bytes) -> usize
Gets the length of a string value.
Returns 0 if the key doesn’t exist.
Sourcepub fn keys(&self, pattern: &str) -> Vec<Bytes>
pub fn keys(&self, pattern: &str) -> Vec<Bytes>
Returns all keys matching a pattern (simplified glob matching).
Supported patterns:
*matches everythingh*llomatches hello, hallo, hxlloh?llomatches hello, hallo, but not hlloh[ae]llomatches hello and hallo, but not hillo
Warning: This operation scans all keys and can be slow on large databases.
Sourcepub fn flush(&self)
pub fn flush(&self)
Clears all data from the database.
This is equivalent to the Redis FLUSHDB command.
Sourcepub fn len(&self) -> u64
pub fn len(&self) -> u64
Returns the approximate number of keys in the database.
This is an approximation because it uses relaxed atomic ordering.
Sourcepub fn stats(&self) -> StorageStats
pub fn stats(&self) -> StorageStats
Returns database statistics.
Sourcepub fn cleanup_expired(&self) -> u64
pub fn cleanup_expired(&self) -> u64
Cleans up expired keys from all shards.
This is called by the background expiry sweeper.
§Returns
Returns the number of keys that were cleaned up.
Sourcepub fn lpush(&self, key: Bytes, values: Vec<Bytes>) -> usize
pub fn lpush(&self, key: Bytes, values: Vec<Bytes>) -> usize
Pushes one or more values to the left (head) of a list. Creates the list if it doesn’t exist.
§Returns
The length of the list after the push operation.
Sourcepub fn rpush(&self, key: Bytes, values: Vec<Bytes>) -> usize
pub fn rpush(&self, key: Bytes, values: Vec<Bytes>) -> usize
Pushes one or more values to the right (tail) of a list. Creates the list if it doesn’t exist.
§Returns
The length of the list after the push operation.
Sourcepub fn lpop(&self, key: &Bytes) -> Option<Bytes>
pub fn lpop(&self, key: &Bytes) -> Option<Bytes>
Removes and returns the first element (head) of a list.
§Returns
The removed element, or None if the list is empty or doesn’t exist.
Sourcepub fn rpop(&self, key: &Bytes) -> Option<Bytes>
pub fn rpop(&self, key: &Bytes) -> Option<Bytes>
Removes and returns the last element (tail) of a list.
§Returns
The removed element, or None if the list is empty or doesn’t exist.
Sourcepub fn lindex(&self, key: &Bytes, index: i64) -> Option<Bytes>
pub fn lindex(&self, key: &Bytes, index: i64) -> Option<Bytes>
Returns the element at the specified index in a list. Negative indices count from the end (-1 is the last element).
§Returns
The element at the index, or None if index is out of range.
Sourcepub fn lrange(&self, key: &Bytes, start: i64, stop: i64) -> Vec<Bytes>
pub fn lrange(&self, key: &Bytes, start: i64, stop: i64) -> Vec<Bytes>
Returns a range of elements from a list. Both start and stop are inclusive. Negative indices count from the end.
§Returns
A vector of elements in the specified range.
Sourcepub fn lset(&self, key: &Bytes, index: i64, value: Bytes) -> Result<(), String>
pub fn lset(&self, key: &Bytes, index: i64, value: Bytes) -> Result<(), String>
Sets the element at the specified index in a list. Negative indices count from the end.
§Returns
Ok(()) if successful, Err with message if index is out of range or list doesn’t exist.
Sourcepub fn lrem(&self, key: &Bytes, count: i64, value: &Bytes) -> usize
pub fn lrem(&self, key: &Bytes, count: i64, value: &Bytes) -> usize
Removes elements equal to the given value from a list.
- count > 0: Remove
countelements equal to value, from head to tail. - count < 0: Remove
|count|elements equal to value, from tail to head. - count = 0: Remove all elements equal to value.
§Returns
The number of removed elements.
Sourcepub fn list_exists(&self, key: &Bytes) -> bool
pub fn list_exists(&self, key: &Bytes) -> bool
Checks if a key exists as a list.
Sourcepub fn key_type(&self, key: &Bytes) -> &'static str
pub fn key_type(&self, key: &Bytes) -> &'static str
Returns the type of a key (“string”, “list”, or “none”).
Sourcepub fn memory_info(&self) -> MemoryInfo
pub fn memory_info(&self) -> MemoryInfo
Returns memory usage information (approximate).