Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: write to balanced layout at width #52

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: {},
})
}
})
})