hawdb-storage 0.16.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
// Copyright 2026 Nowledge
// Licensed under the Apache License, Version 2.0.

//! Reconstruct native read custody from a completed durable selection.
//! Saved locators are descriptions; the caller supplies the original parent.

use super::*;
use crate::immutable_object::ObjectKind;
use hawdb_core::RuntimeTaskContext;

// Covers bounded ledger decode/re-encode, one cloned selection/reservation,
// canonical birth-marker construction and native-owner metadata. The caller
// prepays this envelope on its existing account and retains it through this
// object's destruction. This is not allocator/RSS or serving qualification.
pub const CONSUMER_REOPEN_MEMORY_BOUND: u64 = 16 * MAX_BYTES as u64;

/// Captured source custody only, prepared without decoding the ledger under
/// SourceOwner. Execute after releasing that fence with fresh working/I/O/FD
/// admission. This request alone cannot authorize a read version.
#[doc(hidden)]
#[derive(Debug)]
pub struct ConsumerReopenRequest {
    source: ConsumerAllocationSource,
    database: Uuid,
    consumer: [u8; 128],
    consumer_length: u8,
    source_epoch: u64,
}

/// Exact native generation named by a completed selection. Retains its held
/// marker and directory; it grants no writer, deletion or new selector token.
/// Search must still verify the complete sealed metadata/artifact closure.
#[doc(hidden)]
#[derive(Debug)]
pub struct ReopenedConsumerGeneration {
    marker: File,
    directory: SourceDirectory,
    _parent: SourceDirectory,
    marker_bytes: Vec<u8>,
    receipt: ConsumerSelectionReceipt,
    request: ConsumerReopenRequest,
}

pub(crate) fn prepare(
    source: ConsumerAllocationSource,
    database: Uuid,
    consumer: &str,
    source_epoch: u64,
) -> Result<ConsumerReopenRequest> {
    if consumer.is_empty()
        || consumer.len() > 128
        || !consumer
            .bytes()
            .all(|b| b.is_ascii_alphanumeric() || b"._-".contains(&b))
    {
        return Err(invalid("invalid Consumer reopen identity"));
    }
    let mut identifier = [0; 128];
    identifier[..consumer.len()].copy_from_slice(consumer.as_bytes());
    Ok(ConsumerReopenRequest {
        source,
        database,
        consumer: identifier,
        consumer_length: consumer.len() as u8,
        source_epoch,
    })
}

impl ConsumerReopenRequest {
    /// Resolve only the recorded original selector; a newer graph epoch does
    /// not turn a selected operation into a rollback. No target is adopted by name.
    pub fn resolve_publication(
        &self,
        parent: &SourceDirectory,
        locator: &Path,
        task: &RuntimeTaskContext,
        verify: impl FnOnce(&SourceDirectory, &[u8], &ConsumerSelectionReceipt) -> Result<()>,
    ) -> Result<()> {
        checkpoint(task)?;
        if !parent
            .shares_project_context(&self.source.owner.captured_directory().operation_context())
        {
            return Err(invalid(
                "Consumer recovery parent belongs to another project",
            ));
        }
        let operation = self
            .source
            .project
            .with_locked_catalog(|root, context, catalog| {
                let ledger = check_source(
                    &self.source.project,
                    &self.source.owner,
                    self.source.branch,
                    root,
                    context,
                    &catalog,
                )?;
                let mut pending = ledger.pending.iter().filter(|row| {
                    self.matches(row)
                        && row
                            .publication_intent
                            .as_ref()
                            .is_some_and(PublicationIntent::pending)
                });
                let Some(row) = pending.next() else {
                    return Ok(None);
                };
                if pending.next().is_some() || Path::new(&row.target_locator) != locator {
                    return Err(invalid("Consumer recovery target differs"));
                }
                let next = SelectedRoot::from_candidate(row)?;
                let marker = super::native_birth::marker_bytes(
                    self.source.project.allocation_identity().project_id(),
                    row,
                    &next.generation_name,
                    &next.parent_locator,
                )?;
                Ok(Some((
                    next,
                    row.publication_intent
                        .as_ref()
                        .expect("pending intent")
                        .previous
                        .clone(),
                    marker,
                )))
            })?;
        if let Some((next, previous, marker)) = operation {
            if next.seal.source_epoch > self.source_epoch {
                return Err(invalid(
                    "Consumer recovery source is behind the original operation",
                ));
            }
            let context = parent.operation_context();
            let directory = parent.open_directory(&context, &next.generation_name)?;
            verify_birth_bytes(&directory, &marker, task)?;
            let mut file = directory.open_child(&context, "consumer-generation.meta.hawdb")?;
            let mut observed = Vec::new();
            std::io::Read::by_ref(&mut file)
                .take(MAX_BYTES as u64 + 1)
                .read_to_end(&mut observed)?;
            if ObjectReference::for_bytes(ObjectKind::CheckpointArtifact, 1, &observed)
                != (ConsumerSelectionReceipt {
                    selected: next.clone(),
                })
                .metadata_reference()
            {
                return Err(invalid("Consumer recovery sealed metadata differs"));
            }
            if !directory.matches_child_file(&context, "consumer-generation.meta.hawdb", &file)? {
                return Err(invalid("Consumer recovery metadata identity changed"));
            }
            verify(
                &directory,
                &observed,
                &ConsumerSelectionReceipt {
                    selected: next.clone(),
                },
            )?;
            super::selection::complete_outcome(
                &self.source.project,
                &next,
                &previous,
                super::selection::ExpectedOutcome::Either,
            )?;
        }
        Ok(())
    }

