diff --git a/src/file/layout/balanced.js b/src/file/layout/balanced.js index cfd4b41..e1cbde0 100644 --- a/src/file/layout/balanced.js +++ b/src/file/layout/balanced.js @@ -51,13 +51,13 @@ import * as Chunker from "./../chunker/api.js" * | * ------------------------------------------ * | | - * (root4) (root6) + * (root4) (root6) * | | - * ---------------------------------------------- | - * | | | | - * (root1) (root2) (root3) (root5) - * | | | | - * ----------------- --------|-------- ----------------- | + * ------------------------------------------------- | + * | | | | + * (root1) (root2) (root3) (root5) + * | | | | + * --------|-------- --------|-------- --------|-------- | * | | | | | | | | | | * (leaf1) (leaf2) (leaf3) (leaf4) (leaf5) (leaf6) (leaf7) (leaf8) (leaf9) (leaf10) * ``` @@ -176,7 +176,7 @@ export const write = (layout, chunks) => { leafIndex.push(leaf.id) } - if (leafIndex.length >= layout.width) { + if (leafIndex.length > layout.width) { return flush({ ...layout, leafIndex, head, lastID }, leaves) } else { return { @@ -203,7 +203,7 @@ export const flush = (state, leaves = EMPTY, nodes = [], close = false) => { const { width } = state // Move leaves into nodes - while (leafIndex.length >= width || (leafIndex.length > 0 && close)) { + while (leafIndex.length > width || (leafIndex.length > 0 && close)) { grow(nodeIndex, 1) const node = new Node(++lastID, leafIndex.splice(0, width)) nodeIndex[0].push(node.id) @@ -216,7 +216,7 @@ export const flush = (state, leaves = EMPTY, nodes = [], close = false) => { depth++ while ( - row.length >= width || + row.length > width || (row.length > 0 && close && depth < nodeIndex.length) ) { const node = new Node(++lastID, row.splice(0, width)) diff --git a/test/file/layout/balanced.spec.js b/test/file/layout/balanced.spec.js index c9af6c5..c8bdaa6 100644 --- a/test/file/layout/balanced.spec.js +++ b/test/file/layout/balanced.spec.js @@ -124,4 +124,72 @@ describe("balanced layout", () => { }) } }) + + it("overflows into second node at width boundary", () => { + let balanced = Balanced.open({ width: 3 }) + { + const { nodes, leaves, layout } = Balanced.write(balanced, [ + Slice.create([], 0, 4), + Slice.create([], 4, 8), + Slice.create([], 8, 16), + ]) + assert.deepEqual(nodes, []) + assert.deepEqual(leaves, [ + { + id: 1, + content: Slice.create([], 0, 4), + }, + { + id: 2, + content: Slice.create([], 4, 8), + }, + { + id: 3, + content: Slice.create([], 8, 16), + } + ]) + + balanced = layout + } + + { + const { nodes, leaves, layout } = Balanced.write(balanced, [ + Slice.create([], 16, 28), + ]) + assert.deepEqual(leaves, [ + { + id: 4, + content: Slice.create([], 16, 28), + } + ]) + + assert.deepEqual(nodes, [ + { + id: 5, + children: [1, 2, 3], + metadata: undefined, + }, + ]) + + balanced = layout + } + + { + const { root, nodes, leaves } = Balanced.close(balanced, {}) + assert.deepEqual(leaves, []) + assert.deepEqual(nodes, [ + { + id: 6, + children: [4], + metadata: undefined, + }, + ]) + + assert.deepEqual(root, { + id: 7, + children: [5, 6], + metadata: {}, + }) + } + }) })