Skip to main content

Store

Struct Store 

Source
pub struct Store<V> { /* private fields */ }
Expand description

A simple key-value store backed by SQLite, generic over its value type.

You will normally use one of the aliases KVStore (string values) or BlobStore (binary values) rather than naming Store directly.

Implementations§

Source§

impl<V: StoreValue> Store<V>

Source

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();
Source

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();
Source

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");
Source

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"));
Source

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()));
Source

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);
Source

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);
Source

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());
Source

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);

Auto Trait Implementations§

§

impl<V> !Freeze for Store<V>

§

impl<V> !RefUnwindSafe for Store<V>

§

impl<V> !Sync for Store<V>

§

impl<V> !UnwindSafe for Store<V>

§

impl<V> Send for Store<V>

§

impl<V> Unpin for Store<V>

§

impl<V> UnsafeUnpin for Store<V>

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, 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.