replace ppppp-record with ppppp-dict

This commit is contained in:
Andre Staltz 2023-10-26 13:21:41 +03:00
parent c4fd63239f
commit 617a41330b
No known key found for this signature in database
GPG Key ID: 9EDE23EA7E8A4890
4 changed files with 33 additions and 33 deletions

View File

@ -3,14 +3,14 @@ const Obz = require('obz')
/**
* @typedef {ReturnType<import('ppppp-db').init>} PPPPPDB
* @typedef {ReturnType<import('ppppp-record').init>} PPPPPRecord
* @typedef {ReturnType<import('ppppp-dict').init>} PPPPPDict
* @typedef {import('ppppp-db').RecPresent} RecPresent
* @typedef {import('ppppp-db').Tangle} Tangle
* @typedef {import('ppppp-db').Msg} Msg
* @typedef {ReturnType<PPPPPDB['getTangle']>} DBTangle
* @typedef {string} MsgID
* @typedef {'none'|'all'|`newest-${number}`|'record'|'set'} GoalDSL
* @typedef {'none'|'all'|'newest'|'record'|'set'} GoalType
* @typedef {'none'|'all'|`newest-${number}`|'dict'|'set'} GoalDSL
* @typedef {'none'|'all'|'newest'|'dict'|'set'} GoalType
* @typedef {[number, number]} Range
* @typedef {{ id: string, type: GoalType, count: number }} Goal
* @typedef {{ tangleID: MsgID, span: number }} GhostDetails
@ -56,11 +56,11 @@ function assertDBPlugin(peer) {
}
/**
* @param {{ record: PPPPPRecord | null }} peer
* @returns {asserts peer is { record: PPPPPRecord }}
* @param {{ dict: PPPPPDict | null }} peer
* @returns {asserts peer is { dict: PPPPPDict }}
*/
function assertRecordPlugin(peer) {
if (!peer.record) throw new Error('goals plugin requires ppppp-record plugin')
function assertDictPlugin(peer) {
if (!peer.dict) throw new Error('goals plugin requires ppppp-dict plugin')
}
/**
@ -102,8 +102,8 @@ class GoalImpl {
return
}
if (goalDSL === 'record') {
this.#type = 'record'
if (goalDSL === 'dict') {
this.#type = 'dict'
this.#count = Infinity
return
}
@ -132,7 +132,7 @@ class GoalImpl {
}
/**
* @param {{ db: PPPPPDB | null, record: PPPPPRecord | null }} peer
* @param {{ db: PPPPPDB | null, dict: PPPPPDict | null }} peer
* @param {unknown} config
*/
function initGoals(peer, config) {
@ -161,9 +161,9 @@ function initGoals(peer, config) {
case 'set':
return [0, maxDepth]
case 'record':
assertRecordPlugin(peer)
const minDepth = peer.record.minRequiredDepth(goal.id)
case 'dict':
assertDictPlugin(peer)
const minDepth = peer.dict.minRequiredDepth(goal.id)
return [minDepth, maxDepth]
case 'none':
@ -205,7 +205,7 @@ function initGoals(peer, config) {
assertDBPlugin(peer)
let servesAsTrail = false
// Check whether this record is a goalful root of some tangle:
// Check whether this msg is a goalful root of some tangle:
asRoot: if (goals.has(msgID)) {
const goal = /** @type {GoalImpl} */ (goals.get(msgID))
if (goal.type === 'none') break asRoot
@ -217,7 +217,7 @@ function initGoals(peer, config) {
if (min > 0) servesAsTrail = true
}
// Check whether this record is a goalful affix of some tangle:
// Check whether this msg is a goalful affix of some tangle:
const validTangles =
/** @type {Array<[DBTangle, number, number, number, GoalType]>} */ ([])
asAffix: for (const tangleID in msg.metadata.tangles) {
@ -236,10 +236,10 @@ function initGoals(peer, config) {
for (const [, min, max, recDepth] of validTangles) {
if (min <= recDepth && recDepth <= max) return ['goal']
}
// At this point we know that the record *cannot* serve as 'goal',
// At this point we know that the msg *cannot* serve as 'goal',
// so if it serves as trail, that'll do:
if (servesAsTrail) return ['trail']
// Check whether this record is a trail affix of some tangle:
// Check whether this msg is a trail affix of some tangle:
// (Loop again with heavy computations now that it's inevitable:)
for (const [tangle, min] of validTangles) {
const minMsgIDs = tangle
@ -249,12 +249,12 @@ function initGoals(peer, config) {
if (erasables.has(msgID)) return ['trail']
}
// Check whether this record is a ghost affix of some tangle:
// Check whether this msg is a ghost affix of some tangle:
for (const [tangle, , , , goalType] of validTangles) {
if (goalType === 'record') {
assertRecordPlugin(peer)
const span = peer.record.getGhostSpan()
if (peer.record.isGhostable(msgID, tangle.id)) {
if (goalType === 'dict') {
assertDictPlugin(peer)
const span = peer.dict.getGhostSpan()
if (peer.dict.isGhostable(msgID, tangle.id)) {
return ['ghost', {tangleID: tangle.id, span }]
}
}

View File

@ -33,9 +33,9 @@
"bs58": "^5.0.0",
"c8": "7",
"ppppp-db": "github:staltz/ppppp-db",
"ppppp-dict": "github:staltz/ppppp-dict",
"ppppp-caps": "github:staltz/ppppp-caps",
"ppppp-keypair": "github:staltz/ppppp-keypair",
"ppppp-record": "github:staltz/ppppp-record",
"prettier": "^2.6.2",
"pretty-quick": "^3.1.3",
"rimraf": "^4.4.0",

View File

@ -106,7 +106,7 @@ test('getMsgPurpose', async (t) => {
})
test('getMsgPurpose ghost', async (t) => {
const alice = createPeer({ name: 'alice', record: { ghostSpan: 3 } })
const alice = createPeer({ name: 'alice', dict: { ghostSpan: 3 } })
await alice.db.loaded()
const aliceID = await p(alice.db.account.create)({
@ -114,21 +114,21 @@ test('getMsgPurpose ghost', async (t) => {
_nonce: 'alice',
})
await p(alice.record.load)(aliceID)
await p(alice.record.update)('profile', { name: 'alice' })
await p(alice.record.update)('profile', { name: 'Alice' })
await p(alice.record.update)('profile', { name: 'Alicia' })
await p(alice.record.update)('profile', { name: 'ALICIA' })
await p(alice.record.update)('profile', { name: 'ALICIAA' })
await p(alice.dict.load)(aliceID)
await p(alice.dict.update)('profile', { name: 'alice' })
await p(alice.dict.update)('profile', { name: 'Alice' })
await p(alice.dict.update)('profile', { name: 'Alicia' })
await p(alice.dict.update)('profile', { name: 'ALICIA' })
await p(alice.dict.update)('profile', { name: 'ALICIAA' })
const feedID = alice.record.getFeedID('profile')
const feedID = alice.dict.getFeedID('profile')
const tangle = alice.db.getTangle(feedID)
const msgIDs = tangle.topoSort()
assert.equal(msgIDs.length, 6, 'tangle has root+5 messages')
const recs = msgIDs.map((id) => alice.db.getRecord(id))
alice.goals.set(feedID, 'record')
alice.goals.set(feedID, 'dict')
assert.equal(alice.goals.getMsgPurpose(recs[1].id, recs[1].msg)[0], 'none')
assert.equal(alice.goals.getMsgPurpose(recs[2].id, recs[2].msg)[0], 'ghost')
assert.equal(alice.goals.getMsgPurpose(recs[3].id, recs[3].msg)[0], 'trail')

View File

@ -18,7 +18,7 @@ function createPeer(opts) {
.use(require('secret-stack/plugins/net'))
.use(require('secret-handshake-ext/secret-stack'))
.use(require('ppppp-db'))
.use(require('ppppp-record'))
.use(require('ppppp-dict'))
.use(require('ssb-box'))
.use(require('../lib'))
.call(null, {