    /// Verify the initial immutable directory that carries logical target
    /// exclusion even when a later sibling generation has been selected.
    pub fn capture_registered_target(
        &self,
        parent: &SourceDirectory,
        locator: &Path,
        task: &RuntimeTaskContext,
    ) -> Result<SourceDirectory> {
        let name = locator
            .file_name()
            .and_then(|name| name.to_str())
            .ok_or_else(|| invalid("Consumer target requires a child name"))?;
        let marker = self.source.project.with_locked_catalog(|root, context, catalog| {
            let ledger = check_source(&self.source.project, &self.source.owner, self.source.branch, root, context, &catalog)?;
            let current = ledger.selected.iter().find(|selected| selected.consumer_key() == (self.source.branch.as_uuid().to_string().as_str(), self.database.to_string().as_str(), self.consumer_id()));
            if current.is_some_and(|selected| Path::new(&selected.target_locator) != locator) { return Err(invalid("Consumer target locator differs")); }
            let row = ledger.pending.iter().rev().find(|row| self.matches(row) && Path::new(&row.target_locator) == locator && current.is_none_or(|selected| row.registration_uuid == selected.registration_uuid) && matches!(&row.native_birth, NativeBirth::Recorded { generation_name, .. } if generation_name == name)).ok_or_else(|| invalid("Consumer original target birth is missing"))?;
            let NativeBirth::Recorded { generation_name, parent_locator } = &row.native_birth else { unreachable!() };
            super::native_birth::marker_bytes(self.source.project.allocation_identity().project_id(), row, generation_name, parent_locator)
        })?;
        let target = parent.open_directory(&parent.operation_context(), name)?;
        verify_birth_bytes(&target, &marker, task)?;
        Ok(target)
    }

    fn matches(&self, row: &Reservation) -> bool {
        row.branch_id == self.source.branch.as_uuid().to_string()
            && row.database_uuid == self.database.to_string()
            && row.consumer_id == self.consumer_id()
    }

