Skip to content

Commit

Permalink
regenerated hyperdb schema (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafapaezbas authored Jan 9, 2025
1 parent af01572 commit 4806cd9
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 160 deletions.
9 changes: 4 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
"fs-native-extensions": "^1.2.7",
"hypercore-crypto": "^3.4.1",
"hypercore-id-encoding": "^1.3.0",
"hyperdb": "^4.5.1",
"hyperdb": "^4.7.2",
"hyperdrive": "^11.8.1",
"hyperswarm": "^4.7.14",
"iambus": "^1.0.3",
Expand Down
29 changes: 18 additions & 11 deletions spec/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

const { IndexEncoder, c } = require('hyperdb/runtime')

const { version, resolveStruct } = require('./messages.js')
const { version, getEncoding, setVersion } = require('./messages.js')

const helpers0 = require('../helpers.js')

Expand All @@ -15,10 +15,14 @@ function collection0_indexify (record) {
return []
}

// '@pear/dht' value encoding
const collection0_enc = getEncoding('@pear/dht')

// '@pear/dht' reconstruction function
function collection0_reconstruct (version, keyBuf, valueBuf) {
const value = c.decode(resolveStruct('@pear/dht/value', version), valueBuf)
return value
setVersion(version)
const record = c.decode(collection0_enc, valueBuf)
return record
}
// '@pear/dht' key reconstruction function
function collection0_reconstruct_key (keyBuf) {
Expand All @@ -42,7 +46,8 @@ const collection0 = {
})
},
encodeValue (version, record) {
return c.encode(resolveStruct('@pear/dht/value', version), record)
setVersion(version)
return c.encode(collection0_enc, record)
},
trigger: null,
reconstruct: collection0_reconstruct,
Expand All @@ -60,15 +65,16 @@ function collection1_indexify (record) {
return a === undefined ? [] : [a]
}

// '@pear/bundle' value encoding
const collection1_enc = getEncoding('@pear/bundle/hyperdb#1')

// '@pear/bundle' reconstruction function
function collection1_reconstruct (version, keyBuf, valueBuf) {
const key = collection1_key.decode(keyBuf)
const value = c.decode(resolveStruct('@pear/bundle/value', version), valueBuf)
// TODO: This should be fully code generated
return {
link: key[0],
...value
}
setVersion(version)
const record = c.decode(collection1_enc, valueBuf)
record.link = key[0]
return record
}
// '@pear/bundle' key reconstruction function
function collection1_reconstruct_key (keyBuf) {
Expand All @@ -95,7 +101,8 @@ const collection1 = {
})
},
encodeValue (version, record) {
return c.encode(resolveStruct('@pear/bundle/value', version), record)
setVersion(version)
return c.encode(collection1_enc, record)
},
trigger: null,
reconstruct: collection1_reconstruct,
Expand Down
167 changes: 66 additions & 101 deletions spec/db/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ const encoding0 = {
c.uint.encode(state, m.port)
},
decode (state) {
const res = {}
res.host = null
res.port = 0
const r0 = c.string.decode(state)
const r1 = c.uint.decode(state)

res.host = c.string.decode(state)
res.port = c.uint.decode(state)

return res
return {
host: r0,
port: r1
}
}
}

Expand All @@ -37,29 +36,23 @@ const encoding1_0 = c.frame(c.array(encoding0))
// @pear/dht
const encoding1 = {
preencode (state, m) {
let flags = 0
if (m.nodes) flags |= 1

c.uint.preencode(state, flags)
state.end++ // max flag is 1 so always one byte

if (m.nodes) encoding1_0.preencode(state, m.nodes)
},
encode (state, m) {
let flags = 0
if (m.nodes) flags |= 1
const flags = m.nodes ? 1 : 0

c.uint.encode(state, flags)

if (m.nodes) encoding1_0.encode(state, m.nodes)
},
decode (state) {
const res = {}
res.nodes = null
const flags = c.uint.decode(state)

const flags = state.start < state.end ? c.uint.decode(state) : 0
if ((flags & 1) !== 0) res.nodes = encoding1_0.decode(state)

return res
return {
nodes: (flags & 1) !== 0 ? encoding1_0.decode(state) : null
}
}
}

