pub struct Store<V> { /* private fields */ }Expand description
Implementations§
Source§impl<V: StoreValue> Store<V>
impl<V: StoreValue> Store<V>
Sourcepub fn new_in_memory() -> Result<Store<V>>
pub fn new_in_memory() -> Result<Store<V>>
Creates a new in-memory key-value store.
An in-memory key-value store is in practice worse than
a standard HashMap in every way, so the only use of this function
is for creating a key value store for testing.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();Sourcepub fn new_from_file(filename: impl AsRef<Path>) -> Result<Store<V>>
pub fn new_from_file(filename: impl AsRef<Path>) -> Result<Store<V>>
Creates a new store using a file as the storage.
The database is put into WAL (write-ahead logging) mode, so that
readers can proceed while another connection is writing. This is
persistent and creates two sidecar files next to the database
(<file>-wal and <file>-shm). WAL requires all accessing
processes to be on the same machine as the file; it is not
supported on network filesystems such as NFS.
§Arguments
filename- The path to the file used as the storage for the store.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_from_file("mydata.db").unwrap();Sourcepub fn insert(&self, key: &str, value: &V::Ref)
pub fn insert(&self, key: &str, value: &V::Ref)
Inserts a key-value pair in the store. Overwrites any existing value.
§Arguments
key- The key for the value.value- The value to be stored.
§Panics
Panics if the underlying SQLite write fails.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();
kvstore.insert("key", "value");Sourcepub fn contains_key(&self, key: &str) -> bool
pub fn contains_key(&self, key: &str) -> bool
Checks if a particular key is contained in the store.
§Arguments
key- The key to check for existence.
§Panics
Panics if the underlying SQLite query fails.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();
kvstore.insert("key", "value");
assert!(kvstore.contains_key("key"));
assert!(!kvstore.contains_key("nonexistent_key"));Sourcepub fn get(&self, key: &str) -> Option<V>
pub fn get(&self, key: &str) -> Option<V>
Retrieves the value for a given key from the store.
§Arguments
key- The key to retrieve the value for.
§Panics
Panics if the underlying SQLite query fails.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();
kvstore.insert("key", "value");
assert_eq!(kvstore.get("key"), Some("value".to_string()));Sourcepub fn remove(&self, key: &str) -> Option<V>
pub fn remove(&self, key: &str) -> Option<V>
Removes a key-value pair from the store, if present, and returns the old value if it existed.
§Arguments
key- The key to remove.
§Panics
Panics if the underlying SQLite write fails.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();
kvstore.insert("key", "value");
assert_eq!(kvstore.remove("key"), Some("value".to_string()));
assert_eq!(kvstore.get("key"), None);
assert_eq!(kvstore.remove("key"), None);Sourcepub fn clear(&self)
pub fn clear(&self)
Clears the entire table in the store.
This method removes all key-value pairs from the table, effectively clearing the entire store.
§Panics
Panics if the underlying SQLite write fails.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();
kvstore.insert("key1", "value1");
kvstore.insert("key2", "value2");
kvstore.clear();
assert_eq!(kvstore.get("key1"), None);
assert_eq!(kvstore.get("key2"), None);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Checks if the store is empty.
Note: Since the store can be used concurrently, the result of this method can be out of date almost immediately.
§Panics
Panics if the underlying SQLite query fails.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();
assert!(kvstore.is_empty());
kvstore.insert("key", "value");
assert!(!kvstore.is_empty());Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of key-value pairs in the store.
Note: Since the store can be used concurrently, the result of this method can be out of date almost immediately.
§Panics
Panics if the underlying SQLite query fails.
§Examples
use cute_sqlite_kv::KVStore;
let kvstore = KVStore::new_in_memory().unwrap();
assert_eq!(kvstore.len(), 0);
kvstore.insert("key1", "value1");
kvstore.insert("key2", "value2");
assert_eq!(kvstore.len(), 2);