mirror of https://codeberg.org/pzp/pzp-db.git
refactor to move files around
This commit is contained in:
parent
21c1adbd2a
commit
778dbda588
|
@ -4,6 +4,7 @@ const Path = require('path')
|
||||||
const atomic = require('atomic-file-rw')
|
const atomic = require('atomic-file-rw')
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
const multicb = require('multicb')
|
const multicb = require('multicb')
|
||||||
|
const ReadyGate = require('./utils/ready-gate')
|
||||||
|
|
||||||
// TODO: fs is only supported in node.js. We should support browser by replacing
|
// TODO: fs is only supported in node.js. We should support browser by replacing
|
||||||
// fs.readdir with a browser "file" that just lists all ghost files.
|
// fs.readdir with a browser "file" that just lists all ghost files.
|
||||||
|
@ -16,34 +17,7 @@ const multicb = require('multicb')
|
||||||
* } CB
|
* } CB
|
||||||
*/
|
*/
|
||||||
|
|
||||||
class ReadyGate {
|
class Ghosts {
|
||||||
#waiting
|
|
||||||
#ready
|
|
||||||
constructor() {
|
|
||||||
this.#waiting = new Set()
|
|
||||||
this.#ready = false
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @param {() => void} cb
|
|
||||||
*/
|
|
||||||
onReady(cb) {
|
|
||||||
if (this.#ready) cb()
|
|
||||||
else this.#waiting.add(cb)
|
|
||||||
}
|
|
||||||
|
|
||||||
setReady() {
|
|
||||||
this.#ready = true
|
|
||||||
for (const cb of this.#waiting) cb()
|
|
||||||
this.#waiting.clear()
|
|
||||||
}
|
|
||||||
|
|
||||||
get isReady() {
|
|
||||||
return this.#ready
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class GhostDB {
|
|
||||||
/** @type {string} */
|
/** @type {string} */
|
||||||
#basePath
|
#basePath
|
||||||
|
|
||||||
|
@ -118,7 +92,7 @@ class GhostDB {
|
||||||
#read(tangleID, cb) {
|
#read(tangleID, cb) {
|
||||||
atomic.readFile(
|
atomic.readFile(
|
||||||
this.#path(tangleID),
|
this.#path(tangleID),
|
||||||
GhostDB.encodingOpts,
|
Ghosts.encodingOpts,
|
||||||
(/** @type {any} */ err, /** @type {any} */ str) => {
|
(/** @type {any} */ err, /** @type {any} */ str) => {
|
||||||
// Load Map
|
// Load Map
|
||||||
/** @type {Map<string, number>} */
|
/** @type {Map<string, number>} */
|
||||||
|
@ -166,7 +140,7 @@ class GhostDB {
|
||||||
atomic.writeFile(
|
atomic.writeFile(
|
||||||
this.#path(tangleID),
|
this.#path(tangleID),
|
||||||
this.#serialize(newMap),
|
this.#serialize(newMap),
|
||||||
GhostDB.encodingOpts,
|
Ghosts.encodingOpts,
|
||||||
(/** @type {any} */ err) => {
|
(/** @type {any} */ err) => {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
if (err) return cb(new Error('GhostDB.save() failed to write ghost file', { cause: err }))
|
if (err) return cb(new Error('GhostDB.save() failed to write ghost file', { cause: err }))
|
||||||
|
@ -189,4 +163,4 @@ class GhostDB {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { ReadyGate, GhostDB }
|
module.exports = Ghosts
|
17
lib/index.js
17
lib/index.js
|
@ -15,7 +15,8 @@ const {
|
||||||
ACCOUNT_SELF,
|
ACCOUNT_SELF,
|
||||||
ACCOUNT_ANY,
|
ACCOUNT_ANY,
|
||||||
} = require('./msg-v3/constants')
|
} = require('./msg-v3/constants')
|
||||||
const { ReadyGate, GhostDB } = require('./utils')
|
const ReadyGate = require('./utils/ready-gate')
|
||||||
|
const Ghosts = require('./ghosts')
|
||||||
const { decrypt } = require('./encryption')
|
const { decrypt } = require('./encryption')
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -162,7 +163,7 @@ function initDB(peer, config) {
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
const ghostDB = new GhostDB(Path.join(config.path, 'ghosts'))
|
const ghosts = new Ghosts(Path.join(config.path, 'ghosts'))
|
||||||
|
|
||||||
peer.close.hook(function (/** @type {any} */ fn, /** @type {any} */ args) {
|
peer.close.hook(function (/** @type {any} */ fn, /** @type {any} */ args) {
|
||||||
log.close(() => {
|
log.close(() => {
|
||||||
|
@ -327,7 +328,7 @@ function initDB(peer, config) {
|
||||||
function loaded(cb) {
|
function loaded(cb) {
|
||||||
if (cb === void 0) return promisify(loaded)()
|
if (cb === void 0) return promisify(loaded)()
|
||||||
scannedLog.onReady(() => {
|
scannedLog.onReady(() => {
|
||||||
ghostDB.onReady(cb)
|
ghosts.onReady(cb)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -954,7 +955,7 @@ function initDB(peer, config) {
|
||||||
if (!tangleData) return cb(new Error(`ghosts.add() opts.msg "${opts.msg}" does not belong to opts.tangle "${opts.tangle}"`))
|
if (!tangleData) return cb(new Error(`ghosts.add() opts.msg "${opts.msg}" does not belong to opts.tangle "${opts.tangle}"`))
|
||||||
const depth = tangleData.depth
|
const depth = tangleData.depth
|
||||||
|
|
||||||
ghostDB.save(tangleID, msgID, depth, max, (err) => {
|
ghosts.save(tangleID, msgID, depth, max, (err) => {
|
||||||
// prettier-ignore
|
// prettier-ignore
|
||||||
if (err) cb(new Error('ghosts.add() failed to save to disk', { cause: err }))
|
if (err) cb(new Error('ghosts.add() failed to save to disk', { cause: err }))
|
||||||
else cb()
|
else cb()
|
||||||
|
@ -966,8 +967,8 @@ function initDB(peer, config) {
|
||||||
* @returns {Array<string>}
|
* @returns {Array<string>}
|
||||||
*/
|
*/
|
||||||
function getGhosts(tangleID) {
|
function getGhosts(tangleID) {
|
||||||
const ghosts = ghostDB.read(tangleID)
|
const map = ghosts.read(tangleID)
|
||||||
return [...ghosts.keys()]
|
return [...map.keys()]
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -975,9 +976,9 @@ function initDB(peer, config) {
|
||||||
* @returns {number}
|
* @returns {number}
|
||||||
*/
|
*/
|
||||||
function getMinGhostDepth(tangleID) {
|
function getMinGhostDepth(tangleID) {
|
||||||
const ghosts = ghostDB.read(tangleID)
|
const map = ghosts.read(tangleID)
|
||||||
let minDepth = Infinity
|
let minDepth = Infinity
|
||||||
for (const depth of ghosts.values()) {
|
for (const depth of map.values()) {
|
||||||
if (depth < minDepth) minDepth = depth
|
if (depth < minDepth) minDepth = depth
|
||||||
}
|
}
|
||||||
return minDepth
|
return minDepth
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
class ReadyGate {
|
||||||
|
#waiting
|
||||||
|
#ready
|
||||||
|
constructor() {
|
||||||
|
this.#waiting = new Set()
|
||||||
|
this.#ready = false
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {() => void} cb
|
||||||
|
*/
|
||||||
|
onReady(cb) {
|
||||||
|
if (this.#ready) cb()
|
||||||
|
else this.#waiting.add(cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
setReady() {
|
||||||
|
this.#ready = true
|
||||||
|
for (const cb of this.#waiting) cb()
|
||||||
|
this.#waiting.clear()
|
||||||
|
}
|
||||||
|
|
||||||
|
get isReady() {
|
||||||
|
return this.#ready
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = ReadyGate
|
Loading…
Reference in New Issue