Bulk loading of B+-trees #
Bottom-up bulk loading of the B+-trees of Cslib.DataStructure.BPlusTree: a tree is built from a
list of entries in one pass, level by level from the bottom.
The bottom level cuts the entries into leaves of t - 1 to 2 * t - 1 entries. chunkSizes
chooses the sizes with one look ahead: a leaf of 2 * t - 1 entries unless that would leave
fewer than t - 1 for the rest, then one of t - 1. At most 2 * t - 1 entries, the empty list
included, form a single root leaf. Each leaf after the first is introduced by a separator, which
is a copy of its first key: no entry moves up between leaves.
A level above the leaves is a first tree followed by (separator, tree) branches, all of its
trees of one height. A round cuts the branches of a level into groups of t - 1 to 2 * t - 1
branches with one branch between consecutive groups: each group becomes a node one level taller,
and the separator of each branch between groups moves up, its child becoming the first child of
the next node. This is the round of the classic loader Cslib.DataStructure.BTree.ofSortedList,
and the group sizes are its Cslib.DataStructure.BTree.groupSizes. packM cuts a level by a list
of sizes, and buildM runs rounds until at most 2 * t - 1 branches are left, which become the
root. Below 2 the loader uses minimum degree 2.
Each operation is defined once over an arbitrary monad m, with a node-visit action
visit : m Unit that fires once per node built, the leaves included; no comparator is needed,
since the input order is kept. The pure operation is the Id instantiation with
visit := pure (). All recursion is structural, on a fuel argument or on a list of sizes, so the
definitions are @[expose]d and decide evaluates bulk loading on concrete inputs; when the fuel
or the sizes run out, what remains becomes a single leaf or node, which keeps the entries. Costs
are not stated here.
Main definitions #
chunkSizes: the sizes of the leaves.leafBranchesM,leafLevelM: the leaves after the first with their separators, and the bottom level.packM,buildM: one round, and all rounds.ofSortedListM,ofSortedList: bulk loading, polymorphic and pure.
Main results #
map_ofSortedListM: a monad morphism commutes with bulk loading.toList_ofSortedList: bulk loading keeps the entries in order, for every list and everyt.height_ofSortedList_le: for2 ≤ t, the height is at mostNat.log t l.length + 1, for every list.isWF_ofSortedList: on a sorted list and for2 ≤ t, the result is well-formed.lookup_ofSortedList: on a sorted list, lookup isList.lookup.
References #
- R. Bayer and E. McCreight, Organization and Maintenance of Large Ordered Indexes
- D. Comer, The Ubiquitous B-Tree
- T. H. Cormen, C. E. Leiserson, R. L. Rivest and C. Stein, Introduction to Algorithms, Chapter 18
The statements here are not taken from a source.
The sizes of the leaves when n entries are cut into leaves of t - 1 to 2 * t - 1
entries, with recursion bounded by fuel. A leaf has 2 * t - 1 entries unless that would leave
fewer than t - 1, then t - 1; at most 2 * t - 1 entries form the last leaf. Out of fuel, the
remaining entries form one leaf. The sizes are in bounds for 2 ≤ t; below that a size can be
0.
Cut entries into leaves by a list of sizes, each leaf under a copy of its first key as its
separator. When the sizes run out, the remaining entries form one leaf. A size of 0 gives an
empty leaf under the first remaining key. visit fires once per leaf built.
Cut a level by a list of group sizes: the node over the first group, and the next level's
branches, each a separator moved up with the node over the following group. The last group takes
the remaining branches. visit fires once per node built.
Rounds of bulk loading at minimum degree t, at most fuel of them: a level with no
branches is its first tree, a level with at most 2 * t - 1 branches becomes the root, and a
longer one is cut by Cslib.DataStructure.BTree.groupSizes into the next level. Out of fuel, the
level becomes one node.
A round of bulk loading on a level with branches.
Bulk loading at minimum degree t, or 2 when t is below 2: the bottom level cut by
chunkSizes, then the rounds of buildM, with fuel the number of branches of the bottom
level.
Bulk loading cuts the bottom level and runs the rounds of buildM, at minimum degree
max t 2.
ofSortedList is the Id instantiation of ofSortedListM.
Pure forms of the steps #
Entries #
Bulk loading keeps the entries in order, for every list and every t.
Bulk loading keeps the number of entries.
Leaf sizes #
Shape #
Separators #
Bulk loading a sorted list builds a well-formed tree at a minimum degree of at least 2.
Lookup in a bulk-loaded sorted list is lookup in the list, for every t.