Crate ledger_kv

Crate ledger_kv 

Source
Expand description

This module implements a key-value storage system called LedgerKV.

The LedgerKV struct provides methods for inserting, deleting, and retrieving key-value entries. It uses a journaling approach to store the entries in a binary file. Each entry is appended to the file along with its length, allowing efficient retrieval and updates.

The LedgerKV struct maintains an in-memory index of the entries for quick lookups. It uses a HashMap to store the entries, where the key is an enum value representing the label of the entry, and the value is an IndexMap of key-value pairs.

The LedgerKV struct also maintains a metadata file that keeps track of the number of entries, the last offset, and the parent hash of the entries. The parent hash is used to compute the cumulative hash of each entry, ensuring data integrity.

The LedgerKV struct provides methods for inserting and deleting entries, as well as iterating over the entries by label or in raw form. It also supports refreshing the in-memory index and metadata from the binary file.

Example usage:

use std::path::PathBuf;
use ledger_kv::{LedgerKV, EntryLabel, Operation};

fn main() {
    let data_dir = PathBuf::from("data");
    let description = "example";

    // Create a new LedgerKV instance
    let mut ledger_kv = LedgerKV::new(data_dir, description);

    // Insert a new entry
    let label = EntryLabel::Unspecified;
    let key = b"key".to_vec();
    let value = b"value".to_vec();
    ledger_kv.upsert(label.clone(), key.clone(), value.clone()).unwrap();

    // Retrieve all entries
    let entries = ledger_kv.iter(None).collect::<Vec<_>>();
    println!("All entries: {:?}", entries);

    // Delete an entry
    ledger_kv.delete(label, key).unwrap();
}

Structs§

KvEntry
Struct representing a key-value entry.
LedgerKV
Struct representing the LedgerKV.
Metadata
Struct representing the metadata of the ledger.

Enums§

EntryLabel
Enum defining the different labels for entries.
Operation
Enum defining the different operations that can be performed on entries.