    fn consumer_id(&self) -> &str {
        std::str::from_utf8(&self.consumer[..usize::from(self.consumer_length)])
            .expect("validated ASCII Consumer identifier")
    }
    /// No saved locator is opened. `original_parent` must be an explicitly
    /// supplied native authority in this source's existing descriptor domain.
    /// The caller prepays CONSUMER_REOPEN_MEMORY_BOUND and owns one I/O wave;
    /// resource acquisition and this method run outside SourceOwner.
    pub fn open(
        self,
        original_parent: SourceDirectory,
        task: &RuntimeTaskContext,
    ) -> Result<ReopenedConsumerGeneration> {
        checkpoint(task)?;
        if !task.has_resource_controllers()
            || task
                .memory_reservation()
                .is_none_or(|r| r.memory_bytes() < CONSUMER_REOPEN_MEMORY_BOUND)
        {
            return Err(HawDBError::Execution(
                "Consumer reopen requires prepaid runtime admission".into(),
            ));
        }
        if !original_parent
            .shares_project_context(&self.source.owner.captured_directory().operation_context())
        {
            return Err(invalid(
                "Consumer reopen parent belongs to another descriptor domain",
            ));
        }
        let _descriptors = self
            .source
            .project
            .file_descriptors()
            .reserve_admission(12)?;
        let (receipt, marker_bytes) = self.with_completed(|_, _, ledger, selected| {
            if selected.seal.source_epoch > self.source_epoch {
                return Err(invalid("selected Consumer is ahead of the captured source"));
            }
            let row = ledger
                .pending
                .iter()
                .find(|row| row.operation == selected.operation)
                .ok_or_else(|| invalid("selected Consumer reservation is missing"))?;
            let marker_bytes = super::native_birth::marker_bytes(
                self.source.project.allocation_identity().project_id(),
                row,
                &selected.generation_name,
                &selected.parent_locator,
            )?;
            Ok((
                ConsumerSelectionReceipt {
                    selected: selected.clone(),
                },
                marker_bytes,
            ))
        })?;
        checkpoint(task)?;
        let context = original_parent.operation_context();
        let directory =
            original_parent.open_directory(&context, &receipt.selected.generation_name)?;
        let marker = directory.open_child(&context, super::native_birth::marker_file_name())?;
        let owner = ReopenedConsumerGeneration {
            marker,
            directory,
            _parent: original_parent,
            marker_bytes,
            receipt,
            request: self,
        };
        owner.verify_marker(task)?;
        owner.require_current(task)?;
        Ok(owner)
    }

    fn with_completed<T>(
        &self,
        action: impl FnOnce(&SourceDirectory, &FileOpenContext, &Ledger, &SelectedRoot) -> Result<T>,
    ) -> Result<T> {
        self.source
            .project
            .with_locked_catalog(|root, context, catalog| {
                check_source_custody(
                    &self.source.project,
                    &self.source.owner,
                    self.source.branch,
                    &catalog,
                )?;
                let mut file = root.open_mutable_child(context, LEDGER_FILE, false)?;
                let ledger = decode_held(&mut file)?;
                if selector(&ledger)? != self.source.project.allocation_identity() {
                    return Err(invalid("Consumer reopen project differs"));
                }
                let branch = self.source.branch.as_uuid().to_string();
                let database = self.database.to_string();
                let matches = |row: &Reservation| {
                    row.branch_id == branch
                        && row.database_uuid == database
                        && row.consumer_id == self.consumer_id()
                };
                if ledger.pending.iter().any(|row| {
                    matches(row)
                        && row
                            .publication_intent
                            .as_ref()
                            .is_some_and(PublicationIntent::pending)
                }) {
                    return Err(HawDBError::ProjectionBusy);
                }
                let selected = ledger
                    .selected
                    .iter()
                    .find(|s| {
                        s.consumer_key() == (branch.as_str(), database.as_str(), self.consumer_id())
                    })
                    .ok_or_else(|| invalid("Consumer has no completed selected generation"))?;
                let row = ledger
                    .pending
                    .iter()
                    .find(|row| row.operation == selected.operation && matches(row))
                    .ok_or_else(|| invalid("selected Consumer reservation is missing"))?;
                if row
                    .publication_intent
                    .as_ref()
                    .is_none_or(|intent| !intent.receipt_complete)
                    || SelectedRoot::from_candidate(row)? != *selected
                {
                    return Err(invalid("Consumer selected receipt is incomplete"));
                }
                // A readable complete image can follow a lost final barrier.
                // Complete barriers on that same held image before granting custody.
                file.sync_all()?;
                root.sync(context)?;
                if !root.matches_child_file(context, LEDGER_FILE, &file)? {
                    return Err(HawDBError::ProjectionBusy);
                }
                let result = action(root, context, &ledger, selected)?;
                if !root.matches_child_file(context, LEDGER_FILE, &file)? {
                    return Err(HawDBError::ProjectionBusy);
                }
                Ok(result)
            })
    }
}

