Indexation of files to nodes

This commit is contained in:
Magnus Åhall 2026-06-26 09:45:54 +02:00
parent 70b5285e16
commit f3ca7b9a44
3 changed files with 115 additions and 43 deletions

View file

@ -21,7 +21,7 @@ export class NodeStore {
}//}}}
initializeDB() {//{{{
return new Promise((resolve, reject) => {
const req = indexedDB.open('notes', 10)
const req = indexedDB.open('notes', 12)
// Schema upgrades for IndexedDB.
// These can start from different points depending on updates to Notes2 since a device was online.
@ -31,6 +31,7 @@ export class NodeStore {
let sendQueue
let nodesHistory
let files
let filesNodelink
const db = event.target.result
const trx = event.target.transaction
@ -80,6 +81,14 @@ export class NodeStore {
case 10:
trx.objectStore('files_metadata').createIndex('byUploaded', 'uploaded', { unique: false })
break
case 11:
db.createObjectStore('files_nodelink', { keyPath: 'UUID' })
break
case 12:
trx.objectStore('files_nodelink').createIndex('byNodeCount', 'NodeCount', { unique: false })
break
}
}
}
@ -90,6 +99,7 @@ export class NodeStore {
this.nodesHistory = new NodeHistoryStore(this.db, 'nodes_history')
this.files = new SimpleNodeStore(this.db, 'files')
this.filesMetadata = new SimpleNodeStore(this.db, 'files_metadata')
this.filesNodelink = new FileNodesLinkStore(this.db, 'files_nodelink')
resolve()
}
@ -231,7 +241,6 @@ export class NodeStore {
nodeStore = t.objectStore('nodes')
t.oncomplete = (_event) => {
console.log('complete')
resolve()
}
@ -572,6 +581,51 @@ class NodeHistoryStore extends SimpleNodeStore {
}// }}}
}
class FileNodesLinkStore extends SimpleNodeStore {
async removeFileUUIDs(nodeUUID, fileUUIDs) {// {{{
for (const fileUUID of fileUUIDs) {
let nodelink = await this.get(fileUUID)
// Nothing to remove from.
if (nodelink === undefined)
continue
const idx = nodelink.Nodes.indexOf(nodeUUID)
if (idx > -1)
nodelink.Nodes.splice(idx, 1)
// Maintains an index to quickly find orphaned files.
nodelink.NodeCount = nodelink.Nodes.length
console.log('removing', nodelink)
await this.add({data: nodelink})
}
}// }}}
async addFileUUIDs(nodeUUID, fileUUIDs) {// {{{
for (const fileUUID of fileUUIDs) {
let nodelink = await this.get(fileUUID)
// Needs to be initialized if not existing already.
if (nodelink === undefined)
nodelink = {
UUID: fileUUID,
Nodes: [nodeUUID],
}
// Can get called with the same UUID as already there.
// No duplicates, only append if missing.
if (!nodelink.Nodes.includes(nodeUUID))
nodelink.Nodes.push(nodeUUID)
// Maintains an index to quickly find orphaned files.
nodelink.NodeCount = nodelink.Nodes.length
console.log('pushing', nodelink)
await this.add({data: nodelink})
}
}// }}}
}
export function uuidv7() {// {{{
// random bytes
const value = new Uint8Array(16)