use std::path::PathBuf;
use linera_base::command::resolve_binary;
use linera_views::{
lru_caching::LruCachingConfig,
store::{CommonStoreInternalConfig, KeyValueStoreError},
};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tonic::Status;
pub const MAX_PAYLOAD_SIZE: usize = 4000000;
#[repr(u8)]
pub enum KeyPrefix {
Key,
Namespace,
RootKey,
}
#[derive(Debug, Error)]
pub enum ServiceStoreError {
#[error("Not matching entry")]
NotMatchingEntry,
#[error("Failed to find the linera-storage-server binary")]
FailedToFindStorageServerBinary,
#[error(transparent)]
GrpcError(#[from] Status),
#[error("The key size must be at most 1 MB")]
KeyTooLong,
#[error(transparent)]
TransportError(#[from] tonic::transport::Error),
#[error(transparent)]
VarError(#[from] std::env::VarError),
#[error(transparent)]
BcsError(#[from] bcs::Error),
}
impl KeyValueStoreError for ServiceStoreError {
const BACKEND: &'static str = "service";
}
pub fn storage_service_test_endpoint() -> Result<String, ServiceStoreError> {
Ok(std::env::var("LINERA_STORAGE_SERVICE")?)
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ServiceStoreInternalConfig {
pub endpoint: String,
pub common_config: CommonStoreInternalConfig,
}
pub type ServiceStoreConfig = LruCachingConfig<ServiceStoreInternalConfig>;
impl ServiceStoreInternalConfig {
pub fn http_address(&self) -> String {
format!("http://{}", self.endpoint)
}
}
pub async fn get_service_storage_binary() -> Result<PathBuf, ServiceStoreError> {
let binary = resolve_binary("linera-storage-server", "linera-storage-service").await;
if let Ok(binary) = binary {
return Ok(binary);
}
let binary = resolve_binary("../linera-storage-server", "linera-storage-service").await;
if let Ok(binary) = binary {
return Ok(binary);
}
Err(ServiceStoreError::FailedToFindStorageServerBinary)
}