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

use super::*;
use std::io::Write;
use std::sync::Arc;

#[derive(Debug)]
enum DirectoryOwner<'a> {
    Borrowed(&'a SourceDirectory),
    Shared(Arc<SourceDirectory>),
}

impl std::ops::Deref for DirectoryOwner<'_> {
    type Target = SourceDirectory;

    fn deref(&self) -> &Self::Target {
        match self {
            Self::Borrowed(directory) => directory,
            Self::Shared(directory) => directory,
        }
    }
}

#[cfg(any(test, feature = "test-support"))]
type RenameHook = std::cell::RefCell<Option<Box<dyn FnOnce()>>>;
#[cfg(any(test, feature = "test-support"))]
type RenameObserver = std::cell::RefCell<Option<Box<dyn FnMut() -> io::Result<()>>>>;
#[cfg(any(test, feature = "test-support"))]
thread_local! {
    static AFTER_RENAME: RenameHook = const { std::cell::RefCell::new(None) };
    static AFTER_EACH_RENAME: RenameObserver = const { std::cell::RefCell::new(None) };
}

#[cfg(any(test, feature = "test-support"))]
impl SourceDirectory {
    /// Observe a bounded operation's control selections for failure injection.
    /// The callback must not recursively publish another control.
    #[doc(hidden)]
    pub fn observe_control_renames<T>(
        mut callback: impl FnMut() + 'static,
        operation: impl FnOnce() -> T,
    ) -> io::Result<T> {
        Self::observe_control_replies(
            move || {
                callback();
                Ok(())
            },
            operation,
        )
    }

    /// Inject an IO/lost-response failure after native selection without
    /// pretending that the completed namespace effect rolled back.
    #[doc(hidden)]
    pub fn observe_control_replies<T>(
        callback: impl FnMut() -> io::Result<()> + 'static,
        operation: impl FnOnce() -> T,
    ) -> io::Result<T> {
        AFTER_EACH_RENAME.with(|hook| {
            let mut slot = hook.borrow_mut();
            if slot.is_some() {
                return Err(io::Error::new(
                    io::ErrorKind::WouldBlock,
                    "a control observer is already installed",
                ));
            }
            *slot = Some(Box::new(callback));
            Ok(())
        })?;
        struct Reset;
        impl Drop for Reset {
            fn drop(&mut self) {
                AFTER_EACH_RENAME.with(|hook| *hook.borrow_mut() = None);
            }
        }
        let reset = Reset;
        let result = operation();
        drop(reset);
        Ok(result)
    }

    /// Test-only, thread-local observation after one successful native rename
    /// and before its completion barriers. The scope resets on return/unwind.
    #[doc(hidden)]
    pub fn observe_next_control_rename<T>(
        callback: impl FnOnce() + 'static,
        operation: impl FnOnce() -> T,
    ) -> io::Result<T> {
        AFTER_RENAME.with(|hook| {
            let mut slot = hook.borrow_mut();
            if slot.is_some() {
                return Err(io::Error::new(
                    io::ErrorKind::WouldBlock,
                    "a control rename observer is already installed",
                ));
            }
            *slot = Some(Box::new(callback));
            Ok(())
        })?;
        struct Reset;
        impl Drop for Reset {
            fn drop(&mut self) {
                AFTER_RENAME.with(|hook| *hook.borrow_mut() = None);
            }
        }
        let reset = Reset;
        let result = operation();
        drop(reset);
        Ok(result)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum State {
    Writable,
    Sealed,
    Attempted,
    Complete,
    Discarded,
}

/// A new mutable-control candidate in one captured directory. The caller owns
/// publication exclusion, dependency durability and recovery/cleanup custody.
/// This is not an immutable-artifact publisher or a Consumer activation token.
/// Do not target ownership locks or managed immutable aliases with this API.
/// Drop closes handles only: failed or uncertain publication never deletes data.
#[doc(hidden)]
#[derive(Debug)]
pub struct CapturedControlCandidate<'a> {
    file: File,
    directory: DirectoryOwner<'a>,
    name: [u8; 256],
    length: usize,
    state: State,
}

/// A selected control whose durability is pending. It exclusively borrows the
/// attempted candidate, retaining its file/directory custody through the fence
/// release. Drop performs no rollback, acknowledgement or namespace cleanup.
#[doc(hidden)]
#[derive(Debug)]
#[must_use = "finish durability or retain recovery custody for the selected control"]
pub struct PendingControlPublication<'candidate, 'directory> {
    candidate: &'candidate mut CapturedControlCandidate<'directory>,
    target: [u8; 256],
    length: usize,
}

impl SourceDirectory {
    /// Exclusively create a writable regular control candidate without using a
    /// pathname. The returned owner borrows this capture, not the operation's
    /// FD inventory. Existing entries, including symlinks, are never adopted.
    #[doc(hidden)]
    pub fn create_control_candidate(
        &self,
        context: &FileOpenContext,
        name: &str,
    ) -> io::Result<CapturedControlCandidate<'_>> {
        self.require_context(context)?;
        let name = ChildName::new(name)?;
        let scope = native_scope(context)?;
        self.require_scope(&scope)?;
        let permit = context.acquire(DescriptorKind::Transient)?;
        let open = || {
            self.root
                .with_native(|root| platform::create_control_candidate(root, name))
                .map_err(|error| context.map_open_error(error))
        };
        #[cfg(any(test, feature = "test-support"))]
        let file = match self.trace.as_deref() {
            Some(trace) => {
                let (inner, trace) = trace.create_file(Path::new(name.0), open)?;
                counted_traced(inner, permit, Some(Box::new(trace)))
            }
            None => counted(open()?, permit),
        };
        #[cfg(not(any(test, feature = "test-support")))]
        let file = counted(open()?, permit);
        let mut bytes = [0; 256];
        bytes[..name.0.len()].copy_from_slice(name.0.as_bytes());
        Ok(CapturedControlCandidate {
            file,
            directory: DirectoryOwner::Borrowed(self),
            name: bytes,
            length: name.0.len(),
            state: State::Writable,
        })
    }
}

