radixdb-storage 1.1.0

Storage contracts and physical persistence engine for RadixDB
Documentation
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
use std::path::PathBuf;

use radixdb_catalog::ObjectId;

use super::{ArtifactId, DatabaseGeneration, FormatError, FormatResult};

pub const ARTIFACT_CODEC_VERSION: u16 = 1;
const MIN_ARTIFACT_FILE_BYTES: u64 = 256 + 48;
pub const MAX_ARTIFACT_FILE_BYTES: u64 = 64 * 1024 * 1024 * 1024;
const COMMON_FOOTER_BYTES: u64 = 48;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u16)]
pub enum ArtifactKind {
    Data = 1,
    Index = 2,
}

impl ArtifactKind {
    pub fn from_tag(tag: u16) -> FormatResult<Self> {
        match tag {
            1 => Ok(Self::Data),
            2 => Ok(Self::Index),
            _ => Err(FormatError::UnknownArtifactKind { tag }),
        }
    }

    pub const fn tag(self) -> u16 {
        self as u16
    }

    pub const fn suffix(self) -> ArtifactSuffix {
        match self {
            Self::Data => ArtifactSuffix::Data,
            Self::Index => ArtifactSuffix::Index,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u16)]
pub enum ArtifactSuffix {
    Data = 1,
    Index = 2,
}

impl ArtifactSuffix {
    pub fn from_tag(tag: u16) -> FormatResult<Self> {
        match tag {
            1 => Ok(Self::Data),
            2 => Ok(Self::Index),
            _ => Err(FormatError::InvalidArtifactLocator {
                detail: "unknown locator suffix",
            }),
        }
    }

    pub const fn tag(self) -> u16 {
        self as u16
    }

    pub const fn extension(self) -> &'static str {
        match self {
            Self::Data => "data",
            Self::Index => "idx",
        }
    }

    const fn directory(self) -> &'static str {
        match self {
            Self::Data => "data",
            Self::Index => "index",
        }
    }
}

/// Validated physical locator derived from artifact identity and kind.
///
/// It contains no persisted path string. The pathname is a deterministic
/// locator only and never becomes membership or logical identity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ArtifactLocator {
    shard: u8,
    suffix: ArtifactSuffix,
}

impl ArtifactLocator {
    pub fn derive(id: ArtifactId, kind: ArtifactKind) -> Self {
        Self {
            shard: id.as_bytes()[0],
            suffix: kind.suffix(),
        }
    }

    pub fn from_persisted(
        id: ArtifactId,
        kind: ArtifactKind,
        shard: u16,
        suffix_tag: u16,
    ) -> FormatResult<Self> {
        let shard = u8::try_from(shard).map_err(|_| FormatError::InvalidArtifactLocator {
            detail: "locator shard is outside 0..=255",
        })?;
        let suffix = ArtifactSuffix::from_tag(suffix_tag)?;
        let expected = Self::derive(id, kind);
        if shard != expected.shard {
            return Err(FormatError::InvalidArtifactLocator {
                detail: "locator shard differs from the first artifact ID byte",
            });
        }
        if suffix != expected.suffix {
            return Err(FormatError::InvalidArtifactLocator {
                detail: "locator suffix differs from artifact kind",
            });
        }
        Ok(expected)
    }

    pub const fn shard(self) -> u8 {
        self.shard
    }

    pub const fn suffix(self) -> ArtifactSuffix {
        self.suffix
    }

    pub(crate) fn relative_path(self, id: ArtifactId) -> PathBuf {
        PathBuf::from("artifacts")
            .join(self.suffix.directory())
            .join(format!("{:02x}", self.shard))
            .join(format!("{id}.{}", self.suffix.extension()))
    }
}

/// Complete immutable identity of one V6 `.data` or `.idx` file.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ArtifactRef {
    id: ArtifactId,
    kind: ArtifactKind,
    codec_version: u16,
    creation_generation: DatabaseGeneration,
    byte_length: u64,
    body_sha256: [u8; 32],
    locator: ArtifactLocator,
}

impl ArtifactRef {
    pub fn new(
        id: ArtifactId,
        kind: ArtifactKind,
        creation_generation: DatabaseGeneration,
        byte_length: u64,
        body_sha256: [u8; 32],
    ) -> FormatResult<Self> {
        Self::from_persisted(
            id,
            kind,
            ARTIFACT_CODEC_VERSION,
            0,
            creation_generation,
            byte_length,
            body_sha256,
            u16::from(id.as_bytes()[0]),
            kind.suffix().tag(),
        )
    }

    #[allow(clippy::too_many_arguments)]
    pub fn from_persisted(
        id: ArtifactId,
        kind: ArtifactKind,
        codec_version: u16,
        flags: u32,
        creation_generation: DatabaseGeneration,
        byte_length: u64,
        body_sha256: [u8; 32],
        locator_shard: u16,
        locator_suffix: u16,
    ) -> FormatResult<Self> {
        if codec_version != ARTIFACT_CODEC_VERSION {
            return Err(FormatError::UnsupportedArtifactVersion {
                version: codec_version,
            });
        }
        if flags != 0 {
            return Err(FormatError::UnknownArtifactFlags { flags });
        }
        if byte_length < MIN_ARTIFACT_FILE_BYTES {
            return Err(FormatError::InvalidReference {
                owner: "artifact",
                detail: "byte length is smaller than header plus footer",
            });
        }
        if byte_length > MAX_ARTIFACT_FILE_BYTES {
            return Err(FormatError::InvalidReference {
                owner: "artifact",
                detail: "byte length exceeds the V6 artifact file ceiling",
            });
        }
        Ok(Self {
            id,
            kind,
            codec_version,
            creation_generation,
            byte_length,
            body_sha256,
            locator: ArtifactLocator::from_persisted(id, kind, locator_shard, locator_suffix)?,
        })
    }

