noxu-txn 4.0.0

Transaction management and locking 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
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
//! Lock type definitions and conflict/upgrade matrices.
//!

use crate::{LockConflict, LockUpgrade};

/// Lock types used in the transaction system.
///
/// Noxu DB uses hierarchical locking with five primary lock types.
/// The conflict and upgrade matrices define how locks interact.
///
///
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum LockType {
    /// Basic read lock on a record.
    ///
    /// Allows multiple concurrent readers but blocks writers.
    Read,

    /// Write lock on a record (exclusive).
    ///
    /// Blocks all other lockers from reading or writing this record.
    Write,

    /// Range read lock  -  locks a range to prevent phantom inserts.
    ///
    /// Similar to Read but also prevents inserts into the range.
    RangeRead,

    /// Range write lock  -  combines range and write locking.
    ///
    /// Blocks all access and prevents inserts into the range.
    RangeWrite,

    /// Range insert lock  -  used when inserting into a range.
    ///
    /// Conflicts with range locks to trigger restart on phantom detection.
    RangeInsert,

    /// No lock requested (dirty read).
    ///
    /// Special type indicating no locking should be performed.
    None,

    /// Restart marker  -  not a real lock type.
    ///
    /// Used internally to indicate a range restart is needed.
    Restart,
}

impl LockType {
    /// Returns true if this is a write lock that modifies data.
    ///
    /// Note: Per implementation, RangeInsert is NOT considered a write lock
    /// for the purposes of transaction commit/abort. Only Write and RangeWrite
    /// require undo information.
    #[inline]
    pub fn is_write_lock(self) -> bool {
        matches!(self, LockType::Write | LockType::RangeWrite)
    }

    /// Returns true if this lock type causes a restart when upgraded.
    ///
    /// RangeRead and RangeWrite cause restarts in certain upgrade scenarios.
    #[inline]
    pub fn causes_restart(self) -> bool {
        matches!(self, LockType::RangeRead | LockType::RangeWrite)
    }

    /// Returns the conflict status between a held lock and a requested lock.
    ///
    /// This implements the 5x5 conflict matrix from the:
    /// ```text
    ///              READ    WRITE   RANGE_R  RANGE_W  RANGE_I
    /// READ        ALLOW   BLOCK   ALLOW    BLOCK    ALLOW
    /// WRITE       BLOCK   BLOCK   BLOCK    BLOCK    ALLOW
    /// RANGE_READ  ALLOW   BLOCK   ALLOW    BLOCK    BLOCK
    /// RANGE_WRITE BLOCK   BLOCK   BLOCK    BLOCK    BLOCK
    /// RANGE_INS   ALLOW   ALLOW   RESTART  RESTART  ALLOW
    /// ```
    pub fn get_conflict(self, requested: LockType) -> LockConflict {
        use LockConflict::*;
        use LockType::*;

        match (self, requested) {
            // READ row
            (Read, Read) => Allow,
            (Read, Write) => Block,
            (Read, RangeRead) => Allow,
            (Read, RangeWrite) => Block,
            (Read, RangeInsert) => Allow,

            // WRITE row
            (Write, Read) => Block,
            (Write, Write) => Block,
            (Write, RangeRead) => Block,
            (Write, RangeWrite) => Block,
            (Write, RangeInsert) => Allow,

            // RANGE_READ row
            (RangeRead, Read) => Allow,
            (RangeRead, Write) => Block,
            (RangeRead, RangeRead) => Allow,
            (RangeRead, RangeWrite) => Block,
            (RangeRead, RangeInsert) => Block,

            // RANGE_WRITE row
            (RangeWrite, Read) => Block,
            (RangeWrite, Write) => Block,
            (RangeWrite, RangeRead) => Block,
            (RangeWrite, RangeWrite) => Block,
            (RangeWrite, RangeInsert) => Block,

            // RANGE_INSERT row
            (RangeInsert, Read) => Allow,
            (RangeInsert, Write) => Allow,
            (RangeInsert, RangeRead) => LockConflict::Restart,
            (RangeInsert, RangeWrite) => LockConflict::Restart,
            (RangeInsert, RangeInsert) => Allow,

            // None and Restart are not used in conflict checking
            _ => Allow,
        }
    }

