Struct rain_metadata::Store
source · 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, meta bytes and ExpressionDeployer reproducible data 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. ExpressionDeployers data are in form of a struct mapped to deployedBytecode meta hash and deploy transaction hash.
§Examples
use rain_meta::Store;
use std::collections::HashMap;
// to instantiate with including default subgraphs
let mut store = Store::new();
// to instatiate with default rain subgraphs included
let mut store = Store::default();
// or to instantiate with initial values
let mut store = Store::create(
&vec!["sg-url-1".to_string()],
&HashMap::new(),
&HashMap::new(),
&HashMap::new(),
true
);
// add a new subgraph endpoint url to the subgraph list
store.add_subgraphs(&vec!["sg-url-2".to_string()]);
// update the store with another Store (merges the stores)
store.merge(&Store::default());
// hash of a meta to search and store
let hash = vec![0u8, 1u8, 2u8];
// updates the meta store with a new meta by searching through subgraphs
store.update(&hash);
// updates the meta store with a new meta hash and bytes
store.update_with(&hash, &vec![0u8, 1u8]);
// to get a record from store
let meta = store.get_meta(&hash);
// to get a deployer record from store
let deployer_record = store.get_deployer(&hash);
// path to a .rain file
let dotrain_uri = "path/to/file.rain";
// reading the dotrain content as an example,
// 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, so obtaining and passing the correct
// content is up to the implementer
let dotrain_content = std::fs::read_to_string(&dotrain_uri).unwrap_or(String::new());
// updates the dotrain cache for a dotrain text and uri
let (new_hash, old_hash) = store.set_dotrain(&dotrain_content, &dotrain_uri.to_string(), false).unwrap();
// to get dotrain meta bytes given a uri
let dotrain_meta_bytes = store.get_dotrain_meta(&dotrain_uri.to_string());Implementations§
source§impl Store
impl Store
sourcepub fn new() -> Store
pub fn new() -> Store
lazily creates a new instance it is recommended to use create() instead with initial values
sourcepub fn create(
subgraphs: &Vec<String>,
cache: &HashMap<Vec<u8>, Vec<u8>>,
deployer_cache: &HashMap<Vec<u8>, NPE2Deployer>,
dotrain_cache: &HashMap<String, Vec<u8>>,
include_rain_subgraphs: bool
) -> Store
pub fn create( subgraphs: &Vec<String>, cache: &HashMap<Vec<u8>, Vec<u8>>, deployer_cache: &HashMap<Vec<u8>, NPE2Deployer>, dotrain_cache: &HashMap<String, Vec<u8>>, include_rain_subgraphs: bool ) -> 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 deployer_cache(&self) -> &HashMap<Vec<u8>, NPE2Deployer>
pub fn deployer_cache(&self) -> &HashMap<Vec<u8>, NPE2Deployer>
getter method for the whole authoring meta cache
sourcepub fn get_deployer(&self, hash: &[u8]) -> Option<&NPE2Deployer>
pub fn get_deployer(&self, hash: &[u8]) -> Option<&NPE2Deployer>
get the corresponding DeployerNPRecord of the given deployer hash if it exists
sourcepub async fn search_deployer(&mut self, hash: &[u8]) -> Option<&NPE2Deployer>
pub async fn search_deployer(&mut self, hash: &[u8]) -> Option<&NPE2Deployer>
searches for DeployerNPRecord in the subgraphs given the deployer hash
sourcepub async fn search_deployer_check(
&mut self,
hash: &[u8]
) -> Option<&NPE2Deployer>
pub async fn search_deployer_check( &mut self, hash: &[u8] ) -> Option<&NPE2Deployer>
if the NPE2Deployer record already is cached it returns it immediately else searches for NPE2Deployer in the subgraphs given the deployer hash
sourcepub fn set_deployer_from_query_response(
&mut self,
deployer_query_response: DeployerResponse
) -> NPE2Deployer
pub fn set_deployer_from_query_response( &mut self, deployer_query_response: DeployerResponse ) -> NPE2Deployer
sets deployer record from the deployer query response
sourcepub fn set_deployer(
&mut self,
hash: &[u8],
npe2_deployer: &NPE2Deployer,
tx_hash: Option<&[u8]>
)
pub fn set_deployer( &mut self, hash: &[u8], npe2_deployer: &NPE2Deployer, tx_hash: Option<&[u8]> )
sets NPE2Deployer record skips if the given hash is invalid
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
sourcepub fn merge(&mut self, other: &Store)
pub fn merge(&mut self, other: &Store)
lazilly merges another Store to the current one, avoids duplicates
sourcepub async fn update(&mut self, hash: &[u8]) -> Option<&Vec<u8>>
pub async fn update(&mut self, hash: &[u8]) -> Option<&Vec<u8>>
updates the meta cache by searching through all subgraphs for the given hash returns the reference to the meta bytes in the cache if it was found
sourcepub async fn update_check(&mut self, hash: &[u8]) -> Option<&Vec<u8>>
pub async fn update_check(&mut self, hash: &[u8]) -> Option<&Vec<u8>>
first checks if the meta is stored, if not will perform update()
sourcepub fn update_with(&mut self, hash: &[u8], bytes: &[u8]) -> Option<&Vec<u8>>
pub fn update_with(&mut self, hash: &[u8], bytes: &[u8]) -> Option<&Vec<u8>>
updates the meta cache by the given hash and meta bytes, checks the hash to bytes validity returns the reference to the bytes if the updated meta bytes contained any
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