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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
use std::{
cmp,
io::Write,
ops::DerefMut,
sync::{Arc, RwLock},
usize,
};
use crate::{
btree::{
page::{
BTreeHeaderPage, BTreeInternalPage,
BTreeInternalPageIterator, BTreeLeafPage,
BTreeLeafPageIterator, BTreePage, BTreePageID, Entry,
PageCategory,
},
tuple::WrappedTuple,
},
concurrent_status::Permission,
error::SmallError,
field::IntField,
transaction::Transaction,
types::SmallResult,
utils::HandyRwLock,
BTreeTable, Unique,
};
/// delete-related methods
impl BTreeTable {
/// Delete a tuple from this BTreeFile.
///
/// May cause pages to merge or redistribute entries/tuples if the
/// pages become less than half full.
pub fn delete_tuple(
&self,
tx: &Transaction,
tuple: &WrappedTuple,
) -> SmallResult {
let pid = tuple.get_pid();
let leaf_rc = Unique::mut_page_cache()
.get_leaf_page(tx, Permission::ReadWrite, &pid)
.unwrap();
// hold the leaf page
{
let mut leaf = leaf_rc.wl();
leaf.delete_tuple(tuple.get_slot_number());
}
// release the leaf page
if leaf_rc.rl().stable() {
return Ok(());
} else {
return self.handle_erratic_leaf_page(tx, leaf_rc);
}
}
/// Handle the case when a leaf page becomes less than half full
/// due to deletions.
///
/// If one of its siblings has extra tuples, redistribute those
/// tuples. Otherwise merge with one of the siblings. Update
/// pointers as needed.
fn handle_erratic_leaf_page(
&self,
tx: &Transaction,
page_rc: Arc<RwLock<BTreeLeafPage>>,
) -> SmallResult {
if page_rc.rl().get_parent_pid().category
== PageCategory::RootPointer
{
return Ok(());
}
let left_pid = page_rc.rl().get_left_pid();
let right_pid = page_rc.rl().get_right_pid();
if let Some(left_pid) = left_pid {
let left_rc = Unique::mut_page_cache()
.get_leaf_page(tx, Permission::ReadWrite, &left_pid)
.unwrap();
self.balancing_two_leaf_pages(tx, left_rc, page_rc)?;
} else if let Some(right_pid) = right_pid {
let right_rc = Unique::mut_page_cache()
.get_leaf_page(tx, Permission::ReadWrite, &right_pid)
.unwrap();
self.balancing_two_leaf_pages(tx, page_rc, right_rc)?;
} else {
return Err(SmallError::new(
"BTreeTable::handle_erratic_leaf_page no left or right sibling",
));
};
return Ok(());
}
/// Handle the case when an internal page becomes less than half
/// full due to deletions.
///
/// If one of its siblings has extra entries, redistribute those
/// entries. Otherwise merge with one of the siblings. Update
/// pointers as needed.
///
/// # Arguments
///
/// - page_rc - the erratic internal page to be handled
fn handle_erratic_internal_page(
&self,
tx: &Transaction,
page_rc: Arc<RwLock<BTreeInternalPage>>,
) -> SmallResult {
if page_rc.rl().get_parent_pid().category
== PageCategory::RootPointer
{
return Ok(());
}
let left_pid = page_rc.rl().get_left_sibling_pid(tx);
let right_pid = page_rc.rl().get_right_sibling_pid(tx);
if let Some(left_pid) = left_pid {
let left_rc = Unique::mut_page_cache()
.get_internal_page(
tx,
Permission::ReadWrite,
&left_pid,
)
.unwrap();
self.balancing_two_internal_pages(tx, left_rc, page_rc)?;
} else if let Some(right_pid) = right_pid {
let right_rc = Unique::mut_page_cache()
.get_internal_page(
tx,
Permission::ReadWrite,
&right_pid,
)
.unwrap();
self.balancing_two_internal_pages(tx, page_rc, right_rc)?;
} else {
panic!("Cannot find the left/right sibling of the page");
}
Ok(())
}
fn set_parent_pid(
&self,
tx: &Transaction,
child_pid: &BTreePageID,
parent_pid: &BTreePageID,
) {
match child_pid.category {
PageCategory::Leaf => {
let child_rc = Unique::mut_page_cache()
.get_leaf_page(
tx,
Permission::ReadWrite,
child_pid,
)
.unwrap();
child_rc.wl().set_parent_pid(&parent_pid);
}
PageCategory::Internal => {
let child_rc = Unique::mut_page_cache()
.get_internal_page(
tx,
Permission::ReadOnly,
child_pid,
)
.unwrap();
child_rc.wl().set_parent_pid(&parent_pid);
}
_ => panic!("Invalid page category"),
}
}
/// # Arguments
///
/// - parent_entry - the entry in the parent corresponding to the
/// left and right
fn merge_internal_page(
&self,
tx: &Transaction,
left_rc: Arc<RwLock<BTreeInternalPage>>,
right_rc: Arc<RwLock<BTreeInternalPage>>,
parent_rc: Arc<RwLock<BTreeInternalPage>>,
parent_entry: &Entry,
) -> SmallResult {
// hold left_rc and right_rc
{
let mut left = left_rc.wl();
let mut right = right_rc.wl();
// stage 1: pull down the edge entry from parent and
// insert it into target page
let edge_entry = Entry::new(
parent_entry.get_key(),
&left.get_last_child_pid(),
&right.get_first_child_pid(),
);
self.set_parent_pid(
tx,
&right.get_first_child_pid(),
&left.get_pid(),
);
left.insert_entry(&edge_entry)?;
// stage 2: move the entries from the one page to the
// other
let mut deleted_indexes = Vec::new();
let iter = BTreeInternalPageIterator::new(&right);
for e in iter {
left.insert_entry(&e)?;
self.set_parent_pid(
tx,
&e.get_right_child(),
&left.get_pid(),
);
deleted_indexes.push(e.get_record_id());
}
for i in deleted_indexes {
right.delete_key_and_right_child(i);
}
// stage 3: set the right as empty
self.set_empty_page(tx, &right.get_pid());
}
// release left_rc and right_rc
// stage 4: update the entry in parent which points to the
// left and right
self.delete_parent_entry(
tx,
left_rc,
parent_rc,
parent_entry,
)?;
Ok(())
}
/// # Arguments
///
/// - entry - the entry in the parent corresponding to the
/// left_child and right_child
fn merge_leaf_page(
&self,
tx: &Transaction,
left_rc: Arc<RwLock<BTreeLeafPage>>,
right_rc: Arc<RwLock<BTreeLeafPage>>,
parent_rc: Arc<RwLock<BTreeInternalPage>>,
entry: &Entry,
) -> SmallResult {
// hold the left and right page
{
let mut left = left_rc.wl();
let mut right = right_rc.wl();
// stage 1: move the tuples from right to left
let mut it = BTreeLeafPageIterator::new(&right);
let mut deleted = Vec::new();
for t in it.by_ref() {
left.insert_tuple(&t);
deleted.push(t.get_slot_number());
}
for slot in deleted {
right.delete_tuple(slot);
}
// stage 2: update sibling pointers
// set the right pointer of the left page to the right
// page's right pointer
left.set_right_pid(right.get_right_pid());
// set the left pointer for the newer right page
if let Some(newer_right_pid) = right.get_right_pid() {
let newer_right_rc = Unique::mut_page_cache()
.get_leaf_page(
tx,
Permission::ReadWrite,
&newer_right_pid,
)
.unwrap();
newer_right_rc
.wl()
.set_left_pid(Some(left.get_pid()));
}
// stage 4: set the right page as empty
self.set_empty_page(tx, &right.get_pid());
}
// stage 5: release the left and right page
self.delete_parent_entry(tx, left_rc, parent_rc, entry)?;
Ok(())
}
/// Method to encapsulate the process of deleting an entry
/// (specifically the key and right child) from a parent node.
///
/// If the parent becomes empty (no keys remaining), that
/// indicates that it was the root node and should be replaced
/// by its one remaining child.
///
/// Otherwise, if it gets below minimum occupancy for non-root
/// internal nodes, it should steal from one of its siblings
/// or merge with a sibling.
///
/// # Arguments
///
/// - reserved_child - the child reserved after the key and
/// another child are deleted
/// - page - the parent containing the entry to be
/// deleted
/// - entry - the entry to be deleted
/// - delete_left_child - which child of the entry should be
/// deleted
fn delete_parent_entry<PAGE: BTreePage>(
&self,
tx: &Transaction,
left_rc: Arc<RwLock<PAGE>>,
parent_rc: Arc<RwLock<BTreeInternalPage>>,
entry: &Entry,
) -> SmallResult {
// hold the parent and left page
{
let mut parent = parent_rc.wl();
let mut left = left_rc.wl();
// stage 1: delete the corresponding entry in the parent
// page
parent.delete_key_and_right_child(entry.get_record_id());
// stage 2: handle the parent page according to the
// following cases case 1: parent is empty,
// then the left child is now the new root
if parent.entries_count() == 0 {
let root_ptr_page_rc = self.get_root_ptr_page(tx);
// hold the root pointer page
{
let mut root_ptr_page = root_ptr_page_rc.wl();
left.set_parent_pid(&root_ptr_page.get_pid());
root_ptr_page.set_root_pid(&left.get_pid());
}
// release the root pointer page
// release the page for reuse
self.set_empty_page(tx, &parent.get_pid());
return Ok(());
}
// case 2: parent is stable, return directly
if parent.stable() {
return Ok(());
}
}
// release the parent and left page
// case 3: parent is unstable (erratic), handle it
self.handle_erratic_internal_page(tx, parent_rc)?;
Ok(())
}
/// Mark a page in this BTreeTable as empty. Find the
/// corresponding header page (create it if needed), and mark
/// the corresponding slot in the header page as empty.
fn set_empty_page(&self, tx: &Transaction, pid: &BTreePageID) {
Unique::mut_page_cache().discard_page(pid);
let root_ptr_rc = self.get_root_ptr_page(tx);
let header_rc: Arc<RwLock<BTreeHeaderPage>>;
// let mut root_ptr = root_ptr_rc.wl();
match root_ptr_rc.rl().get_header_pid() {
Some(header_pid) => {
header_rc = Unique::mut_page_cache()
.get_header_page(
tx,
Permission::ReadWrite,
&header_pid,
)
.unwrap();
}
None => {
// if there are no header pages, create the first
// header page and update the header
// pointer in the BTreeRootPtrPage
header_rc = self.get_empty_header_page(tx);
}
}
root_ptr_rc.wl().set_header_pid(&header_rc.rl().get_pid());
// borrow of header_rc start here
{
let mut header = header_rc.wl();
let slot_index =
pid.page_index as usize % header.get_slots_count();
header.mark_slot_status(slot_index, false);
}
// borrow of header_rc end here
}
/// Balancing two internal pages according the situation:
///
/// 1. Merge the two pages if the count of entries in the two
/// pages is less than the maximum capacity of a single page.
///
/// 2. Otherwise, steal entries from the sibling and copy them to
/// the given page so that both pages are at least half full.
///
/// Keys can be thought of as rotating through the parent entry,
/// so the original key in the parent is "pulled down" to the
/// erratic page, and the last key in the sibling page is
/// "pushed up" to the parent. Update parent pointers as
/// needed.
fn balancing_two_internal_pages(
&self,
tx: &Transaction,
left_rc: Arc<RwLock<BTreeInternalPage>>,
right_rc: Arc<RwLock<BTreeInternalPage>>,
) -> SmallResult {
let parent_rc = Unique::mut_page_cache()
.get_internal_page(
tx,
Permission::ReadWrite,
&left_rc.rl().get_parent_pid(),
)
.unwrap();
let mut parent_entry = parent_rc
.rl()
.get_entry_by_children(
&left_rc.rl().get_pid(),
&right_rc.rl().get_pid(),
)
.unwrap();
let left_children = left_rc.rl().children_count();
let right_children = right_rc.rl().children_count();
if left_children + right_children
<= left_rc.rl().get_children_capacity()
{
// if the two pages can be merged, merge them
return self.merge_internal_page(
tx,
left_rc,
right_rc,
parent_rc,
&parent_entry,
);
}
// if there aren't any entries to move, return immediately
let move_count = (left_children + right_children) / 2
- cmp::min(left_children, right_children);
if move_count == 0 {
return Ok(());
}
let mut middle_key = parent_entry.get_key();
// hold the left and right page
{
let mut left = left_rc.wl();
let mut right = right_rc.wl();
if left_children < right_children {
// The edge child of the destination page.
let edge_child_pid = left.get_last_child_pid();
let right_iter =
BTreeInternalPageIterator::new(&right);
let moved_records = self.move_entries(
tx,
right_iter,
left,
move_count,
&mut middle_key,
edge_child_pid,
|edge_pid: BTreePageID, _e: &Entry| edge_pid,
|_edge_pid: BTreePageID, e: &Entry| {
e.get_left_child()
},
|e: &Entry| e.get_left_child(),
)?;
for i in moved_records {
right.delete_key_and_left_child(i);
}
} else {
// The edge child of the destination page.
let edge_child_pid = right.get_first_child_pid();
let left_iter =
BTreeInternalPageIterator::new(&left).rev();
let moved_records = self.move_entries(
tx,
left_iter,
right,
move_count,
&mut middle_key,
edge_child_pid,
|_edge_pid: BTreePageID, e: &Entry| {
e.get_right_child()
},
|edge_pid: BTreePageID, _e: &Entry| edge_pid,
|e: &Entry| e.get_right_child(),
)?;
for i in moved_records {
left.delete_key_and_right_child(i);
}
}
}
// release the left and right page
parent_entry.set_key(middle_key);
parent_rc.wl().update_entry(&parent_entry);
Ok(())
}
/// # Arguments:
///
/// * `middle_key`: The key between the left and right pages. This
/// key is always larger than children in the left page and
/// smaller than children in the right page. It should be
/// updated each time an entry is moved from the left/right page
/// to the otherside.
///
/// * `edge_child_pid`: The edge child of the destination page.
///
/// * `fn_get_edge_left_child`: A function to get the left child
/// of the new entry, the first argument is the edge child of
/// the destination page, the second argument is the current
/// entry of the source page (iterator).
///
/// * `fn_get_edge_right_child`: Same as `fn_get_edge_left_child`,
/// but for the right child of the new entry.
///
/// * `fn_get_moved_child`: A function to get the moved child
/// page, the argument is the current entry of the source page
/// (iterator).
///
/// Return:
/// * The index of the moved entries in the source page.
fn move_entries(
&self,
tx: &Transaction,
src_iter: impl Iterator<Item = Entry>,
mut dest: impl DerefMut<Target = BTreeInternalPage>,
move_count: usize,
middle_key: &mut IntField,
mut edge_child_pid: BTreePageID,
fn_get_edge_left_child: impl Fn(
BTreePageID,
&Entry,
) -> BTreePageID,
fn_get_edge_right_child: impl Fn(
BTreePageID,
&Entry,
) -> BTreePageID,
fn_get_moved_child: impl Fn(&Entry) -> BTreePageID,
) -> Result<Vec<usize>, SmallError> {
// Remember the entries for deletion later (cause we can't
// modify the page while iterating though it)
let mut moved_records = Vec::new();
for e in src_iter.take(move_count) {
// 1. delete the entry from the src page
moved_records.push(e.get_record_id());
// 2. insert new entry to dest page
let new_entry = Entry::new(
*middle_key,
&fn_get_edge_left_child(edge_child_pid, &e),
&fn_get_edge_right_child(edge_child_pid, &e),
);
dest.insert_entry(&new_entry)?;
// 3. update parent id for the moved child
self.set_parent_pid(
tx,
&fn_get_moved_child(&e),
&dest.get_pid(),
);
// 4. update key and edge child for the next iteration
*middle_key = e.get_key();
edge_child_pid = fn_get_moved_child(&e);
}
return Ok(moved_records);
}
/// Steal tuples from a sibling and copy them to the given page so
/// that both pages are at least half full. Update the
/// parent's entry so that the key matches the key field of
/// the first tuple in the right-hand page.
///
/// # Arguments
///
/// - page - the leaf page which is less than half full
/// - sibling - the sibling which has tuples to spare
/// - parent - the parent of the two leaf pages
/// - entry - the entry in the parent pointing to the two
/// leaf pages
/// - is_right_sibling - whether the sibling is a right-sibling
fn balancing_two_leaf_pages(
&self,
tx: &Transaction,
left_rc: Arc<RwLock<BTreeLeafPage>>,
right_rc: Arc<RwLock<BTreeLeafPage>>,
) -> SmallResult {
let parent_rc = Unique::mut_page_cache()
.get_internal_page(
tx,
Permission::ReadWrite,
&left_rc.rl().get_parent_pid(),
)
.unwrap();
let mut entry = parent_rc
.rl()
.get_entry_by_children(
&left_rc.rl().get_pid(),
&right_rc.rl().get_pid(),
)
.unwrap();
let left_tuples = left_rc.rl().tuples_count();
let right_tuples = right_rc.rl().tuples_count();
if left_tuples + right_tuples
<= left_rc.rl().get_slots_count()
{
// if the two pages can be merged, merge them
return self.merge_leaf_page(
tx, left_rc, right_rc, parent_rc, &entry,
);
}
let move_count = (left_tuples + right_tuples) / 2
- cmp::min(left_tuples, right_tuples);
if move_count == 0 {
return self.merge_leaf_page(
tx, left_rc, right_rc, parent_rc, &entry,
);
}
let mut key = entry.get_key();
// hold left and right page
{
let mut left = left_rc.wl();
let mut right = right_rc.wl();
if left_tuples < right_tuples {
let iter = BTreeLeafPageIterator::new(&right);
let mut deleted_indexes = Vec::new();
for tuple in iter.take(move_count) {
left.insert_tuple(&tuple);
deleted_indexes.push(tuple.get_slot_number());
key = tuple.get_field(self.key_field);
}
for i in deleted_indexes {
right.delete_tuple(i);
}
} else {
let iter = BTreeLeafPageIterator::new(&left);
let mut deleted_indexes = Vec::new();
for tuple in iter.rev().take(move_count) {
right.insert_tuple(&tuple);
deleted_indexes.push(tuple.get_slot_number());
key = tuple.get_field(self.key_field);
}
for i in deleted_indexes {
left.delete_tuple(i);
}
}
}
// release left and right page
entry.set_key(key);
parent_rc.wl().update_entry(&entry);
Ok(())
}
}