Skip to content

Commit

Permalink
fix: write to balanced layout at width (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw authored Sep 8, 2023
1 parent e7b9ad5 commit 989d96a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/file/layout/balanced.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
* ```
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand All @@ -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))
Expand Down
68 changes: 68 additions & 0 deletions test/file/layout/balanced.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {},
})
}
})
})

0 comments on commit 989d96a

Please sign in to comment.