use marsdb_storage::{ReadableTable, Txn, WriteTransaction};
use crate::error::GraphError;
use crate::id::next_id;
pub(crate) fn intern_prop(write_txn: &WriteTransaction, prop: &str) -> Result<u32, GraphError> {
{
let p2i = write_txn.open_table(marsdb_storage::tables::PROP_TO_ID)?;
let existing = p2i.get(prop)?.map(|g| g.value());
if let Some(existing) = existing {
return Ok(existing);
}
}
let id = next_id(write_txn, "next_prop_id")? as u32;
{
let mut p2i = write_txn.open_table(marsdb_storage::tables::PROP_TO_ID)?;
p2i.insert(prop, id)?;
}
{
let mut i2p = write_txn.open_table(marsdb_storage::tables::ID_TO_PROP)?;
i2p.insert(id, prop)?;
}
Ok(id)
}
pub(crate) fn resolve_prop(txn: Txn, prop_id: u32) -> Result<String, GraphError> {
let i2p = txn.open_table(marsdb_storage::tables::ID_TO_PROP)?;
let value = i2p.get(prop_id)?.ok_or_else(|| {
GraphError::CorruptData(format!("prop id {prop_id} has no interned string"))
})?;
Ok(value.value().to_string())
}
pub(crate) fn lookup_prop_id(txn: Txn, prop: &str) -> Result<Option<u32>, GraphError> {
let p2i = match txn.open_table(marsdb_storage::tables::PROP_TO_ID) {
Ok(table) => table,
Err(marsdb_storage::StorageError::Table(redb::TableError::TableDoesNotExist(_))) => {
return Ok(None)
}
Err(e) => return Err(e.into()),
};
let found = p2i.get(prop)?.map(|g| g.value());
Ok(found)
}