hawdb-storage 0.8.0

An embedded Rust graph database for local data-plane workloads
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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
// Copyright 2026 Nowledge
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::{
    durability, manifest, relational_row_page_artifact_file,
    relational_row_page_manifest_generation_file, relational_row_page_root_descriptor_file,
    relational_row_page_root_key_file, root, RelationalRowPagePublicationConfig,
    RelationalRowPagePublicationError, RelationalRowPageRootDescriptor,
    RelationalRowPageRootManifest, RelationalRowPageTableRoot, RELATIONAL_ROW_PAGE_MANIFEST_FILE,
};
use crate::cache::{
    ContentDigest, ManifestGeneration, RepresentationKind, SegmentCache, SegmentCacheError,
    SegmentCacheKey, StoreId,
};
use crate::file_io::{self as fs, File};
use crate::relational::row_page::{VerifiedRowPage, VerifiedRowPageMetadata};
use crate::relational::{
    ordered_key::encode_ordered_relational_key, ImmutableRelationalRowPage, RelationalKey,
    RelationalOverflowRootBinding, RelationalOverflowRootReader, RelationalRowPageView,
};
use hawdb_integrity::integrity_digest;
use std::io::{Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::Arc;

#[derive(Debug, Clone)]
pub struct RelationalRowPageRootReader {
    directory: PathBuf,
    pub(super) manifest: Arc<RelationalRowPageRootManifest>,
    config: RelationalRowPagePublicationConfig,
}

pub(crate) struct RelationalRowPageSlotRead {
    pub page: VerifiedRowPage,
    pub cache_hit: bool,
    pub cache_miss: bool,
    pub cache_admission_rejected: bool,
}

impl RelationalRowPageRootReader {
    pub fn open_latest(
        directory: &Path,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Option<Self>, RelationalRowPagePublicationError> {
        let path = directory.join(RELATIONAL_ROW_PAGE_MANIFEST_FILE);
        manifest::read_manifest_if_exists(&path, config)?
            .map(|manifest| Self::from_manifest(directory, manifest, config))
            .transpose()
    }

    pub fn open_generation(
        directory: &Path,
        generation: u64,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        let path = directory.join(relational_row_page_manifest_generation_file(generation));
        let manifest = manifest::read_manifest(&path, config)?;
        if manifest.generation != generation {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "generation manifest {generation} identifies generation {}",
                manifest.generation
            )));
        }
        Self::from_manifest(directory, manifest, config)
    }

    pub fn open_bound_generation(
        directory: &Path,
        binding: super::RelationalRowPageGenerationArtifacts,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        let path = directory.join(relational_row_page_manifest_generation_file(
            binding.generation,
        ));
        let manifest = manifest::read_bound_manifest(&path, config, binding.manifest_artifact)?;
        if manifest.generation != binding.generation
            || manifest.source_commit_epoch != binding.source_commit_epoch
            || manifest.root_set_digest != binding.root_set_digest
        {
            return Err(RelationalRowPagePublicationError::Corrupt(
                "row-page generation identity does not match its canonical binding".to_string(),
            ));
        }
        Self::from_manifest(directory, manifest, config)
    }

    fn from_manifest(
        directory: &Path,
        manifest: RelationalRowPageRootManifest,
        config: RelationalRowPagePublicationConfig,
    ) -> Result<Self, RelationalRowPagePublicationError> {
        validate_artifact_length(
            &directory.join(relational_row_page_artifact_file(manifest.generation)),
            manifest.page_artifact.encoded_len,
            "row-page artifact",
        )?;
        validate_artifact_length(
            &directory.join(relational_row_page_root_descriptor_file(
                manifest.generation,
            )),
            manifest.root_descriptor_artifact.encoded_len,
            "row-page root descriptor artifact",
        )?;
        validate_artifact_length(
            &directory.join(relational_row_page_root_key_file(manifest.generation)),
            manifest.root_key_artifact.encoded_len,
            "row-page root key artifact",
        )?;
        Ok(Self {
            directory: directory.to_path_buf(),
            manifest: Arc::new(manifest),
            config,
        })
    }

    pub fn manifest(&self) -> &RelationalRowPageRootManifest {
        &self.manifest
    }

    /// Verifies all referenced slots and their physical-generation accounting.
    /// Keeps one page and one counter per manifest inventory entry in memory;
    /// this explicit full scrub is not part of opening a reader.
    pub fn scrub_physical_pages(&self) -> Result<(), RelationalRowPagePublicationError> {
        let inventory = &self.manifest.physical_generations;
        let mut live_counts = vec![0u64; inventory.len()];
        for entry in inventory {
            validate_artifact_length(
                &self
                    .directory
                    .join(relational_row_page_artifact_file(entry.generation)),
                entry.allocated_pages * self.manifest.page_bytes,
                "row-page physical generation",
            )?;
        }
        for table in &self.manifest.tables {
            self.visit_table_pages(&table.table, |descriptor| {
                let index = inventory
                    .binary_search_by_key(&descriptor.physical_generation, |entry| entry.generation)
                    .map_err(|_| {
                        RelationalRowPagePublicationError::Corrupt(
                            "row-page descriptor references an unaccounted generation".to_string(),
                        )
                    })?;
                live_counts[index] += 1;
                self.read_page(descriptor)?;
                Ok(())
            })?;
        }
        for (entry, actual) in inventory.iter().zip(live_counts) {
            if entry.live_pages != actual {
                return Err(RelationalRowPagePublicationError::Corrupt(format!(
                    "row-page physical generation {} has {actual} live pages, expected {}",
                    entry.generation, entry.live_pages,
                )));
            }
        }
        Ok(())
    }

    pub(crate) const fn publication_config(&self) -> RelationalRowPagePublicationConfig {
        self.config
    }

    pub fn overflow_root_binding(&self) -> Option<RelationalOverflowRootBinding> {
        self.manifest.overflow_root
    }

    pub fn validate_overflow_root(
        &self,
        overflow_root: &RelationalOverflowRootReader,
    ) -> Result<(), RelationalRowPagePublicationError> {
        let Some(expected) = self.manifest.overflow_root else {
            return Ok(());
        };
        let actual = overflow_root.manifest().binding();
        if actual != expected {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "row-page overflow binding {expected:?} differs from selected root {actual:?}"
            )));
        }
        Ok(())
    }

    pub fn read_table_page_descriptor(
        &self,
        table: &str,
        ordinal: u64,
    ) -> Result<RelationalRowPageRootDescriptor, RelationalRowPagePublicationError> {
        let table_root = self.table_root(table)?;
        let mut descriptors = File::open(self.descriptor_path())
            .map_err(durability("open row-page root descriptor artifact"))?;
        let mut keys =
            File::open(self.key_path()).map_err(durability("open row-page root key artifact"))?;
        self.read_table_page_descriptor_from(
            &mut descriptors,
            &mut keys,
            table,
            table_root,
            ordinal,
        )
    }

    pub fn find_table_page_descriptor(
        &self,
        table: &str,
        primary_key: &RelationalKey,
    ) -> Result<Option<RelationalRowPageRootDescriptor>, RelationalRowPagePublicationError> {
        self.find_table_page_descriptor_accounted(table, primary_key)
            .map(|(descriptor, _)| descriptor.map(|(_, descriptor)| descriptor))
    }

    pub(crate) fn find_table_page_descriptor_accounted(
        &self,
        table: &str,
        primary_key: &RelationalKey,
    ) -> Result<
        (Option<(u64, RelationalRowPageRootDescriptor)>, usize),
        RelationalRowPagePublicationError,
    > {
        let encoded_key = encode_ordered_relational_key(primary_key).map_err(|error| {
            RelationalRowPagePublicationError::Admission(format!(
                "row-page lookup key cannot be encoded: {error}"
            ))
        })?;
        self.find_table_page_descriptor_accounted_encoded(table, &encoded_key)
    }

    pub(crate) fn find_table_page_descriptor_accounted_encoded(
        &self,
        table: &str,
        encoded_key: &[u8],
    ) -> Result<
        (Option<(u64, RelationalRowPageRootDescriptor)>, usize),
        RelationalRowPagePublicationError,
    > {
        if encoded_key.len() > self.config.page_limits.max_key_bytes.get() {
            return Err(RelationalRowPagePublicationError::Admission(format!(
                "row-page lookup key contains {} bytes, exceeding limit {}",
                encoded_key.len(),
                self.config.page_limits.max_key_bytes
            )));
        }
        let table_root = self.table_root(table)?;
        if table_root.page_count == 0 {
            return Ok((None, 0));
        }
        let mut descriptors = File::open(self.descriptor_path())
            .map_err(durability("open row-page root descriptor artifact"))?;
        let mut keys =
            File::open(self.key_path()).map_err(durability("open row-page root key artifact"))?;

        let mut lower = 0u64;
        let mut upper = table_root.page_count;
        let mut descriptor_reads = 0usize;
        let mut candidate = None;
        while lower < upper {
            let middle = lower + (upper - lower) / 2;
            let descriptor = self.read_table_page_descriptor_from(
                &mut descriptors,
                &mut keys,
                table,
                table_root,
                middle,
            )?;
            descriptor_reads = descriptor_reads.checked_add(1).ok_or_else(|| {
                RelationalRowPagePublicationError::Admission(
                    "row-page descriptor read counter overflow".to_string(),
                )
            })?;
            if descriptor.upper_bound.as_slice() < encoded_key {
                lower = middle.checked_add(1).ok_or_else(|| {
                    RelationalRowPagePublicationError::Corrupt(
                        "row-page descriptor search overflow".to_string(),
                    )
                })?;
                if lower == table_root.page_count {
                    // Preserve the last-page result for keys above the table.
                    candidate = Some((middle, descriptor));
                }
            } else if descriptor.lower_bound.as_slice() <= encoded_key {
                // Published page bounds do not overlap. The descriptor and
                // both keys have already passed their binding checks, so an
                // in-range key needs neither more probes nor a second read.
                return Ok((Some((middle, descriptor)), descriptor_reads));
            } else {
                upper = middle;
                candidate = Some((middle, descriptor));
            }
        }
        let candidate = candidate.ok_or_else(|| {
            RelationalRowPagePublicationError::Corrupt(
                "row-page descriptor search did not select a page".to_string(),
            )
        })?;
        Ok((Some(candidate), descriptor_reads))
    }

    pub fn read_page(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
    ) -> Result<ImmutableRelationalRowPage, RelationalRowPagePublicationError> {
        let slot = self.read_page_slot_bytes(descriptor)?;
        let view = self.validate_page_slot(descriptor, &slot)?;
        ImmutableRelationalRowPage::decode_view(view).map_err(Into::into)
    }

    pub(crate) fn read_page_slot_accounted(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        cache: &SegmentCache,
        store_id: StoreId,
    ) -> Result<RelationalRowPageSlotRead, RelationalRowPagePublicationError> {
        let cache_key = SegmentCacheKey {
            store_id,
            manifest_generation: ManifestGeneration(descriptor.physical_generation),
            segment_id: descriptor.physical_slot,
            content_digest: ContentDigest(descriptor.slot_integrity.slot_crc32c as u64),
            representation: RepresentationKind::RelationalRowPageSlot,
        };
        let verification_tag = *descriptor.slot_integrity.slot_sha256.as_bytes();
        if let Some(lease) = cache.get_verified(&cache_key, verification_tag) {
            let bytes = lease.into_bytes();
            let metadata = self.validate_verified_page(descriptor, &bytes)?;
            return Ok(RelationalRowPageSlotRead {
                page: VerifiedRowPage::new(bytes, metadata),
                cache_hit: true,
                cache_miss: false,
                cache_admission_rejected: false,
            });
        }
        let mut slot = self.read_page_slot_bytes(descriptor)?;
        let view = self.validate_page_slot(descriptor, &slot)?;
        let metadata = VerifiedRowPageMetadata::from_view(&view);
        let encoded_len = view.encoded_len();
        slot.truncate(encoded_len);
        match cache.insert_verified(cache_key, verification_tag, slot) {
            Ok(lease) => Ok(RelationalRowPageSlotRead {
                page: VerifiedRowPage::new(lease.into_bytes(), metadata),
                cache_hit: false,
                cache_miss: true,
                cache_admission_rejected: false,
            }),
            Err(rejection) => {
                let (error, bytes) = rejection.into_parts();
                match error {
                    SegmentCacheError::EntryTooLarge { .. }
                    | SegmentCacheError::PinnedCapacity { .. } => Ok(RelationalRowPageSlotRead {
                        page: VerifiedRowPage::new(bytes.into(), metadata),
                        cache_hit: false,
                        cache_miss: true,
                        cache_admission_rejected: true,
                    }),
                    error => Err(RelationalRowPagePublicationError::Corrupt(format!(
                        "row-page cache rejected immutable slot identity: {error}"
                    ))),
                }
            }
        }
    }

    fn read_page_slot_bytes(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
    ) -> Result<Vec<u8>, RelationalRowPagePublicationError> {
        let page_bytes = self.config.page_limits.max_page_bytes.get() as u64;
        let offset = descriptor
            .physical_slot
            .checked_mul(page_bytes)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Corrupt(
                    "row-page physical offset overflow".to_string(),
                )
            })?;
        let mut artifact = File::open(self.directory.join(relational_row_page_artifact_file(
            descriptor.physical_generation,
        )))
        .map_err(durability("open row-page generation artifact"))?;
        artifact
            .seek(SeekFrom::Start(offset))
            .map_err(durability("seek row-page generation slot"))?;
        let mut slot = vec![0u8; self.config.page_limits.max_page_bytes.get()];
        artifact
            .read_exact(&mut slot)
            .map_err(durability("read row-page generation slot"))?;
        Ok(slot)
    }

    fn validate_page_slot<'a>(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        slot: &'a [u8],
    ) -> Result<RelationalRowPageView<'a>, RelationalRowPagePublicationError> {
        let digest = integrity_digest(slot);
        if digest.crc32c.get() != descriptor.slot_integrity.slot_crc32c
            || digest.sha256 != descriptor.slot_integrity.slot_sha256
        {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "row-page {} slot checksum mismatch",
                descriptor.logical_page_id.get()
            )));
        }
        let view = RelationalRowPageView::open_slot(slot, self.config.page_limits)?;
        self.validate_page_identity(descriptor, &view)?;
        Ok(view)
    }

    fn validate_verified_page(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        encoded: &[u8],
    ) -> Result<VerifiedRowPageMetadata, RelationalRowPagePublicationError> {
        let view = RelationalRowPageView::open_verified(encoded, self.config.page_limits)?;
        self.validate_page_identity(descriptor, &view)?;
        Ok(VerifiedRowPageMetadata::from_view(&view))
    }

    fn validate_page_identity(
        &self,
        descriptor: &RelationalRowPageRootDescriptor,
        view: &RelationalRowPageView<'_>,
    ) -> Result<(), RelationalRowPagePublicationError> {
        if view.page_id() != descriptor.logical_page_id
            || view.generation() != descriptor.physical_generation
            || view.source_commit_epoch() != descriptor.source_commit_epoch
            || view.row_count() != descriptor.row_count as usize
            || view.encoded_len() != descriptor.slot_integrity.encoded_len as usize
            || view.lower_bound_bytes() != descriptor.lower_bound
            || view.upper_bound_bytes() != descriptor.upper_bound
        {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "row-page {} content does not match its root descriptor",
                descriptor.logical_page_id.get()
            )));
        }
        Ok(())
    }

    pub fn visit_table_pages<F>(
        &self,
        table: &str,
        mut visitor: F,
    ) -> Result<(), RelationalRowPagePublicationError>
    where
        F: FnMut(&RelationalRowPageRootDescriptor) -> Result<(), RelationalRowPagePublicationError>,
    {
        let table_root = self.table_root(table)?;
        let mut descriptors = File::open(self.descriptor_path())
            .map_err(durability("open row-page root descriptor artifact"))?;
        let mut keys =
            File::open(self.key_path()).map_err(durability("open row-page root key artifact"))?;
        let mut previous_upper: Option<Vec<u8>> = None;
        for offset in 0..table_root.page_count {
            let ordinal = table_root
                .first_descriptor
                .checked_add(offset)
                .ok_or_else(|| {
                    RelationalRowPagePublicationError::Corrupt(
                        "row-page descriptor ordinal overflow".to_string(),
                    )
                })?;
            let descriptor = root::read_descriptor(
                &mut descriptors,
                &mut keys,
                ordinal,
                &self.manifest,
                self.config,
            )?;
            if previous_upper
                .as_ref()
                .is_some_and(|upper| upper.as_slice() >= descriptor.lower_bound.as_slice())
            {
                return Err(RelationalRowPagePublicationError::Corrupt(format!(
                    "table {table} row-page bounds overlap or are unordered"
                )));
            }
            previous_upper = Some(descriptor.upper_bound.clone());
            visitor(&descriptor)?;
        }
        Ok(())
    }

    pub fn table_root(
        &self,
        table: &str,
    ) -> Result<&RelationalRowPageTableRoot, RelationalRowPagePublicationError> {
        self.manifest
            .tables
            .binary_search_by(|candidate| candidate.table.as_str().cmp(table))
            .map(|index| &self.manifest.tables[index])
            .map_err(|_| RelationalRowPagePublicationError::MissingTable(table.to_string()))
    }

    fn descriptor_path(&self) -> PathBuf {
        self.directory
            .join(relational_row_page_root_descriptor_file(
                self.manifest.generation,
            ))
    }

    fn key_path(&self) -> PathBuf {
        self.directory
            .join(relational_row_page_root_key_file(self.manifest.generation))
    }

    fn read_table_page_descriptor_from(
        &self,
        descriptors: &mut File,
        keys: &mut File,
        table: &str,
        table_root: &RelationalRowPageTableRoot,
        ordinal: u64,
    ) -> Result<RelationalRowPageRootDescriptor, RelationalRowPagePublicationError> {
        if ordinal >= table_root.page_count {
            return Err(RelationalRowPagePublicationError::Admission(format!(
                "row-page ordinal {ordinal} exceeds table {table} page count {}",
                table_root.page_count
            )));
        }
        let descriptor_ordinal = table_root
            .first_descriptor
            .checked_add(ordinal)
            .ok_or_else(|| {
                RelationalRowPagePublicationError::Corrupt(
                    "row-page descriptor ordinal overflow".to_string(),
                )
            })?;
        let descriptor = root::read_descriptor(
            descriptors,
            keys,
            descriptor_ordinal,
            &self.manifest,
            self.config,
        )?;
        if descriptor.logical_page_id.get() >= table_root.next_page_id.get() {
            return Err(RelationalRowPagePublicationError::Corrupt(format!(
                "table {table} descriptor page id {} is not below next page id {}",
                descriptor.logical_page_id.get(),
                table_root.next_page_id
            )));
        }
        Ok(descriptor)
    }
}

fn validate_artifact_length(
    path: &Path,
    expected: u64,
    context: &str,
) -> Result<(), RelationalRowPagePublicationError> {
    let actual = fs::metadata(path)
        .map_err(durability("read row-page artifact metadata"))?
        .len();
    if actual != expected {
        return Err(RelationalRowPagePublicationError::Corrupt(format!(
            "{context} contains {actual} bytes, expected {expected}"
        )));
    }
    Ok(())
}