2024-08-13 23:17:47 -07:00
|
|
|
use std::collections::HashMap;
|
2024-03-14 14:03:11 +00:00
|
|
|
|
2024-08-13 23:17:47 -07:00
|
|
|
use crate::db::KrataDatabase;
|
2024-03-14 14:03:11 +00:00
|
|
|
use anyhow::Result;
|
2024-07-18 20:47:18 -07:00
|
|
|
use krata::v1::common::Zone;
|
2024-03-14 14:03:11 +00:00
|
|
|
use log::error;
|
|
|
|
use prost::Message;
|
2024-08-13 23:17:47 -07:00
|
|
|
use redb::{ReadableTable, TableDefinition};
|
2024-03-14 14:03:11 +00:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2024-08-13 23:17:47 -07:00
|
|
|
const ZONE_TABLE: TableDefinition<u128, &[u8]> = TableDefinition::new("zone");
|
2024-03-14 14:03:11 +00:00
|
|
|
|
|
|
|
#[derive(Clone)]
|
2024-07-18 20:47:18 -07:00
|
|
|
pub struct ZoneStore {
|
2024-08-13 23:17:47 -07:00
|
|
|
db: KrataDatabase,
|
2024-03-14 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
impl ZoneStore {
|
2024-08-13 23:17:47 -07:00
|
|
|
pub fn open(db: KrataDatabase) -> Result<Self> {
|
|
|
|
let write = db.database.begin_write()?;
|
|
|
|
let _ = write.open_table(ZONE_TABLE);
|
2024-03-14 14:03:11 +00:00
|
|
|
write.commit()?;
|
2024-08-13 23:17:47 -07:00
|
|
|
Ok(ZoneStore { db })
|
2024-03-14 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
pub async fn read(&self, id: Uuid) -> Result<Option<Zone>> {
|
2024-08-13 23:17:47 -07:00
|
|
|
let read = self.db.database.begin_read()?;
|
|
|
|
let table = read.open_table(ZONE_TABLE)?;
|
2024-03-14 14:03:11 +00:00
|
|
|
let Some(entry) = table.get(id.to_u128_le())? else {
|
|
|
|
return Ok(None);
|
|
|
|
};
|
|
|
|
let bytes = entry.value();
|
2024-07-18 20:47:18 -07:00
|
|
|
Ok(Some(Zone::decode(bytes)?))
|
2024-03-14 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
pub async fn list(&self) -> Result<HashMap<Uuid, Zone>> {
|
|
|
|
let mut zones: HashMap<Uuid, Zone> = HashMap::new();
|
2024-08-13 23:17:47 -07:00
|
|
|
let read = self.db.database.begin_read()?;
|
|
|
|
let table = read.open_table(ZONE_TABLE)?;
|
2024-03-14 14:03:11 +00:00
|
|
|
for result in table.iter()? {
|
|
|
|
let (key, value) = result?;
|
|
|
|
let uuid = Uuid::from_u128_le(key.value());
|
2024-07-18 20:47:18 -07:00
|
|
|
let state = match Zone::decode(value.value()) {
|
2024-03-14 14:03:11 +00:00
|
|
|
Ok(state) => state,
|
|
|
|
Err(error) => {
|
|
|
|
error!(
|
2024-07-18 20:47:18 -07:00
|
|
|
"found invalid zone state in database for uuid {}: {}",
|
2024-03-14 14:03:11 +00:00
|
|
|
uuid, error
|
|
|
|
);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
};
|
2024-07-18 20:47:18 -07:00
|
|
|
zones.insert(uuid, state);
|
2024-03-14 14:03:11 +00:00
|
|
|
}
|
2024-07-18 20:47:18 -07:00
|
|
|
Ok(zones)
|
2024-03-14 14:03:11 +00:00
|
|
|
}
|
|
|
|
|
2024-07-18 20:47:18 -07:00
|
|
|
pub async fn update(&self, id: Uuid, entry: Zone) -> Result<()> {
|
2024-08-13 23:17:47 -07:00
|
|
|
let write = self.db.database.begin_write()?;
|
2024-03-14 14:03:11 +00:00
|
|
|
{
|
2024-08-13 23:17:47 -07:00
|
|
|
let mut table = write.open_table(ZONE_TABLE)?;
|
2024-03-14 14:03:11 +00:00
|
|
|
let bytes = entry.encode_to_vec();
|
|
|
|
table.insert(id.to_u128_le(), bytes.as_slice())?;
|
|
|
|
}
|
|
|
|
write.commit()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn remove(&self, id: Uuid) -> Result<()> {
|
2024-08-13 23:17:47 -07:00
|
|
|
let write = self.db.database.begin_write()?;
|
2024-03-14 14:03:11 +00:00
|
|
|
{
|
2024-08-13 23:17:47 -07:00
|
|
|
let mut table = write.open_table(ZONE_TABLE)?;
|
2024-03-14 14:03:11 +00:00
|
|
|
table.remove(id.to_u128_le())?;
|
|
|
|
}
|
|
|
|
write.commit()?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|