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

[refactor] Store -> HyperDB #455

Open
wants to merge 34 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
f45cfc7
hyperdb: identity schema
AndreiRegiani Nov 21, 2024
dc76956
Merge branch 'main' into hyperdb-store-refactor
AndreiRegiani Nov 21, 2024
e9ceaeb
Add `bundle`, remove `identity`
AndreiRegiani Nov 25, 2024
e0e29a6
Merge branch 'main' into hyperdb-store-refactor
AndreiRegiani Nov 26, 2024
6bd7f5b
New collections `permits` and `encryption-keys`
AndreiRegiani Nov 28, 2024
0c00a15
WIP bundle schema implementation
AndreiRegiani Nov 29, 2024
2f69059
Sidecar integration
AndreiRegiani Dec 2, 2024
08c17ef
sidecar/ops implementation
AndreiRegiani Dec 2, 2024
eb37ba0
class EncryptionKey
AndreiRegiani Dec 3, 2024
c54257b
permit() check for nulls
AndreiRegiani Dec 3, 2024
9ea0258
Fix encryption-key property name
AndreiRegiani Dec 3, 2024
1ab3b89
Refactor encryption-key -> encryptionKey
AndreiRegiani Dec 3, 2024
e9281c5
sidecar/ops isValid
AndreiRegiani Dec 4, 2024
edc2da2
Merge branch 'main' into hyperdb-store-refactor
AndreiRegiani Dec 4, 2024
0516ec7
key->link, remove --unsafe-clear-preferences, code style
AndreiRegiani Dec 5, 2024
50236ac
Sidecar: re-usable hyperdb instance
AndreiRegiani Dec 6, 2024
2a67f3f
Merge branch 'main' into hyperdb-store-refactor
AndreiRegiani Dec 6, 2024
dfbd90c
use this.sidecar.db
AndreiRegiani Dec 6, 2024
7f7b4ba
Add sidecar instance into `ops/encryptionKey`
AndreiRegiani Dec 9, 2024
8bb80e4
ops: query bundle.encryptionKey
AndreiRegiani Dec 9, 2024
b8eb847
ops/stage: handle both key and params.encryptionKey
AndreiRegiani Dec 9, 2024
8244d75
fixed worker args (#486)
rafapaezbas Dec 9, 2024
39a8a87
fixed encryption key in hyperdb
rafapaezbas Dec 9, 2024
0baa5aa
Merge branch 'main' into hyperdb-store-refactor
rafapaezbas Dec 9, 2024
71e42bc
removed log from example app
rafapaezbas Dec 9, 2024
f227d38
Remove `cmd/encryption-key`
AndreiRegiani Dec 10, 2024
c30d2fb
revert remove of encryption-key flags
rafapaezbas Dec 10, 2024
e82c82a
pass encryption key to worker
rafapaezbas Dec 10, 2024
70faa82
corrected encryption-key flag for dump, seed and info
rafapaezbas Dec 10, 2024
d0aae11
removed encryption-key op
rafapaezbas Dec 10, 2024
d2a5b5a
lint fix
rafapaezbas Dec 10, 2024
ea2a419
storedEncryptedKey -> storedEncryptionKey
AndreiRegiani Dec 10, 2024
eb11a8f
remove encryption-key flags
rafapaezbas Dec 10, 2024
40a3da7
removed encryption key flag from ops and cmds
rafapaezbas Dec 10, 2024
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
15 changes: 14 additions & 1 deletion hyperdb/db/db.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,24 @@
"offset": 0,
"schema": [
{
"name": "dht",
"name": "bundle",
"namespace": "pear",
"id": 0,
"type": 1,
"indexes": [],
"schema": "@pear/bundle",
"derived": false,
"key": [
"key"
],
"trigger": null
},
{
"name": "dht",
"namespace": "pear",
"id": 1,
"type": 1,
"indexes": [],
"schema": "@pear/dht",
"derived": false,
"key": [],
Expand Down
81 changes: 68 additions & 13 deletions hyperdb/db/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,40 @@ const { IndexEncoder, c } = require('hyperdb/runtime')

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

// '@pear/dht' collection key
// '@pear/bundle' collection key
const collection0_key = new IndexEncoder([
IndexEncoder.STRING
], { prefix: 0 })

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

// '@pear/dht' reconstruction function
// '@pear/bundle' reconstruction function
function collection0_reconstruct (version, keyBuf, valueBuf) {
const value = c.decode(resolveStruct('@pear/dht/value', version), valueBuf)
return value
const key = collection0_key.decode(keyBuf)
const value = c.decode(resolveStruct('@pear/bundle/value', version), valueBuf)
// TODO: This should be fully code generated
return {
key: key[0],
...value
}
}
// '@pear/dht' key reconstruction function
// '@pear/bundle' key reconstruction function
function collection0_reconstruct_key (keyBuf) {
return {}
const key = collection0_key.decode(keyBuf)
return {
key: key[0]
}
}

// '@pear/dht'
// '@pear/bundle'
const collection0 = {
name: '@pear/dht',
name: '@pear/bundle',
id: 0,
encodeKey (record) {
const key = []
const key = [record.key]
return collection0_key.encode(key)
},
encodeKeyRange ({ gt, lt, gte, lte } = {}) {
Expand All @@ -40,18 +50,62 @@ const collection0 = {
})
},
encodeValue (version, record) {
return c.encode(resolveStruct('@pear/dht/value', version), record)
return c.encode(resolveStruct('@pear/bundle/value', version), record)
},
trigger: null,
reconstruct: collection0_reconstruct,
reconstructKey: collection0_reconstruct_key,
indexes: []
}

// '@pear/dht' collection key
const collection1_key = new IndexEncoder([
], { prefix: 1 })

function collection1_indexify (record) {
return []
}

// '@pear/dht' reconstruction function
function collection1_reconstruct (version, keyBuf, valueBuf) {
const value = c.decode(resolveStruct('@pear/dht/value', version), valueBuf)
return value
}
// '@pear/dht' key reconstruction function
function collection1_reconstruct_key (keyBuf) {
return {}
}

// '@pear/dht'
const collection1 = {
name: '@pear/dht',
id: 1,
encodeKey (record) {
const key = []
return collection1_key.encode(key)
},
encodeKeyRange ({ gt, lt, gte, lte } = {}) {
return collection1_key.encodeRange({
gt: gt ? collection1_indexify(gt) : null,
lt: lt ? collection1_indexify(lt) : null,
gte: gte ? collection1_indexify(gte) : null,
lte: lte ? collection1_indexify(lte) : null
})
},
encodeValue (version, record) {
return c.encode(resolveStruct('@pear/dht/value', version), record)
},
trigger: null,
reconstruct: collection1_reconstruct,
reconstructKey: collection1_reconstruct_key,
indexes: []
}

module.exports = {
version,
collections: [
collection0
collection0,
collection1
],
indexes: [
],
Expand All @@ -61,7 +115,8 @@ module.exports = {

function resolveCollection (name) {
switch (name) {
case '@pear/dht': return collection0
case '@pear/bundle': return collection0
case '@pear/dht': return collection1
default: return null
}
}
Expand Down
107 changes: 95 additions & 12 deletions hyperdb/db/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,65 +31,146 @@ const encoding0 = {
}
}

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

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

c.string.preencode(state, m.key)
c.uint.preencode(state, flags)

if (m.encryptionKey) c.string.preencode(state, m.encryptionKey)
if (m.tags) encoding1_2.preencode(state, m.tags)
},
encode (state, m) {
let flags = 0
if (m.encryptionKey) flags |= 1
if (m.tags) flags |= 2

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

if (m.encryptionKey) c.string.encode(state, m.encryptionKey)
if (m.tags) encoding1_2.encode(state, m.tags)
},
decode (state) {
const res = {}
res.key = null
res.encryptionKey = null
res.tags = null

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

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

return res
}
}

// @pear/dht.nodes
const encoding1_0 = c.frame(c.array(encoding0))
const encoding2_0 = c.frame(c.array(encoding0))

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

c.uint.preencode(state, flags)

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

c.uint.encode(state, flags)

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

return res
}
}

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

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

