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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
//! Cell-Level MVCC Boundary Classification (C2, bd-l9k8e.2)
//!
//! This module defines the boundary between LOGICAL and STRUCTURAL B-tree operations
//! for the cell-level MVCC redesign. This classification is load-bearing: it determines
//! which operations use the cheap cell-level path and which stay on the existing
//! page-level MVCC path.
//!
//! # Background: Why This Matters
//!
//! The current MVCC design creates full-page version copies for every mutation.
//! A single-row INSERT that touches 50 bytes of a 4KB page creates a 4KB page copy,
//! links it into a version chain, walks that chain on every read, and GCs it later.
//!
//! The cell-level MVCC redesign splits operations into two classes:
//! 1. **LOGICAL** (cell mutations without page structural change) -> CellVisibilityLog only
//! 2. **STRUCTURAL** (splits, merges, freelist, rebalance) -> full-page versioning
//!
//! # Classification Criteria
//!
//! ## LOGICAL Operations (Cell-Level MVCC)
//!
//! These operations modify cell contents but DO NOT change the page's structural layout
//! in a way that affects other cells. They are candidates for the cheap cell-level path.
//!
//! | Operation | Condition | Implementation |
//! |-----------|-----------|----------------|
//! | `table_insert` | Page has room (`try_insert_on_leaf` returns `Ok(true)`) | cursor.rs:2602 |
//! | `index_insert` | Page has room (`try_insert_on_leaf` returns `Ok(true)`) | cursor.rs:2453 |
//! | `delete` | Page doesn't become empty (no balance needed) | cursor.rs:2613 |
//! | Cell update (same slot) | Cell size unchanged or shrinks | Not yet implemented |
//!
//! ## STRUCTURAL Operations (Page-Level MVCC)
//!
//! These operations change the page structure itself, affecting child pointers,
//! page allocation, or requiring page redistribution. They MUST use the existing
//! full-page versioning path.
//!
//! | Operation | Trigger | Implementation |
//! |-----------|---------|----------------|
//! | `balance_deeper` | Root page split (increases tree depth) | balance.rs:96 |
//! | `balance_quick` | Fast-path rightmost leaf split | balance.rs:201 |
//! | `balance_nonroot` | 3-way sibling rebalancing | balance.rs:389 |
//! | `balance_table_leaf_local_split` | Table leaf split | balance.rs:1104 |
//! | `balance_for_insert` | `try_insert_on_leaf` returns `Ok(false)` | cursor.rs:1805 |
//! | `balance_for_delete` | Leaf becomes empty (needs merge) | cursor.rs:1966 |
//! | `balance_shallower` | Decreases tree height | balance.rs:1818 |
//! | `allocate_page` | New page from freelist or file extension | freelist.rs:154 |
//! | `free_page` | Return page to freelist | freelist.rs:179 |
//! | Overflow chain creation | Cell exceeds local storage | overflow.rs:143 |
//! | Overflow chain deletion | Freeing overflow pages | cursor.rs:2341 |
//! | Interior page operations | Always structural (child pointers) | cursor.rs:2625 |
//!
//! # Edge Cases and Decisions
//!
//! ## Edge Case 1: Cell Pointer Array Shift
//!
//! **Question:** INSERT that fits in the page but changes the cell pointer array
//! offset — is this structural?
//!
//! **Decision:** NO. Cell pointer array is part of page-local bookkeeping.
//! Adding a cell to an existing page is the common case. The cell pointer shift
//! is handled transparently during page materialization at checkpoint time.
//! This remains LOGICAL.
//!
//! **Rationale:** If we made this structural, almost every INSERT would be structural,
//! defeating the purpose of the optimization. The cell pointer array is internal
//! bookkeeping, not a structural dependency visible to other transactions.
//!
//! ## Edge Case 2: UPDATE That Changes Cell Size
//!
//! **Question:** At what threshold does an UPDATE become structural?
//!
//! **Decision:** An UPDATE becomes STRUCTURAL when:
//! 1. The new cell requires overflow (creates new pages), OR
//! 2. The new cell doesn't fit in the page (exceeds usable space)
//!
//! Otherwise, an UPDATE that grows or shrinks a cell in-place remains LOGICAL.
//!
//! **Rationale:** The cell can grow/shrink within the page's available space.
//! The content area offset adjusts, but this is the same as for INSERT.
//!
//! ## Edge Case 3: DELETE That Leaves Page Underfull
//!
//! **Question:** Do we eagerly merge or lazily defer?
//!
//! **Decision:** LAZY. Mark as underfull; merge on next structural operation or checkpoint.
//!
//! **Rationale:** Eager merging after every DELETE would make most DELETEs structural.
//! SQLite itself doesn't eagerly merge — it only merges when a page becomes completely
//! empty. We follow the same policy: DELETE is LOGICAL unless `new_count == 0`.
//!
//! ## Edge Case 4: Interior (Non-Leaf) Page Operations
//!
//! **Question:** Should interior pages use cell-level MVCC?
//!
//! **Decision:** NO. Interior pages should ALWAYS use page-level MVCC.
//!
//! **Rationale:** Interior pages contain child pointers. Modifying an interior page
//! affects the entire subtree's navigation. The complexity of tracking cell-level
//! visibility for interior nodes isn't worth it — interior page modifications are
//! rare (only during splits/merges) and inherently structural.
//!
//! ## Edge Case 5: Index Pages vs Table Pages
//!
//! **Question:** Do index leaf pages use the same boundary as table leaf pages?
//!
//! **Decision:** YES. Both table and index leaf pages follow the same LOGICAL/STRUCTURAL
//! boundary. The only difference is the CellKey representation:
//! - Table pages: `CellKey::TableRow(RowId)`
//! - Index pages: `CellKey::IndexEntry { key_hash: u64, rowid: RowId }`
//!
//! **Rationale:** The page structure is identical for table and index leaf pages.
//! Only the cell format differs, which the CellKey abstraction handles.
use ;
use PageNumber;
// ---------------------------------------------------------------------------
// Classification Types
// ---------------------------------------------------------------------------
/// Classification of a B-tree operation for MVCC purposes.
/// The result of attempting a B-tree mutation.
// ---------------------------------------------------------------------------
// Boundary Classification Predicate
// ---------------------------------------------------------------------------
/// Describes a B-tree operation for classification purposes.
/// Minimal page metadata needed for classification.
/// Determines whether a B-tree operation is LOGICAL (cell-level MVCC) or
/// STRUCTURAL (page-level MVCC).
///
/// This is the authoritative boundary predicate for the cell-level MVCC redesign.
///
/// # Arguments
///
/// * `op` - The B-tree operation being performed.
/// * `page` - Metadata about the target page (for capacity checks).
///
/// # Returns
///
/// `MvccOpClass::Logical` for operations that can use cell-level MVCC.
/// `MvccOpClass::Structural` for operations that must use page-level MVCC.
// ---------------------------------------------------------------------------
// Function Classification Table
// ---------------------------------------------------------------------------
/// Complete classification table for all B-tree mutation functions.
///
/// This table documents every function in the fsqlite-btree crate that performs
/// mutations, along with its classification and the conditions under which
/// it may transition from LOGICAL to STRUCTURAL.
///
/// # Cursor Operations (cursor.rs)
///
/// | Function | Default Class | Becomes Structural When |
/// |----------|---------------|-------------------------|
/// | `table_insert` | Logical | Page full → `balance_for_insert` |
/// | `table_insert_from_current_position` | Logical | Page full → `balance_for_insert` |
/// | `table_insert_prechecked_absent` | Logical | Page full → `balance_for_insert` |
/// | `index_insert` | Logical | Page full → `balance_for_insert` |
/// | `index_insert_unique` | Logical | Page full → `balance_for_insert` |
/// | `delete` | Logical | Page empties → `balance_for_delete` |
/// | `replace_interior_cell` | Structural | Always (interior page) |
/// | `remove_cell_from_leaf` | Logical* | *Cell removal is logical; emptying triggers structural |
/// | `free_overflow_chain` | Structural | Always (page deallocation) |
/// | `try_insert_on_leaf` | Logical | Returns `Ok(false)` → structural path |
///
/// # Balance Operations (balance.rs)
///
/// | Function | Class | Description |
/// |----------|-------|-------------|
/// | `balance_deeper` | Structural | Root split, increases tree depth |
/// | `balance_quick` | Structural | Fast-path rightmost leaf split |
/// | `balance_nonroot` | Structural | 3-way sibling rebalancing |
/// | `balance_table_leaf_local_split` | Structural | Table leaf split |
/// | `balance_shallower` | Structural | Decreases tree depth |
/// | `split_overflowing_root` | Structural | Root page overflow split |
/// | `split_overflowing_nonroot_interior_page` | Structural | Interior page split |
/// | `insert_cell_into_page` | Mixed | Logical if fits, else structural |
///
/// # Freelist Operations (freelist.rs)
///
/// | Function | Class | Description |
/// |----------|-------|-------------|
/// | `Freelist::allocate` | Structural | Page allocation |
/// | `Freelist::deallocate` | Structural | Page deallocation |
///
/// # Overflow Operations (overflow.rs)
///
/// | Function | Class | Description |
/// |----------|-------|-------------|
/// | `write_overflow_chain` | Structural | Creates new overflow pages |
/// | `free_overflow_chain` | Structural | Frees overflow pages |
///
/// # Page Writer Operations (cursor.rs, PageWriter trait)
///
/// | Function | Class | Description |
/// |----------|-------|-------------|
/// | `write_page` | Mixed | Logical if cell update, structural if layout change |
/// | `allocate_page` | Structural | Always |
/// | `free_page` | Structural | Always |
pub const CLASSIFICATION_TABLE_VERSION: &str = "1.0.0";
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------