use std::path::Path;
use snops_common::{
aot_cmds::Authorization,
db::{error::DatabaseError, tree::DbTree, Database as DatabaseTrait},
format::PackedUint,
state::{AgentId, CannonId, EnvId, NetworkId, StorageId},
};
use crate::{
cannon::status::TransactionSendState,
persist::{PersistEnv, PersistStorage},
state::Agent,
};
pub type TxEntry = (EnvId, CannonId, String);
pub struct Database {
#[allow(unused)]
pub(crate) db: sled::Db,
pub(crate) envs: DbTree<EnvId, PersistEnv>,
pub(crate) storage: DbTree<(NetworkId, StorageId), PersistStorage>,
pub(crate) agents: DbTree<AgentId, Agent>,
pub(crate) tx_auths: DbTree<TxEntry, Authorization>,
pub(crate) tx_blobs: DbTree<TxEntry, serde_json::Value>,
pub(crate) tx_status: DbTree<TxEntry, TransactionSendState>,
pub(crate) tx_index: DbTree<TxEntry, PackedUint>,
pub(crate) tx_attempts: DbTree<TxEntry, PackedUint>,
}
impl DatabaseTrait for Database {
fn open(path: &Path) -> Result<Self, DatabaseError> {
let db = sled::open(path)?;
let envs = DbTree::new(db.open_tree(b"v2/envs")?);
let storage = DbTree::new(db.open_tree(b"v2/storage")?);
let agents = DbTree::new(db.open_tree(b"v2/agents")?);
let tx_auths = DbTree::new(db.open_tree(b"v2/tx_auths")?);
let tx_blobs = DbTree::new(db.open_tree(b"v2/tx_blobs")?);
let tx_status = DbTree::new(db.open_tree(b"v2/tx_status")?);
let tx_index = DbTree::new(db.open_tree(b"v2/tx_index")?);
let tx_attempts = DbTree::new(db.open_tree(b"v2/tx_attempts")?);
Ok(Self {
db,
envs,
storage,
agents,
tx_auths,
tx_blobs,
tx_status,
tx_index,
tx_attempts,
})
}
}