noxu-tree 6.2.0

B-tree implementation for Noxu DB
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
//! Slot state bit flags for BIN entries.
//!
//! Each slot in a BIN (Bottom Internal Node) has associated state flags
//! that track metadata about the entry (deleted, dirty, embedded, etc.).
//!
//! **Provenance (T-7):** the bit layout below is bit-exact to the NoSQL-DB
//! (`kvmain`) fork's `EntryStates`, NOT to the base BDB-JE `EntryStates.java`
//! (the fork added/renamed several bits — e.g. the embedded-LN and no-data bits).
//! Where the doc comments cite "JE `EntryStates.java`" they describe the
//! *conceptual* JE meaning; the concrete bit VALUES follow the kvmain fork.
//! This is a FORK-OK deviation (kvmain-aligned), not a base-JE divergence.

/// Known deleted bit - slot is known to be deleted (obsolete).
pub const KNOWN_DELETED_BIT: u8 = 0x01;

/// Dirty bit - slot has been modified since last log write.
pub const DIRTY_BIT: u8 = 0x02;

/// Transient migrate bit - formerly used for migration, always transient.
/// 0x04 is reserved as transient forever (it was accidentally persisted
/// historically, so it cannot be reused as a persistent bit).
pub const MIGRATE_BIT: u8 = 0x04;

/// Pending deleted bit - delete is pending (not yet committed).
pub const PENDING_DELETED_BIT: u8 = 0x08;

/// Embedded LN bit - LN data is embedded directly in the BIN slot.
pub const EMBEDDED_LN_BIT: u8 = 0x10;

/// No data LN bit - LN has no data (zero-length embedded data).
pub const NO_DATA_LN_BIT: u8 = 0x20;

/// Update key when logged bit - transient flag to update key on next log.
///
pub const UPDATE_KEY_WHEN_LOGGED: u8 = 0x40;

/// Tombstone bit - slot is a blind-deletion tombstone.
///
/// **Noxu-only extension bit — NOT present in JE.**
///
/// JE `EntryStates.java` uses bits 0x01–0x40 only; 0x80 is unused in JE.
/// In Noxu, TOMBSTONE_BIT marks a slot as a blind-deletion marker:
/// the key exists in the BIN but its LN has been deleted without a
/// full transaction (used by `ExtinctionScanner` / `discard_extinct_records`).
///
/// This bit IS persisted to disk (it is NOT in `TRANSIENT_BITS`). This is
/// intentional: a tombstone must survive checkpoints so that the cleaner
/// can reclaim the space on the next log cleaning pass.
///
/// Compatibility note: a JE-format reader encountering a slot with 0x80 set
/// will see an unknown bit in the state byte. JE processes
/// `KNOWN_DELETED_BIT`, `DIRTY_BIT`, `PENDING_DELETED_BIT`, `EMBEDDED_LN_BIT`,
/// and `NO_DATA_LN_BIT` independently by bit-masking; it does not reject
/// unknown bits, so a slot with TOMBSTONE_BIT set is safe to read by JE
/// (the bit is simply ignored). No JE-compat concern exists for read paths.
///
/// DRIFT-7 audit finding disposition: TOMBSTONE_BIT is a deliberate
/// Noxu extension, not a missing transient-mask entry.
pub const TOMBSTONE_BIT: u8 = 0x80;

/// Mask for transient state bits (not logged to disk).
///
/// JE `EntryStates.TRANSIENT_BITS = OFFHEAP_DIRTY_BIT | OFFHEAP_PRI2_BIT`.
/// Noxu maps those off-heap bits to different purposes but preserves the
/// transient semantics:
///   - `MIGRATE_BIT (0x04)` ≈ JE `OFFHEAP_DIRTY_BIT (0x04)` — always transient.
///   - `UPDATE_KEY_WHEN_LOGGED (0x40)` ≈ JE `OFFHEAP_PRI2_BIT (0x40)` — transient.
///
/// `TOMBSTONE_BIT (0x80)` is intentionally NOT in TRANSIENT_BITS:
/// it is a Noxu-only persisted bit for blind-deletion markers.
/// See `TOMBSTONE_BIT` documentation.
pub const TRANSIENT_BITS: u8 = MIGRATE_BIT | UPDATE_KEY_WHEN_LOGGED;

/// A newtype wrapper around slot state flags.
///
/// Provides type-safe access to individual state bits.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SlotState(u8);

impl SlotState {
    /// Creates a new slot state with all flags cleared.
    #[inline]
    pub fn new() -> Self {
        SlotState(0)
    }

    /// Creates a slot state from a raw byte value.
    #[inline]
    pub fn from_byte(byte: u8) -> Self {
        SlotState(byte)
    }

    /// Returns the raw byte value of the state flags.
    #[inline]
    pub fn as_byte(self) -> u8 {
        self.0
    }

