Skip to content

Commit

Permalink
All storage to be opened without discoveryKey (#13)
Browse files Browse the repository at this point in the history
* use b4a

* discoveryKey can be passed in create

* init discovery key to null

* use allocUnsafe
  • Loading branch information
chm-diederichs authored Jul 10, 2024
1 parent ae921e3 commit 5a5aa4e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
39 changes: 25 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ const RocksDB = require('rocksdb-native')
const c = require('compact-encoding')
const { UINT } = require('index-encoder')
const RW = require('read-write-mutexify')
const b4a = require('b4a')
const assert = require('nanoassert')
const m = require('./lib/messages')

const INF = Buffer.from([0xff])
const INF = b4a.from([0xff])

// <TL_INFO> = { version, free, total }
// <TL_LOCAL_SEED> = seed
Expand Down Expand Up @@ -60,7 +61,7 @@ const DATA = {
const SLAB = {
start: 0,
end: 65536,
buffer: Buffer.allocUnsafe(65536)
buffer: b4a.allocUnsafe(65536)
}

// PREFIX + BATCH + TYPE + INDEX
Expand Down Expand Up @@ -246,8 +247,8 @@ module.exports = class CoreStorage {

list () {
const s = this.db.iterator({
gt: Buffer.from([TL.DKEYS]),
lt: Buffer.from([TL.DKEYS + 1])
gt: b4a.from([TL.DKEYS]),
lt: b4a.from([TL.DKEYS + 1])
})

s._readableState.map = mapOnlyDiscoveryKey
Expand All @@ -264,7 +265,7 @@ module.exports = class CoreStorage {

async clear () {
const b = this.db.write()
b.tryDeleteRange(Buffer.from([TL.STORAGE_INFO]), INF)
b.tryDeleteRange(b4a.from([TL.STORAGE_INFO]), INF)
await b.flush()
}

Expand All @@ -278,7 +279,7 @@ class HypercoreStorage {
this.db = db
this.mutex = mutex

this.discoveryKey = discoveryKey
this.discoveryKey = discoveryKey || null

// pointers
this.corePointer = -1
Expand All @@ -288,7 +289,7 @@ class HypercoreStorage {
async open () {
if (!this.discoveryKey) {
const discoveryKey = await getDefaultKey(this.db)
if (!discoveryKey) throw new Error('No discovery key was provided')
if (!discoveryKey) return null

this.discoveryKey = discoveryKey
}
Expand All @@ -304,7 +305,7 @@ class HypercoreStorage {
return this.getCoreInfo()
}

async create ({ key, manifest, keyPair, encryptionKey }) {
async create ({ key, manifest, keyPair, encryptionKey, discoveryKey }) {
await this.mutex.write.lock()

try {
Expand All @@ -315,22 +316,32 @@ class HypercoreStorage {
return false
}

if (this.discoveryKey && discoveryKey && !b4a.equals(this.discoveryKey, discoveryKey)) {
throw new Error('Discovery key does correspond')
}

if (!this.discoveryKey && !discoveryKey) {
throw new Error('No discovery key is provided')
}

if (!this.discoveryKey) this.discoveryKey = discoveryKey

if (!key) throw new Error('No key was provided')

let info = await getStorageInfo(this.db)

const write = this.db.write()

if (!info) {
write.tryPut(Buffer.from([TL.DEFAULT_KEY]), this.discoveryKey)
write.tryPut(b4a.from([TL.DEFAULT_KEY]), this.discoveryKey)
info = { free: 0, total: 0 }
}

const core = info.total++
const data = info.free++

write.tryPut(encodeDiscoveryKey(this.discoveryKey), encode(m.CorePointer, { core, data }))
write.tryPut(Buffer.from([TL.STORAGE_INFO]), encode(m.StorageInfo, info))
write.tryPut(b4a.from([TL.STORAGE_INFO]), encode(m.StorageInfo, info))

this.corePointer = core
this.dataPointer = data
Expand Down Expand Up @@ -522,18 +533,18 @@ function mapOnlyDiscoveryKey (data) {
}

async function getDefaultKey (db) {
return db.get(Buffer.from([TL.DEFAULT_KEY]))
return db.get(b4a.from([TL.DEFAULT_KEY]))
}

async function getStorageInfo (db) {
const value = await db.get(Buffer.from([TL.STORAGE_INFO]))
const value = await db.get(b4a.from([TL.STORAGE_INFO]))
if (value === null) return null
return c.decode(m.StorageInfo, value)
}

function ensureSlab (size) {
if (SLAB.buffer.byteLength - SLAB.start < size) {
SLAB.buffer = Buffer.allocUnsafe(SLAB.end)
SLAB.buffer = b4a.allocUnsafe(SLAB.end)
SLAB.start = 0
}

Expand Down Expand Up @@ -588,7 +599,7 @@ function encodeDataIndex (pointer, type, index) {

function encodeUserDataIndex (pointer, type, key) {
const end = 128 + key.length
const state = { start: 0, end, buffer: Buffer.alloc(end) }
const state = { start: 0, end, buffer: b4a.allocUnsafe(end) }
const start = state.start
UINT.encode(state, TL.DATA)
UINT.encode(state, pointer)
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"license": "Apache-2.0",
"description": "RocksDB storage driver for Hypercore",
"dependencies": {
"b4a": "^1.6.6",
"compact-encoding": "^2.15.0",
"index-encoder": "^3.0.1",
"nanoassert": "^2.0.0",
Expand Down

0 comments on commit 5a5aa4e

Please sign in to comment.