Skip to main content

MemoryStore

Struct MemoryStore 

Source
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

Source

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

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for MemoryStore

Source§

fn default() -> MemoryStore

Returns the “default value” for a type. Read more
Source§

impl ObjectStore for MemoryStore

Source§

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>

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>

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>

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

Auto Trait Implementations§

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.