sled/pagecache/
disk_pointer.rs

1use super::{BlobPointer, LogOffset};
2use crate::*;
3
4/// A pointer to a location on disk or an off-log blob.
5#[derive(Debug, Clone, PartialOrd, Ord, Copy, Eq, PartialEq)]
6pub enum DiskPtr {
7    /// Points to a value stored in the single-file log.
8    Inline(LogOffset),
9    /// Points to a value stored off-log in the blob directory.
10    Blob(LogOffset, BlobPointer),
11}
12
13impl DiskPtr {
14    pub(crate) fn new_inline(l: LogOffset) -> Self {
15        DiskPtr::Inline(l)
16    }
17
18    pub(crate) fn new_blob(lid: LogOffset, ptr: BlobPointer) -> Self {
19        DiskPtr::Blob(lid, ptr)
20    }
21
22    pub(crate) fn is_inline(&self) -> bool {
23        match self {
24            DiskPtr::Inline(_) => true,
25            DiskPtr::Blob(_, _) => false,
26        }
27    }
28
29    pub(crate) fn is_blob(&self) -> bool {
30        match self {
31            DiskPtr::Blob(_, _) => true,
32            DiskPtr::Inline(_) => false,
33        }
34    }
35
36    pub(crate) fn blob(&self) -> (LogOffset, BlobPointer) {
37        match self {
38            DiskPtr::Blob(lid, ptr) => (*lid, *ptr),
39            DiskPtr::Inline(_) => {
40                panic!("blob called on Internal disk pointer")
41            }
42        }
43    }
44
45    #[doc(hidden)]
46    pub fn lid(&self) -> LogOffset {
47        match self {
48            DiskPtr::Blob(lid, _) | DiskPtr::Inline(lid) => *lid,
49        }
50    }
51}
52
53impl fmt::Display for DiskPtr {
54    fn fmt(
55        &self,
56        f: &mut fmt::Formatter<'_>,
57    ) -> std::result::Result<(), fmt::Error> {
58        write!(f, "{:?}", self)
59    }
60}