impl SourceDirectory {
    /// Retain an existing shared capture without duplicating its native handle
    /// or preserving a producer's FD inventory. This still grants no exclusion.
    #[doc(hidden)]
    pub fn create_owned_control_candidate(
        self: &Arc<Self>,
        context: &FileOpenContext,
        name: &str,
    ) -> io::Result<CapturedControlCandidate<'static>> {
        let CapturedControlCandidate {
            file,
            name,
            length,
            state,
            ..
        } = self.create_control_candidate(context, name)?;
        Ok(CapturedControlCandidate {
            file,
            directory: DirectoryOwner::Shared(self.clone()),
            name,
            length,
            state,
        })
    }
}

impl<'directory> CapturedControlCandidate<'directory> {
    /// Complete and synchronize candidate bytes before attempting publication.
    /// Successful sealing permanently removes this owner's write capability.
    pub fn seal(&mut self) -> io::Result<()> {
        self.require_writable()?;
        self.file.sync_all()?;
        self.state = State::Sealed;
        Ok(())
    }

    /// Replace one mutable head relative to the same capture, then synchronize
    /// the held candidate and captured directory. An error may follow a complete
    /// effect. Every attempt revokes further writes/retries; retain the actual
    /// publication lease and all recovery dependencies until outcome resolution.
    /// The caller keeps candidate names and contents immutable under publication exclusion.
    pub fn replace_head(&mut self, context: &FileOpenContext, name: &str) -> io::Result<()> {
        self.select_head(context, name)?.finish(context)
    }