    pub const fn id(self) -> ArtifactId {
        self.id
    }

    pub const fn kind(self) -> ArtifactKind {
        self.kind
    }

    pub const fn codec_version(self) -> u16 {
        self.codec_version
    }

    pub const fn creation_generation(self) -> DatabaseGeneration {
        self.creation_generation
    }

    pub const fn byte_length(self) -> u64 {
        self.byte_length
    }

    pub const fn body_sha256(&self) -> &[u8; 32] {
        &self.body_sha256
    }

    pub const fn locator(self) -> ArtifactLocator {
        self.locator
    }

    pub fn relative_path(self) -> PathBuf {
        self.locator.relative_path(self.id)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u16)]
pub enum IndexSectionKind {
    KeyDescriptor = 1,
    ExactPages = 2,
    OrderedPages = 3,
    HnswMetadata = 4,
    HnswNodes = 5,
    HnswAdjacency = 6,
}

impl IndexSectionKind {
    pub fn from_tag(tag: u16) -> FormatResult<Self> {
        match tag {
            1 => Ok(Self::KeyDescriptor),
            2 => Ok(Self::ExactPages),
            3 => Ok(Self::OrderedPages),
            4 => Ok(Self::HnswMetadata),
            5 => Ok(Self::HnswNodes),
            6 => Ok(Self::HnswAdjacency),
            _ => Err(FormatError::UnknownIndexSectionKind { tag }),
        }
    }

    pub const fn tag(self) -> u16 {
        self as u16
    }
}

/// A checksummed section range inside one immutable index artifact.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ArtifactSliceRef {
    artifact: ArtifactRef,
    section_index: u32,
    section_kind: IndexSectionKind,
    section_version: u16,
    offset: u64,
    stored_length: u64,
    logical_length: u64,
    item_count: u64,
    stored_crc32: u32,
}

impl ArtifactSliceRef {
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        artifact: ArtifactRef,
        section_index: u32,
        section_kind: IndexSectionKind,
        section_version: u16,
        offset: u64,
        stored_length: u64,
        logical_length: u64,
        item_count: u64,
        stored_crc32: u32,
    ) -> FormatResult<Self> {
        if artifact.kind() != ArtifactKind::Index {
            return Err(FormatError::InvalidReference {
                owner: "artifact slice",
                detail: "index section points to a non-index artifact",
            });
        }
        if section_version != 1 {
            return Err(FormatError::UnsupportedIndexSectionVersion {
                version: section_version,
            });
        }
        if !offset.is_multiple_of(8) {
            return Err(FormatError::InvalidReference {
                owner: "artifact slice",
                detail: "section offset is not 8-byte aligned",
            });
        }
        if stored_length == 0 || logical_length == 0 {
            return Err(FormatError::InvalidReference {
                owner: "artifact slice",
                detail: "section range cannot be empty",
            });
        }
        let end = offset
            .checked_add(stored_length)
            .ok_or(FormatError::InvalidReference {
                owner: "artifact slice",
                detail: "section range overflows",
            })?;
        let body_end = artifact
            .byte_length()
            .checked_sub(COMMON_FOOTER_BYTES)
            .ok_or(FormatError::InvalidReference {
                owner: "artifact slice",
                detail: "artifact has no footer boundary",
            })?;
        if offset < 256 || end > body_end {
            return Err(FormatError::InvalidReference {
                owner: "artifact slice",
                detail: "section range is outside artifact body",
            });
        }
        Ok(Self {
            artifact,
            section_index,
            section_kind,
            section_version,
            offset,
            stored_length,
            logical_length,
            item_count,
            stored_crc32,
        })
    }

    pub const fn artifact(self) -> ArtifactRef {
        self.artifact
    }

    pub const fn section_index(self) -> u32 {
        self.section_index
    }

    pub const fn section_kind(self) -> IndexSectionKind {
        self.section_kind
    }

    pub const fn section_version(self) -> u16 {
        self.section_version
    }

    pub const fn offset(self) -> u64 {
        self.offset
    }

    pub const fn stored_length(self) -> u64 {
        self.stored_length
    }

    pub const fn logical_length(self) -> u64 {
        self.logical_length
    }

    pub const fn item_count(self) -> u64 {
        self.item_count
    }

    pub const fn stored_crc32(self) -> u32 {
        self.stored_crc32
    }
}

/// Binds a stable logical catalog index to one physical section identity.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct IndexSectionRef {
    logical_index_id: ObjectId,
    slice: ArtifactSliceRef,
}

impl IndexSectionRef {
    pub const fn new(logical_index_id: ObjectId, slice: ArtifactSliceRef) -> Self {
        Self {
            logical_index_id,
            slice,
        }
    }

    pub const fn logical_index_id(self) -> ObjectId {
        self.logical_index_id
    }

    pub const fn slice(self) -> ArtifactSliceRef {
        self.slice
    }
}