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

tree nodes should be a map for out of order entries #29

Merged
merged 4 commits into from
Oct 30, 2024
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
32 changes: 11 additions & 21 deletions lib/memory-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,7 @@ class MemoryOverlay {
}

async peekLastTreeNode () {
const mem = this.treeNodes !== null ? this.treeNodes.get(this.treeNodes.length() - 1) : { valid: false }

const node = mem.valid ? mem.value : null
const disk = await this.storage.peekLastTreeNode()

return (node && (!disk || node.index > disk.index)) ? node : disk
todo()
}

async peekLastBitfieldPage () {
Expand All @@ -93,7 +88,7 @@ class MemoryOverlay {
if (overlay.dataInfo !== null) this.dataInfo = overlay.dataInfo
if (overlay.userData !== null) this.userData = mergeMap(this.userData, overlay.userData)
if (overlay.blocks !== null) this.blocks = mergeTip(this.blocks, overlay.blocks)
if (overlay.treeNodes !== null) this.treeNodes = mergeTip(this.treeNodes, overlay.treeNodes)
if (overlay.treeNodes !== null) this.treeNodes = mergeMap(this.treeNodes, overlay.treeNodes)
if (overlay.bitfields !== null) this.bitfields = mergeTip(this.bitfields, overlay.bitfields)
}
}
Expand Down Expand Up @@ -153,19 +148,15 @@ class MemoryOverlayReadBatch {
}

async hasTreeNode (index) {
if (this.overlay.treeNodes !== null && index >= this.overlay.treeNodes.offset) {
const node = this.overlay.treeNodes.get(index)
if (node.valid) return true
}
return this.read.hasBlock(index)
return this.overlay.treeNodes !== null
? this.overlay.treeNodes.has(index)
: this.read.hasTreeNode(index)
}

async getTreeNode (index, error) {
if (this.overlay.treeNodes !== null && index >= this.overlay.treeNodes.offset) {
const node = this.overlay.treeNodes.get(index)
if (node.valid) return node.value
}
return this.read.getTreeNode(index, error)
return this.overlay.treeNodes !== null && this.overlay.treeNodes.has(index)
? this.overlay.treeNodes.get(index)
: this.read.getTreeNode(index, error)
}

async getBitfieldPage (index) {
Expand Down Expand Up @@ -243,17 +234,16 @@ class MemoryOverlayWriteBatch {
}

putTreeNode (node) {
if (this.overlay.treeNodes === null) this.overlay.treeNodes = new TipList()
this.overlay.treeNodes.put(node.index, node)
if (this.overlay.treeNodes === null) this.overlay.treeNodes = new Map()
this.overlay.treeNodes.set(node.index, node)
}

deleteTreeNode (index) {
todo()
}

deleteTreeNodeRange (start, end) {
if (this.overlay.treeNodes === null) this.overlay.treeNodes = new TipList()
this.overlay.treeNodes.delete(start, end)
todo()
}

putBitfieldPage (index, page) {
Expand Down
111 changes: 4 additions & 107 deletions test/memory-overlay.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ test.skip('memory overlay - delete nodes', async function (t) {
}
})

test('memory overlay - delete tree node range', async function (t) {
// not supported yet
test.skip('memory overlay - delete tree node range', async function (t) {
const c = await getCore(t)

{
Expand Down Expand Up @@ -139,7 +140,8 @@ test('memory overlay - delete tree node range', async function (t) {
}
})

test('memory overlay - delete tree node range: no end', async function (t) {
// not supported yet
test.skip('memory overlay - delete tree node range: no end', async function (t) {
const c = await getCore(t)

{
Expand Down Expand Up @@ -198,111 +200,6 @@ test('memory overlay - delete tree node range: no end', async function (t) {
}
})

test('memory overlay - peek last tree node', async function (t) {
const c = await getCore(t)

{
const b = c.createWriteBatch()
b.putTreeNode({
index: 10000000,
hash: HASH,
size: 10
})
await b.flush()
}

{
const b = c.createWriteBatch()
b.putTreeNode({
index: 1,
hash: HASH,
size: 10
})
await t.exception(() => b.flush())
}

{
const b = c.createWriteBatch()
b.putTreeNode({ index: 10000001, hash: HASH, size: 11 })
b.putTreeNode({ index: 10000002, hash: HASH, size: 12 })
b.putTreeNode({ index: 10000003, hash: HASH, size: 13 })
await b.flush()
}

{
const node = await c.peekLastTreeNode()
t.alike(await node, { index: 10000003, hash: HASH, size: 13 })
}
})

test('memory overlay - invalid tree node add', async function (t) {
const c = await getCore(t)

{
const b = c.createWriteBatch()
b.putTreeNode({ index: 10000000, hash: HASH, size: 10 })
await b.flush()
}

{
const b = c.createWriteBatch()
b.putTreeNode({ index: 1, hash: HASH, size: 10 })
await t.exception(() => b.flush())
}

{
const b = c.createWriteBatch()
b.putTreeNode({ index: 10000001, hash: HASH, size: 11 })
b.putTreeNode({ index: 10000002, hash: HASH, size: 12 })
b.putTreeNode({ index: 10000003, hash: HASH, size: 13 })
await t.execution(() => b.flush())
}
})

test('memory overlay - peek last tree node', async function (t) {
const c = await getCore(t)

{
const b = c.createWriteBatch()
b.putTreeNode({ index: 10000000, hash: HASH, size: 10 })
b.putTreeNode({ index: 10000001, hash: HASH, size: 11 })
b.putTreeNode({ index: 10000002, hash: HASH, size: 12 })
b.putTreeNode({ index: 10000003, hash: HASH, size: 13 })
await b.flush()
}

{
const node = await c.peekLastTreeNode()
t.alike(await node, { index: 10000003, hash: HASH, size: 13 })
}
})

test('memory overlay - peek tree node falls back to disk', async function (t) {
const c = await getCore(t)

{
// write to disk
const w = c.storage.createWriteBatch()
w.putTreeNode({ index: 1, hash: HASH, size: 1 })
w.putTreeNode({ index: 20000000, hash: HASH, size: 20 })
await w.flush()
}

{
const b = c.createWriteBatch()
b.putTreeNode({ index: 10000000, hash: HASH, size: 10 })
b.putTreeNode({ index: 10000001, hash: HASH, size: 11 })
b.putTreeNode({ index: 10000002, hash: HASH, size: 12 })
b.putTreeNode({ index: 10000003, hash: HASH, size: 13 })
await b.flush()
}

{
const node = await c.peekLastTreeNode()
t.alike(await node, { index: 20000000, hash: HASH, size: 20 })
}
})

test('memory overlay - put blocks', async function (t) {
const c = await getCore(t)

Expand Down
Loading