use crate::util::fnv1a_hash;
pub fn stable_collection_oid(tenant_id: u64, collection_name: &str) -> i64 {
let key = format!("{tenant_id}:{collection_name}");
16384 + (fnv1a_hash(key.as_bytes()) & 0x7FFF_FFFF) as i64
}
pub fn stable_index_oid(tenant_id: u64, collection_name: &str, index_name: &str) -> i64 {
let key = format!("idx:{tenant_id}:{collection_name}:{index_name}");
16384 + (fnv1a_hash(key.as_bytes()) & 0x7FFF_FFFF) as i64
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn collection_oid_is_deterministic() {
let a = stable_collection_oid(1, "users");
let b = stable_collection_oid(1, "users");
assert_eq!(a, b);
}
#[test]
fn collection_oid_is_positive() {
let oid = stable_collection_oid(1, "users");
assert!(oid > 0, "OID must be positive i64");
}
#[test]
fn collection_oid_above_reserved_range() {
let oid = stable_collection_oid(1, "users");
assert!(oid >= 16384, "OID must be above the reserved range");
}
#[test]
fn different_collection_names_produce_different_oids() {
let a = stable_collection_oid(1, "users");
let b = stable_collection_oid(1, "orders");
assert_ne!(a, b);
}
#[test]
fn same_name_different_tenants_produce_different_oids() {
let a = stable_collection_oid(1, "users");
let b = stable_collection_oid(2, "users");
assert_ne!(a, b);
}
#[test]
fn index_oid_is_deterministic() {
let a = stable_index_oid(1, "users", "idx_email");
let b = stable_index_oid(1, "users", "idx_email");
assert_eq!(a, b);
}
#[test]
fn index_oid_differs_from_collection_oid() {
let coll = stable_collection_oid(1, "users");
let idx = stable_index_oid(1, "users", "idx_email");
assert_ne!(coll, idx);
}
#[test]
fn different_index_names_produce_different_oids() {
let a = stable_index_oid(1, "users", "idx_email");
let b = stable_index_oid(1, "users", "idx_name");
assert_ne!(a, b);
}
}