Pure OCaml B-tree implementation for persistent storage
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

btree: Format code

+22 -7
+22 -7
lib/btree.ml
··· 568 568 in 569 569 find 0 570 570 571 - (* Find child page for rowid in interior page *) 571 + (* Find child page for rowid in interior page. 572 + SQLite B-tree: keys < separator go left, keys >= separator go right. *) 572 573 let find_child _t page header rowid = 573 574 let ptrs = cell_pointers page 0 header in 574 575 let rec loop i = 575 576 if i >= header.cell_count then Option.get header.right_child 576 577 else 577 578 let cell, _ = Cell.parse_table_interior page ptrs.(i) in 578 - if rowid <= cell.rowid then cell.left_child else loop (i + 1) 579 + if rowid < cell.rowid then cell.left_child else loop (i + 1) 579 580 in 580 581 loop 0 581 582 ··· 586 587 if i >= header.cell_count then i (* right child *) 587 588 else 588 589 let cell, _ = Cell.parse_table_interior page ptrs.(i) in 589 - if rowid <= cell.rowid then i else loop (i + 1) 590 + if rowid < cell.rowid then i else loop (i + 1) 590 591 in 591 592 loop 0 592 593 ··· 820 821 (* Need to split *) 821 822 let split = split_leaf t page_num in 822 823 823 - (* Insert into appropriate page *) 824 - if rowid < split.separator_rowid then 825 - insert_into_leaf t page_num ~rowid ~data ~parent_stack:[] 826 - else insert_into_leaf t split.new_page ~rowid ~data ~parent_stack:[]; 824 + (* Determine target page and insert directly (no recursion needed - 825 + after split, both pages have ~half capacity, plenty of room) *) 826 + let target_page = 827 + if rowid < split.separator_rowid then page_num else split.new_page 828 + in 829 + let target = Pager.read t.pager target_page in 830 + let target_header = parse_page_header target 0 in 831 + let target_buf = Bytes.of_string target in 832 + let insert_idx = find_insert_idx t target target_header rowid in 833 + let cell_start = 834 + write_cell target_buf 835 + ~cell_content_start:target_header.cell_content_start ~cell 836 + in 837 + set_u16_be target_buf 5 cell_start; 838 + insert_cell_pointer target_buf ~header_offset:0 ~page_type:Leaf_table 839 + ~cell_count:target_header.cell_count ~index:insert_idx ~ptr:cell_start; 840 + set_u16_be target_buf 3 (target_header.cell_count + 1); 841 + Pager.write t.pager target_page (Bytes.unsafe_to_string target_buf); 827 842 828 843 (* Propagate split up *) 829 844 propagate_split t ~parent_stack ~left_page:page_num