2021-10-16 22:13:34 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import sql from "../sql.js";
|
|
|
|
import shaca from "./shaca.js";
|
|
|
|
import log from "../../services/log.js";
|
|
|
|
import SNote from "./entities/snote.js";
|
|
|
|
import SBranch from "./entities/sbranch.js";
|
|
|
|
import SAttribute from "./entities/sattribute.js";
|
|
|
|
import SAttachment from "./entities/sattachment.js";
|
|
|
|
import shareRoot from "../share_root.js";
|
|
|
|
import eventService from "../../services/events.js";
|
2025-01-09 18:36:24 +02:00
|
|
|
import type { SAttachmentRow, SAttributeRow, SBranchRow, SNoteRow } from "./entities/rows.js";
|
2021-10-16 22:13:34 +02:00
|
|
|
|
|
|
|
function load() {
|
|
|
|
const start = Date.now();
|
|
|
|
shaca.reset();
|
|
|
|
|
2023-06-30 11:18:34 +02:00
|
|
|
// using a raw query and passing arrays to avoid allocating new objects
|
2021-10-16 22:13:34 +02:00
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const noteIds = sql.getColumn(
|
|
|
|
`
|
2021-10-17 14:44:59 +02:00
|
|
|
WITH RECURSIVE
|
|
|
|
tree(noteId) AS (
|
|
|
|
SELECT ?
|
|
|
|
UNION
|
|
|
|
SELECT branches.noteId FROM branches
|
2024-12-22 15:45:54 +02:00
|
|
|
JOIN tree ON branches.parentNoteId = tree.noteId
|
2021-10-17 14:44:59 +02:00
|
|
|
WHERE branches.isDeleted = 0
|
|
|
|
)
|
2025-01-09 18:07:02 +02:00
|
|
|
SELECT noteId FROM tree`,
|
|
|
|
[shareRoot.SHARE_ROOT_NOTE_ID]
|
|
|
|
);
|
2021-10-16 22:13:34 +02:00
|
|
|
|
2021-10-17 14:44:59 +02:00
|
|
|
if (noteIds.length === 0) {
|
|
|
|
shaca.loaded = true;
|
2021-10-16 22:13:34 +02:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
const noteIdStr = noteIds.map((noteId) => `'${noteId}'`).join(",");
|
2021-10-16 22:13:34 +02:00
|
|
|
|
2024-04-09 22:49:05 +03:00
|
|
|
const rawNoteRows = sql.getRawRows<SNoteRow>(`
|
2023-03-16 11:02:07 +01:00
|
|
|
SELECT noteId, title, type, mime, blobId, utcDateModified, isProtected
|
2024-12-22 15:45:54 +02:00
|
|
|
FROM notes
|
|
|
|
WHERE isDeleted = 0
|
|
|
|
AND noteId IN (${noteIdStr})`);
|
2021-12-23 12:54:21 +01:00
|
|
|
|
|
|
|
for (const row of rawNoteRows) {
|
2023-01-03 13:40:21 +01:00
|
|
|
new SNote(row);
|
2021-10-16 22:13:34 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:49:05 +03:00
|
|
|
const rawBranchRows = sql.getRawRows<SBranchRow>(`
|
2024-12-22 15:45:54 +02:00
|
|
|
SELECT branchId, noteId, parentNoteId, prefix, isExpanded, utcDateModified
|
|
|
|
FROM branches
|
|
|
|
WHERE isDeleted = 0
|
|
|
|
AND parentNoteId IN (${noteIdStr})
|
2021-12-23 12:54:21 +01:00
|
|
|
ORDER BY notePosition`);
|
|
|
|
|
|
|
|
for (const row of rawBranchRows) {
|
2023-01-03 13:40:21 +01:00
|
|
|
new SBranch(row);
|
2021-10-16 22:13:34 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:49:05 +03:00
|
|
|
const rawAttributeRows = sql.getRawRows<SAttributeRow>(`
|
2024-12-22 15:45:54 +02:00
|
|
|
SELECT attributeId, noteId, type, name, value, isInheritable, position, utcDateModified
|
|
|
|
FROM attributes
|
|
|
|
WHERE isDeleted = 0
|
|
|
|
AND noteId IN (${noteIdStr})`);
|
2021-12-07 23:03:49 +01:00
|
|
|
|
2021-12-23 12:54:21 +01:00
|
|
|
for (const row of rawAttributeRows) {
|
2023-01-03 13:40:21 +01:00
|
|
|
new SAttribute(row);
|
2021-10-16 22:13:34 +02:00
|
|
|
}
|
|
|
|
|
2024-04-09 22:49:05 +03:00
|
|
|
const rawAttachmentRows = sql.getRawRows<SAttachmentRow>(`
|
2024-12-22 15:45:54 +02:00
|
|
|
SELECT attachmentId, ownerId, role, mime, title, blobId, utcDateModified
|
|
|
|
FROM attachments
|
|
|
|
WHERE isDeleted = 0
|
|
|
|
AND ownerId IN (${noteIdStr})`);
|
2023-06-05 23:05:05 +02:00
|
|
|
|
|
|
|
for (const row of rawAttachmentRows) {
|
|
|
|
new SAttachment(row);
|
|
|
|
}
|
|
|
|
|
2021-10-17 14:44:59 +02:00
|
|
|
shaca.loaded = true;
|
2021-10-16 22:13:34 +02:00
|
|
|
|
2023-06-05 23:05:05 +02:00
|
|
|
log.info(`Shaca loaded ${rawNoteRows.length} notes, ${rawBranchRows.length} branches, ${rawAttachmentRows.length} attributes took ${Date.now() - start}ms`);
|
2021-10-16 22:13:34 +02:00
|
|
|
}
|
|
|
|
|
2021-10-17 14:44:59 +02:00
|
|
|
function ensureLoad() {
|
|
|
|
if (!shaca.loaded) {
|
|
|
|
load();
|
2021-10-16 22:13:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-09 18:07:02 +02:00
|
|
|
eventService.subscribe(
|
|
|
|
[eventService.ENTITY_CREATED, eventService.ENTITY_CHANGED, eventService.ENTITY_DELETED, eventService.ENTITY_CHANGE_SYNCED, eventService.ENTITY_DELETE_SYNCED],
|
|
|
|
({ entityName, entity }) => {
|
|
|
|
shaca.reset();
|
|
|
|
}
|
|
|
|
);
|
2021-10-16 22:13:34 +02:00
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
2021-10-16 22:13:34 +02:00
|
|
|
load,
|
2021-10-17 14:44:59 +02:00
|
|
|
ensureLoad
|
2021-10-16 22:13:34 +02:00
|
|
|
};
|