Files
pubky-core/mast/src/storage/memory.rs
2023-12-14 19:28:19 +03:00

37 lines
751 B
Rust

//! In memory Mast storage.
use blake3::{Hash, Hasher};
use bytes::{Bytes, BytesMut};
use std::collections::HashMap;
// TODO: storage abstraction.
#[derive(Debug)]
pub struct Storage {
storage: HashMap<Hash, Bytes>,
}
impl Storage {
pub fn new() -> Self {
Self {
storage: HashMap::default(),
}
}
pub fn get(&self, hash: &Hash) -> Option<Bytes> {
self.storage.get(hash).cloned()
}
pub fn insert_bytes(&mut self, bytes: Bytes) -> Hash {
let hash = Hasher::new().update(&bytes).finalize();
// TODO: should I add a prefix here?
self.storage.insert(hash, bytes);
hash
}
}
impl Default for Storage {
fn default() -> Self {
Self::new()
}
}