1pub const FIRST_NORMAL_OBJECT_ID: u32 = 16_384;
11
12pub fn stable_oid(kind: &str, name: &str) -> i64 {
13 stable_oid_bytes(kind, name.bytes())
14}
15
16pub fn stable_numeric_oid(kind: &str, mut name: u32) -> i64 {
18 let mut digits = [0_u8; 10];
19 let mut start = digits.len();
20 loop {
21 start -= 1;
22 digits[start] = b'0' + (name % 10) as u8;
23 name /= 10;
24 if name == 0 {
25 break;
26 }
27 }
28 stable_oid_bytes(kind, digits[start..].iter().copied())
29}
30
31pub fn stable_object_oid(kind: &str, object_id: &[u8; 16]) -> i64 {
32 stable_oid_bytes(kind, object_id.iter().copied())
33}
34
35fn stable_oid_bytes(kind: &str, name: impl Iterator<Item = u8>) -> i64 {
36 let mut hash = 14_695_981_039_346_656_037_u64;
37 for byte in kind.bytes().chain(*b":").chain(name) {
38 hash ^= u64::from(byte);
39 hash = hash.wrapping_mul(1_099_511_628_211);
40 }
41 10_000 + i64::try_from(hash % 2_000_000_000).unwrap_or(0)
42}
43
44pub fn schema_oid(schema: &str) -> i64 {
45 match schema {
46 "pg_catalog" => 11,
47 "public" => 2200,
48 "information_schema" => 13_293,
49 other => stable_oid("namespace", other),
50 }
51}
52
53pub fn relation_oid(kind: &str, schema: &str, name: &str) -> i64 {
54 stable_oid(kind, &format!("{schema}.{name}"))
55}
56
57pub fn current_user_oid() -> i64 {
58 10
59}
60
61pub fn current_user_name() -> &'static str {
62 "uqa"
63}
64
65#[cfg(test)]
66mod tests {
67 use super::*;
68
69 #[test]
70 fn numeric_identities_preserve_decimal_hashes_without_allocating() {
71 for oid in [0, 1, 9, 10, 99, 100, 16_384, 1_000_000_000, u32::MAX] {
72 let expected = stable_oid("domain_array", &oid.to_string());
73 let allocations = allocation_counter::measure(|| {
74 assert_eq!(stable_numeric_oid("domain_array", oid), expected);
75 });
76 assert_eq!(allocations.count_total, 0);
77 }
78 }
79}