    /// Returns the upgrade result when a locker holds this lock and requests another.
    ///
    /// This implements the 5x5 upgrade matrix from the:
    /// ```text
    ///              READ           WRITE              RANGE_R            RANGE_W              RANGE_I
    /// READ        EXISTING       WRITE_PROMOTE      RANGE_READ_IMMED   RANGE_WRITE_PROMOTE  ILLEGAL
    /// WRITE       EXISTING       EXISTING           RANGE_WRITE_IMMED  RANGE_WRITE_IMMED    ILLEGAL
    /// RANGE_READ  EXISTING       RANGE_WRITE_PROM   EXISTING           RANGE_WRITE_PROMOTE  ILLEGAL
    /// RANGE_WRITE EXISTING       EXISTING           EXISTING           EXISTING             ILLEGAL
    /// RANGE_INS   ILLEGAL        ILLEGAL            ILLEGAL            ILLEGAL              EXISTING
    /// ```
    pub fn get_upgrade(self, requested: LockType) -> LockUpgrade {
        use LockType::*;
        use LockUpgrade::*;

        match (self, requested) {
            // READ row
            (Read, Read) => Existing,
            (Read, Write) => WritePromote,
            (Read, RangeRead) => RangeReadImmed,
            (Read, RangeWrite) => RangeWritePromote,
            (Read, RangeInsert) => Illegal,

            // WRITE row
            (Write, Read) => Existing,
            (Write, Write) => Existing,
            (Write, RangeRead) => RangeWriteImmed,
            (Write, RangeWrite) => RangeWriteImmed,
            (Write, RangeInsert) => Illegal,

            // RANGE_READ row
            (RangeRead, Read) => Existing,
            (RangeRead, Write) => RangeWritePromote,
            (RangeRead, RangeRead) => Existing,
            (RangeRead, RangeWrite) => RangeWritePromote,
            (RangeRead, RangeInsert) => Illegal,

            // RANGE_WRITE row
            (RangeWrite, Read) => Existing,
            (RangeWrite, Write) => Existing,
            (RangeWrite, RangeRead) => Existing,
            (RangeWrite, RangeWrite) => Existing,
            (RangeWrite, RangeInsert) => Illegal,

            // RANGE_INSERT row
            (RangeInsert, Read) => Illegal,
            (RangeInsert, Write) => Illegal,
            (RangeInsert, RangeRead) => Illegal,
            (RangeInsert, RangeWrite) => Illegal,
            (RangeInsert, RangeInsert) => Existing,

            // None and Restart upgrades
            (None, _) => Illegal,
            (_, None) => Existing,
            (Restart, _) => Illegal,
            (_, Restart) => Illegal,
        }
    }

