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

add userData API #11

Merged
merged 7 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ class WriteBatch {
this.write.tryPut(encodeDataIndex(this.dataPointer, DATA.INFO), encode(m.DataInfo, info))
}

setUserData (key, value) {
this.write.tryPut(encodeUserDataIndex(this.dataPointer, DATA.INFO, key), value)
}

putBlock (index, data) {
this.write.tryPut(encodeDataIndex(this.storage.dataPointer, DATA.BLOCK, index), data)
}
Expand Down Expand Up @@ -161,6 +165,10 @@ class ReadBatch {
return this._get(encodeDataIndex(this.dataPointer, DATA.INFO), m.DataInfo)
}

getUserData (key) {
return this._get(encodeUserDataIndex(this.dataPointer, DATA.INFO, key), null)
}

async hasBlock (index) {
return this._has(encodeDataIndex(this.storage.dataPointer, DATA.BLOCK, index))
}
Expand Down Expand Up @@ -387,6 +395,13 @@ class HypercoreStorage {
return p
}

getUserData (key) {
const b = this.createReadBatch()
const p = b.getUserData(key)
b.tryFlush()
return p
}

getLocalKeyPair () {
const b = this.createReadBatch()
const p = b.getLocalKeyPair()
Expand Down Expand Up @@ -513,6 +528,17 @@ function encodeDataIndex (pointer, type, index) {
return state.buffer.subarray(start, state.start)
}

function encodeUserDataIndex (pointer, type, key) {
const state = ensureSlab(128 + key.byteLength)
const start = state.start
UINT.encode(state, TL.DATA)
UINT.encode(state, pointer)
UINT.encode(state, type)
c.buffer.encode(state, key)

return state.buffer.subarray(start, state.start)
}

function encodeDiscoveryKey (discoveryKey) {
const state = ensureSlab(128)
const start = state.start
Expand Down
35 changes: 35 additions & 0 deletions test/basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,41 @@ test('header', async function (t) {
t.alike(await c2.getCoreHead(), head)
})

test('user data', async function (t) {
const c = await getCore(t)

{
const b = c.createWriteBatch()

b.setUserData(Buffer.from('hello'), Buffer.from('world'))
b.setUserData(Buffer.from('hej'), Buffer.from('verden'))

await b.flush()
}

{
const b = c.createReadBatch()
const data1 = b.getUserData(Buffer.from('hello'))
const data2 = b.getUserData(Buffer.from('hej'))
b.tryFlush()

t.alike(await data1, Buffer.from('world'))
t.alike(await data2, Buffer.from('verden'))
}

{
const b = c.createWriteBatch()

b.setUserData(Buffer.from('hello'), null)
b.setUserData(Buffer.from('hej'), Buffer.from('verden'))

await b.flush()
}

t.alike(await c.getUserData(Buffer.from('hello')), null)
t.alike(await c.getUserData(Buffer.from('hej')), Buffer.from('verden'))
})

async function getStorage (t, dir) {
if (!dir) dir = await tmp(t)
const s = new CoreStorage(dir)
Expand Down
Loading