1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
use super::*;

use io::LogReader;

/// A pointer to a location on disk or an off-log blob.
#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)]
pub enum DiskPtr {
    /// Points to a value stored in the single-file log.
    Inline(LogID),
    /// Points to a value stored off-log in the blob directory.
    External(LogID, ExternalPointer),
}

impl DiskPtr {
    pub(crate) fn new_inline(l: LogID) -> DiskPtr {
        DiskPtr::Inline(l)
    }

    pub(crate) fn new_external(
        lid: LogID,
        ptr: ExternalPointer,
    ) -> DiskPtr {
        DiskPtr::External(lid, ptr)
    }

    pub(crate) fn is_inline(&self) -> bool {
        match self {
            DiskPtr::Inline(_) => true,
            DiskPtr::External(_, _) => false,
        }
    }

    pub(crate) fn is_external(&self) -> bool {
        match self {
            DiskPtr::External(_, _) => true,
            DiskPtr::Inline(_) => false,
        }
    }

    pub(crate) fn inline(&self) -> LogID {
        match self {
            DiskPtr::Inline(l) => *l,
            DiskPtr::External(_, _) => {
                panic!("inline called on External disk pointer")
            }
        }
    }

    pub(crate) fn external(&self) -> (LogID, ExternalPointer) {
        match self {
            DiskPtr::External(lid, ptr) => (*lid, *ptr),
            DiskPtr::Inline(_) => {
                panic!("external called on Internal disk pointer")
            }
        }
    }

    #[doc(hidden)]
    pub fn lid(&self) -> LogID {
        match self {
            DiskPtr::External(lid, _) | DiskPtr::Inline(lid) => *lid,
        }
    }

    pub(crate) fn read(
        &self,
        config: &Config,
    ) -> CacheResult<Vec<u8>, ()> {
        match self {
            DiskPtr::External(_lid, ptr) => read_blob(*ptr, &config),
            DiskPtr::Inline(lid) => {
                let mut f = config.file()?;

                f.read_message(*lid, &config).map(|log_read| {
                    log_read
                        .inline()
                        .expect(
                            "call to DiskPtr::read with \
                             an Inline pointer should result \
                             in a valid Inline read. It's \
                             possible the DiskPtr outlived \
                             an outer guard.",
                        )
                        .1
                })
            }
        }
    }
}

impl std::fmt::Display for DiskPtr {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter,
    ) -> Result<(), std::fmt::Error> {
        write!(f, "{:?}", self)
    }
}