Skip to content

Commit

Permalink
New collections permits and encryption-keys
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreiRegiani committed Nov 28, 2024
1 parent e0e29a6 commit 6bd7f5b
Show file tree
Hide file tree
Showing 7 changed files with 349 additions and 32 deletions.
26 changes: 26 additions & 0 deletions hyperdb/db/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,32 @@
"derived": false,
"key": [],
"trigger": null
},
{
"name": "permits",
"namespace": "pear",
"id": 2,
"type": 1,
"indexes": [],
"schema": "@pear/permits",
"derived": false,
"key": [
"key"
],
"trigger": null
},
{
"name": "encryption-keys",
"namespace": "pear",
"id": 3,
"type": 1,
"indexes": [],
"schema": "@pear/encryption-keys",
"derived": false,
"key": [
"key"
],
"trigger": null
}
]
}
112 changes: 111 additions & 1 deletion hyperdb/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,119 @@ const collection1 = {
indexes: []
}

// '@pear/permits' collection key
const collection2_key = new IndexEncoder([
IndexEncoder.BUFFER
], { prefix: 2 })

function collection2_indexify (record) {
const a = record.key
return a === undefined ? [] : [a]
}

// '@pear/permits' reconstruction function
function collection2_reconstruct (version, keyBuf, valueBuf) {
const key = collection2_key.decode(keyBuf)
const value = c.decode(resolveStruct('@pear/permits/value', version), valueBuf)
// TODO: This should be fully code generated
return {
key: key[0],
...value
}
}
// '@pear/permits' key reconstruction function
function collection2_reconstruct_key (keyBuf) {
const key = collection2_key.decode(keyBuf)
return {
key: key[0]
}
}

// '@pear/permits'
const collection2 = {
name: '@pear/permits',
id: 2,
encodeKey (record) {
const key = [record.key]
return collection2_key.encode(key)
},
encodeKeyRange ({ gt, lt, gte, lte } = {}) {
return collection2_key.encodeRange({
gt: gt ? collection2_indexify(gt) : null,
lt: lt ? collection2_indexify(lt) : null,
gte: gte ? collection2_indexify(gte) : null,
lte: lte ? collection2_indexify(lte) : null
})
},
encodeValue (version, record) {
return c.encode(resolveStruct('@pear/permits/value', version), record)
},
trigger: null,
reconstruct: collection2_reconstruct,
reconstructKey: collection2_reconstruct_key,
indexes: []
}

// '@pear/encryption-keys' collection key
const collection3_key = new IndexEncoder([
IndexEncoder.BUFFER
], { prefix: 3 })

function collection3_indexify (record) {
const a = record.key
return a === undefined ? [] : [a]
}

// '@pear/encryption-keys' reconstruction function
function collection3_reconstruct (version, keyBuf, valueBuf) {
const key = collection3_key.decode(keyBuf)
const value = c.decode(resolveStruct('@pear/encryption-keys/value', version), valueBuf)
// TODO: This should be fully code generated
return {
key: key[0],
...value
}
}
// '@pear/encryption-keys' key reconstruction function
function collection3_reconstruct_key (keyBuf) {
const key = collection3_key.decode(keyBuf)
return {
key: key[0]
}
}

// '@pear/encryption-keys'
const collection3 = {
name: '@pear/encryption-keys',
id: 3,
encodeKey (record) {
const key = [record.key]
return collection3_key.encode(key)
},
encodeKeyRange ({ gt, lt, gte, lte } = {}) {
return collection3_key.encodeRange({
gt: gt ? collection3_indexify(gt) : null,
lt: lt ? collection3_indexify(lt) : null,
gte: gte ? collection3_indexify(gte) : null,
lte: lte ? collection3_indexify(lte) : null
})
},
encodeValue (version, record) {
return c.encode(resolveStruct('@pear/encryption-keys/value', version), record)
},
trigger: null,
reconstruct: collection3_reconstruct,
reconstructKey: collection3_reconstruct_key,
indexes: []
}

