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

Expose user data stream from lib/core.js #604

Merged
merged 5 commits into from
Nov 28, 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
5 changes: 5 additions & 0 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ module.exports = class Core {
}
}

createUserDataStream (opts) {
const storage = (opts && opts.session) ? opts.session.state.storage : this.storage
return storage.createUserDataStream(opts)
}

allSessions () {
const sessions = []
for (const state of this.sessionStates) sessions.push(...state.sessions)
Expand Down
52 changes: 52 additions & 0 deletions test/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,58 @@ test('core - append and truncate', async function (t) {
// t.is(coreReopen.header.hints.reorgs.length, 4)
})

test('core - user data', async function (t) {
const { core, reopen } = await create(t)

await setUserData(core.storage, 'hello', b4a.from('world'))

for await (const { key, value } of core.createUserDataStream()) {
t.alike(key, 'hello')
t.alike(value, b4a.from('world'))
}

t.is(await countEntries(core.createUserDataStream({ gt: 'x', lt: 'z' })), 0)

await setUserData(core.storage, 'hej', b4a.from('verden'))

t.is(await countEntries(core.createUserDataStream()), 2)

for await (const { key, value } of core.createUserDataStream({ gt: 'hej' })) {
t.alike(key, 'hello')
t.alike(value, b4a.from('world'))
}

await setUserData(core.storage, 'hello', null)

t.is(await countEntries(core.createUserDataStream()), 1)
t.is(await countEntries(core.createUserDataStream({ gt: 'hej' })), 0)

await setUserData(core.storage, 'hej', b4a.from('world'))

// check that it was persisted
const coreReopen = await reopen()

for await (const { key, value } of coreReopen.createUserDataStream()) {
t.alike(key, 'hej')
t.alike(value, b4a.from('world'))
}

t.is(await countEntries(coreReopen.createUserDataStream({ gt: 'hej' })), 0)

function setUserData (storage, key, value) {
const b = storage.createWriteBatch()
b.setUserData(key, value)
return b.flush()
}

async function countEntries (stream) {
let count = 0
// eslint-disable-next-line no-unused-vars
for await (const entry of stream) count++
return count
}
})

test('core - header does not retain slabs', async function (t) {
const { core, reopen } = await create(t)

Expand Down