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: Fix interior page insert when inserting in middle

When inserting a separator into an interior page at position i < cell_count,
the displaced cell (now at i+1) must have its left_child updated to point
to right_child. Previously, only the page's right_child was updated when
inserting at the end.

This fixes lookups failing after multiple splits with non-sequential
insertion order (e.g., reverse or random).

+14 -2
+14 -2
lib/btree.ml
··· 760 760 (* Update cell count *) 761 761 set_u16_be buf 3 (header.cell_count + 1); 762 762 763 - (* Update right child if inserting at end *) 764 - if insert_idx = header.cell_count then set_u32_be buf 8 right_child; 763 + (* Update child pointers: the cell we displaced (now at insert_idx+1) 764 + needs its left_child updated to right_child, OR if we inserted at end, 765 + update the page's right_child. *) 766 + if insert_idx < header.cell_count then begin 767 + (* Update the displaced cell's left_child to right_child *) 768 + let ptr_start = page_header_size Interior_table in 769 + let displaced_ptr = 770 + get_u16_be 771 + (Bytes.unsafe_to_string buf) 772 + (ptr_start + ((insert_idx + 1) * 2)) 773 + in 774 + set_u32_be buf displaced_ptr right_child 775 + end 776 + else set_u32_be buf 8 right_child; 765 777 766 778 Pager.write t.pager page_num (Bytes.unsafe_to_string buf); 767 779 None