shuffle some things around

This commit is contained in:
Andre Staltz 2023-04-15 16:31:16 +03:00
parent 7e90efd48d
commit 4f8d2f1355
1 changed files with 38 additions and 38 deletions

View File

@ -147,6 +147,19 @@ exports.init = function initDB(peer, config) {
return encryptionFormat return encryptionFormat
} }
function populateTangles(tangleIds) {
const tangles = {}
for (const tangleId of tangleIds) {
tangles[tangleId] ??= new Tangle(tangleId, records())
}
return tangles
}
function loaded(cb) {
if (cb === void 0) return promisify(loaded)()
scannedLog.onReady(cb)
}
function add(msg, tangleRootHash, cb) { function add(msg, tangleRootHash, cb) {
// TODO: optimize this. This may be slow if you're adding many msgs in a // TODO: optimize this. This may be slow if you're adding many msgs in a
// row, because it creates a new Map() each time. // row, because it creates a new Map() each time.
@ -168,21 +181,6 @@ exports.init = function initDB(peer, config) {
} }
} }
function getFeedRoot(findWho, findType) {
for (const rec of records()) {
if (FeedV1.isFeedRoot(rec.msg, findWho, findType)) return rec.hash
}
return null
}
function populateTangles(tangleIds) {
const tangles = {}
for (const tangleId of tangleIds) {
tangles[tangleId] ??= new Tangle(tangleId, records())
}
return tangles
}
function create(opts, cb) { function create(opts, cb) {
const keys = opts.keys ?? config.keys const keys = opts.keys ?? config.keys
@ -251,6 +249,28 @@ exports.init = function initDB(peer, config) {
}) })
} }
function getFeedRoot(findWho, findType) {
for (const rec of records()) {
if (FeedV1.isFeedRoot(rec.msg, findWho, findType)) return rec.hash
}
return null
}
function getRecord(msgId) {
const isUri = msgId.startsWith('ppppp:')
for (let i = 0; i < recs.length; i++) {
const rec = recs[i]
if (!rec) continue
if (isUri && msgId.endsWith(rec.hash)) return rec
else if (!isUri && rec.hash === msgId) return rec
}
return null
}
function get(msgId) {
return getRecord(msgId)?.msg
}
function del(msgId, cb) { function del(msgId, cb) {
const rec = getRecord(msgId) const rec = getRecord(msgId)
const { offset, size, seq } = rec.misc const { offset, size, seq } = rec.misc
@ -274,39 +294,19 @@ exports.init = function initDB(peer, config) {
} }
} }
function getRecord(msgId) {
const isUri = msgId.startsWith('ppppp:')
for (let i = 0; i < recs.length; i++) {
const rec = recs[i]
if (!rec) continue
if (isUri && msgId.endsWith(rec.hash)) return rec
else if (!isUri && rec.hash === msgId) return rec
}
return null
}
function get(msgId) {
return getRecord(msgId)?.msg
}
function loaded(cb) {
if (cb === void 0) return promisify(loaded)()
scannedLog.onReady(cb)
}
return { return {
// public // public
installEncryptionFormat, installEncryptionFormat,
loaded, loaded,
add, add,
create, create,
getFeedRoot,
getRecord,
get,
del, del,
onRecordAdded, onRecordAdded,
msgs, msgs,
records, records,
getRecord,
get,
getFeedRoot,
// internal // internal
findEncryptionFormatFor, findEncryptionFormatFor,