store.rs

Overview

This file defines an abstraction layer for key-value storage systems using the KeyValueStore trait, designed to facilitate interaction with Aerospike databases or any other key-value stores implementing the same interface. It provides asynchronous-safe methods for retrieving and storing data in the form of Aerospike Bins and Values. The file also includes default batch processing implementations for get and put operations, enabling efficient bulk access patterns.

Types and Traits

ValueMap

pub type ValueMap = HashMap<String, Value>;

KeyValueStore Trait

pub trait KeyValueStore: Send + Sync {
    fn get(
        &self,
        key: &Key,
        values: &Bins,
        label: &'static str,
    ) -> anyhow::Result<Option<ValueMap>>;

    fn batch_get(
        &self,
        gets: Vec<(Key, Bins)>,
        label: &'static str,
    ) -> anyhow::Result<Vec<Option<ValueMap>>>;

    fn put(
        &self,
        key: &Key,
        bins: &[Bin],
        until_success: bool,
        label: &'static str,
    ) -> anyhow::Result<()>;

    fn batch_put(
        &self,
        puts: Vec<(Key, Vec<Bin>)>,
        until_success: bool,
        label: &'static str,
    ) -> anyhow::Result<()>;

    #[cfg(debug_assertions)]
    fn db_reads(&self) -> usize;

    #[cfg(debug_assertions)]
    fn db_writes(&self) -> usize;
}

Purpose

Defines the interface for any key-value store implementation, ensuring thread safety (Send + Sync) and providing methods to:

Methods

get
fn get(
    &self,
    key: &Key,
    values: &Bins,
    label: &'static str,
) -> anyhow::Result<Option<ValueMap>>;
let key = Key::new("test", "users", "user123");
let bins = Bins::All;
let label = "fetch-user";
let result = store.get(&key, &bins, label)?;
if let Some(values) = result {
    // Process values
}
batch_get
fn batch_get(
    &self,
    gets: Vec<(Key, Bins)>,
    label: &'static str,
) -> anyhow::Result<Vec<Option<ValueMap>>>;
let requests = vec![
    (Key::new("test", "users", "user123"), Bins::All),
    (Key::new("test", "users", "user456"), Bins::All),
];
let results = store.batch_get(requests, "batch-fetch")?;
put
fn put(
    &self,
    key: &Key,
    bins: &[Bin],
    until_success: bool,
    label: &'static str,
) -> anyhow::Result<()>;
let bins = vec![Bin::new("name", "Alice"), Bin::new("age", 30)];
store.put(&key, &bins, false, "store-user")?;
batch_put
fn batch_put(
    &self,
    puts: Vec<(Key, Vec<Bin>)>,
    until_success: bool,
    label: &'static str,
) -> anyhow::Result<()>;
let batch_data = vec![
    (Key::new("test", "users", "user123"), vec![Bin::new("name", "Alice")]),
    (Key::new("test", "users", "user456"), vec![Bin::new("name", "Bob")]),
];
store.batch_put(batch_data, false, "batch-store")?;
db_reads and db_writes

Implementation Details

Interaction with Other System Components


Mermaid Diagram

classDiagram
class KeyValueStore {
<<trait>>
+get()
+batch_get()
+put()
+batch_put()
+db_reads()
+db_writes()
}
class ValueMap {
<<type alias>>
}
KeyValueStore ..> ValueMap : uses

This diagram shows the KeyValueStore trait with its main methods and the ValueMap type alias used in method signatures.