    /// Returns true if the known-deleted bit is set.
    #[inline]
    pub fn is_known_deleted(self) -> bool {
        (self.0 & KNOWN_DELETED_BIT) != 0
    }

    /// Sets the known-deleted bit.
    #[inline]
    pub fn set_known_deleted(&mut self) {
        self.0 |= KNOWN_DELETED_BIT;
    }

    /// Clears the known-deleted bit.
    #[inline]
    pub fn clear_known_deleted(&mut self) {
        self.0 &= !KNOWN_DELETED_BIT;
    }

    /// Returns true if the dirty bit is set.
    #[inline]
    pub fn is_dirty(self) -> bool {
        (self.0 & DIRTY_BIT) != 0
    }

    /// Sets the dirty bit.
    #[inline]
    pub fn set_dirty(&mut self) {
        self.0 |= DIRTY_BIT;
    }

    /// Clears the dirty bit.
    #[inline]
    pub fn clear_dirty(&mut self) {
        self.0 &= !DIRTY_BIT;
    }

    /// Returns true if the migrate (transient) bit is set.
    #[inline]
    pub fn is_migrate(self) -> bool {
        (self.0 & MIGRATE_BIT) != 0
    }

    /// Sets the migrate bit.
    #[inline]
    pub fn set_migrate(&mut self) {
        self.0 |= MIGRATE_BIT;
    }

    /// Clears the migrate bit.
    #[inline]
    pub fn clear_migrate(&mut self) {
        self.0 &= !MIGRATE_BIT;
    }

    /// Returns true if the pending-deleted bit is set.
    #[inline]
    pub fn is_pending_deleted(self) -> bool {
        (self.0 & PENDING_DELETED_BIT) != 0
    }

    /// Sets the pending-deleted bit.
    #[inline]
    pub fn set_pending_deleted(&mut self) {
        self.0 |= PENDING_DELETED_BIT;
    }

    /// Clears the pending-deleted bit.
    #[inline]
    pub fn clear_pending_deleted(&mut self) {
        self.0 &= !PENDING_DELETED_BIT;
    }

    /// Returns true if the embedded-LN bit is set.
    #[inline]
    pub fn is_embedded_ln(self) -> bool {
        (self.0 & EMBEDDED_LN_BIT) != 0
    }

    /// Sets the embedded-LN bit.
    #[inline]
    pub fn set_embedded_ln(&mut self) {
        self.0 |= EMBEDDED_LN_BIT;
    }

    /// Clears the embedded-LN bit.
    #[inline]
    pub fn clear_embedded_ln(&mut self) {
        self.0 &= !EMBEDDED_LN_BIT;
    }

    /// Returns true if the no-data-LN bit is set.
    #[inline]
    pub fn is_no_data_ln(self) -> bool {
        (self.0 & NO_DATA_LN_BIT) != 0
    }

    /// Sets the no-data-LN bit.
    #[inline]
    pub fn set_no_data_ln(&mut self) {
        self.0 |= NO_DATA_LN_BIT;
    }

    /// Clears the no-data-LN bit.
    #[inline]
    pub fn clear_no_data_ln(&mut self) {
        self.0 &= !NO_DATA_LN_BIT;
    }

    /// Returns true if the update-key-when-logged (transient) bit is set.
    ///
    ///
    #[inline]
    pub fn is_update_key_when_logged(self) -> bool {
        (self.0 & UPDATE_KEY_WHEN_LOGGED) != 0
    }

    /// Sets the update-key-when-logged bit.
    #[inline]
    pub fn set_update_key_when_logged(&mut self) {
        self.0 |= UPDATE_KEY_WHEN_LOGGED;
    }

    /// Clears the update-key-when-logged bit.
    #[inline]
    pub fn clear_update_key_when_logged(&mut self) {
        self.0 &= !UPDATE_KEY_WHEN_LOGGED;
    }

    /// Returns true if the tombstone bit is set.
    ///
    /// A tombstone slot is a blind-deletion marker (extended capability).
    ///
    #[inline]
    pub fn is_tombstone(self) -> bool {
        (self.0 & TOMBSTONE_BIT) != 0
    }

    /// Sets the tombstone bit.
    #[inline]
    pub fn set_tombstone(&mut self) {
        self.0 |= TOMBSTONE_BIT;
    }

    /// Clears the tombstone bit.
    #[inline]
    pub fn clear_tombstone(&mut self) {
        self.0 &= !TOMBSTONE_BIT;
    }

    /// Clears all transient bits (not persisted to disk).
    ///
    /// Transient bits are: MIGRATE_BIT (0x04), UPDATE_KEY_WHEN_LOGGED (0x40).
    ///
    #[inline]
    pub fn clear_transient_bits(&mut self) {
        self.0 &= !TRANSIENT_BITS;
    }

