lance_core/utils/
address.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright The Lance Authors
3
4use std::ops::Range;
5
6#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct RowAddress(u64);
8
9impl RowAddress {
10    pub const FRAGMENT_SIZE: u64 = 1 << 32;
11    // A fragment id that will never be used
12    pub const TOMBSTONE_FRAG: u32 = 0xffffffff;
13    // A row id that will never be used
14    pub const TOMBSTONE_ROW: u64 = 0xffffffffffffffff;
15
16    pub fn new_from_u64(row_addr: u64) -> Self {
17        Self(row_addr)
18    }
19
20    pub fn new_from_parts(fragment_id: u32, row_offset: u32) -> Self {
21        Self(((fragment_id as u64) << 32) | row_offset as u64)
22    }
23
24    pub fn first_row(fragment_id: u32) -> Self {
25        Self::new_from_parts(fragment_id, 0)
26    }
27
28    pub fn address_range(fragment_id: u32) -> Range<u64> {
29        u64::from(Self::first_row(fragment_id))..u64::from(Self::first_row(fragment_id + 1))
30    }
31
32    pub fn fragment_id(&self) -> u32 {
33        (self.0 >> 32) as u32
34    }
35
36    pub fn row_offset(&self) -> u32 {
37        self.0 as u32
38    }
39}
40
41impl From<RowAddress> for u64 {
42    fn from(row_id: RowAddress) -> Self {
43        row_id.0
44    }
45}
46
47impl From<u64> for RowAddress {
48    fn from(row_id: u64) -> Self {
49        Self(row_id)
50    }
51}
52
53impl std::fmt::Debug for RowAddress {
54    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
55        write!(f, "{}", self) // use Display
56    }
57}
58
59impl std::fmt::Display for RowAddress {
60    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61        write!(f, "({}, {})", self.fragment_id(), self.row_offset())
62    }
63}