pub struct MemoryStore { /* private fields */ }Expand description
Re-export of the in-memory object store.
§Purpose
Stores raw, serialized version control objects in a HashMap residing in
RAM, addressable by their Hash.
§Examples
use libvctrl::{Hash, MemoryStore, ObjectStore};
use std::io::Read;
let mut store = MemoryStore::new();
let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
store.put(&hash, b"data").unwrap();
let mut buf = Vec::new();
store.get(&hash).unwrap().read_to_end(&mut buf).unwrap();
assert_eq!(buf, b"data");An in-memory implementation of the ObjectStore trait.
§Purpose
Stores version control objects in a HashMap residing in RAM. This backend
is primarily intended for testing and ephemeral operations.
§Design rationale
This struct derives Default to allow easy instantiation via
MemoryStore::default() or MemoryStore::new(). The internal HashMap is
kept private to ensure that all modifications go through the ObjectStore
trait implementation, preserving the integrity of the storage interface.
§Examples
Storing and retrieving a blob via streaming:
use libvctrl_core::store::MemoryStore;
use libvctrl_handler::{Hash, ObjectStore};
use std::io::Read;
let mut store = MemoryStore::new();
let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
store.put(&hash, b"my data").unwrap();
assert!(store.exists(&hash).unwrap());
let mut reader = store.get(&hash).unwrap();
let mut buf = Vec::new();
reader.read_to_end(&mut buf).unwrap();
assert_eq!(buf, b"my data");Implementations§
Source§impl MemoryStore
impl MemoryStore
Sourcepub fn new() -> MemoryStore
pub fn new() -> MemoryStore
Creates a new, empty MemoryStore.
§Design rationale
This is a standard constructor that initializes an empty HashMap. It
is functionally equivalent to MemoryStore::default().
§Examples
use libvctrl_core::store::MemoryStore;
use libvctrl_handler::{Hash, ObjectStore};
let store = MemoryStore::new();
let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
assert!(!store.exists(&hash).unwrap());Trait Implementations§
Source§impl Debug for MemoryStore
impl Debug for MemoryStore
Source§impl Default for MemoryStore
impl Default for MemoryStore
Source§fn default() -> MemoryStore
fn default() -> MemoryStore
Source§impl ObjectStore for MemoryStore
impl ObjectStore for MemoryStore
Source§fn put(&mut self, hash: &Hash, data: &[u8]) -> Result<(), VctrlError>
fn put(&mut self, hash: &Hash, data: &[u8]) -> Result<(), VctrlError>
Stores a raw object in memory under the given hash.
§Design rationale
If an object with the same hash already exists, it is overwritten. The
data is copied into a new Vec<u8> to ensure the store owns its own
independent buffer.
§Errors
This implementation is infallible, but returns a Result to satisfy the
trait interface.
§Examples
use libvctrl_core::store::MemoryStore;
use libvctrl_handler::{Hash, ObjectStore};
let mut store = MemoryStore::new();
let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
store.put(&hash, b"blob").unwrap();Source§fn get(&self, hash: &Hash) -> Result<Box<dyn Read + '_>, VctrlError>
fn get(&self, hash: &Hash) -> Result<Box<dyn Read + '_>, VctrlError>
Retrieves a raw object from memory by its hash as a stream.
§Design rationale
Returns a Box<dyn Read>. The internal Vec<u8> is cloned and wrapped
in a std::io::Cursor. This prevents the caller from holding a mutable
reference to the store’s internals and allows for streaming reads.
§Errors
Returns VctrlError::ObjectNotFound
if no object exists for the given hash.
§Examples
use libvctrl_core::store::MemoryStore;
use libvctrl_handler::{Hash, ObjectStore};
use std::io::Read;
let mut store = MemoryStore::new();
let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
store.put(&hash, b"blob").unwrap();
let mut reader = store.get(&hash).unwrap();
let mut buf = Vec::new();
reader.read_to_end(&mut buf).unwrap();
assert_eq!(buf, b"blob");Source§fn delete(&mut self, hash: &Hash) -> Result<(), VctrlError>
fn delete(&mut self, hash: &Hash) -> Result<(), VctrlError>
Deletes an object from memory by its hash.
§Design rationale
This operation is idempotent. If the hash does not exist, the method silently succeeds without returning an error.
§Errors
This implementation is infallible, but returns a Result to satisfy the
trait interface.
§Examples
use libvctrl_core::store::MemoryStore;
use libvctrl_handler::{Hash, ObjectStore};
let mut store = MemoryStore::new();
let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
store.put(&hash, b"blob").unwrap();
store.delete(&hash).unwrap();
assert!(!store.exists(&hash).unwrap());Source§fn exists(&self, hash: &Hash) -> Result<bool, VctrlError>
fn exists(&self, hash: &Hash) -> Result<bool, VctrlError>
Checks if an object exists in memory.
§Design rationale
This is an O(1) operation that checks the keys of the HashMap.
§Errors
This implementation is infallible, but returns a Result to satisfy the
trait interface.
§Examples
use libvctrl_core::store::MemoryStore;
use libvctrl_handler::{Hash, ObjectStore};
let store = MemoryStore::new();
let hash = Hash::from_bytes(&[0u8; 64]).unwrap();
assert!(!store.exists(&hash).unwrap());