c.uint.preencode(state, flags)

if (m.encryptionKey) c.string.preencode(state, m.encryptionKey)
if (m.tags) encoding3_1.preencode(state, m.tags)
},
encode (state, m) {
let flags = 0
if (m.encryptionKey) flags |= 1
if (m.tags) flags |= 2

c.uint.encode(state, flags)

if (m.encryptionKey) c.string.encode(state, m.encryptionKey)
if (m.tags) encoding3_1.encode(state, m.tags)
},
decode (state) {
const res = {}
res.encryptionKey = null
res.tags = null

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

return res
}
}

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

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

c.uint.preencode(state, flags)

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

c.uint.encode(state, flags)

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

return res
}
Expand All @@ -98,8 +179,10 @@ const encoding2 = {
function getStructByName (name) {
switch (name) {
case '@pear/node': return encoding0
case '@pear/dht': return encoding1
case '@pear/dht/value': return encoding2
case '@pear/bundle': return encoding1
case '@pear/dht': return encoding2
case '@pear/bundle/value': return encoding3
case '@pear/dht/value': return encoding4
default: throw new Error('Encoder not found ' + name)
}
}
Expand Down
31 changes: 31 additions & 0 deletions hyperdb/generate.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ const Builder = require('hyperdb/builder')

const SCHEMA_DIR = path.join(__dirname, 'schema')
const DB_DIR = path.join(__dirname, 'db')

// hyperdb/schema
const schema = Hyperschema.from(SCHEMA_DIR)
const pearSchema = schema.namespace('pear')

// custom types
pearSchema.register({
name: 'node',
fields: [
Expand All @@ -23,6 +26,27 @@ pearSchema.register({
]
})

// structs
pearSchema.register({
name: 'bundle',
fields: [
{
name: 'key',
type: 'string',
required: true
},
{
name: 'encryptionKey',
type: 'string'
},
{
name: 'tags',
type: 'string',
array: true
}
]
})

pearSchema.register({
name: 'dht',
fields: [
Expand All @@ -36,9 +60,16 @@ pearSchema.register({

Hyperschema.toDisk(schema)

// hyperdb/db
const db = Builder.from(SCHEMA_DIR, DB_DIR)
const pearDB = db.namespace('pear')

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

pearDB.collections.register({
name: 'dht',
schema: '@pear/dht'
Expand Down
Loading
Loading