Skip to content

Commit

Permalink
fix: take MyDatabaseId into handle to resolve conflicts of OIDs in …
Browse files Browse the repository at this point in the history
…multidatabases (#445)

Signed-off-by: usamoi <[email protected]>
  • Loading branch information
usamoi authored Mar 23, 2024
1 parent fcf2da1 commit 9227095
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
29 changes: 19 additions & 10 deletions crates/base/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,44 @@ use std::fmt::Display;

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Handle {
newtype: u128,
tenant_id: u128,
cluster_id: u64,
database_id: u32,
index_id: u32,
}

impl Handle {
pub fn new(newtype: u128) -> Self {
Self { newtype }
}
pub fn as_u128(self) -> u128 {
self.newtype
pub fn new(tenant_id: u128, cluster_id: u64, database_id: u32, index_id: u32) -> Self {
Self {
tenant_id,
cluster_id,
database_id,
index_id,
}
}
}

impl Display for Handle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:x}", self.as_u128())
write!(
f,
"{:032x}{:016x}{:08x}{:08x}",
self.tenant_id, self.cluster_id, self.database_id, self.index_id
)
}
}

#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Pointer {
newtype: u64,
value: u64,
}

impl Pointer {
pub fn new(value: u64) -> Self {
Self { newtype: value }
Self { value }
}
pub fn as_u64(self) -> u64 {
self.newtype
self.value
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/service/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct Version {
}

impl Version {
const VERSION: u64 = 3;
const VERSION: u64 = 4;
const SOFT_VERSION: u64 = 0;
}

Expand Down
9 changes: 5 additions & 4 deletions src/index/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ pub fn from_oid_to_handle(oid: pgrx::pg_sys::Oid) -> Handle {
if SYSTEM_IDENTIFIER.get() == 0 {
SYSTEM_IDENTIFIER.set(unsafe { pgrx::pg_sys::GetSystemIdentifier() });
}
let a = 0u128;
let b = SYSTEM_IDENTIFIER.get() as u128;
let c = oid.as_u32() as u128;
Handle::new(a << 96 | b << 32 | c)
let tenant_id = 0_u128;
let cluster_id = SYSTEM_IDENTIFIER.get();
let database_id = unsafe { pgrx::pg_sys::MyDatabaseId.as_u32() };
let index_id = oid.as_u32();
Handle::new(tenant_id, cluster_id, database_id, index_id)
}

pub fn pointer_to_ctid(pointer: Pointer) -> pgrx::pg_sys::ItemPointerData {
Expand Down

0 comments on commit 9227095

Please sign in to comment.