mindoodb 0.3.3 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.mjs +6 -0
- package/dist/core/BaseMindooTenantDirectory.d.ts +10 -0
- package/dist/core/BaseMindooTenantDirectory.d.ts.map +1 -1
- package/dist/core/BaseMindooTenantDirectory.js +81 -3
- package/dist/core/BaseMindooTenantDirectory.js.map +1 -1
- package/dist/core/accesscontrol/directoryProjection.d.ts.map +1 -1
- package/dist/core/accesscontrol/directoryProjection.js +1 -0
- package/dist/core/accesscontrol/directoryProjection.js.map +1 -1
- package/dist/core/accesscontrol/types.d.ts +11 -2
- package/dist/core/accesscontrol/types.d.ts.map +1 -1
- package/dist/core/accesscontrol/types.js +3 -0
- package/dist/core/accesscontrol/types.js.map +1 -1
- package/dist/core/appendonlystores/network/AuthenticationService.d.ts +1 -0
- package/dist/core/appendonlystores/network/AuthenticationService.d.ts.map +1 -1
- package/dist/core/appendonlystores/network/AuthenticationService.js +1 -1
- package/dist/core/appendonlystores/network/AuthenticationService.js.map +1 -1
- package/dist/core/appendonlystores/network/types.d.ts +2 -0
- package/dist/core/appendonlystores/network/types.d.ts.map +1 -1
- package/dist/core/appendonlystores/network/types.js.map +1 -1
- package/dist/core/appendonlystores/rpc/SqlRpc.d.ts +8 -1
- package/dist/core/appendonlystores/rpc/SqlRpc.d.ts.map +1 -1
- package/dist/core/appendonlystores/rpc/SqlRpc.js +17 -0
- package/dist/core/appendonlystores/rpc/SqlRpc.js.map +1 -1
- package/dist/core/appendonlystores/sqlite/SqliteDriver.d.ts +6 -0
- package/dist/core/appendonlystores/sqlite/SqliteDriver.d.ts.map +1 -1
- package/dist/core/appendonlystores/sqlite/SqliteDriver.js.map +1 -1
- package/dist/core/builtinDbInvariants.d.ts +2 -1
- package/dist/core/builtinDbInvariants.d.ts.map +1 -1
- package/dist/core/builtinDbInvariants.js +2 -1
- package/dist/core/builtinDbInvariants.js.map +1 -1
- package/dist/core/guestAdmissions.d.ts +31 -2
- package/dist/core/guestAdmissions.d.ts.map +1 -1
- package/dist/core/guestAdmissions.js +26 -3
- package/dist/core/guestAdmissions.js.map +1 -1
- package/dist/core/guestTickets.d.ts +13 -0
- package/dist/core/guestTickets.d.ts.map +1 -1
- package/dist/core/guestTickets.js +16 -1
- package/dist/core/guestTickets.js.map +1 -1
- package/dist/core/index.d.ts +3 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +9 -3
- package/dist/core/index.js.map +1 -1
- package/dist/core/indexing/summary/SummaryVirtualViewDataProvider.d.ts +19 -0
- package/dist/core/indexing/summary/SummaryVirtualViewDataProvider.d.ts.map +1 -1
- package/dist/core/indexing/summary/SummaryVirtualViewDataProvider.js +149 -52
- package/dist/core/indexing/summary/SummaryVirtualViewDataProvider.js.map +1 -1
- package/dist/core/indexing/virtualviews/IViewEntryAccessCheck.d.ts +8 -7
- package/dist/core/indexing/virtualviews/IViewEntryAccessCheck.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/IViewEntryAccessCheck.js +3 -3
- package/dist/core/indexing/virtualviews/IViewEntryAccessCheck.js.map +1 -1
- package/dist/core/indexing/virtualviews/MindooDBVirtualViewDataProvider.d.ts +9 -0
- package/dist/core/indexing/virtualviews/MindooDBVirtualViewDataProvider.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/MindooDBVirtualViewDataProvider.js +20 -3
- package/dist/core/indexing/virtualviews/MindooDBVirtualViewDataProvider.js.map +1 -1
- package/dist/core/indexing/virtualviews/ViewEntry.d.ts +48 -0
- package/dist/core/indexing/virtualviews/ViewEntry.d.ts.map +1 -0
- package/dist/core/indexing/virtualviews/ViewEntry.js +3 -0
- package/dist/core/indexing/virtualviews/ViewEntry.js.map +1 -0
- package/dist/core/indexing/virtualviews/ViewEntrySortKeyComparator.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/ViewEntrySortKeyComparator.js +6 -6
- package/dist/core/indexing/virtualviews/ViewEntrySortKeyComparator.js.map +1 -1
- package/dist/core/indexing/virtualviews/ViewNavigator.d.ts +58 -0
- package/dist/core/indexing/virtualviews/ViewNavigator.d.ts.map +1 -0
- package/dist/core/indexing/virtualviews/ViewNavigator.js +3 -0
- package/dist/core/indexing/virtualviews/ViewNavigator.js.map +1 -0
- package/dist/core/indexing/virtualviews/VirtualView.d.ts +38 -0
- package/dist/core/indexing/virtualviews/VirtualView.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualView.js +150 -0
- package/dist/core/indexing/virtualviews/VirtualView.js.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewDataChange.d.ts +6 -0
- package/dist/core/indexing/virtualviews/VirtualViewDataChange.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewDataChange.js +8 -1
- package/dist/core/indexing/virtualviews/VirtualViewDataChange.js.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewEntryData.d.ts +4 -1
- package/dist/core/indexing/virtualviews/VirtualViewEntryData.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewEntryData.js +14 -0
- package/dist/core/indexing/virtualviews/VirtualViewEntryData.js.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewFactory.d.ts +9 -1
- package/dist/core/indexing/virtualviews/VirtualViewFactory.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewFactory.js +25 -0
- package/dist/core/indexing/virtualviews/VirtualViewFactory.js.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewMergedNavigator.d.ts +19 -17
- package/dist/core/indexing/virtualviews/VirtualViewMergedNavigator.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewMergedNavigator.js +20 -17
- package/dist/core/indexing/virtualviews/VirtualViewMergedNavigator.js.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewNavigator.d.ts +77 -301
- package/dist/core/indexing/virtualviews/VirtualViewNavigator.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewNavigator.js +158 -895
- package/dist/core/indexing/virtualviews/VirtualViewNavigator.js.map +1 -1
- package/dist/core/indexing/virtualviews/VirtualViewTreeCursor.d.ts +336 -0
- package/dist/core/indexing/virtualviews/VirtualViewTreeCursor.d.ts.map +1 -0
- package/dist/core/indexing/virtualviews/VirtualViewTreeCursor.js +1015 -0
- package/dist/core/indexing/virtualviews/VirtualViewTreeCursor.js.map +1 -0
- package/dist/core/indexing/virtualviews/index.d.ts +6 -0
- package/dist/core/indexing/virtualviews/index.d.ts.map +1 -1
- package/dist/core/indexing/virtualviews/index.js +10 -1
- package/dist/core/indexing/virtualviews/index.js.map +1 -1
- package/dist/core/indexing/virtualviews/sql/SqlViewIndex.d.ts +112 -0
- package/dist/core/indexing/virtualviews/sql/SqlViewIndex.d.ts.map +1 -0
- package/dist/core/indexing/virtualviews/sql/SqlViewIndex.js +364 -0
- package/dist/core/indexing/virtualviews/sql/SqlViewIndex.js.map +1 -0
- package/dist/core/indexing/virtualviews/sql/SqlViewNavigator.d.ts +250 -0
- package/dist/core/indexing/virtualviews/sql/SqlViewNavigator.d.ts.map +1 -0
- package/dist/core/indexing/virtualviews/sql/SqlViewNavigator.js +878 -0
- package/dist/core/indexing/virtualviews/sql/SqlViewNavigator.js.map +1 -0
- package/dist/core/indexing/virtualviews/viewRows.d.ts +59 -0
- package/dist/core/indexing/virtualviews/viewRows.d.ts.map +1 -0
- package/dist/core/indexing/virtualviews/viewRows.js +105 -0
- package/dist/core/indexing/virtualviews/viewRows.js.map +1 -0
- package/dist/core/indexing/virtualviews/viewSortKeys.d.ts +44 -0
- package/dist/core/indexing/virtualviews/viewSortKeys.d.ts.map +1 -0
- package/dist/core/indexing/virtualviews/viewSortKeys.js +85 -0
- package/dist/core/indexing/virtualviews/viewSortKeys.js.map +1 -0
- package/dist/core/types.d.ts +4 -0
- package/dist/core/types.d.ts.map +1 -1
- package/dist/guest/guestCall.d.ts +1 -0
- package/dist/guest/guestCall.d.ts.map +1 -1
- package/dist/guest/guestCall.js +2 -1
- package/dist/guest/guestCall.js.map +1 -1
- package/dist/server/GuestAdmissionService.d.ts +2 -1
- package/dist/server/GuestAdmissionService.d.ts.map +1 -1
- package/dist/server/GuestAdmissionService.js +4 -2
- package/dist/server/GuestAdmissionService.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,878 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqlViewNavigator = exports.SqlViewEntry = exports.SQL_VIEW_READ_ENTRIES = void 0;
|
|
4
|
+
exports.sqlViewReadEntriesProcedure = sqlViewReadEntriesProcedure;
|
|
5
|
+
const types_1 = require("../types");
|
|
6
|
+
const viewSortKeys_1 = require("../viewSortKeys");
|
|
7
|
+
const SqlViewIndex_1 = require("./SqlViewIndex");
|
|
8
|
+
/** SQL RPC procedure: `readEntries` where SQLite runs (see sqlViewReadEntriesProcedure). */
|
|
9
|
+
exports.SQL_VIEW_READ_ENTRIES = "view.readEntries";
|
|
10
|
+
/** An entry of a view in SQLite (see {@link ViewEntry}). */
|
|
11
|
+
class SqlViewEntry {
|
|
12
|
+
constructor(origin, docId, category,
|
|
13
|
+
/** Category key (categories) or the row's category key (documents). */
|
|
14
|
+
catKey,
|
|
15
|
+
/** Sort key of a document row ('' for categories). */
|
|
16
|
+
sortKey, level, parentPosition, siblingIndex, siblingCount, categoryPath, categoryValue, columnValues, totals, counts, decryptionKeyId) {
|
|
17
|
+
this.origin = origin;
|
|
18
|
+
this.docId = docId;
|
|
19
|
+
this.category = category;
|
|
20
|
+
this.catKey = catKey;
|
|
21
|
+
this.sortKey = sortKey;
|
|
22
|
+
this.level = level;
|
|
23
|
+
this.parentPosition = parentPosition;
|
|
24
|
+
this.siblingIndex = siblingIndex;
|
|
25
|
+
this.siblingCount = siblingCount;
|
|
26
|
+
this.categoryPath = categoryPath;
|
|
27
|
+
this.categoryValue = categoryValue;
|
|
28
|
+
this.columnValues = columnValues;
|
|
29
|
+
this.totals = totals;
|
|
30
|
+
this.counts = counts;
|
|
31
|
+
this.decryptionKeyId = decryptionKeyId;
|
|
32
|
+
}
|
|
33
|
+
/** The constructor's arguments, to send the entry elsewhere (see fromArgs). */
|
|
34
|
+
toArgs() {
|
|
35
|
+
return [
|
|
36
|
+
this.origin, this.docId, this.category, this.catKey, this.sortKey, this.level, this.parentPosition,
|
|
37
|
+
this.siblingIndex, this.siblingCount, this.categoryPath, this.categoryValue, this.columnValues,
|
|
38
|
+
this.totals, this.counts, this.decryptionKeyId,
|
|
39
|
+
];
|
|
40
|
+
}
|
|
41
|
+
static fromArgs(args) {
|
|
42
|
+
return new SqlViewEntry(...args);
|
|
43
|
+
}
|
|
44
|
+
isCategory() {
|
|
45
|
+
return this.category;
|
|
46
|
+
}
|
|
47
|
+
isDocument() {
|
|
48
|
+
return !this.category;
|
|
49
|
+
}
|
|
50
|
+
getLevel() {
|
|
51
|
+
return this.level;
|
|
52
|
+
}
|
|
53
|
+
getPosition() {
|
|
54
|
+
return [...this.parentPosition, this.siblingIndex];
|
|
55
|
+
}
|
|
56
|
+
getPositionStr() {
|
|
57
|
+
return this.getPosition().join(".");
|
|
58
|
+
}
|
|
59
|
+
getParentPositionStr() {
|
|
60
|
+
return this.parentPosition.length > 0 ? this.parentPosition.join(".") : null;
|
|
61
|
+
}
|
|
62
|
+
getSiblingIndex() {
|
|
63
|
+
return this.siblingIndex;
|
|
64
|
+
}
|
|
65
|
+
getSiblingCount() {
|
|
66
|
+
return this.siblingCount;
|
|
67
|
+
}
|
|
68
|
+
getCategoryPath() {
|
|
69
|
+
return [...this.categoryPath];
|
|
70
|
+
}
|
|
71
|
+
getCategoryValue() {
|
|
72
|
+
return this.category ? this.categoryValue : null;
|
|
73
|
+
}
|
|
74
|
+
getColumnValue(name) {
|
|
75
|
+
return Object.prototype.hasOwnProperty.call(this.columnValues, name) ? this.columnValues[name] : undefined;
|
|
76
|
+
}
|
|
77
|
+
getColumnValues() {
|
|
78
|
+
return { ...this.columnValues };
|
|
79
|
+
}
|
|
80
|
+
getAsString(name, defaultValue) {
|
|
81
|
+
const value = this.getColumnValue(name);
|
|
82
|
+
return value === null || value === undefined ? defaultValue : String(value);
|
|
83
|
+
}
|
|
84
|
+
getAsNumber(name, defaultValue) {
|
|
85
|
+
const value = this.getColumnValue(name);
|
|
86
|
+
if (value === null || value === undefined)
|
|
87
|
+
return defaultValue;
|
|
88
|
+
if (typeof value === "number")
|
|
89
|
+
return value;
|
|
90
|
+
const parsed = parseFloat(String(value));
|
|
91
|
+
return Number.isNaN(parsed) ? defaultValue : parsed;
|
|
92
|
+
}
|
|
93
|
+
getAsStringList(name, defaultValue) {
|
|
94
|
+
const value = this.getColumnValue(name);
|
|
95
|
+
if (value === null || value === undefined)
|
|
96
|
+
return defaultValue;
|
|
97
|
+
return Array.isArray(value) ? value.map((v) => String(v)) : [String(value)];
|
|
98
|
+
}
|
|
99
|
+
getTotalValue(itemName) {
|
|
100
|
+
return this.totals[itemName.toLowerCase()] ?? null;
|
|
101
|
+
}
|
|
102
|
+
getTotalValues() {
|
|
103
|
+
return { ...this.totals };
|
|
104
|
+
}
|
|
105
|
+
getDecryptionKeyId() {
|
|
106
|
+
return this.decryptionKeyId;
|
|
107
|
+
}
|
|
108
|
+
getChildCount() {
|
|
109
|
+
return this.counts.childCategoryCount + this.counts.childDocumentCount;
|
|
110
|
+
}
|
|
111
|
+
getChildCategoryCount() {
|
|
112
|
+
return this.counts.childCategoryCount;
|
|
113
|
+
}
|
|
114
|
+
getChildDocumentCount() {
|
|
115
|
+
return this.counts.childDocumentCount;
|
|
116
|
+
}
|
|
117
|
+
getDescendantCount() {
|
|
118
|
+
return this.counts.descendantCategoryCount + this.counts.descendantDocumentCount;
|
|
119
|
+
}
|
|
120
|
+
getDescendantCategoryCount() {
|
|
121
|
+
return this.counts.descendantCategoryCount;
|
|
122
|
+
}
|
|
123
|
+
getDescendantDocumentCount() {
|
|
124
|
+
return this.counts.descendantDocumentCount;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
exports.SqlViewEntry = SqlViewEntry;
|
|
128
|
+
class SqlViewNavigator {
|
|
129
|
+
constructor(index, sql, options = {}) {
|
|
130
|
+
this.index = index;
|
|
131
|
+
this.stack = [];
|
|
132
|
+
this.top = null;
|
|
133
|
+
/** Documents selected with their parent categories, resolved before the next read (see select). */
|
|
134
|
+
this.pendingParentSelections = [];
|
|
135
|
+
this.selectedOrDeselectedEntries = new Set();
|
|
136
|
+
this.selectAll = false;
|
|
137
|
+
this.expandedOrCollapsedEntries = new Set();
|
|
138
|
+
this.expandAllByDefault = false;
|
|
139
|
+
this.expandLevel = 0;
|
|
140
|
+
/** Sibling windows of the bulk read in progress (see withWindows), by direction and parent key. */
|
|
141
|
+
this.windows = null;
|
|
142
|
+
this.windowSize = 1;
|
|
143
|
+
this.sql = sql;
|
|
144
|
+
this.viewId = index.viewId;
|
|
145
|
+
this.withCategories = options.withCategories ?? true;
|
|
146
|
+
this.withDocuments = options.withDocuments ?? true;
|
|
147
|
+
this.accessCheck = options.accessCheck ?? null;
|
|
148
|
+
this.dontShowEmptyCategories = options.dontShowEmptyCategories ?? false;
|
|
149
|
+
if (!this.withCategories && !this.withDocuments) {
|
|
150
|
+
throw new Error("Navigator must include categories, documents, or both");
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
// ===== Building entries =====
|
|
154
|
+
totalsColumnValues(totals, descendantDocs) {
|
|
155
|
+
const values = {};
|
|
156
|
+
for (const column of this.index.layout.totalColumns) {
|
|
157
|
+
const total = totals[column.name.toLowerCase()];
|
|
158
|
+
if (total === undefined)
|
|
159
|
+
continue;
|
|
160
|
+
if (column.mode === types_1.TotalMode.SUM)
|
|
161
|
+
values[column.name] = total;
|
|
162
|
+
else if (column.mode === types_1.TotalMode.AVERAGE && descendantDocs > 0)
|
|
163
|
+
values[column.name] = total / descendantDocs;
|
|
164
|
+
}
|
|
165
|
+
return values;
|
|
166
|
+
}
|
|
167
|
+
categoryEntry(raw, parent, siblingIndex) {
|
|
168
|
+
const value = (0, SqlViewIndex_1.decodeViewValues)(raw.value).v ?? null;
|
|
169
|
+
const rawTotals = JSON.parse(raw.totals);
|
|
170
|
+
const totals = {};
|
|
171
|
+
for (const [name, total] of Object.entries(rawTotals))
|
|
172
|
+
totals[name.toLowerCase()] = total;
|
|
173
|
+
const descendantDocs = Number(raw.descendant_doc_count);
|
|
174
|
+
const columnValues = value !== null ? { [raw.col]: value } : {};
|
|
175
|
+
Object.assign(columnValues, this.totalsColumnValues(totals, descendantDocs));
|
|
176
|
+
const parentEntry = parent.entry;
|
|
177
|
+
return new SqlViewEntry(types_1.ORIGIN_VIRTUALVIEW, raw.cat_id, true, raw.cat_key, "", parentEntry ? parentEntry.getLevel() + 1 : 0, parentEntry ? parentEntry.getPosition() : [], siblingIndex, parent.childCount, [...(parentEntry ? parentEntry.getCategoryPath() : []), value], value, columnValues, totals, {
|
|
178
|
+
childCategoryCount: Number(raw.child_cat_count),
|
|
179
|
+
childDocumentCount: Number(raw.child_doc_count),
|
|
180
|
+
descendantCategoryCount: Number(raw.descendant_cat_count),
|
|
181
|
+
descendantDocumentCount: descendantDocs,
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
documentEntry(raw, parent, siblingIndex) {
|
|
185
|
+
const parentEntry = parent.entry;
|
|
186
|
+
return new SqlViewEntry(raw.origin, raw.doc_id, false, raw.cat_key, raw.sort_key, parentEntry ? parentEntry.getLevel() + 1 : 0, parentEntry ? parentEntry.getPosition() : [], siblingIndex, parent.childCount, parentEntry ? parentEntry.getCategoryPath() : [], null, (0, SqlViewIndex_1.decodeViewValues)(raw.cols), {}, { childCategoryCount: 0, childDocumentCount: 0, descendantCategoryCount: 0, descendantDocumentCount: 0 }, raw.key_id ?? undefined);
|
|
187
|
+
}
|
|
188
|
+
async parentNode(key, entry) {
|
|
189
|
+
const row = (await this.sql.get(`SELECT child_cat_count, child_doc_count FROM ${SqlViewIndex_1.SQL_VIEW_TABLES.cat} WHERE view_id = ? AND cat_key = ?`, [this.viewId, key]));
|
|
190
|
+
const cats = Number(row?.child_cat_count ?? 0);
|
|
191
|
+
const docs = Number(row?.child_doc_count ?? 0);
|
|
192
|
+
return { key, entry, childrenAreCategories: cats > 0, childCount: cats + docs };
|
|
193
|
+
}
|
|
194
|
+
async topNode() {
|
|
195
|
+
if (!this.top) {
|
|
196
|
+
await this.index.init();
|
|
197
|
+
this.top = await this.parentNode("", null);
|
|
198
|
+
}
|
|
199
|
+
return this.top;
|
|
200
|
+
}
|
|
201
|
+
// ===== Child queries (keyset) =====
|
|
202
|
+
/** First (or last) child of `parent` after (or before) `from`, by key; `from` null = from the edge. */
|
|
203
|
+
async step(parent, from, forward) {
|
|
204
|
+
if (!this.windows)
|
|
205
|
+
return (await this.fetchSiblings(parent, from, forward, 1))[0] ?? null;
|
|
206
|
+
// Inside a bulk read: serve steps from a window of siblings read in one query.
|
|
207
|
+
const windowKey = `${forward ? ">" : "<"}${parent.key}`;
|
|
208
|
+
const window = this.windows.get(windowKey);
|
|
209
|
+
if (window) {
|
|
210
|
+
const at = from ? window.positions.get(this.siblingKey(parent, from)) : window.after === null ? -1 : undefined;
|
|
211
|
+
if (at !== undefined) {
|
|
212
|
+
if (at + 1 < window.entries.length)
|
|
213
|
+
return window.entries[at + 1];
|
|
214
|
+
if (window.exhausted)
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
const entries = await this.fetchSiblings(parent, from, forward, this.windowSize);
|
|
219
|
+
const positions = new Map();
|
|
220
|
+
entries.forEach((entry, i) => positions.set(this.siblingKey(parent, entry), i));
|
|
221
|
+
this.windows.set(windowKey, {
|
|
222
|
+
after: from ? this.siblingKey(parent, from) : null,
|
|
223
|
+
entries,
|
|
224
|
+
positions,
|
|
225
|
+
exhausted: entries.length < this.windowSize,
|
|
226
|
+
});
|
|
227
|
+
return entries[0] ?? null;
|
|
228
|
+
}
|
|
229
|
+
/** The key `step` moves by among the children of `parent`. */
|
|
230
|
+
siblingKey(parent, entry) {
|
|
231
|
+
return parent.childrenAreCategories ? entry.catKey : entry.sortKey;
|
|
232
|
+
}
|
|
233
|
+
/** Up to `limit` children of `parent` after (or before) `from`, in that direction. */
|
|
234
|
+
async fetchSiblings(parent, from, forward, limit) {
|
|
235
|
+
const t = SqlViewIndex_1.SQL_VIEW_TABLES;
|
|
236
|
+
const first = from ? from.getSiblingIndex() + (forward ? 1 : -1) : forward ? 1 : parent.childCount;
|
|
237
|
+
const direction = forward ? 1 : -1;
|
|
238
|
+
const cmp = forward ? ">" : "<";
|
|
239
|
+
const order = forward ? "ASC" : "DESC";
|
|
240
|
+
if (parent.childrenAreCategories) {
|
|
241
|
+
const rows = (await this.sql.all(`SELECT * FROM ${t.cat} WHERE view_id = ? AND parent_key = ? AND cat_key <> ''${from ? ` AND cat_key ${cmp} ?` : ""} ORDER BY cat_key ${order} LIMIT ?`, from ? [this.viewId, parent.key, from.catKey, limit] : [this.viewId, parent.key, limit]));
|
|
242
|
+
return rows.map((raw, i) => this.categoryEntry(raw, parent, first + i * direction));
|
|
243
|
+
}
|
|
244
|
+
const rows = (await this.sql.all(`SELECT cat_key, sort_key, origin, doc_id, cols, key_id FROM ${t.row} WHERE view_id = ? AND cat_key = ?${from ? ` AND sort_key ${cmp} ?` : ""} ORDER BY sort_key ${order} LIMIT ?`, from ? [this.viewId, parent.key, from.sortKey, limit] : [this.viewId, parent.key, limit]));
|
|
245
|
+
return rows.map((raw, i) => this.documentEntry(raw, parent, first + i * direction));
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Runs `work` with sibling windows: each `step` reads up to `size`
|
|
249
|
+
* siblings at once and serves the following steps from them, so a page
|
|
250
|
+
* of N entries costs a few queries instead of N. The windows live only
|
|
251
|
+
* for this call, so later reads see later changes.
|
|
252
|
+
*/
|
|
253
|
+
async withWindows(size, work) {
|
|
254
|
+
if (this.windows)
|
|
255
|
+
return work();
|
|
256
|
+
this.windows = new Map();
|
|
257
|
+
this.windowSize = Math.min(Math.max(size, 2), SqlViewNavigator.MAX_WINDOW);
|
|
258
|
+
try {
|
|
259
|
+
return await work();
|
|
260
|
+
}
|
|
261
|
+
finally {
|
|
262
|
+
this.windows = null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/** The child at a 1-based sibling index. */
|
|
266
|
+
async childAt(parent, siblingIndex) {
|
|
267
|
+
if (siblingIndex < 1 || siblingIndex > parent.childCount)
|
|
268
|
+
return null;
|
|
269
|
+
const t = SqlViewIndex_1.SQL_VIEW_TABLES;
|
|
270
|
+
if (parent.childrenAreCategories) {
|
|
271
|
+
const raw = (await this.sql.get(`SELECT * FROM ${t.cat} WHERE view_id = ? AND parent_key = ? AND cat_key <> '' ORDER BY cat_key LIMIT 1 OFFSET ?`, [this.viewId, parent.key, siblingIndex - 1]));
|
|
272
|
+
return raw ? this.categoryEntry(raw, parent, siblingIndex) : null;
|
|
273
|
+
}
|
|
274
|
+
const raw = (await this.sql.get(`SELECT cat_key, sort_key, origin, doc_id, cols, key_id FROM ${t.row} WHERE view_id = ? AND cat_key = ? ORDER BY sort_key LIMIT 1 OFFSET ?`, [this.viewId, parent.key, siblingIndex - 1]));
|
|
275
|
+
return raw ? this.documentEntry(raw, parent, siblingIndex) : null;
|
|
276
|
+
}
|
|
277
|
+
async allChildren(parent) {
|
|
278
|
+
const t = SqlViewIndex_1.SQL_VIEW_TABLES;
|
|
279
|
+
if (parent.childrenAreCategories) {
|
|
280
|
+
const rows = (await this.sql.all(`SELECT * FROM ${t.cat} WHERE view_id = ? AND parent_key = ? AND cat_key <> '' ORDER BY cat_key`, [this.viewId, parent.key]));
|
|
281
|
+
return rows.map((raw, i) => this.categoryEntry(raw, parent, i + 1));
|
|
282
|
+
}
|
|
283
|
+
const rows = (await this.sql.all(`SELECT cat_key, sort_key, origin, doc_id, cols, key_id FROM ${t.row} WHERE view_id = ? AND cat_key = ? ORDER BY sort_key`, [this.viewId, parent.key]));
|
|
284
|
+
return rows.map((raw, i) => this.documentEntry(raw, parent, i + 1));
|
|
285
|
+
}
|
|
286
|
+
async asParent(entry) {
|
|
287
|
+
if (entry.isDocument())
|
|
288
|
+
return { key: "\u0000doc", entry, childrenAreCategories: false, childCount: 0 };
|
|
289
|
+
return {
|
|
290
|
+
key: entry.catKey,
|
|
291
|
+
entry,
|
|
292
|
+
childrenAreCategories: entry.getChildCategoryCount() > 0,
|
|
293
|
+
childCount: entry.getChildCount(),
|
|
294
|
+
};
|
|
295
|
+
}
|
|
296
|
+
// ===== Visibility, expansion, selection =====
|
|
297
|
+
isVisibleRaw(entry) {
|
|
298
|
+
return !this.accessCheck || this.accessCheck.isVisible(entry);
|
|
299
|
+
}
|
|
300
|
+
isVisible(entry) {
|
|
301
|
+
if (!this.isVisibleRaw(entry))
|
|
302
|
+
return false;
|
|
303
|
+
if (this.dontShowEmptyCategories && entry.isCategory() && entry.getDescendantDocumentCount() === 0)
|
|
304
|
+
return false;
|
|
305
|
+
return true;
|
|
306
|
+
}
|
|
307
|
+
matchesType(entry) {
|
|
308
|
+
return entry.isCategory() ? this.withCategories : this.withDocuments;
|
|
309
|
+
}
|
|
310
|
+
isExpanded(entry) {
|
|
311
|
+
if (this.expandLevel > 0 && entry.getLevel() <= this.expandLevel)
|
|
312
|
+
return true;
|
|
313
|
+
return this.isExpandedByDocId(entry.origin, entry.docId);
|
|
314
|
+
}
|
|
315
|
+
isExpandedByDocId(origin, docId) {
|
|
316
|
+
const key = (0, types_1.scopedDocIdKey)((0, types_1.createScopedDocId)(origin, docId));
|
|
317
|
+
return this.expandAllByDefault ? !this.expandedOrCollapsedEntries.has(key) : this.expandedOrCollapsedEntries.has(key);
|
|
318
|
+
}
|
|
319
|
+
// ===== Stack helpers =====
|
|
320
|
+
async ensureStack() {
|
|
321
|
+
if (this.stack.length === 0)
|
|
322
|
+
this.stack = [{ parent: await this.topNode(), current: null, atEnd: false }];
|
|
323
|
+
if (this.pendingParentSelections.length > 0) {
|
|
324
|
+
const pending = this.pendingParentSelections;
|
|
325
|
+
this.pendingParentSelections = [];
|
|
326
|
+
for (const [origin, docId] of pending)
|
|
327
|
+
await this.selectParentCategoriesOf(origin, docId);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
currentRaw() {
|
|
331
|
+
const level = this.stack[this.stack.length - 1];
|
|
332
|
+
return level ? level.current : null;
|
|
333
|
+
}
|
|
334
|
+
currentFiltered() {
|
|
335
|
+
const entry = this.currentRaw();
|
|
336
|
+
if (!entry || !this.isVisible(entry) || !this.matchesType(entry))
|
|
337
|
+
return null;
|
|
338
|
+
return entry;
|
|
339
|
+
}
|
|
340
|
+
/** Next sibling with `isVisibleRaw` (for gotoNext). */
|
|
341
|
+
async nextSiblingRaw() {
|
|
342
|
+
const level = this.stack[this.stack.length - 1];
|
|
343
|
+
if (!level || level.atEnd)
|
|
344
|
+
return false;
|
|
345
|
+
let candidate = await this.step(level.parent, level.current, true);
|
|
346
|
+
while (candidate && !this.isVisibleRaw(candidate))
|
|
347
|
+
candidate = await this.step(level.parent, candidate, true);
|
|
348
|
+
if (!candidate)
|
|
349
|
+
return false;
|
|
350
|
+
level.current = candidate;
|
|
351
|
+
return true;
|
|
352
|
+
}
|
|
353
|
+
async siblingVisible(forward) {
|
|
354
|
+
const level = this.stack[this.stack.length - 1];
|
|
355
|
+
if (!level)
|
|
356
|
+
return false;
|
|
357
|
+
if (forward && level.atEnd)
|
|
358
|
+
return false;
|
|
359
|
+
if (!forward && !level.current)
|
|
360
|
+
return false;
|
|
361
|
+
let candidate = await this.step(level.parent, level.current, forward);
|
|
362
|
+
while (candidate && !this.isVisible(candidate))
|
|
363
|
+
candidate = await this.step(level.parent, candidate, forward);
|
|
364
|
+
if (!candidate)
|
|
365
|
+
return false;
|
|
366
|
+
level.current = candidate;
|
|
367
|
+
return true;
|
|
368
|
+
}
|
|
369
|
+
async deepestExpandedDescendant(entry) {
|
|
370
|
+
if (entry.getChildCount() > 0 && this.isExpanded(entry)) {
|
|
371
|
+
const parent = await this.asParent(entry);
|
|
372
|
+
let candidate = await this.step(parent, null, false);
|
|
373
|
+
while (candidate && !this.isVisible(candidate))
|
|
374
|
+
candidate = await this.step(parent, candidate, false);
|
|
375
|
+
if (candidate) {
|
|
376
|
+
this.stack.push({ parent, current: candidate, atEnd: false });
|
|
377
|
+
await this.deepestExpandedDescendant(candidate);
|
|
378
|
+
}
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
// ===== Navigation (the tree cursor's rules) =====
|
|
382
|
+
async getCurrentEntry() {
|
|
383
|
+
await this.ensureStack();
|
|
384
|
+
return this.currentFiltered();
|
|
385
|
+
}
|
|
386
|
+
async gotoFirst() {
|
|
387
|
+
await this.ensureStack();
|
|
388
|
+
this.stack.length = 1;
|
|
389
|
+
const level = this.stack[0];
|
|
390
|
+
level.current = null;
|
|
391
|
+
level.atEnd = false;
|
|
392
|
+
let candidate = await this.step(level.parent, null, true);
|
|
393
|
+
while (candidate) {
|
|
394
|
+
level.current = candidate;
|
|
395
|
+
if (this.isVisibleRaw(candidate)) {
|
|
396
|
+
if (this.currentFiltered())
|
|
397
|
+
return true;
|
|
398
|
+
return this.gotoNext();
|
|
399
|
+
}
|
|
400
|
+
candidate = await this.step(level.parent, candidate, true);
|
|
401
|
+
}
|
|
402
|
+
return false;
|
|
403
|
+
}
|
|
404
|
+
async gotoLast() {
|
|
405
|
+
await this.ensureStack();
|
|
406
|
+
this.stack.length = 1;
|
|
407
|
+
const level = this.stack[0];
|
|
408
|
+
level.atEnd = false;
|
|
409
|
+
let candidate = await this.step(level.parent, null, false);
|
|
410
|
+
while (candidate) {
|
|
411
|
+
level.current = candidate;
|
|
412
|
+
if (this.isVisible(candidate)) {
|
|
413
|
+
await this.deepestExpandedDescendant(candidate);
|
|
414
|
+
return true;
|
|
415
|
+
}
|
|
416
|
+
candidate = await this.step(level.parent, candidate, false);
|
|
417
|
+
}
|
|
418
|
+
return false;
|
|
419
|
+
}
|
|
420
|
+
async gotoNext() {
|
|
421
|
+
await this.ensureStack();
|
|
422
|
+
// Iterative form of the tree cursor's recursion.
|
|
423
|
+
for (;;) {
|
|
424
|
+
const current = this.currentRaw();
|
|
425
|
+
if (!current) {
|
|
426
|
+
if (await this.siblingVisible(true)) {
|
|
427
|
+
if (this.currentFiltered())
|
|
428
|
+
return true;
|
|
429
|
+
continue;
|
|
430
|
+
}
|
|
431
|
+
return false;
|
|
432
|
+
}
|
|
433
|
+
if (this.isExpanded(current) && current.getChildCount() > 0) {
|
|
434
|
+
const parent = await this.asParent(current);
|
|
435
|
+
let child = await this.step(parent, null, true);
|
|
436
|
+
while (child && !this.isVisibleRaw(child))
|
|
437
|
+
child = await this.step(parent, child, true);
|
|
438
|
+
if (child) {
|
|
439
|
+
this.stack.push({ parent, current: child, atEnd: false });
|
|
440
|
+
if (this.currentFiltered())
|
|
441
|
+
return true;
|
|
442
|
+
continue;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
if (await this.nextSiblingRaw()) {
|
|
446
|
+
if (this.currentFiltered())
|
|
447
|
+
return true;
|
|
448
|
+
continue;
|
|
449
|
+
}
|
|
450
|
+
let moved = false;
|
|
451
|
+
while (this.stack.length > 1) {
|
|
452
|
+
this.stack.pop();
|
|
453
|
+
if (await this.nextSiblingRaw()) {
|
|
454
|
+
moved = true;
|
|
455
|
+
break;
|
|
456
|
+
}
|
|
457
|
+
}
|
|
458
|
+
if (!moved)
|
|
459
|
+
return false;
|
|
460
|
+
if (this.currentFiltered())
|
|
461
|
+
return true;
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
async gotoPrev() {
|
|
465
|
+
await this.ensureStack();
|
|
466
|
+
if (!this.currentFiltered())
|
|
467
|
+
return false;
|
|
468
|
+
if (await this.siblingVisible(false)) {
|
|
469
|
+
const previous = this.currentFiltered();
|
|
470
|
+
if (previous)
|
|
471
|
+
await this.deepestExpandedDescendant(previous);
|
|
472
|
+
return true;
|
|
473
|
+
}
|
|
474
|
+
return this.gotoParent();
|
|
475
|
+
}
|
|
476
|
+
async gotoNextSibling() {
|
|
477
|
+
await this.ensureStack();
|
|
478
|
+
return this.siblingVisible(true);
|
|
479
|
+
}
|
|
480
|
+
async gotoPrevSibling() {
|
|
481
|
+
await this.ensureStack();
|
|
482
|
+
return this.siblingVisible(false);
|
|
483
|
+
}
|
|
484
|
+
async gotoParent() {
|
|
485
|
+
await this.ensureStack();
|
|
486
|
+
if (this.stack.length > 1) {
|
|
487
|
+
this.stack.pop();
|
|
488
|
+
return true;
|
|
489
|
+
}
|
|
490
|
+
return false;
|
|
491
|
+
}
|
|
492
|
+
async gotoChild(first) {
|
|
493
|
+
await this.ensureStack();
|
|
494
|
+
const current = this.currentFiltered();
|
|
495
|
+
if (!current)
|
|
496
|
+
return false;
|
|
497
|
+
const parent = await this.asParent(current);
|
|
498
|
+
if (parent.childCount === 0)
|
|
499
|
+
return false;
|
|
500
|
+
let candidate = await this.step(parent, null, first);
|
|
501
|
+
while (candidate && !this.isVisible(candidate))
|
|
502
|
+
candidate = await this.step(parent, candidate, first);
|
|
503
|
+
if (!candidate)
|
|
504
|
+
return false;
|
|
505
|
+
this.stack.push({ parent, current: candidate, atEnd: false });
|
|
506
|
+
return true;
|
|
507
|
+
}
|
|
508
|
+
async gotoFirstChild() {
|
|
509
|
+
return this.gotoChild(true);
|
|
510
|
+
}
|
|
511
|
+
async gotoLastChild() {
|
|
512
|
+
return this.gotoChild(false);
|
|
513
|
+
}
|
|
514
|
+
async gotoNextSelected() {
|
|
515
|
+
while (await this.gotoNext()) {
|
|
516
|
+
const entry = this.currentFiltered();
|
|
517
|
+
if (entry && this.isSelected(entry.origin, entry.docId))
|
|
518
|
+
return true;
|
|
519
|
+
}
|
|
520
|
+
return false;
|
|
521
|
+
}
|
|
522
|
+
async gotoPrevSelected() {
|
|
523
|
+
while (await this.gotoPrev()) {
|
|
524
|
+
const entry = this.currentFiltered();
|
|
525
|
+
if (entry && this.isSelected(entry.origin, entry.docId))
|
|
526
|
+
return true;
|
|
527
|
+
}
|
|
528
|
+
return false;
|
|
529
|
+
}
|
|
530
|
+
async gotoPos(posStr) {
|
|
531
|
+
return this.gotoPosArray(posStr.split(".").map((part) => parseInt(part, 10)));
|
|
532
|
+
}
|
|
533
|
+
async gotoPosArray(pos) {
|
|
534
|
+
this.stack = [{ parent: await this.topNode(), current: null, atEnd: false }];
|
|
535
|
+
let level = this.stack[0];
|
|
536
|
+
for (let i = 0; i < pos.length; i++) {
|
|
537
|
+
const child = await this.childAt(level.parent, pos[i]);
|
|
538
|
+
if (!child || !this.isVisible(child))
|
|
539
|
+
return false;
|
|
540
|
+
level.current = child;
|
|
541
|
+
if (i < pos.length - 1) {
|
|
542
|
+
const next = { parent: await this.asParent(child), current: null, atEnd: false };
|
|
543
|
+
this.stack.push(next);
|
|
544
|
+
level = next;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
return true;
|
|
548
|
+
}
|
|
549
|
+
async getPos(posStr) {
|
|
550
|
+
return this.getPosArray(posStr.split(".").map((part) => parseInt(part, 10)));
|
|
551
|
+
}
|
|
552
|
+
async getPosArray(pos) {
|
|
553
|
+
const saved = this.stack.map((level) => ({ ...level }));
|
|
554
|
+
const found = await this.gotoPosArray(pos);
|
|
555
|
+
const entry = found ? this.currentFiltered() : null;
|
|
556
|
+
this.stack = saved;
|
|
557
|
+
return entry;
|
|
558
|
+
}
|
|
559
|
+
async findCategoryEntry(category) {
|
|
560
|
+
return this.findCategoryEntryByParts(category.split("\\"));
|
|
561
|
+
}
|
|
562
|
+
async findCategoryEntryByParts(categoryParts) {
|
|
563
|
+
let parent = await this.topNode();
|
|
564
|
+
let found = null;
|
|
565
|
+
for (const part of categoryParts) {
|
|
566
|
+
if (!parent.childrenAreCategories)
|
|
567
|
+
return null;
|
|
568
|
+
// The tree compares String(part) with the category values (compareValues).
|
|
569
|
+
const text = String(part);
|
|
570
|
+
let match = null;
|
|
571
|
+
for (const child of await this.allChildren(parent)) {
|
|
572
|
+
const value = child.getCategoryValue();
|
|
573
|
+
const both = [value, text].map((v) => (0, viewSortKeys_1.viewCategoryComponent)(v, false, this.index.layout.collation));
|
|
574
|
+
if (this.isVisible(child) && value !== null && value !== undefined && typeof value === "string" && both[0] === both[1]) {
|
|
575
|
+
match = child;
|
|
576
|
+
break;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
if (!match)
|
|
580
|
+
return null;
|
|
581
|
+
found = match;
|
|
582
|
+
parent = await this.asParent(match);
|
|
583
|
+
}
|
|
584
|
+
return found;
|
|
585
|
+
}
|
|
586
|
+
async readEntries(options) {
|
|
587
|
+
const out = [];
|
|
588
|
+
if (options.count <= 0)
|
|
589
|
+
return out;
|
|
590
|
+
if (this.sql.callProcedure && !this.accessCheck && !SqlViewNavigator.noProcedure.has(this.sql)) {
|
|
591
|
+
// Over a bridge: read the page where SQLite runs, in one round trip.
|
|
592
|
+
await this.ensureStack();
|
|
593
|
+
try {
|
|
594
|
+
return await this.readEntriesWhereSqliteRuns(options);
|
|
595
|
+
}
|
|
596
|
+
catch (error) {
|
|
597
|
+
if (!/unknown (op|procedure)/.test(error instanceof Error ? error.message : String(error)))
|
|
598
|
+
throw error;
|
|
599
|
+
// An older server: read here from now on.
|
|
600
|
+
SqlViewNavigator.noProcedure.add(this.sql);
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
const step = options.backwards
|
|
604
|
+
? options.selectedOnly
|
|
605
|
+
? () => this.gotoPrevSelected()
|
|
606
|
+
: () => this.gotoPrev()
|
|
607
|
+
: options.selectedOnly
|
|
608
|
+
? () => this.gotoNextSelected()
|
|
609
|
+
: () => this.gotoNext();
|
|
610
|
+
const take = () => {
|
|
611
|
+
const entry = this.currentFiltered();
|
|
612
|
+
if (entry && (!options.selectedOnly || this.isSelected(entry.origin, entry.docId)))
|
|
613
|
+
out.push(entry);
|
|
614
|
+
};
|
|
615
|
+
await this.ensureStack();
|
|
616
|
+
return this.withWindows(options.count + 1, async () => {
|
|
617
|
+
if (options.includeCurrent)
|
|
618
|
+
take();
|
|
619
|
+
while (out.length < options.count && (await step()))
|
|
620
|
+
take();
|
|
621
|
+
return out;
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
async *entriesForward(selectedOnly = types_1.SelectedOnly.NO) {
|
|
625
|
+
if (!(await this.gotoFirst()))
|
|
626
|
+
return;
|
|
627
|
+
do {
|
|
628
|
+
const entry = this.currentFiltered();
|
|
629
|
+
if (entry && (selectedOnly === types_1.SelectedOnly.NO || this.isSelected(entry.origin, entry.docId)))
|
|
630
|
+
yield entry;
|
|
631
|
+
} while (selectedOnly === types_1.SelectedOnly.YES ? await this.gotoNextSelected() : await this.gotoNext());
|
|
632
|
+
}
|
|
633
|
+
async *entriesBackward(selectedOnly = types_1.SelectedOnly.NO) {
|
|
634
|
+
if (!(await this.gotoLast()))
|
|
635
|
+
return;
|
|
636
|
+
do {
|
|
637
|
+
const entry = this.currentFiltered();
|
|
638
|
+
if (entry && (selectedOnly === types_1.SelectedOnly.NO || this.isSelected(entry.origin, entry.docId)))
|
|
639
|
+
yield entry;
|
|
640
|
+
} while (selectedOnly === types_1.SelectedOnly.YES ? await this.gotoPrevSelected() : await this.gotoPrev());
|
|
641
|
+
}
|
|
642
|
+
// ===== Reading where SQLite runs =====
|
|
643
|
+
async readEntriesWhereSqliteRuns(options) {
|
|
644
|
+
const request = {
|
|
645
|
+
viewId: this.viewId,
|
|
646
|
+
layout: this.index.layout,
|
|
647
|
+
options: {
|
|
648
|
+
withCategories: this.withCategories,
|
|
649
|
+
withDocuments: this.withDocuments,
|
|
650
|
+
dontShowEmptyCategories: this.dontShowEmptyCategories,
|
|
651
|
+
},
|
|
652
|
+
state: this.exportState(),
|
|
653
|
+
read: {
|
|
654
|
+
count: options.count,
|
|
655
|
+
backwards: options.backwards,
|
|
656
|
+
selectedOnly: options.selectedOnly,
|
|
657
|
+
includeCurrent: options.includeCurrent,
|
|
658
|
+
},
|
|
659
|
+
};
|
|
660
|
+
const reply = (0, SqlViewIndex_1.decodeViewValues)(await this.sql.callProcedure(exports.SQL_VIEW_READ_ENTRIES, (0, SqlViewIndex_1.encodeViewValues)({ request })));
|
|
661
|
+
this.importState(reply.state);
|
|
662
|
+
return reply.entries.map((args) => SqlViewEntry.fromArgs(args));
|
|
663
|
+
}
|
|
664
|
+
/** What a navigator elsewhere needs to move on from here: the path, expansion and selection. */
|
|
665
|
+
exportState() {
|
|
666
|
+
const parent = (node) => ({ ...node, entry: node.entry ? node.entry.toArgs() : null });
|
|
667
|
+
return {
|
|
668
|
+
top: this.top ? parent(this.top) : null,
|
|
669
|
+
stack: this.stack.map((level) => ({
|
|
670
|
+
parent: parent(level.parent),
|
|
671
|
+
current: level.current ? level.current.toArgs() : null,
|
|
672
|
+
atEnd: level.atEnd,
|
|
673
|
+
})),
|
|
674
|
+
selectAll: this.selectAll,
|
|
675
|
+
selected: [...this.selectedOrDeselectedEntries],
|
|
676
|
+
expandAll: this.expandAllByDefault,
|
|
677
|
+
expandLevel: this.expandLevel,
|
|
678
|
+
expanded: [...this.expandedOrCollapsedEntries],
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
/** Takes over the path (and expansion and selection) of a navigator elsewhere. */
|
|
682
|
+
importState(state) {
|
|
683
|
+
const parent = (node) => ({
|
|
684
|
+
...node,
|
|
685
|
+
entry: node.entry ? SqlViewEntry.fromArgs(node.entry) : null,
|
|
686
|
+
});
|
|
687
|
+
this.top = state.top ? parent(state.top) : null;
|
|
688
|
+
this.stack = state.stack.map((level) => ({
|
|
689
|
+
parent: level.parent.key === "" && this.top ? this.top : parent(level.parent),
|
|
690
|
+
current: level.current ? SqlViewEntry.fromArgs(level.current) : null,
|
|
691
|
+
atEnd: level.atEnd,
|
|
692
|
+
}));
|
|
693
|
+
this.selectAll = state.selectAll;
|
|
694
|
+
this.selectedOrDeselectedEntries = new Set(state.selected);
|
|
695
|
+
this.expandAllByDefault = state.expandAll;
|
|
696
|
+
this.expandLevel = state.expandLevel;
|
|
697
|
+
this.expandedOrCollapsedEntries = new Set(state.expanded);
|
|
698
|
+
}
|
|
699
|
+
/** The server side of SQL_VIEW_READ_ENTRIES (see sqlViewReadEntriesProcedure). */
|
|
700
|
+
static async readEntriesFor(executor, request) {
|
|
701
|
+
const index = new SqlViewIndex_1.SqlViewIndex(executor, request.viewId, request.layout);
|
|
702
|
+
const navigator = new SqlViewNavigator(index, executor, request.options);
|
|
703
|
+
navigator.importState(request.state);
|
|
704
|
+
const entries = (await navigator.readEntries(request.read));
|
|
705
|
+
return (0, SqlViewIndex_1.encodeViewValues)({ state: navigator.exportState(), entries: entries.map((entry) => entry.toArgs()) });
|
|
706
|
+
}
|
|
707
|
+
// ===== Children and documents in view order =====
|
|
708
|
+
async parentFor(entry) {
|
|
709
|
+
if (!entry)
|
|
710
|
+
return this.topNode();
|
|
711
|
+
if (entry instanceof SqlViewEntry)
|
|
712
|
+
return this.asParent(entry);
|
|
713
|
+
const found = await this.getPos(entry.getPositionStr());
|
|
714
|
+
if (!found)
|
|
715
|
+
throw new Error(`View entry ${entry.getPositionStr()} is not in this view`);
|
|
716
|
+
return this.asParent(found);
|
|
717
|
+
}
|
|
718
|
+
async childEntries(entry, descending = false) {
|
|
719
|
+
const children = (await this.allChildren(await this.parentFor(entry))).filter((child) => this.isVisible(child));
|
|
720
|
+
return descending ? children.reverse() : children;
|
|
721
|
+
}
|
|
722
|
+
async childDocuments(entry, descending = false) {
|
|
723
|
+
return (await this.childEntries(entry, descending)).filter((child) => child.isDocument());
|
|
724
|
+
}
|
|
725
|
+
async childCategories(entry, descending = false) {
|
|
726
|
+
return (await this.childEntries(entry, descending)).filter((child) => child.isCategory());
|
|
727
|
+
}
|
|
728
|
+
/** Rows of documents sorted by view position (category path, then sort key). */
|
|
729
|
+
async getSortedDocIdsScoped(scopedDocIds) {
|
|
730
|
+
const t = SqlViewIndex_1.SQL_VIEW_TABLES;
|
|
731
|
+
const found = [];
|
|
732
|
+
for (const scoped of scopedDocIds) {
|
|
733
|
+
const rows = (await this.sql.all(`SELECT cat_key, sort_key FROM ${t.row} WHERE view_id = ? AND origin = ? AND doc_id = ?`, [this.viewId, scoped.origin, scoped.docId]));
|
|
734
|
+
for (const row of rows)
|
|
735
|
+
found.push({ catKey: row.cat_key, sortKey: row.sort_key, id: scoped });
|
|
736
|
+
}
|
|
737
|
+
found.sort((a, b) => a.catKey < b.catKey ? -1 : a.catKey > b.catKey ? 1 : a.sortKey < b.sortKey ? -1 : a.sortKey > b.sortKey ? 1 : 0);
|
|
738
|
+
const seen = new Set();
|
|
739
|
+
const out = [];
|
|
740
|
+
for (const { id } of found) {
|
|
741
|
+
const key = (0, types_1.scopedDocIdKey)(id);
|
|
742
|
+
if (!seen.has(key)) {
|
|
743
|
+
seen.add(key);
|
|
744
|
+
out.push({ origin: id.origin, docId: id.docId });
|
|
745
|
+
}
|
|
746
|
+
}
|
|
747
|
+
return out;
|
|
748
|
+
}
|
|
749
|
+
async getSortedDocIds(origin, docIds) {
|
|
750
|
+
const scoped = new Set([...docIds].map((docId) => ({ origin, docId })));
|
|
751
|
+
return (await this.getSortedDocIdsScoped(scoped)).map((id) => id.docId);
|
|
752
|
+
}
|
|
753
|
+
// ===== Selection =====
|
|
754
|
+
/**
|
|
755
|
+
* Selects an entry. With `selectParentCategories`, the categories above all
|
|
756
|
+
* rows of the document are selected too; they are looked up in SQLite
|
|
757
|
+
* before the next navigation call (selection is synchronous state).
|
|
758
|
+
*/
|
|
759
|
+
select(origin, docId, selectParentCategories = false) {
|
|
760
|
+
const key = (0, types_1.scopedDocIdKey)((0, types_1.createScopedDocId)(origin, docId));
|
|
761
|
+
if (this.selectAll)
|
|
762
|
+
this.selectedOrDeselectedEntries.delete(key);
|
|
763
|
+
else
|
|
764
|
+
this.selectedOrDeselectedEntries.add(key);
|
|
765
|
+
if (selectParentCategories)
|
|
766
|
+
this.pendingParentSelections.push([origin, docId]);
|
|
767
|
+
return this;
|
|
768
|
+
}
|
|
769
|
+
async selectParentCategoriesOf(origin, docId) {
|
|
770
|
+
const rows = (await this.sql.all(`SELECT DISTINCT cat_key FROM ${SqlViewIndex_1.SQL_VIEW_TABLES.row} WHERE view_id = ? AND origin = ? AND doc_id = ?`, [this.viewId, origin, docId]));
|
|
771
|
+
for (const { cat_key } of rows) {
|
|
772
|
+
let key = cat_key;
|
|
773
|
+
while (key !== "") {
|
|
774
|
+
const category = (await this.sql.get(`SELECT cat_id, parent_key FROM ${SqlViewIndex_1.SQL_VIEW_TABLES.cat} WHERE view_id = ? AND cat_key = ?`, [this.viewId, key]));
|
|
775
|
+
if (!category)
|
|
776
|
+
break;
|
|
777
|
+
this.select(types_1.ORIGIN_VIRTUALVIEW, category.cat_id);
|
|
778
|
+
key = category.parent_key;
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
}
|
|
782
|
+
deselect(origin, docId) {
|
|
783
|
+
const key = (0, types_1.scopedDocIdKey)((0, types_1.createScopedDocId)(origin, docId));
|
|
784
|
+
if (this.selectAll)
|
|
785
|
+
this.selectedOrDeselectedEntries.add(key);
|
|
786
|
+
else
|
|
787
|
+
this.selectedOrDeselectedEntries.delete(key);
|
|
788
|
+
return this;
|
|
789
|
+
}
|
|
790
|
+
selectAllEntries() {
|
|
791
|
+
this.selectedOrDeselectedEntries.clear();
|
|
792
|
+
this.selectAll = true;
|
|
793
|
+
return this;
|
|
794
|
+
}
|
|
795
|
+
deselectAllEntries() {
|
|
796
|
+
this.selectedOrDeselectedEntries.clear();
|
|
797
|
+
this.selectAll = false;
|
|
798
|
+
return this;
|
|
799
|
+
}
|
|
800
|
+
isSelected(origin, docId) {
|
|
801
|
+
const key = (0, types_1.scopedDocIdKey)((0, types_1.createScopedDocId)(origin, docId));
|
|
802
|
+
return this.selectAll ? !this.selectedOrDeselectedEntries.has(key) : this.selectedOrDeselectedEntries.has(key);
|
|
803
|
+
}
|
|
804
|
+
isSelectAllByDefault() {
|
|
805
|
+
return this.selectAll;
|
|
806
|
+
}
|
|
807
|
+
isDeselectAllByDefault() {
|
|
808
|
+
return !this.selectAll;
|
|
809
|
+
}
|
|
810
|
+
getSelectedOrDeselectedEntries() {
|
|
811
|
+
return new Set(this.selectedOrDeselectedEntries);
|
|
812
|
+
}
|
|
813
|
+
setSelectedOrDeselectedEntries(entries) {
|
|
814
|
+
this.selectedOrDeselectedEntries = new Set(entries);
|
|
815
|
+
return this;
|
|
816
|
+
}
|
|
817
|
+
// ===== Expansion =====
|
|
818
|
+
expand(origin, docId) {
|
|
819
|
+
const key = (0, types_1.scopedDocIdKey)((0, types_1.createScopedDocId)(origin, docId));
|
|
820
|
+
if (!this.expandAllByDefault)
|
|
821
|
+
this.expandedOrCollapsedEntries.add(key);
|
|
822
|
+
else
|
|
823
|
+
this.expandedOrCollapsedEntries.delete(key);
|
|
824
|
+
return this;
|
|
825
|
+
}
|
|
826
|
+
collapse(origin, docId) {
|
|
827
|
+
const key = (0, types_1.scopedDocIdKey)((0, types_1.createScopedDocId)(origin, docId));
|
|
828
|
+
if (this.expandAllByDefault)
|
|
829
|
+
this.expandedOrCollapsedEntries.add(key);
|
|
830
|
+
else
|
|
831
|
+
this.expandedOrCollapsedEntries.delete(key);
|
|
832
|
+
return this;
|
|
833
|
+
}
|
|
834
|
+
collapseAll() {
|
|
835
|
+
this.expandedOrCollapsedEntries.clear();
|
|
836
|
+
this.expandAllByDefault = false;
|
|
837
|
+
return this;
|
|
838
|
+
}
|
|
839
|
+
expandAll() {
|
|
840
|
+
this.expandedOrCollapsedEntries.clear();
|
|
841
|
+
this.expandAllByDefault = true;
|
|
842
|
+
return this;
|
|
843
|
+
}
|
|
844
|
+
expandToLevel(level) {
|
|
845
|
+
this.expandLevel = level;
|
|
846
|
+
return this;
|
|
847
|
+
}
|
|
848
|
+
isExpandAllByDefault() {
|
|
849
|
+
return this.expandAllByDefault;
|
|
850
|
+
}
|
|
851
|
+
isCollapseAllByDefault() {
|
|
852
|
+
return !this.expandAllByDefault;
|
|
853
|
+
}
|
|
854
|
+
getExpandLevel() {
|
|
855
|
+
return this.expandLevel;
|
|
856
|
+
}
|
|
857
|
+
getExpandedOrCollapsedEntries() {
|
|
858
|
+
return new Set(this.expandedOrCollapsedEntries);
|
|
859
|
+
}
|
|
860
|
+
setExpandedOrCollapsedEntries(entries) {
|
|
861
|
+
this.expandedOrCollapsedEntries = new Set(entries);
|
|
862
|
+
return this;
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
exports.SqlViewNavigator = SqlViewNavigator;
|
|
866
|
+
SqlViewNavigator.MAX_WINDOW = 500;
|
|
867
|
+
/** Executors whose server has no `view.readEntries` procedure. */
|
|
868
|
+
SqlViewNavigator.noProcedure = new WeakSet();
|
|
869
|
+
/**
|
|
870
|
+
* SQL RPC procedure {@link SQL_VIEW_READ_ENTRIES}: reads a page of a view
|
|
871
|
+
* kept in SQLite on the server, with the local executor, so a page costs
|
|
872
|
+
* one round trip instead of one per query. Built into `SqlRpcServer`.
|
|
873
|
+
*/
|
|
874
|
+
async function sqlViewReadEntriesProcedure(executor, args) {
|
|
875
|
+
const { request } = (0, SqlViewIndex_1.decodeViewValues)(args);
|
|
876
|
+
return SqlViewNavigator.readEntriesFor(executor, request);
|
|
877
|
+
}
|
|
878
|
+
//# sourceMappingURL=SqlViewNavigator.js.map
|