Expand All @@ -69,21 +62,17 @@ const encoding2_3 = c.array(c.string)
// @pear/bundle
const encoding2 = {
preencode (state, m) {
let flags = 0
if (m.encryptionKey) flags |= 1
if (m.tags) flags |= 2

c.string.preencode(state, m.link)
c.string.preencode(state, m.appStorage)
c.uint.preencode(state, flags)
state.end++ // max flag is 2 so always one byte

if (m.encryptionKey) c.fixed32.preencode(state, m.encryptionKey)
if (m.tags) encoding2_3.preencode(state, m.tags)
},
encode (state, m) {
let flags = 0
if (m.encryptionKey) flags |= 1
if (m.tags) flags |= 2
const flags =
(m.encryptionKey ? 1 : 0) |
(m.tags ? 2 : 0)

c.string.encode(state, m.link)
c.string.encode(state, m.appStorage)
Expand All @@ -93,111 +82,87 @@ const encoding2 = {
if (m.tags) encoding2_3.encode(state, m.tags)
},
decode (state) {
const res = {}
res.link = null
res.appStorage = null
res.encryptionKey = null
res.tags = null

res.link = c.string.decode(state)
res.appStorage = c.string.decode(state)

const flags = state.start < state.end ? c.uint.decode(state) : 0
if ((flags & 1) !== 0) res.encryptionKey = c.fixed32.decode(state)
if ((flags & 2) !== 0) res.tags = encoding2_3.decode(state)

return res
const r0 = c.string.decode(state)
const r1 = c.string.decode(state)
const flags = c.uint.decode(state)

return {
link: r0,
appStorage: r1,
encryptionKey: (flags & 1) !== 0 ? c.fixed32.decode(state) : null,
tags: (flags & 2) !== 0 ? encoding2_3.decode(state) : null
}
}
}

// @pear/dht/value.nodes
const encoding3_0 = c.frame(c.array(encoding0))
// @pear/bundle/hyperdb#1.tags
const encoding3_3 = c.array(c.string)

// @pear/dht/value
// @pear/bundle/hyperdb#1
const encoding3 = {
preencode (state, m) {
let flags = 0
if (m.nodes) flags |= 1

c.uint.preencode(state, flags)

if (m.nodes) encoding3_0.preencode(state, m.nodes)
},
encode (state, m) {
let flags = 0
if (m.nodes) flags |= 1

c.uint.encode(state, flags)

if (m.nodes) encoding3_0.encode(state, m.nodes)
},
decode (state) {
const res = {}
res.nodes = null

const flags = state.start < state.end ? c.uint.decode(state) : 0
if ((flags & 1) !== 0) res.nodes = encoding3_0.decode(state)

return res
}
}

// @pear/bundle/value.tags
const encoding4_2 = c.array(c.string)

// @pear/bundle/value
const encoding4 = {
preencode (state, m) {
let flags = 0
if (m.encryptionKey) flags |= 1
if (m.tags) flags |= 2

c.string.preencode(state, m.appStorage)
c.uint.preencode(state, flags)
state.end++ // max flag is 2 so always one byte

if (m.encryptionKey) c.fixed32.preencode(state, m.encryptionKey)
if (m.tags) encoding4_2.preencode(state, m.tags)
if (m.tags) encoding3_3.preencode(state, m.tags)
},
encode (state, m) {
let flags = 0
if (m.encryptionKey) flags |= 1
if (m.tags) flags |= 2
const flags =
(m.encryptionKey ? 1 : 0) |
(m.tags ? 2 : 0)

c.string.encode(state, m.appStorage)
c.uint.encode(state, flags)

if (m.encryptionKey) c.fixed32.encode(state, m.encryptionKey)
if (m.tags) encoding4_2.encode(state, m.tags)
if (m.tags) encoding3_3.encode(state, m.tags)
},
decode (state) {
const res = {}
res.appStorage = null
res.encryptionKey = null
res.tags = null
const r1 = c.string.decode(state)
const flags = c.uint.decode(state)

return {
link: null,
appStorage: r1,
encryptionKey: (flags & 1) !== 0 ? c.fixed32.decode(state) : null,
tags: (flags & 2) !== 0 ? encoding3_3.decode(state) : null
}
}
}

function setVersion (v) {
version = v
}

res.appStorage = c.string.decode(state)
function encode (name, value, v = VERSION) {
version = v
return c.encode(getEncoding(name), value)
}

const flags = state.start < state.end ? c.uint.decode(state) : 0
if ((flags & 1) !== 0) res.encryptionKey = c.fixed32.decode(state)
if ((flags & 2) !== 0) res.tags = encoding4_2.decode(state)
function decode (name, buffer, v = VERSION) {
version = v
return c.decode(getEncoding(name), buffer)
}

return res
function getEnum (name) {
switch (name) {
default: throw new Error('Enum not found ' + name)
}
}

function getStructByName (name) {
function getEncoding (name) {
switch (name) {
case '@pear/node': return encoding0
case '@pear/dht': return encoding1
case '@pear/bundle': return encoding2
case '@pear/dht/value': return encoding3
case '@pear/bundle/value': return encoding4
case '@pear/bundle/hyperdb#1': return encoding3
default: throw new Error('Encoder not found ' + name)
}
}

function resolveStruct (name, v = VERSION) {
const enc = getStructByName(name)
function getStruct (name, v = VERSION) {
const enc = getEncoding(name)
return {
preencode (state, m) {
version = v
Expand All @@ -214,4 +179,4 @@ function resolveStruct (name, v = VERSION) {
}
}

module.exports = { resolveStruct, version }
module.exports = { resolveStruct: getStruct, getStruct, getEnum, getEncoding, encode, decode, setVersion, version }
Loading

0 comments on commit 4806cd9

Please sign in to comment.