    /// Returns a copy with transient bits cleared.
    #[inline]
    pub fn with_transient_bits_cleared(self) -> Self {
        SlotState(self.0 & !TRANSIENT_BITS)
    }
}

impl From<u8> for SlotState {
    fn from(byte: u8) -> Self {
        SlotState(byte)
    }
}

impl From<SlotState> for u8 {
    fn from(state: SlotState) -> Self {
        state.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_state_is_clear() {
        let state = SlotState::new();
        assert_eq!(state.as_byte(), 0);
        assert!(!state.is_known_deleted());
        assert!(!state.is_dirty());
        assert!(!state.is_embedded_ln());
    }

    #[test]
    fn test_known_deleted() {
        let mut state = SlotState::new();
        assert!(!state.is_known_deleted());

        state.set_known_deleted();
        assert!(state.is_known_deleted());
        assert_eq!(state.as_byte(), KNOWN_DELETED_BIT);

        state.clear_known_deleted();
        assert!(!state.is_known_deleted());
        assert_eq!(state.as_byte(), 0);
    }

    #[test]
    fn test_dirty() {
        let mut state = SlotState::new();
        assert!(!state.is_dirty());

        state.set_dirty();
        assert!(state.is_dirty());
        assert_eq!(state.as_byte(), DIRTY_BIT);

        state.clear_dirty();
        assert!(!state.is_dirty());
    }

    #[test]
    fn test_multiple_flags() {
        let mut state = SlotState::new();

        state.set_dirty();
        state.set_embedded_ln();
        assert!(state.is_dirty());
        assert!(state.is_embedded_ln());
        assert!(!state.is_known_deleted());
        assert_eq!(state.as_byte(), DIRTY_BIT | EMBEDDED_LN_BIT);

        state.clear_dirty();
        assert!(!state.is_dirty());
        assert!(state.is_embedded_ln());
    }

    #[test]
    fn test_transient_bits() {
        let mut state = SlotState::new();
        state.set_migrate();
        state.set_update_key_when_logged();
        state.set_dirty();

        assert!(state.is_migrate());
        assert!(state.is_update_key_when_logged());
        assert!(state.is_dirty());

        state.clear_transient_bits();
        assert!(!state.is_migrate());
        assert!(!state.is_update_key_when_logged());
        assert!(state.is_dirty()); // Non-transient bit should remain
    }

    #[test]
    fn test_with_transient_bits_cleared() {
        let mut state = SlotState::new();
        state.set_migrate();
        state.set_dirty();
        state.set_embedded_ln();

        let cleared = state.with_transient_bits_cleared();
        assert!(!cleared.is_migrate());
        assert!(cleared.is_dirty());
        assert!(cleared.is_embedded_ln());

        // Original should be unchanged
        assert!(state.is_migrate());
    }

    #[test]
    fn test_tombstone() {
        let mut state = SlotState::new();
        assert!(!state.is_tombstone());

        state.set_tombstone();
        assert!(state.is_tombstone());
        assert_eq!(state.as_byte(), TOMBSTONE_BIT);

        state.clear_tombstone();
        assert!(!state.is_tombstone());
    }

    #[test]
    fn test_update_key_when_logged() {
        let mut state = SlotState::new();
        assert!(!state.is_update_key_when_logged());

        state.set_update_key_when_logged();
        assert!(state.is_update_key_when_logged());
        // It's a transient bit, so it clears with clear_transient_bits
        state.clear_transient_bits();
        assert!(!state.is_update_key_when_logged());
    }

    #[test]
    fn test_from_byte() {
        let state = SlotState::from_byte(DIRTY_BIT | EMBEDDED_LN_BIT);
        assert!(state.is_dirty());
        assert!(state.is_embedded_ln());
        assert!(!state.is_known_deleted());
    }

    #[test]
    fn test_conversions() {
        let byte: u8 = DIRTY_BIT | PENDING_DELETED_BIT;
        let state = SlotState::from(byte);
        assert!(state.is_dirty());
        assert!(state.is_pending_deleted());

        let back: u8 = state.into();
        assert_eq!(back, byte);
    }

    #[test]
    fn test_all_flags() {
        let mut state = SlotState::new();

        // Set all persistent + transient flags
        state.set_known_deleted();
        state.set_dirty();
        state.set_migrate();
        state.set_pending_deleted();
        state.set_embedded_ln();
        state.set_no_data_ln();
        state.set_update_key_when_logged();
        state.set_tombstone();

        // Check all are set
        assert!(state.is_known_deleted());
        assert!(state.is_dirty());
        assert!(state.is_migrate());
        assert!(state.is_pending_deleted());
        assert!(state.is_embedded_ln());
        assert!(state.is_no_data_ln());
        assert!(state.is_update_key_when_logged());
        assert!(state.is_tombstone());
    }
}