Skip to content

Commit

Permalink
fix: header and correct logic of update oid
Browse files Browse the repository at this point in the history
  • Loading branch information
J0HN50N133 committed Aug 9, 2024
1 parent 86067e7 commit a67b273
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion src/catalog/src/system_schema/pg_catalog/pg_namespace/oid_map.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
// Copyright 2023 Greptime Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use std::hash::BuildHasher;
use std::sync::Arc;

Expand Down Expand Up @@ -35,10 +49,52 @@ impl PGNamespaceOidMap {
} else {
let mut oid = self.hasher.hash_one(schema_name) as u32;
while self.oid_is_used(oid) {
oid = self.hasher.hash_one(schema_name) as u32;
oid = self.hasher.hash_one(oid) as u32;
}
self.oid_map.insert(schema_name.to_string(), oid);
oid
}
}
}

#[cfg(test)]
mod tests {

use super::*;

#[test]
fn oid_is_stable() {
let oid_map_1 = PGNamespaceOidMap::new();
let oid_map_2 = PGNamespaceOidMap::new();

let schema = "schema";
let oid = oid_map_1.get_oid(schema);

// oid keep stable in the same instance
assert_eq!(oid, oid_map_1.get_oid(schema));

// oid keep stable between different instances
assert_eq!(oid, oid_map_2.get_oid(schema));
}

#[test]
fn oid_collision() {
let oid_map = PGNamespaceOidMap::new();

let key1 = "3178510";
let key2 = "4215648";

// have collision
assert_eq!(
oid_map.hasher.hash_one(key1) as u32,
oid_map.hasher.hash_one(key2) as u32
);

// insert them into oid_map
let oid1 = oid_map.get_oid(key1);
let oid2 = oid_map.get_oid(key2);

// they should have different id
assert_ne!(oid1, oid2);
}
}

0 comments on commit a67b273

Please sign in to comment.