hashiverse_server_lib/environment/
environment_store.rs1use bytes::Bytes;
21use crate::environment::environment::PostBundleMetadata;
22use anyhow::Context;
23use hashiverse_lib::tools::time::{TimeMillis, TimeMillisBytes};
24use hashiverse_lib::tools::types::Id;
25use std::collections::HashMap;
26use hashiverse_lib::protocol::posting::encoded_post_feedback::EncodedPostFeedbackV1;
27
28pub trait EnvironmentStore: Sync + Send {
29 fn post_bundle_count(&self) -> anyhow::Result<usize>;
30 fn post_bundle_metadata_get(&self, location_id: &Id) -> anyhow::Result<Option<PostBundleMetadata>>;
31 fn post_bundle_metadata_put(&self, location_id: &Id, post_bundle_metadata: &PostBundleMetadata) -> anyhow::Result<()>;
32 fn post_bundle_bytes_get(&self, location_id: &Id) -> anyhow::Result<Option<Bytes>>;
33 fn post_bundle_bytes_put(&self, location_id: &Id, bytes: &[u8]) -> anyhow::Result<()>;
34 fn post_bundles_last_accessed_flush(&self, post_bundles_last_accessed: &HashMap<Id, TimeMillis>) -> anyhow::Result<()>;
35 fn post_bundles_delete(&self, location_ids: &[Id]) -> anyhow::Result<()>;
36 fn post_bundles_last_accessed_iter(&self, location_id: &Id) -> Box<dyn Iterator<Item = Result<(Id, TimeMillisBytes), anyhow::Error>> + '_>;
37 fn config_get_bytes(&self, key: &str) -> anyhow::Result<Option<Vec<u8>>>;
38 fn config_put_bytes(&self, key: &str, v: Vec<u8>) -> anyhow::Result<()>;
39 fn config_get_usize(&self, key: &str) -> anyhow::Result<Option<usize>> {
40 let Some(bytes_guard) = self.config_get_bytes(key)?
41 else {
42 return Ok(None);
43 };
44
45 let bytes: &[u8] = bytes_guard.as_ref();
46 let arr: [u8; size_of::<usize>()] = bytes.try_into().with_context(|| format!("stored data has wrong byte length: {}", bytes.len()))?;
47
48 Ok(Some(usize::from_be_bytes(arr)))
49 }
50 fn config_put_usize(&self, key: &str, v: usize) -> anyhow::Result<()> {
51 self.config_put_bytes(key, v.to_be_bytes().to_vec())?;
52 Ok(())
53 }
54
55 fn post_bundle_feedbacks_bytes_get(&self, post_bundle_location_id: &Id) -> anyhow::Result<Bytes>;
59
60 fn post_feedback_put_if_more_powerful(&self, location_id: &Id, encoded_post_feedback: &EncodedPostFeedbackV1) -> anyhow::Result<()>;
62}