pub struct Store { /* private fields */ }Expand description
§Meta Storage(CAS)
In-memory CAS (content addressed storage) for Rain metadata which basically stores k/v pairs of meta hash and meta bytes, as well as providing functionalities to easliy read/write to the CAS.
Hashes are normal bytes and meta bytes are valid cbor encoded as data bytes.
§Examples
use rain_metadata::Store;
use rain_metadata::meta::cache::MetaCache;
use std::collections::HashMap;
// to instantiate with an empty subgraph list
let mut store = Store::new();
// or to instantiate with initial values
let mut store = Store::create(
&vec!["sg-url-1".to_string()],
&MetaCache::default(),
&HashMap::new(),
);
// add a new subgraph endpoint url to the subgraph list
store.add_subgraphs(&vec!["sg-url-2".to_string()]);
// merge another Store into this one
store.merge(&Store::new());
// updates the meta store with some bytes and the hash they hash to - a
// pair that does not is refused, so the hash is derived rather than picked
let bytes = vec![0u8, 1u8];
let hash = alloy::primitives::keccak256(&bytes).0.to_vec();
store.update_with(&hash, &bytes).unwrap();
// `Store::update(&hash)` is async; it searches each subgraph for `hash` and
// populates the cache with the result. Call it from an async context with `.await`.
// to get a record from the store
let _meta = store.get_meta(&hash);
// Store is agnostic to dotrain contents — it just maps the hash of the content
// to the given uri and puts it as a new meta into the meta cache.
let dotrain_uri = "path/to/file.rain";
let dotrain_content = "/* some dotrain source */";
let (_new_hash, _old_hash) = store
.set_dotrain(dotrain_content, dotrain_uri, false)
.unwrap();
// to get dotrain meta bytes given a uri
let _dotrain_meta_bytes = store.get_dotrain_meta(dotrain_uri);Implementations§
Source§impl Store
impl Store
Sourcepub fn new() -> Store
pub fn new() -> Store
lazily creates a new instance with no subgraphs it is recommended to use create() instead with initial values
Sourcepub fn create(
subgraphs: &Vec<String>,
cache: &MetaCache,
dotrain_cache: &HashMap<String, Vec<u8>>,
) -> Store
pub fn create( subgraphs: &Vec<String>, cache: &MetaCache, dotrain_cache: &HashMap<String, Vec<u8>>, ) -> Store
creates new instance of Store with given initial values it checks the validity of each item of the provided values and only stores those that are valid
Sourcepub fn add_subgraphs(&mut self, subgraphs: &Vec<String>)
pub fn add_subgraphs(&mut self, subgraphs: &Vec<String>)
add new subgraph endpoints
Sourcepub fn get_meta(&self, hash: &[u8]) -> Option<&Vec<u8>>
pub fn get_meta(&self, hash: &[u8]) -> Option<&Vec<u8>>
get the corresponding meta bytes of the given hash if it exists
Sourcepub fn dotrain_cache(&self) -> &HashMap<String, Vec<u8>>
pub fn dotrain_cache(&self) -> &HashMap<String, Vec<u8>>
getter method for the whole dotrain cache
Sourcepub fn get_dotrain_hash(&self, uri: &str) -> Option<&Vec<u8>>
pub fn get_dotrain_hash(&self, uri: &str) -> Option<&Vec<u8>>
get the corresponding dotrain hash of the given dotrain uri if it exists
Sourcepub fn get_dotrain_uri(&self, hash: &[u8]) -> Option<&String>
pub fn get_dotrain_uri(&self, hash: &[u8]) -> Option<&String>
get the corresponding uri of the given dotrain hash if it exists
Sourcepub fn get_dotrain_meta(&self, uri: &str) -> Option<&Vec<u8>>
pub fn get_dotrain_meta(&self, uri: &str) -> Option<&Vec<u8>>
get the corresponding meta bytes of the given dotrain uri if it exists
Sourcepub fn delete_dotrain(&mut self, uri: &str, keep_meta: bool)
pub fn delete_dotrain(&mut self, uri: &str, keep_meta: bool)
deletes a dotrain record given a uri, keep_meta = false drops the meta
bytes only when no other dotrain uri still maps to them
Sourcepub fn merge(&mut self, other: &Store)
pub fn merge(&mut self, other: &Store)
lazilly merges another Store to the current one, avoids duplicates every map keeps the entry this Store already has on a key collision
Sourcepub async fn update(&mut self, hash: &[u8]) -> Result<&Vec<u8>, Error>
pub async fn update(&mut self, hash: &[u8]) -> Result<&Vec<u8>, Error>
updates the meta cache by searching through all subgraphs for the given hash, and returns the reference to the meta bytes in the cache if it was found. Refreshes unconditionally; Self::update_check is the variant that leaves an already cached hash alone.
Sourcepub async fn update_check(&mut self, hash: &[u8]) -> Result<&Vec<u8>, Error>
pub async fn update_check(&mut self, hash: &[u8]) -> Result<&Vec<u8>, Error>
first checks if the meta is stored, if not will perform update()
Sourcepub fn update_with(
&mut self,
hash: &[u8],
bytes: &[u8],
) -> Result<&Vec<u8>, Error>
pub fn update_with( &mut self, hash: &[u8], bytes: &[u8], ) -> Result<&Vec<u8>, Error>
updates the meta cache with the given hash and meta bytes, and returns the reference to the bytes if they were accepted. Leaves an already cached hash alone, as Self::update_check does for the subgraph path.
Sourcepub fn set_dotrain(
&mut self,
text: &str,
uri: &str,
keep_old: bool,
) -> Result<(Vec<u8>, Vec<u8>), Error>
pub fn set_dotrain( &mut self, text: &str, uri: &str, keep_old: bool, ) -> Result<(Vec<u8>, Vec<u8>), Error>
stores (or updates in case the URI already exists) the given dotrain text as meta into the store cache
and maps it to the given uri (path), it should be noted that reading the content of the dotrain is not in
the scope of Store and handling and passing on a correct URI (path) for the given text must be handled
externally by the implementer, keep_old = false drops the replaced meta
bytes only when no other dotrain uri still maps to them
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Store
impl<'de> Deserialize<'de> for Store
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl StructuralPartialEq for Store
Auto Trait Implementations§
impl Freeze for Store
impl RefUnwindSafe for Store
impl Send for Store
impl Sync for Store
impl Unpin for Store
impl UnsafeUnpin for Store
impl UnwindSafe for Store
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<'de, T> BorrowedRpcObject<'de> for T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more