impl ReopenedConsumerGeneration {
    pub fn receipt(&self) -> &ConsumerSelectionReceipt {
        &self.receipt
    }
    pub fn generation_name(&self) -> &str {
        &self.receipt.selected.generation_name
    }

    /// Check native marker identity/content without reopening a saved path.
    /// This does not certify that this historical generation is still selected.
    pub fn directory(&self, task: &RuntimeTaskContext) -> Result<&SourceDirectory> {
        self.verify_marker(task)?;
        Ok(&self.directory)
    }

    /// Require the same completed selector after expensive reader construction.
    /// A subsequent host registry/read-slot fence remains a separate contract.
    pub fn require_current(&self, task: &RuntimeTaskContext) -> Result<()> {
        checkpoint(task)?;
        self.request.with_completed(|_, _, _, selected| {
            if selected != &self.receipt.selected {
                return Err(HawDBError::ProjectionBusy);
            }
            Ok(())
        })?;
        checkpoint(task)
    }

    fn verify_marker(&self, task: &RuntimeTaskContext) -> Result<()> {
        checkpoint(task)?;
        let context = self.directory.operation_context();
        let name = super::native_birth::marker_file_name();
        if !self
            .directory
            .matches_child_file(&context, name, &self.marker)?
        {
            return Err(invalid("reopened Consumer birth marker identity changed"));
        }
        let mut observed = self.directory.open_child(&context, name)?;
        let metadata = observed.metadata()?;
        if !metadata.is_file() || metadata.len() != self.marker_bytes.len() as u64 {
            return Err(invalid("reopened Consumer birth marker length changed"));
        }
        let mut buffer = [0u8; 8192];
        for expected in self.marker_bytes.chunks(buffer.len()) {
            checkpoint(task)?;
            observed.read_exact(&mut buffer[..expected.len()])?;
            if &buffer[..expected.len()] != expected {
                return Err(invalid("reopened Consumer birth marker bytes changed"));
            }
        }
        if observed.read(&mut buffer[..1])? != 0
            || !self
                .directory
                .matches_child_file(&context, name, &observed)?
            || !self
                .directory
                .matches_child_file(&context, name, &self.marker)?
        {
            return Err(invalid("reopened Consumer birth marker changed"));
        }
        Ok(())
    }
}

fn checkpoint(task: &RuntimeTaskContext) -> Result<()> {
    task.checkpoint()
        .map_err(|reason| HawDBError::Execution(format!("Consumer reopen {reason}")))
}

fn verify_birth_bytes(
    directory: &SourceDirectory,
    expected: &[u8],
    task: &RuntimeTaskContext,
) -> Result<()> {
    checkpoint(task)?;
    let context = directory.operation_context();
    let mut file = directory.open_child(&context, super::native_birth::marker_file_name())?;
    if file.metadata()?.len() != expected.len() as u64 {
        return Err(invalid("Consumer birth marker length differs"));
    }
    let mut scratch = [0; 8192];
    for bytes in expected.chunks(scratch.len()) {
        checkpoint(task)?;
        file.read_exact(&mut scratch[..bytes.len()])?;
        if &scratch[..bytes.len()] != bytes {
            return Err(invalid("Consumer birth marker differs"));
        }
    }
    if file.read(&mut scratch[..1])? != 0
        || !directory.matches_child_file(
            &context,
            super::native_birth::marker_file_name(),
            &file,
        )?
    {
        return Err(invalid("Consumer birth marker changed"));
    }
    directory.sync(&context)?;
    Ok(())
}