module.exports = {
version,
collections: [
collection0,
collection1
collection1,
collection2,
collection3
],
indexes: [
],
Expand All @@ -107,6 +215,8 @@ function resolveCollection (name) {
switch (name) {
case '@pear/bundle': return collection0
case '@pear/dht': return collection1
case '@pear/permits': return collection2
case '@pear/encryption-keys': return collection3
default: return null
}
}
Expand Down
93 changes: 85 additions & 8 deletions hyperdb/db/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,48 @@ const encoding2 = {
}
}

// @pear/bundle/value
// @pear/permits
const encoding3 = {
preencode (state, m) {
c.fixed32.preencode(state, m.key)
},
encode (state, m) {
c.fixed32.encode(state, m.key)
},
decode (state) {
const res = {}
res.key = null

res.key = c.fixed32.decode(state)

return res
}
}

// @pear/encryption-keys
const encoding4 = {
preencode (state, m) {
c.fixed32.preencode(state, m.key)
c.string.preencode(state, m.encryptionKey)
},
encode (state, m) {
c.fixed32.encode(state, m.key)
c.string.encode(state, m.encryptionKey)
},
decode (state) {
const res = {}
res.key = null
res.encryptionKey = null

res.key = c.fixed32.decode(state)
res.encryptionKey = c.string.decode(state)

return res
}
}

// @pear/bundle/value
const encoding5 = {
preencode (state, m) {

},
Expand All @@ -94,32 +134,65 @@ const encoding3 = {
}

// @pear/dht/value.nodes
const encoding4_0 = c.frame(c.array(encoding0))
const encoding6_0 = c.frame(c.array(encoding0))

// @pear/dht/value
const encoding4 = {
const encoding6 = {
preencode (state, m) {
let flags = 0
if (m.nodes) flags |= 1

c.uint.preencode(state, flags)

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

c.uint.encode(state, flags)

if (m.nodes) encoding4_0.encode(state, m.nodes)
if (m.nodes) encoding6_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 = encoding4_0.decode(state)
if ((flags & 1) !== 0) res.nodes = encoding6_0.decode(state)

return res
}
}

// @pear/permits/value
const encoding7 = {
preencode (state, m) {

},
encode (state, m) {

},
decode (state) {
const res = {}

return res
}
}

// @pear/encryption-keys/value
const encoding8 = {
preencode (state, m) {
c.string.preencode(state, m.encryptionKey)
},
encode (state, m) {
c.string.encode(state, m.encryptionKey)
},
decode (state) {
const res = {}
res.encryptionKey = null

res.encryptionKey = c.string.decode(state)

return res
}
Expand All @@ -130,8 +203,12 @@ function getStructByName (name) {
case '@pear/node': return encoding0
case '@pear/bundle': return encoding1
case '@pear/dht': return encoding2
case '@pear/bundle/value': return encoding3
case '@pear/dht/value': return encoding4
case '@pear/permits': return encoding3
case '@pear/encryption-keys': return encoding4
case '@pear/bundle/value': return encoding5
case '@pear/dht/value': return encoding6
case '@pear/permits/value': return encoding7
case '@pear/encryption-keys/value': return encoding8
default: throw new Error('Encoder not found ' + name)
}
}
Expand Down
39 changes: 39 additions & 0 deletions hyperdb/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,33 @@ pearSchema.register({
]
})

pearSchema.register({
name: 'permits',
fields: [
{
name: 'key',
type: 'fixed32',
required: true
}
]
})

pearSchema.register({
name: 'encryption-keys',
fields: [
{
name: 'key',
type: 'fixed32',
required: true
},
{
name: 'encryptionKey',
type: 'string',
required: true
}
]
})

Hyperschema.toDisk(schema)

// hyperdb/db
Expand All @@ -59,4 +86,16 @@ pearDB.collections.register({
schema: '@pear/dht'
})

pearDB.collections.register({
name: 'permits',
schema: '@pear/permits',
key: ['key']
})

pearDB.collections.register({
name: 'encryption-keys',
schema: '@pear/encryption-keys',
key: ['key']
})

Builder.toDisk(db)
Loading

0 comments on commit 6bd7f5b

Please sign in to comment.