    /// Returns the array index for this lock type (0-4 for the 5 main types).
    ///
    /// Used for indexing into statistics arrays.
    #[inline]
    pub fn index(self) -> usize {
        match self {
            LockType::Read => 0,
            LockType::Write => 1,
            LockType::RangeRead => 2,
            LockType::RangeWrite => 3,
            LockType::RangeInsert => 4,
            LockType::None => 5,
            LockType::Restart => 6,
        }
    }
}

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

    #[test]
    fn test_is_write_lock() {
        assert!(!LockType::Read.is_write_lock());
        assert!(LockType::Write.is_write_lock());
        assert!(!LockType::RangeRead.is_write_lock());
        assert!(LockType::RangeWrite.is_write_lock());
        assert!(!LockType::RangeInsert.is_write_lock()); // Important: NOT a write lock per 
        assert!(!LockType::None.is_write_lock());
        assert!(!LockType::Restart.is_write_lock());
    }

    #[test]
    fn test_causes_restart() {
        assert!(!LockType::Read.causes_restart());
        assert!(!LockType::Write.causes_restart());
        assert!(LockType::RangeRead.causes_restart());
        assert!(LockType::RangeWrite.causes_restart());
        assert!(!LockType::RangeInsert.causes_restart());
        assert!(!LockType::None.causes_restart());
        assert!(!LockType::Restart.causes_restart());
    }

    #[test]
    fn test_index() {
        assert_eq!(LockType::Read.index(), 0);
        assert_eq!(LockType::Write.index(), 1);
        assert_eq!(LockType::RangeRead.index(), 2);
        assert_eq!(LockType::RangeWrite.index(), 3);
        assert_eq!(LockType::RangeInsert.index(), 4);
        assert_eq!(LockType::None.index(), 5);
        assert_eq!(LockType::Restart.index(), 6);
    }

    // Test all 25 entries of the conflict matrix
    #[test]
    fn test_conflict_matrix_read_row() {
        assert_eq!(
            LockType::Read.get_conflict(LockType::Read),
            LockConflict::Allow
        );
        assert_eq!(
            LockType::Read.get_conflict(LockType::Write),
            LockConflict::Block
        );
        assert_eq!(
            LockType::Read.get_conflict(LockType::RangeRead),
            LockConflict::Allow
        );
        assert_eq!(
            LockType::Read.get_conflict(LockType::RangeWrite),
            LockConflict::Block
        );
        assert_eq!(
            LockType::Read.get_conflict(LockType::RangeInsert),
            LockConflict::Allow
        );
    }

    #[test]
    fn test_conflict_matrix_write_row() {
        assert_eq!(
            LockType::Write.get_conflict(LockType::Read),
            LockConflict::Block
        );
        assert_eq!(
            LockType::Write.get_conflict(LockType::Write),
            LockConflict::Block
        );
        assert_eq!(
            LockType::Write.get_conflict(LockType::RangeRead),
            LockConflict::Block
        );
        assert_eq!(
            LockType::Write.get_conflict(LockType::RangeWrite),
            LockConflict::Block
        );
        assert_eq!(
            LockType::Write.get_conflict(LockType::RangeInsert),
            LockConflict::Allow
        );
    }

    #[test]
    fn test_conflict_matrix_range_read_row() {
        assert_eq!(
            LockType::RangeRead.get_conflict(LockType::Read),
            LockConflict::Allow
        );
        assert_eq!(
            LockType::RangeRead.get_conflict(LockType::Write),
            LockConflict::Block
        );
        assert_eq!(
            LockType::RangeRead.get_conflict(LockType::RangeRead),
            LockConflict::Allow
        );
        assert_eq!(
            LockType::RangeRead.get_conflict(LockType::RangeWrite),
            LockConflict::Block
        );
        assert_eq!(
            LockType::RangeRead.get_conflict(LockType::RangeInsert),
            LockConflict::Block
        );
    }

    #[test]
    fn test_conflict_matrix_range_write_row() {
        assert_eq!(
            LockType::RangeWrite.get_conflict(LockType::Read),
            LockConflict::Block
        );
        assert_eq!(
            LockType::RangeWrite.get_conflict(LockType::Write),
            LockConflict::Block
        );
        assert_eq!(
            LockType::RangeWrite.get_conflict(LockType::RangeRead),
            LockConflict::Block
        );
        assert_eq!(
            LockType::RangeWrite.get_conflict(LockType::RangeWrite),
            LockConflict::Block
        );
        assert_eq!(
            LockType::RangeWrite.get_conflict(LockType::RangeInsert),
            LockConflict::Block
        );
    }

    #[test]
    fn test_conflict_matrix_range_insert_row() {
        assert_eq!(
            LockType::RangeInsert.get_conflict(LockType::Read),
            LockConflict::Allow
        );
        assert_eq!(
            LockType::RangeInsert.get_conflict(LockType::Write),
            LockConflict::Allow
        );
        assert_eq!(
            LockType::RangeInsert.get_conflict(LockType::RangeRead),
            LockConflict::Restart
        );
        assert_eq!(
            LockType::RangeInsert.get_conflict(LockType::RangeWrite),
            LockConflict::Restart
        );
        assert_eq!(
            LockType::RangeInsert.get_conflict(LockType::RangeInsert),
            LockConflict::Allow
        );
    }

    // Test all 25 entries of the upgrade matrix
    #[test]
    fn test_upgrade_matrix_read_row() {
        assert_eq!(
            LockType::Read.get_upgrade(LockType::Read),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::Read.get_upgrade(LockType::Write),
            LockUpgrade::WritePromote
        );
        assert_eq!(
            LockType::Read.get_upgrade(LockType::RangeRead),
            LockUpgrade::RangeReadImmed
        );
        assert_eq!(
            LockType::Read.get_upgrade(LockType::RangeWrite),
            LockUpgrade::RangeWritePromote
        );
        assert_eq!(
            LockType::Read.get_upgrade(LockType::RangeInsert),
            LockUpgrade::Illegal
        );
    }

    #[test]
    fn test_upgrade_matrix_write_row() {
        assert_eq!(
            LockType::Write.get_upgrade(LockType::Read),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::Write.get_upgrade(LockType::Write),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::Write.get_upgrade(LockType::RangeRead),
            LockUpgrade::RangeWriteImmed
        );
        assert_eq!(
            LockType::Write.get_upgrade(LockType::RangeWrite),
            LockUpgrade::RangeWriteImmed
        );
        assert_eq!(
            LockType::Write.get_upgrade(LockType::RangeInsert),
            LockUpgrade::Illegal
        );
    }

    #[test]
    fn test_upgrade_matrix_range_read_row() {
        assert_eq!(
            LockType::RangeRead.get_upgrade(LockType::Read),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::RangeRead.get_upgrade(LockType::Write),
            LockUpgrade::RangeWritePromote
        );
        assert_eq!(
            LockType::RangeRead.get_upgrade(LockType::RangeRead),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::RangeRead.get_upgrade(LockType::RangeWrite),
            LockUpgrade::RangeWritePromote
        );
        assert_eq!(
            LockType::RangeRead.get_upgrade(LockType::RangeInsert),
            LockUpgrade::Illegal
        );
    }

    #[test]
    fn test_upgrade_matrix_range_write_row() {
        assert_eq!(
            LockType::RangeWrite.get_upgrade(LockType::Read),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::RangeWrite.get_upgrade(LockType::Write),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::RangeWrite.get_upgrade(LockType::RangeRead),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::RangeWrite.get_upgrade(LockType::RangeWrite),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::RangeWrite.get_upgrade(LockType::RangeInsert),
            LockUpgrade::Illegal
        );
    }

    #[test]
    fn test_upgrade_matrix_range_insert_row() {
        assert_eq!(
            LockType::RangeInsert.get_upgrade(LockType::Read),
            LockUpgrade::Illegal
        );
        assert_eq!(
            LockType::RangeInsert.get_upgrade(LockType::Write),
            LockUpgrade::Illegal
        );
        assert_eq!(
            LockType::RangeInsert.get_upgrade(LockType::RangeRead),
            LockUpgrade::Illegal
        );
        assert_eq!(
            LockType::RangeInsert.get_upgrade(LockType::RangeWrite),
            LockUpgrade::Illegal
        );
        assert_eq!(
            LockType::RangeInsert.get_upgrade(LockType::RangeInsert),
            LockUpgrade::Existing
        );
    }

    #[test]
    fn test_upgrade_matrix_none() {
        // None as held lock is illegal
        assert_eq!(
            LockType::None.get_upgrade(LockType::Read),
            LockUpgrade::Illegal
        );
        assert_eq!(
            LockType::None.get_upgrade(LockType::Write),
            LockUpgrade::Illegal
        );

        // None as requested lock is always existing (no upgrade needed)
        assert_eq!(
            LockType::Read.get_upgrade(LockType::None),
            LockUpgrade::Existing
        );
        assert_eq!(
            LockType::Write.get_upgrade(LockType::None),
            LockUpgrade::Existing
        );
    }
}