    /// Perform only the native selection and return owned pending-barrier
    /// custody. A higher-level coordinator must validate/swap its read view
    /// under the same fence, release SourceOwner, then call `finish`.
    pub fn select_head<'candidate>(
        &'candidate mut self,
        context: &FileOpenContext,
        name: &str,
    ) -> io::Result<PendingControlPublication<'candidate, 'directory>> {
        self.directory.require_context(context)?;
        let target = ChildName::new(name)?;
        let source = ChildName(std::str::from_utf8(&self.name[..self.length]).expect("ASCII name"));
        if self.state != State::Sealed || source.0 == target.0 {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "control candidate must be sealed and have a distinct head name",
            ));
        }
        // Resource refusal and identity checks precede the first namespace
        // attempt. They leave this same sealed owner retryable.
        let _preflight = context.acquire(DescriptorKind::Transient)?;
        {
            let scope = native_scope(context)?;
            self.directory.require_scope(&scope)?;
            let FileBacking::Native(candidate) = &self.file.backing else {
                unreachable!("control candidates exclusively create native files");
            };
            let mut native = || {
                self.directory.root.with_native(|root| {
                    // NativeDirectoryTrace owns the namespace transaction here.
                    // Reentering the candidate's file trace would relock ModelCore.
                    {
                        let entry = platform::open_child(root, source)?;
                        if crate::native_file_identity::of_file(&entry)?
                            != crate::native_file_identity::of_file(&candidate.inner)?
                        {
                            return Err(io::Error::new(
                                io::ErrorKind::InvalidData,
                                "control candidate entry changed after creation",
                            ));
                        }
                    }
                    {
                        match platform::open_child(root, target) {
                            Ok(entry) => {
                                if !entry.metadata()?.is_file() {
                                    return Err(io::Error::new(
                                        io::ErrorKind::InvalidData,
                                        "control head is not a regular file",
                                    ));
                                }
                                if crate::native_file_identity::of_file(&entry)?
                                    == crate::native_file_identity::of_file(&candidate.inner)?
                                {
                                    return Err(io::Error::new(
                                        io::ErrorKind::AlreadyExists,
                                        "control head aliases the private candidate",
                                    ));
                                }
                            }
                            Err(error) if error.kind() == io::ErrorKind::NotFound => {}
                            Err(error) => return Err(error),
                        }
                    }
                    self.state = State::Attempted;
                    platform::replace_control(root, &candidate.inner, source, target)
                })
            };
            #[cfg(any(test, feature = "test-support"))]
            match self.directory.trace.as_deref() {
                Some(trace) => {
                    trace.replace_file(Path::new(source.0), Path::new(target.0), native)?
                }
                None => native()?,
            }
            #[cfg(not(any(test, feature = "test-support")))]
            native()?;
        }
        drop(_preflight);
        #[cfg(any(test, feature = "test-support"))]
        AFTER_EACH_RENAME.with(|hook| {
            if let Some(callback) = hook.borrow_mut().as_mut() {
                callback()?;
            }
            Ok::<_, io::Error>(())
        })?;
        #[cfg(any(test, feature = "test-support"))]
        AFTER_RENAME.with(|hook| {
            let callback = hook.borrow_mut().take();
            if let Some(callback) = callback {
                callback();
            }
        });
        let mut target = [0; 256];
        target[..name.len()].copy_from_slice(name.as_bytes());
        Ok(PendingControlPublication {
            candidate: self,
            target,
            length: name.len(),
        })
    }

    pub fn is_complete(&self) -> bool {
        self.state == State::Complete
    }

    pub fn selection_attempted(&self) -> bool {
        matches!(self.state, State::Attempted | State::Complete)
    }

    /// Remove only an unselected candidate whose entry still names this held
    /// file. The caller excludes other writers; unknown rename effects retain
    /// their original recovery owner instead of authorizing cleanup.
    pub fn discard_unselected(&mut self, context: &FileOpenContext) -> io::Result<()> {
        if self.state == State::Discarded {
            return self.directory.sync(context);
        }
        if !matches!(self.state, State::Writable | State::Sealed) {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "attempted control cannot be discarded",
            ));
        }
        let name = std::str::from_utf8(&self.name[..self.length]).expect("ASCII name");
        if !self
            .directory
            .matches_child_file(context, name, &self.file)?
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "private control entry changed before cleanup",
            ));
        }
        self.directory.remove_child(context, name)?;
        self.state = State::Discarded;
        self.directory.sync(context)
    }

    fn require_writable(&self) -> io::Result<()> {
        if self.state != State::Writable {
            return Err(io::Error::new(
                io::ErrorKind::PermissionDenied,
                "sealed or attempted control candidate is not writable",
            ));
        }
        Ok(())
    }
}

impl PendingControlPublication<'_, '_> {
    /// Complete barriers with a fresh operation context after releasing the
    /// source-owner fence. Errors retain the candidate in attempted state.
    pub fn finish(self, context: &FileOpenContext) -> io::Result<()> {
        self.candidate.directory.require_context(context)?;
        let target = std::str::from_utf8(&self.target[..self.length]).expect("ASCII name");
        let candidate = self.candidate;
        // Separate traced operations preserve completed barriers and lost
        // responses. Never hide a post-rename sync inside the native callback.
        candidate.file.sync_all()?;
        let selected = candidate.directory.open_child(context, target)?;
        if selected.with_native(crate::native_file_identity::of_file)?
            != candidate
                .file
                .with_native(crate::native_file_identity::of_file)?
        {
            return Err(io::Error::new(
                io::ErrorKind::InvalidData,
                "control head differs from the held publication",
            ));
        }
        drop(selected);
        candidate.directory.sync(context)?;
        candidate.state = State::Complete;
        Ok(())
    }
}

impl Write for CapturedControlCandidate<'_> {
    fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
        self.require_writable()?;
        self.file.write(bytes)
    }
    fn flush(&mut self) -> io::Result<()> {
        self.require_writable()?;
        self.file.flush()
    }
}

#[cfg(all(test, any(target_os = "linux", target_vendor = "apple", windows)))]
mod tests;