lock-db 1.0.0

Lock manager and deadlock detection for Rust databases - row/range locks, multiple granularities, and wait-for cycle detection.
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
//! Lock modes and the compatibility matrix.
//!
//! The compatibility matrix is the correctness core of a lock manager: it
//! decides, for any two transactions contending for the same resource, whether
//! both requests can be held at once. Get this wrong and the manager hands out
//! conflicting locks; everything above it then corrupts data. For that reason
//! the matrix lives in one small, `const`, exhaustively tested place rather than
//! being scattered across the acquire path.
//!
//! `lock-db` implements the five standard multi-granularity locking (MGL)
//! modes. Two are the fundamental modes — shared (read) and exclusive (write).
//! The other three are *intention* modes that a transaction takes on a coarse
//! resource (a table, say) to announce that it intends to take a finer lock (a
//! row) underneath it. Intention locks are what make hierarchical locking
//! correct without forcing every reader to inspect every individual row lock:
//! a transaction wanting to lock a whole table exclusively need only check that
//! no one holds an intention lock on the table, instead of scanning every row.
//!
//! See the [crate-level docs](crate) for the granularity protocol that ties the
//! modes to a database/table/page/row hierarchy.

/// The mode in which a transaction holds, or wants to hold, a lock.
///
/// The five modes form a lattice ordered by privilege: holding a stronger mode
/// grants the capabilities of every weaker mode it [covers](LockMode::covers).
/// Acquiring a mode while already holding another upgrades to their
/// [join](LockMode::join) (least upper bound) — for example shared + intention
/// exclusive becomes [`SharedIntentionExclusive`](LockMode::SharedIntentionExclusive).
///
/// # Examples
///
/// ```
/// use lock_db::LockMode;
///
/// // Two readers coexist; a writer excludes everyone.
/// assert!(LockMode::Shared.compatible_with(LockMode::Shared));
/// assert!(!LockMode::Shared.compatible_with(LockMode::Exclusive));
///
/// // Intention-shared coexists with everything except an exclusive lock.
/// assert!(LockMode::IntentionShared.compatible_with(LockMode::SharedIntentionExclusive));
/// assert!(!LockMode::IntentionShared.compatible_with(LockMode::Exclusive));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum LockMode {
    /// Intention shared (IS). Announces intent to take shared locks on finer
    /// resources beneath this one. Compatible with everything except exclusive.
    IntentionShared,

    /// Intention exclusive (IX). Announces intent to take exclusive (or shared)
    /// locks on finer resources. Compatible with the intention modes only.
    IntentionExclusive,

    /// Shared (S). A read lock. Any number of transactions may hold a resource
    /// `Shared` at once, alongside intention-shared holders.
    Shared,

    /// Shared and intention exclusive (SIX). The transaction reads the whole
    /// subtree (S) and intends to write part of it (IX). Compatible only with
    /// intention-shared.
    SharedIntentionExclusive,

    /// Exclusive (X). A write lock. Held by at most one transaction, and only
    /// when no other transaction holds the resource in any mode.
    Exclusive,
}

impl LockMode {
    /// Maps a mode to its row/column in the compatibility and join tables.
    #[inline]
    const fn index(self) -> usize {
        match self {
            LockMode::IntentionShared => 0,
            LockMode::IntentionExclusive => 1,
            LockMode::Shared => 2,
            LockMode::SharedIntentionExclusive => 3,
            LockMode::Exclusive => 4,
        }
    }

    /// Returns `true` if a lock in `self` mode and a lock in `other` mode may be
    /// held on the same resource by two different transactions at once.
    ///
    /// This is the symmetric compatibility relation of the standard MGL matrix:
    ///
    /// |       | IS | IX | S  | SIX | X  |
    /// |-------|----|----|----|-----|----|
    /// | **IS**  | ✓ | ✓ | ✓ | ✓  | ✗ |
    /// | **IX**  | ✓ | ✓ | ✗ | ✗  | ✗ |
    /// | **S**   | ✓ | ✗ | ✓ | ✗  | ✗ |
    /// | **SIX** | ✓ | ✗ | ✗ | ✗  | ✗ |
    /// | **X**   | ✗ | ✗ | ✗ | ✗  | ✗ |
    ///
    /// # Examples
    ///
    /// ```
    /// use lock_db::LockMode;
    ///
    /// assert!(LockMode::IntentionShared.compatible_with(LockMode::IntentionExclusive));
    /// assert!(!LockMode::IntentionExclusive.compatible_with(LockMode::Shared));
    /// assert!(!LockMode::Exclusive.compatible_with(LockMode::IntentionShared));
    /// ```
    #[inline]
    #[must_use]
    pub const fn compatible_with(self, other: LockMode) -> bool {
        // Rows/cols ordered IS, IX, S, SIX, X.
        const COMPAT: [[bool; 5]; 5] = [
            [true, true, true, true, false],     // IS
            [true, true, false, false, false],   // IX
            [true, false, true, false, false],   // S
            [true, false, false, false, false],  // SIX
            [false, false, false, false, false], // X
        ];
        COMPAT[self.index()][other.index()]
    }

