use crate::control::surrogate::SurrogateAssigner;
use crate::types::{DatabaseId, TenantId};
pub(super) struct DecodeCtx<'a> {
pub(super) assigner: Option<&'a SurrogateAssigner>,
pub(super) database_id: DatabaseId,
pub(super) tenant_id: TenantId,
}
pub(super) fn assign_or_zero(
ctx: &DecodeCtx,
collection: &str,
pk_bytes: &[u8],
) -> crate::Result<nodedb_types::Surrogate> {
match ctx.assigner {
Some(a) => a.assign(ctx.database_id, ctx.tenant_id, collection, pk_bytes),
None => Ok(nodedb_types::Surrogate::ZERO),
}
}
pub(super) fn bind_or_lookup(
ctx: &DecodeCtx,
collection: &str,
pk_bytes: &[u8],
carried: nodedb_types::Surrogate,
) -> crate::Result<nodedb_types::Surrogate> {
match ctx.assigner {
Some(a) if carried != nodedb_types::Surrogate::ZERO => a.bind(
ctx.database_id,
ctx.tenant_id,
collection,
pk_bytes,
carried,
),
Some(a) => Ok(a
.lookup(ctx.database_id, ctx.tenant_id, collection, pk_bytes)?
.unwrap_or(nodedb_types::Surrogate::ZERO)),
None => Ok(carried),
}
}