use marsdb_storage::{ReadableTable, Txn};
use crate::error::GraphError;
use crate::id::next_id;
use crate::write_ctx::WriteCtx;
pub(crate) fn intern_prop(ctx: &mut WriteCtx, prop: &str) -> Result<u32, GraphError> {
if let Some(existing) = ctx.prop_to_id()?.get(prop)?.map(|g| g.value()) {
return Ok(existing);
}
let id = next_id(ctx, "next_prop_id")? as u32;
ctx.prop_to_id()?.insert(prop, id)?;
ctx.id_to_prop()?.insert(id, prop)?;
Ok(id)
}
pub(crate) fn prop_resolver(
txn: Txn<'_>,
) -> Result<impl FnMut(u32) -> Result<String, GraphError> + '_, GraphError> {
let i2p = txn.open_table(marsdb_storage::tables::ID_TO_PROP)?;
Ok(move |prop_id: u32| {
i2p.get(prop_id)?
.map(|g| g.value().to_string())
.ok_or_else(|| {
GraphError::CorruptData(format!("prop id {prop_id} has no interned 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)
}