    /// Returns the least mode that grants everything both `self` and `other`
    /// grant — their least upper bound in the privilege lattice.
    ///
    /// This is what an upgrade resolves to: a transaction already holding `self`
    /// that requests `other` ends up holding `self.join(other)`. For example a
    /// reader (`Shared`) that announces intent to write part of the subtree
    /// (`IntentionExclusive`) upgrades to `SharedIntentionExclusive`. `join` is
    /// commutative and associative.
    ///
    /// # Examples
    ///
    /// ```
    /// use lock_db::LockMode;
    ///
    /// assert_eq!(
    ///     LockMode::Shared.join(LockMode::IntentionExclusive),
    ///     LockMode::SharedIntentionExclusive,
    /// );
    /// assert_eq!(LockMode::IntentionShared.join(LockMode::Shared), LockMode::Shared);
    /// assert_eq!(LockMode::Shared.join(LockMode::Exclusive), LockMode::Exclusive);
    /// // join with itself is itself.
    /// assert_eq!(LockMode::IntentionExclusive.join(LockMode::IntentionExclusive), LockMode::IntentionExclusive);
    /// ```
    #[inline]
    #[must_use]
    pub const fn join(self, other: LockMode) -> LockMode {
        use LockMode::{
            Exclusive, IntentionExclusive, IntentionShared, Shared, SharedIntentionExclusive,
        };
        // Rows/cols ordered IS, IX, S, SIX, X. Symmetric.
        const JOIN: [[LockMode; 5]; 5] = [
            // IS
            [
                IntentionShared,
                IntentionExclusive,
                Shared,
                SharedIntentionExclusive,
                Exclusive,
            ],
            // IX
            [
                IntentionExclusive,
                IntentionExclusive,
                SharedIntentionExclusive,
                SharedIntentionExclusive,
                Exclusive,
            ],
            // S
            [
                Shared,
                SharedIntentionExclusive,
                Shared,
                SharedIntentionExclusive,
                Exclusive,
            ],
            // SIX
            [
                SharedIntentionExclusive,
                SharedIntentionExclusive,
                SharedIntentionExclusive,
                SharedIntentionExclusive,
                Exclusive,
            ],
            // X
            [Exclusive, Exclusive, Exclusive, Exclusive, Exclusive],
        ];
        JOIN[self.index()][other.index()]
    }

    /// Returns `true` if holding `self` already grants everything `other` would.
    ///
    /// Equivalent to `self.join(other) == self`: `self` sits at or above `other`
    /// in the lattice. This drives the idempotent path of acquisition — a
    /// request for a mode you already cover is a no-op.
    ///
    /// # Examples
    ///
    /// ```
    /// use lock_db::LockMode;
    ///
    /// assert!(LockMode::Exclusive.covers(LockMode::Shared));
    /// assert!(LockMode::SharedIntentionExclusive.covers(LockMode::Shared));
    /// assert!(LockMode::SharedIntentionExclusive.covers(LockMode::IntentionExclusive));
    /// assert!(!LockMode::Shared.covers(LockMode::IntentionExclusive));
    /// ```
    #[inline]
    #[must_use]
    pub const fn covers(self, other: LockMode) -> bool {
        // `PartialEq` is not `const`, so compare discriminants directly.
        self.join(other) as u8 == self as u8
    }

    /// Returns `true` for [`LockMode::Exclusive`].
    ///
    /// # Examples
    ///
    /// ```
    /// use lock_db::LockMode;
    ///
    /// assert!(LockMode::Exclusive.is_exclusive());
    /// assert!(!LockMode::Shared.is_exclusive());
    /// ```
    #[inline]
    #[must_use]
    pub const fn is_exclusive(self) -> bool {
        matches!(self, LockMode::Exclusive)
    }

    /// Returns `true` for the intention modes (IS, IX, SIX).
    ///
    /// Intention modes are placeholders taken on a coarse resource to signal
    /// that a finer lock will be taken beneath it; they are not themselves read
    /// or write locks on the resource's own data.
    ///
    /// # Examples
    ///
    /// ```
    /// use lock_db::LockMode;
    ///
    /// assert!(LockMode::IntentionShared.is_intention());
    /// assert!(LockMode::SharedIntentionExclusive.is_intention());
    /// assert!(!LockMode::Shared.is_intention());
    /// assert!(!LockMode::Exclusive.is_intention());
    /// ```
    #[inline]
    #[must_use]
    pub const fn is_intention(self) -> bool {
        matches!(
            self,
            LockMode::IntentionShared
                | LockMode::IntentionExclusive
                | LockMode::SharedIntentionExclusive
        )
    }
}

