pub struct KvStore<'conn> { /* private fields */ }Expand description
A handle to one named key-value store, backed by KV_TABLE.
Borrows its Connection for the handle’s lifetime ('conn), matching
the crate’s Catalog/Inserter
borrow convention. Open one with
Connection::kv_store.
§Examples
use hyperdb_api::{Connection, CreateMode, Result};
fn main() -> Result<()> {
let conn = Connection::connect("localhost:7483", "app.hyper", CreateMode::CreateIfNotExists)?;
let kv = conn.kv_store("settings")?;
kv.set("theme", "dark")?;
assert_eq!(kv.get("theme")?, Some("dark".to_string()));
Ok(())
}Implementations§
Source§impl<'conn> KvStore<'conn>
impl<'conn> KvStore<'conn>
Sourcepub fn get(&self, key: &str) -> Result<Option<String>>
pub fn get(&self, key: &str) -> Result<Option<String>>
Returns the value for key, or None if the key is absent or NULL.
§Errors
Error::InvalidNameifkeyis invalid.Error::FeatureNotSupportedon gRPC transport.Error::Serverif the query fails.
Sourcepub fn set(&self, key: &str, value: &str) -> Result<SetOutcome>
pub fn set(&self, key: &str, value: &str) -> Result<SetOutcome>
Sets key to value, inserting or overwriting (upsert). Returns
SetOutcome indicating whether the key was newly created.
§Errors
Error::InvalidNameifkeyis invalid.Error::FeatureNotSupportedon gRPC transport.Error::Serverif theUPDATE/INSERTfails.
Sourcepub fn get_as<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
pub fn get_as<T: DeserializeOwned>(&self, key: &str) -> Result<Option<T>>
Deserializes the JSON-encoded value for key into T.
Returns None if the key is absent.
§Errors
Error::InvalidNameifkeyis invalid.Error::Serializationif the stored value is not valid JSON forT.Error::FeatureNotSupported/Error::Serveras forget.
Sourcepub fn set_as<T: Serialize>(&self, key: &str, value: &T) -> Result<SetOutcome>
pub fn set_as<T: Serialize>(&self, key: &str, value: &T) -> Result<SetOutcome>
Serializes value to JSON and stores it under key (upsert). Returns
SetOutcome indicating whether the key was newly created.
§Errors
Error::InvalidNameifkeyis invalid.Error::Serializationifvaluecannot be serialized to JSON.Error::FeatureNotSupported/Error::Serveras forset.
Sourcepub fn set_if_absent(&self, key: &str, value: &str) -> Result<bool>
pub fn set_if_absent(&self, key: &str, value: &str) -> Result<bool>
Inserts value under key only if key is absent.
Returns true if a row was written, false if the key already existed
(in which case nothing is written). A single INSERT ... WHERE NOT EXISTS statement decides, so there is no check-then-write race within a
connection. The backing table has no unique constraint on
(store_name, key), so two separate processes writing the same key to
a shared persistent store concurrently could both pass NOT EXISTS and
double-insert; within a single process (including the MCP daemon, which
serializes engine access) the guard is exact.
§Errors
Error::InvalidNameifkeyis invalid.Error::FeatureNotSupportedon gRPC transport.Error::Serverif theINSERTfails.
Sourcepub fn delete(&self, key: &str) -> Result<bool>
pub fn delete(&self, key: &str) -> Result<bool>
Deletes key; returns true if a row was removed.
§Errors
Error::InvalidNameifkeyis invalid.Error::FeatureNotSupported/Error::Server.
Sourcepub fn exists(&self, key: &str) -> Result<bool>
pub fn exists(&self, key: &str) -> Result<bool>
Returns whether key is present in this store.
§Errors
Error::InvalidNameifkeyis invalid.Error::FeatureNotSupported/Error::Server.
Sourcepub fn byte_size(&self) -> Result<i64>
pub fn byte_size(&self) -> Result<i64>
Returns the total byte length of all values in this store
(SUM(OCTET_LENGTH(value))). Returns 0 for an empty store; NULL
values contribute 0 via COALESCE.
§Errors
Sourcepub fn entries(&self) -> Result<Vec<(String, String)>>
pub fn entries(&self) -> Result<Vec<(String, String)>>
Returns this store’s (key, value) pairs, sorted by key ascending.
Materializes the whole store — intended for small scratchpad stores.
§Errors
Sourcepub fn clear(&self) -> Result<u64>
pub fn clear(&self) -> Result<u64>
Deletes every key in this store; returns the number removed.
The shared backing table survives; only this store’s rows are removed.
§Errors
Sourcepub fn pop(&self) -> Result<Option<(String, String)>>
pub fn pop(&self) -> Result<Option<(String, String)>>
Removes and returns the lowest-ordered key/value pair, or None if empty.
The peek and delete run in one transaction, so they apply atomically — either both the read and the delete commit, or neither does (on error the transaction is rolled back). A SQL-NULL value is returned as an empty string.
§Errors
Sourcepub fn set_batch(&self, entries: &[(&str, &str)]) -> Result<BatchSetOutcome>
pub fn set_batch(&self, entries: &[(&str, &str)]) -> Result<BatchSetOutcome>
Upserts every (key, value) pair in one transaction. Returns
BatchSetOutcome reporting how many keys were newly inserted vs.
overwritten.
All keys are validated before the transaction opens, so an invalid key aborts the whole batch without writing anything.
§Errors
Error::InvalidNameif any key is invalid (checked before writing).Error::FeatureNotSupported/Error::Server.
Sourcepub fn set_batch_if_absent(
&self,
entries: &[(&str, &str)],
) -> Result<BatchGuardOutcome>
pub fn set_batch_if_absent( &self, entries: &[(&str, &str)], ) -> Result<BatchGuardOutcome>
Inserts every absent (key, value) pair in one transaction, skipping
keys that already exist. All keys are validated before the transaction
opens, so an invalid key aborts the whole batch without writing anything.
§Errors
Error::InvalidNameif any key is invalid (checked before writing).Error::FeatureNotSupported/Error::Server.
Trait Implementations§
Auto Trait Implementations§
impl<'conn> !RefUnwindSafe for KvStore<'conn>
impl<'conn> !UnwindSafe for KvStore<'conn>
impl<'conn> Freeze for KvStore<'conn>
impl<'conn> Send for KvStore<'conn>
impl<'conn> Sync for KvStore<'conn>
impl<'conn> Unpin for KvStore<'conn>
impl<'conn> UnsafeUnpin for KvStore<'conn>
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request