#[cfg(test)]
mod tests {
    use super::LockMode::{
        self, Exclusive, IntentionExclusive, IntentionShared, Shared, SharedIntentionExclusive,
    };

    const ALL: [LockMode; 5] = [
        IntentionShared,
        IntentionExclusive,
        Shared,
        SharedIntentionExclusive,
        Exclusive,
    ];

    #[test]
    fn test_compatibility_matches_standard_mgl_matrix() {
        // The canonical Gray/Reuter compatibility matrix, IS IX S SIX X.
        let expected = [
            [true, true, true, true, false],
            [true, true, false, false, false],
            [true, false, true, false, false],
            [true, false, false, false, false],
            [false, false, false, false, false],
        ];
        for (i, a) in ALL.iter().enumerate() {
            for (j, b) in ALL.iter().enumerate() {
                assert_eq!(
                    a.compatible_with(*b),
                    expected[i][j],
                    "compat({a:?}, {b:?})"
                );
            }
        }
    }

    #[test]
    fn test_compatible_is_symmetric() {
        for a in ALL {
            for b in ALL {
                assert_eq!(a.compatible_with(b), b.compatible_with(a), "{a:?} vs {b:?}");
            }
        }
    }

    #[test]
    fn test_exclusive_incompatible_with_all() {
        for m in ALL {
            assert!(!Exclusive.compatible_with(m));
            assert!(!m.compatible_with(Exclusive));
        }
    }

    #[test]
    fn test_intention_shared_compatible_with_all_but_exclusive() {
        for m in ALL {
            assert_eq!(IntentionShared.compatible_with(m), m != Exclusive);
        }
    }

    #[test]
    fn test_join_is_commutative() {
        for a in ALL {
            for b in ALL {
                assert_eq!(a.join(b), b.join(a), "join({a:?}, {b:?})");
            }
        }
    }

    #[test]
    fn test_join_is_associative() {
        for a in ALL {
            for b in ALL {
                for c in ALL {
                    assert_eq!(a.join(b).join(c), a.join(b.join(c)));
                }
            }
        }
    }

    #[test]
    fn test_join_idempotent() {
        for m in ALL {
            assert_eq!(m.join(m), m);
        }
    }

    #[test]
    fn test_join_examples() {
        assert_eq!(Shared.join(IntentionExclusive), SharedIntentionExclusive);
        assert_eq!(IntentionShared.join(Shared), Shared);
        assert_eq!(IntentionShared.join(IntentionExclusive), IntentionExclusive);
        assert_eq!(Shared.join(Exclusive), Exclusive);
        assert_eq!(SharedIntentionExclusive.join(Exclusive), Exclusive);
    }

    #[test]
    fn test_covers_is_join_equals_self() {
        for a in ALL {
            for b in ALL {
                assert_eq!(a.covers(b), a.join(b) == a, "covers({a:?}, {b:?})");
            }
        }
    }

    #[test]
    fn test_exclusive_covers_everything() {
        for m in ALL {
            assert!(Exclusive.covers(m));
        }
    }

    #[test]
    fn test_six_covers_its_components() {
        assert!(SharedIntentionExclusive.covers(Shared));
        assert!(SharedIntentionExclusive.covers(IntentionExclusive));
        assert!(SharedIntentionExclusive.covers(IntentionShared));
        assert!(!SharedIntentionExclusive.covers(Exclusive));
    }

    #[test]
    fn test_covers_reflexive() {
        for m in ALL {
            assert!(m.covers(m));
        }
    }

    #[test]
    fn test_is_exclusive_and_is_intention() {
        assert!(Exclusive.is_exclusive());
        assert!(!Shared.is_exclusive());
        for m in [
            IntentionShared,
            IntentionExclusive,
            SharedIntentionExclusive,
        ] {
            assert!(m.is_intention());
        }
        assert!(!Shared.is_intention());
        assert!(!Exclusive.is_intention());
    }

    #[test]
    fn test_two_compatible_modes_join_below_any_conflict() {
        // Sanity: if two modes are compatible, neither is exclusive.
        for a in ALL {
            for b in ALL {
                if a.compatible_with(b) {
                    assert!(!(a.is_exclusive() && b.is_exclusive()));
                }
            }
        }
    }
}