@delali/narsil 0.3.1-next.131 → 0.3.1-next.141
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/adapters/filesystem.mjs +1 -1
- package/dist/adapters/indexeddb.mjs +1 -1
- package/dist/analysis-change-HQUT2Z3I.mjs +43 -0
- package/dist/analysis-change-entry-TZTG4AZB.mjs +1 -0
- package/dist/browser/analysis-change-JPV775LR.mjs +900 -0
- package/dist/browser/analysis-change-entry-7HR4LWL3.mjs +1 -0
- package/dist/browser/{chunk-UDGB2MWT.mjs → chunk-35B42UFM.mjs} +4 -0
- package/dist/browser/chunk-3ZBJYAS4.mjs +19 -0
- package/dist/browser/{chunk-2Y3CO4EQ.mjs → chunk-5DB2VDGI.mjs} +1149 -1853
- package/dist/browser/chunk-6RHTAVZX.mjs +656 -0
- package/dist/browser/chunk-CCOAOO73.mjs +86 -0
- package/dist/browser/{chunk-AIIJDWMG.mjs → chunk-D33A5VMU.mjs} +16 -16
- package/dist/browser/{chunk-VBBLTD7O.mjs → chunk-HKX2G6WH.mjs} +1520 -2159
- package/dist/browser/{chunk-NSCSR6MA.mjs → chunk-KTOK275N.mjs} +606 -830
- package/dist/browser/{chunk-D7WQF6DV.mjs → chunk-LVY2LI35.mjs} +2 -2
- package/dist/browser/chunk-MVM33ZUL.mjs +993 -0
- package/dist/browser/chunk-MWUK3DA5.mjs +2634 -0
- package/dist/browser/{chunk-VEGHFP2P.mjs → chunk-MZI3TEOW.mjs} +4 -2
- package/dist/browser/chunk-OIZS5KIX.mjs +6 -0
- package/dist/browser/chunk-R4WUDZQV.mjs +176 -0
- package/dist/browser/{chunk-M5UAAXGU.mjs → chunk-TCLEOZ4P.mjs} +3 -3
- package/dist/browser/chunk-VFYMPCOV.mjs +212 -0
- package/dist/browser/chunk-WCFCHIBB.mjs +177 -0
- package/dist/browser/chunk-XNWPP5NS.mjs +21 -0
- package/dist/browser/chunk-ZEV26AQ6.mjs +270 -0
- package/dist/browser/cold-module-MJMKNUYJ.mjs +745 -0
- package/dist/browser/{durability-wiring-FNZSRH4N.mjs → durability-wiring-SNOWIT6Z.mjs} +370 -95
- package/dist/browser/import-TLB2A5LJ.mjs +9 -0
- package/dist/browser/module-BUFNZMEU.mjs +12 -0
- package/dist/browser/{module-4HIMOQM5.mjs → module-BVP4TKZU.mjs} +397 -109
- package/dist/browser/{phrase-module-WESYVU7K.mjs → phrase-module-HGVBO24W.mjs} +1 -1
- package/dist/{restore-queue-JAMNRC6T.mjs → browser/restore-queue-ONYNYUDE.mjs} +8 -2
- package/dist/browser/{runtime-3X7ZJ5T3.mjs → runtime-VEUH674B.mjs} +2 -2
- package/dist/browser/{snapshot-V7VF6BN6.mjs → snapshot-QXBKHJAG.mjs} +135 -183
- package/dist/browser/{write-YIC2MKRY.mjs → write-WHFBDWQS.mjs} +64 -90
- package/dist/{capabilities-B6WxrWxt.d.ts → capabilities-BCdUwK_L.d.ts} +10 -3
- package/dist/chunk-46NZOPHS.mjs +21 -0
- package/dist/{chunk-JT3YZGCE.mjs → chunk-5WYVUPGD.mjs} +3 -188
- package/dist/chunk-6SNBQESJ.mjs +214 -0
- package/dist/{chunk-2C3WXBQS.mjs → chunk-74XB2SXI.mjs} +1 -1
- package/dist/chunk-7AUASRE7.mjs +653 -0
- package/dist/{chunk-NMXRXFGZ.mjs → chunk-AST3SHFA.mjs} +27 -25
- package/dist/chunk-BOJZGUS6.mjs +177 -0
- package/dist/{chunk-T4OFG4IQ.mjs → chunk-BRUUBXCB.mjs} +92 -17
- package/dist/{chunk-5LX6MOIB.mjs → chunk-C3YDYX6C.mjs} +1 -1
- package/dist/chunk-CZQ33DLW.mjs +78 -0
- package/dist/{chunk-4GDLFDPZ.mjs → chunk-DD2673TX.mjs} +3 -2
- package/dist/{chunk-EUCX5K74.mjs → chunk-DQNKVOJ2.mjs} +65 -65
- package/dist/{chunk-RNSC4NFI.mjs → chunk-EEBCDP36.mjs} +3 -1
- package/dist/chunk-FOCV4CBZ.mjs +87 -0
- package/dist/{chunk-HCGJKDZV.mjs → chunk-GLZHNSGB.mjs} +1 -1
- package/dist/chunk-GWOS5DYC.mjs +2637 -0
- package/dist/{chunk-PATMDIMN.mjs → chunk-HJJWY6PT.mjs} +1 -1
- package/dist/{chunk-CZ7ISLXA.mjs → chunk-HNHCNTVG.mjs} +4 -0
- package/dist/{chunk-Y2U5VVWX.mjs → chunk-ICW45TTU.mjs} +1 -1
- package/dist/{chunk-TGHNXN7W.mjs → chunk-IO5F7ZBE.mjs} +261 -948
- package/dist/{chunk-P36MUFEJ.mjs → chunk-JDDXV6UF.mjs} +2 -1
- package/dist/{chunk-7OIQ2SIO.mjs → chunk-KEIYVZUX.mjs} +5 -5
- package/dist/{chunk-PU6SKH7O.mjs → chunk-KGOXHLLZ.mjs} +7 -3
- package/dist/{chunk-WVRDRLIC.mjs → chunk-KZL72VTC.mjs} +70 -18
- package/dist/chunk-L76W3IZJ.mjs +193 -0
- package/dist/{chunk-2JWNJSNE.mjs → chunk-LA4WNDHL.mjs} +88 -801
- package/dist/{chunk-ZR4VM4A6.mjs → chunk-N37VMC62.mjs} +1 -18
- package/dist/{chunk-JJ6EUMXB.mjs → chunk-NJJCPFXN.mjs} +2 -2
- package/dist/chunk-NN33CZRE.mjs +86 -0
- package/dist/{chunk-2WBY4Y4M.mjs → chunk-NOT5QOHU.mjs} +1 -1
- package/dist/{chunk-D5TFRB5G.mjs → chunk-NPNK57CA.mjs} +1 -1
- package/dist/chunk-NRUXBYPH.mjs +121 -0
- package/dist/{chunk-6SSVIW7Y.mjs → chunk-O4LJQI4G.mjs} +31 -5
- package/dist/{chunk-UJWGQIXU.mjs → chunk-QHPYS427.mjs} +1 -0
- package/dist/{chunk-F6AAXI7T.mjs → chunk-RHKRTYGO.mjs} +13 -4
- package/dist/{chunk-2SEL6SBS.mjs → chunk-RKAVHNRW.mjs} +2 -2
- package/dist/{chunk-RCMNL7RI.mjs → chunk-SBMAWTRG.mjs} +41 -4
- package/dist/chunk-SRJBFG2T.mjs +881 -0
- package/dist/{chunk-X5CNUKD5.mjs → chunk-TNBIK2LQ.mjs} +1663 -1652
- package/dist/{chunk-NNO7CO3A.mjs → chunk-UIKN2OTQ.mjs} +1 -1
- package/dist/chunk-UUZCQQG2.mjs +209 -0
- package/dist/{chunk-6M6SDH3S.mjs → chunk-VEFN6X6Q.mjs} +8 -8
- package/dist/chunk-VJEGOELZ.mjs +177 -0
- package/dist/chunk-W2COJ5AH.mjs +148 -0
- package/dist/{chunk-RWESFSJI.mjs → chunk-WJGO5PGV.mjs} +317 -174
- package/dist/{chunk-7X5JST5L.mjs → chunk-WNBRW4Q2.mjs} +16 -2
- package/dist/{chunk-SZGRYEIC.mjs → chunk-XS2VM7UE.mjs} +3 -3
- package/dist/{chunk-Y4BVTQ53.mjs → chunk-XZ3SRNDJ.mjs} +25 -64
- package/dist/{chunk-K77FJXKS.mjs → chunk-YHUTF6EA.mjs} +474 -93
- package/dist/{chunk-OGLV4I3L.mjs → chunk-YLNSJQLX.mjs} +12 -7
- package/dist/{chunk-SOHMMNCH.mjs → chunk-Z4HFMYNX.mjs} +32 -27
- package/dist/{chunk-5W7AEVTH.mjs → chunk-ZMBFWQXN.mjs} +2 -2
- package/dist/{chunk-2MKX2SCO.mjs → chunk-ZPWS2SKJ.mjs} +297 -274
- package/dist/{chunk-7XY6VD7B.mjs → chunk-ZSWHPKGM.mjs} +1 -1
- package/dist/{client-gNAfaHcE.d.ts → client-B12YFALP.d.ts} +18 -3
- package/dist/client.d.ts +7 -7
- package/dist/client.mjs +11 -1
- package/dist/cold-module-4X7WJCFM.mjs +783 -0
- package/dist/{config-D1cDv3sE.d.ts → config-BhZxIeb1.d.ts} +10 -1
- package/dist/{config-CmYJO34X.d.ts → config-CIG5yDyg.d.ts} +2 -2
- package/dist/{direct-executor-HQ4QVDX7.mjs → direct-executor-7XOQ7VQP.mjs} +14 -14
- package/dist/distribution/coordinator/etcd.d.ts +2 -2
- package/dist/distribution/coordinator/etcd.mjs +1 -1
- package/dist/distribution/coordinator/in-memory.d.ts +2 -2
- package/dist/distribution/transport/grpc.mjs +1 -1
- package/dist/distribution.d.ts +24 -8
- package/dist/distribution.mjs +982 -211
- package/dist/{durability-wiring-S7OX2MAI.mjs → durability-wiring-324D2DDD.mjs} +362 -94
- package/dist/embeddings/openai.mjs +2 -2
- package/dist/{engine-m_Ddr1bz.d.ts → engine-CtzbHOmG.d.ts} +64 -7
- package/dist/import-OOBUW3RR.mjs +44 -0
- package/dist/{index-HYnqS1SE.d.ts → index-DsEurhSl.d.ts} +1 -1
- package/dist/index.browser.mjs +5 -5
- package/dist/index.d.ts +6 -6
- package/dist/index.mjs +22 -22
- package/dist/{long-query-thread-BSL25PD6.mjs → long-query-thread-4SW7RWDE.mjs} +15 -15
- package/dist/{module-2E4XYSGT.mjs → module-DXSJ5LT2.mjs} +378 -87
- package/dist/module-IYPLWHGC.mjs +48 -0
- package/dist/paged-partition-archive-HFP45PLU.mjs +269 -0
- package/dist/partial-words.mjs +5 -5
- package/dist/pattern.mjs +3 -3
- package/dist/persistence/durability/checkpoint-worker.d.ts +16 -1
- package/dist/persistence/durability/checkpoint-worker.mjs +28 -26
- package/dist/{phrase-module-JERF2IDG.mjs → phrase-module-5QHBD2VJ.mjs} +1 -1
- package/dist/react.d.ts +9 -9
- package/dist/react.mjs +4 -0
- package/dist/{rerank-DMMT4FZA.mjs → rerank-KTLC3E6N.mjs} +5 -5
- package/dist/rerankers/http.mjs +2 -2
- package/dist/{browser/restore-queue-GEZ54CA5.mjs → restore-queue-FEYAC4SB.mjs} +8 -2
- package/dist/{runtime-7MLQ7J64.mjs → runtime-DYV75DCB.mjs} +6 -6
- package/dist/{schema-BTXbHCqA.d.ts → schema-B-Runv0n.d.ts} +7 -0
- package/dist/server.d.ts +12 -10
- package/dist/server.mjs +54 -21
- package/dist/{snapshot-DOQLGGEX.mjs → snapshot-CGM65VCD.mjs} +101 -197
- package/dist/synonyms.mjs +6 -4
- package/dist/{tasks-Co8HfRD7.d.ts → tasks-CjRJRGTb.d.ts} +26 -8
- package/dist/{thread-SKO7CN3R.mjs → thread-KGNAIKPD.mjs} +14 -14
- package/dist/vector/search-worker.d.ts +1 -1
- package/dist/vector/search-worker.mjs +6 -5
- package/dist/workers/entry.mjs +8 -8
- package/dist/{write-ZKR7SNRT.mjs → write-2DZPEQL2.mjs} +82 -107
- package/openapi.json +114 -6
- package/package.json +12 -8
|
@@ -0,0 +1,2637 @@
|
|
|
1
|
+
import { encodeDocumentJson } from './chunk-NN33CZRE.mjs';
|
|
2
|
+
import { loadFailed, sectionNamed, PAGED_SEGMENT_ENVELOPE_VERSION, encodePagedSegmentHead, SECTION_ALIGNMENT_BYTES, SEGMENT_PAGE_BYTES, PAYLOAD_PREFIX_BYTES, decodePagedSegmentHead, BLOCK_SUMMARY_BYTES, SMALLEST_BLOCK_POSTINGS, DOCUMENT_BLOCK_BYTES, decodeSections, DOCUMENT_BLOCK_MIN_RAW_BYTES, ID_SAMPLE_INTERVAL, POSITION_WIDTH_BYTES, POSTING_WIDTH_BYTES, RECORD_PAGE_BYTES, MAX_DOCUMENT_BLOCK_RAW_BYTES } from './chunk-W2COJ5AH.mjs';
|
|
3
|
+
import { createVectorFileReader } from './chunk-FOCV4CBZ.mjs';
|
|
4
|
+
import { writeSegmentsOf, MessagePackChunks, writeSurfaceForms, writeFieldLengths, writeValueColumns, writeBooleanColumns, writeEnumColumns, writeGeopointColumns, writePatternColumns, writePartialWords, FILE_ASSEMBLY_CHUNK_BYTES, REWRITE_SPILL_BYTES, LIST_VIEWS_HELD_PER_SEGMENT, TOMBSTONES_BETWEEN_YIELDS, readSegmentFile, READS_AHEAD_AT_ONCE, WRITER_YIELD_INTERVAL_TOKENS, LARGEST_CACHED_ENTRY_SHARE, MAX_THREAD_FILE_NUMBER, REMOVAL_BUCKET_WRITE_BUFFER_BYTES, SECTION_WRITE_BUFFER_BYTES, READ_AHEAD_CHUNK_BYTES, LARGEST_REMOVAL_BUCKET_COUNT, REMOVAL_BUCKET_TARGET_BYTES, THREAD_READ_CACHE_BYTES, MERGE_READ_CACHE_BYTES } from './chunk-7AUASRE7.mjs';
|
|
5
|
+
import { requireOffsets, requireUtf8Entries, requireEntriesAscend, decodeFieldLengths, decodePartialWords, decodePatternColumns, decodeGeopointColumns, decodeEnumColumns, decodeBooleanColumns, decodeValueColumns, setNestedValue, littleEndianBytes, uint32sFrom, uint64Bytes, decodeSurfaceForms } from './chunk-KEIYVZUX.mjs';
|
|
6
|
+
import { HEADER_SIZE, requireWithinFileLimit, crc32Init, crc32Update, crc32Final, engineVersionParts, writeHeader, crc32, readHeader } from './chunk-IO5F7ZBE.mjs';
|
|
7
|
+
import { wrapFrozenTokenTable, createEncodedDocumentSource, assemblePagedFrozenSegment, decodeStoredDocument, mergeFieldLengths, mergeSurfaceForms, encodeSurfaceTable, mergePartialWords, mergeFieldIndexes, resolvePartitionInsertOptions, decodeSurfaceTable, createSharedFrozenSegment, shareSegmentArrays, selectionKeeps, fieldIndexRemapOf, freezeSegmentShared, createFrozenSegment, holdsADiscardedField, endOfValue } from './chunk-WJGO5PGV.mjs';
|
|
8
|
+
import { isCompositePartition, buildPartialWordTableFrom, createPartitionIndex, createCompositePartition, mergeDocFrequencies, scanPostingBlocks } from './chunk-ZPWS2SKJ.mjs';
|
|
9
|
+
import { generateId } from './chunk-N37VMC62.mjs';
|
|
10
|
+
import { parseVectorDimension } from './chunk-DQNKVOJ2.mjs';
|
|
11
|
+
import { flattenSchema, encodeDocument } from './chunk-KGOXHLLZ.mjs';
|
|
12
|
+
import { compareCodePoints } from './chunk-YTJCGKM4.mjs';
|
|
13
|
+
import { VECTOR_PATTERN } from './chunk-L5ORED7V.mjs';
|
|
14
|
+
import { NarsilError, ErrorCodes } from './chunk-HNHCNTVG.mjs';
|
|
15
|
+
import { decode, encode } from '@msgpack/msgpack';
|
|
16
|
+
import { deflateRawSync, inflateRawSync } from 'zlib';
|
|
17
|
+
|
|
18
|
+
// src/persistence/paged/counts.ts
|
|
19
|
+
var VALUE_BYTES = 4;
|
|
20
|
+
function createPagedLiveCounts(snapshot, table, tombstones, removalOffsets, removals) {
|
|
21
|
+
let removedDocuments = null;
|
|
22
|
+
let removedOccurrences = null;
|
|
23
|
+
let lengths = null;
|
|
24
|
+
function documentFrequency(sortedIndex) {
|
|
25
|
+
const held = table.documentFrequencyAt(sortedIndex);
|
|
26
|
+
return removedDocuments === null ? held : held - removedDocuments[sortedIndex];
|
|
27
|
+
}
|
|
28
|
+
function subtractTerms(ordinal) {
|
|
29
|
+
const bounds = removalOffsets.bytesAt(ordinal * VALUE_BYTES, VALUE_BYTES * 2);
|
|
30
|
+
const boundsView = new DataView(bounds.buffer, bounds.byteOffset, bounds.byteLength);
|
|
31
|
+
const from = boundsView.getUint32(0, true);
|
|
32
|
+
const to = boundsView.getUint32(VALUE_BYTES, true);
|
|
33
|
+
if (to < from || to * VALUE_BYTES > removals.length || from % 2 !== 0 || to % 2 !== 0) {
|
|
34
|
+
loadFailed(`The removal entry of document ${ordinal} of "${snapshot.key}" leaves its list`, { ordinal });
|
|
35
|
+
}
|
|
36
|
+
const entry = removals.bytesAt(from * VALUE_BYTES, (to - from) * VALUE_BYTES);
|
|
37
|
+
const view = new DataView(entry.buffer, entry.byteOffset, entry.byteLength);
|
|
38
|
+
removedDocuments ??= new Uint32Array(table.size);
|
|
39
|
+
removedOccurrences ??= new Float64Array(table.size);
|
|
40
|
+
for (let at = 0; at + VALUE_BYTES * 2 <= entry.byteLength; at += VALUE_BYTES * 2) {
|
|
41
|
+
const token = view.getUint32(at, true);
|
|
42
|
+
if (token >= table.size) {
|
|
43
|
+
loadFailed(`The removal entry of document ${ordinal} of "${snapshot.key}" names token ${token}`, { ordinal });
|
|
44
|
+
}
|
|
45
|
+
removedDocuments[token] += 1;
|
|
46
|
+
removedOccurrences[token] += view.getUint32(at + VALUE_BYTES, true);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
totalDocuments() {
|
|
51
|
+
return snapshot.documentCount - tombstones.size;
|
|
52
|
+
},
|
|
53
|
+
totalFieldLengths() {
|
|
54
|
+
return lengths ?? snapshot.totalFieldLengths;
|
|
55
|
+
},
|
|
56
|
+
documentFrequency,
|
|
57
|
+
removedOccurrences(sortedIndex) {
|
|
58
|
+
return removedOccurrences === null ? 0 : removedOccurrences[sortedIndex];
|
|
59
|
+
},
|
|
60
|
+
docFrequencies() {
|
|
61
|
+
const frequencies = /* @__PURE__ */ Object.create(null);
|
|
62
|
+
for (let at = 0; at < table.size; at++) {
|
|
63
|
+
const documents = documentFrequency(at);
|
|
64
|
+
if (documents > 0) frequencies[table.tokenAt(at)] = documents;
|
|
65
|
+
}
|
|
66
|
+
return frequencies;
|
|
67
|
+
},
|
|
68
|
+
subtractRemoved(ordinal) {
|
|
69
|
+
lengths ??= { ...snapshot.totalFieldLengths };
|
|
70
|
+
for (let field = 0; field < snapshot.fieldLengthNames.length; field++) {
|
|
71
|
+
lengths[snapshot.fieldLengthNames[field]] -= snapshot.fieldLengthColumns[field][ordinal];
|
|
72
|
+
}
|
|
73
|
+
subtractTerms(ordinal);
|
|
74
|
+
},
|
|
75
|
+
bytes() {
|
|
76
|
+
return (removedDocuments?.byteLength ?? 0) + (removedOccurrences?.byteLength ?? 0);
|
|
77
|
+
}
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
function deflateBlock(raw) {
|
|
81
|
+
return deflateRawSync(raw);
|
|
82
|
+
}
|
|
83
|
+
function inflateBlock(compressed, rawLength) {
|
|
84
|
+
return inflateRawSync(compressed, { maxOutputLength: rawLength });
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// src/persistence/paged/read-cache.ts
|
|
88
|
+
var FILE_NUMBER_SCALE = 2 ** 34;
|
|
89
|
+
var ENTRY_NUMBER_SCALE = 4;
|
|
90
|
+
var CACHED_PAGE = 0;
|
|
91
|
+
var CACHED_LIST = 1;
|
|
92
|
+
var CACHED_POSITIONS = 2;
|
|
93
|
+
var CACHED_DOCUMENT_BLOCK = 3;
|
|
94
|
+
function createReadCache(capacity) {
|
|
95
|
+
const entries = /* @__PURE__ */ new Map();
|
|
96
|
+
const perFile = /* @__PURE__ */ new Map();
|
|
97
|
+
const freedFileNumbers = [];
|
|
98
|
+
let held = 0;
|
|
99
|
+
let nextFileNumber = 1;
|
|
100
|
+
function drop(key, entry) {
|
|
101
|
+
entries.delete(key);
|
|
102
|
+
held -= entry.bytes;
|
|
103
|
+
const remaining = (perFile.get(entry.fileNumber) ?? 0) - entry.bytes;
|
|
104
|
+
if (remaining <= 0) perFile.delete(entry.fileNumber);
|
|
105
|
+
else perFile.set(entry.fileNumber, remaining);
|
|
106
|
+
}
|
|
107
|
+
function evictDownTo(limit) {
|
|
108
|
+
for (const [key, entry] of entries) {
|
|
109
|
+
if (held <= limit) return;
|
|
110
|
+
drop(key, entry);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
function clear() {
|
|
114
|
+
entries.clear();
|
|
115
|
+
perFile.clear();
|
|
116
|
+
held = 0;
|
|
117
|
+
}
|
|
118
|
+
return {
|
|
119
|
+
get bytes() {
|
|
120
|
+
return held;
|
|
121
|
+
},
|
|
122
|
+
capacity,
|
|
123
|
+
fileNumber() {
|
|
124
|
+
const freed = freedFileNumbers.pop();
|
|
125
|
+
if (freed !== void 0) return freed;
|
|
126
|
+
if (nextFileNumber >= MAX_THREAD_FILE_NUMBER) {
|
|
127
|
+
throw new NarsilError(
|
|
128
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
129
|
+
`This thread holds ${nextFileNumber} open segment files, which is the most that its read cache can tell apart`,
|
|
130
|
+
{ limit: MAX_THREAD_FILE_NUMBER }
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
const number = nextFileNumber;
|
|
134
|
+
nextFileNumber += 1;
|
|
135
|
+
return number;
|
|
136
|
+
},
|
|
137
|
+
bytesOfFile(fileNumber) {
|
|
138
|
+
return perFile.get(fileNumber) ?? 0;
|
|
139
|
+
},
|
|
140
|
+
get(fileNumber, kind, entry) {
|
|
141
|
+
const key = fileNumber * FILE_NUMBER_SCALE + entry * ENTRY_NUMBER_SCALE + kind;
|
|
142
|
+
const found = entries.get(key);
|
|
143
|
+
if (found === void 0) return void 0;
|
|
144
|
+
entries.delete(key);
|
|
145
|
+
entries.set(key, found);
|
|
146
|
+
return found.value;
|
|
147
|
+
},
|
|
148
|
+
put(fileNumber, kind, entry, value, bytes) {
|
|
149
|
+
if (bytes > capacity * LARGEST_CACHED_ENTRY_SHARE) return;
|
|
150
|
+
const key = fileNumber * FILE_NUMBER_SCALE + entry * ENTRY_NUMBER_SCALE + kind;
|
|
151
|
+
const existing = entries.get(key);
|
|
152
|
+
if (existing !== void 0) drop(key, existing);
|
|
153
|
+
entries.set(key, { value, bytes, fileNumber });
|
|
154
|
+
held += bytes;
|
|
155
|
+
perFile.set(fileNumber, (perFile.get(fileNumber) ?? 0) + bytes);
|
|
156
|
+
evictDownTo(capacity);
|
|
157
|
+
},
|
|
158
|
+
releaseFile(fileNumber) {
|
|
159
|
+
freedFileNumbers.push(fileNumber);
|
|
160
|
+
if (!perFile.has(fileNumber)) return;
|
|
161
|
+
for (const [key, entry] of entries) {
|
|
162
|
+
if (entry.fileNumber === fileNumber) drop(key, entry);
|
|
163
|
+
}
|
|
164
|
+
},
|
|
165
|
+
clear
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
var threadCache = null;
|
|
169
|
+
function readCacheOfThisThread() {
|
|
170
|
+
threadCache ??= createReadCache(THREAD_READ_CACHE_BYTES);
|
|
171
|
+
return threadCache;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
// src/persistence/paged/documents.ts
|
|
175
|
+
function createDocumentBlockReader(snapshot, documents, cache, fileNumber) {
|
|
176
|
+
const { documentBlocks, documentCount, key } = snapshot;
|
|
177
|
+
const view = new DataView(documentBlocks.buffer, documentBlocks.byteOffset, documentBlocks.byteLength);
|
|
178
|
+
const blockCount = documentBlocks.byteLength / DOCUMENT_BLOCK_BYTES;
|
|
179
|
+
function firstOrdinalOf(block) {
|
|
180
|
+
return view.getUint32(block * DOCUMENT_BLOCK_BYTES, true);
|
|
181
|
+
}
|
|
182
|
+
function blockOf(ordinal) {
|
|
183
|
+
let low = 0;
|
|
184
|
+
let high = blockCount;
|
|
185
|
+
while (high - low > 1) {
|
|
186
|
+
const middle = low + high >>> 1;
|
|
187
|
+
if (firstOrdinalOf(middle) <= ordinal) low = middle;
|
|
188
|
+
else high = middle;
|
|
189
|
+
}
|
|
190
|
+
return low;
|
|
191
|
+
}
|
|
192
|
+
function inflate(block) {
|
|
193
|
+
const place = block * DOCUMENT_BLOCK_BYTES;
|
|
194
|
+
const offset = Number(view.getBigUint64(place + 4, true));
|
|
195
|
+
const length = view.getUint32(place + 12, true);
|
|
196
|
+
const rawLength = view.getUint32(place + 16, true);
|
|
197
|
+
if (rawLength > MAX_DOCUMENT_BLOCK_RAW_BYTES) {
|
|
198
|
+
loadFailed(`Document block ${block} of "${key}" claims ${rawLength} bytes`, { block, rawLength });
|
|
199
|
+
}
|
|
200
|
+
const compressed = documents.bytesAt(offset, length);
|
|
201
|
+
let raw;
|
|
202
|
+
try {
|
|
203
|
+
raw = inflateBlock(compressed, rawLength);
|
|
204
|
+
} catch (error) {
|
|
205
|
+
return loadFailed(
|
|
206
|
+
`Document block ${block} of "${key}" fails to inflate: ${error instanceof Error ? error.message : String(error)}`,
|
|
207
|
+
{ block }
|
|
208
|
+
);
|
|
209
|
+
}
|
|
210
|
+
if (raw.byteLength !== rawLength) {
|
|
211
|
+
loadFailed(`Document block ${block} of "${key}" inflates to ${raw.byteLength} bytes of ${rawLength}`, { block });
|
|
212
|
+
}
|
|
213
|
+
return raw;
|
|
214
|
+
}
|
|
215
|
+
function startsOf(block, raw) {
|
|
216
|
+
const first = firstOrdinalOf(block);
|
|
217
|
+
const end = block + 1 < blockCount ? firstOrdinalOf(block + 1) : documentCount;
|
|
218
|
+
const starts = new Uint32Array(end - first + 1);
|
|
219
|
+
const bytes = new DataView(raw.buffer, raw.byteOffset, raw.byteLength);
|
|
220
|
+
let cursor = 0;
|
|
221
|
+
for (let at = 0; at < end - first; at++) {
|
|
222
|
+
starts[at] = cursor;
|
|
223
|
+
const next = cursor < raw.byteLength ? endOfValue(bytes, cursor) : null;
|
|
224
|
+
if (next === null)
|
|
225
|
+
loadFailed(`Document block ${block} of "${key}" holds a broken document at ${at}`, { block, at });
|
|
226
|
+
cursor = next;
|
|
227
|
+
}
|
|
228
|
+
starts[end - first] = cursor;
|
|
229
|
+
if (cursor !== raw.byteLength)
|
|
230
|
+
loadFailed(`Document block ${block} of "${key}" holds bytes past its documents`, { block });
|
|
231
|
+
return starts;
|
|
232
|
+
}
|
|
233
|
+
function loadBlock(block) {
|
|
234
|
+
const held = cache.get(fileNumber, CACHED_DOCUMENT_BLOCK, block);
|
|
235
|
+
if (held !== void 0) return held;
|
|
236
|
+
const bytes = inflate(block);
|
|
237
|
+
const loaded = { bytes, starts: startsOf(block, bytes) };
|
|
238
|
+
cache.put(fileNumber, CACHED_DOCUMENT_BLOCK, block, loaded, bytes.byteLength + loaded.starts.byteLength);
|
|
239
|
+
return loaded;
|
|
240
|
+
}
|
|
241
|
+
return {
|
|
242
|
+
encodedDocumentAt(ordinal) {
|
|
243
|
+
if (ordinal < 0 || ordinal >= documentCount) {
|
|
244
|
+
loadFailed(`Document ${ordinal} lies outside "${key}", which holds ${documentCount}`, { ordinal });
|
|
245
|
+
}
|
|
246
|
+
const block = blockOf(ordinal);
|
|
247
|
+
const { bytes, starts } = loadBlock(block);
|
|
248
|
+
const row = ordinal - firstOrdinalOf(block);
|
|
249
|
+
return bytes.subarray(starts[row], starts[row + 1]);
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// src/persistence/paged/ids.ts
|
|
255
|
+
var VALUE_BYTES2 = 4;
|
|
256
|
+
var decoder = new TextDecoder("utf-8", { fatal: true });
|
|
257
|
+
function uint32At(reader, index) {
|
|
258
|
+
const bytes = reader.bytesAt(index * VALUE_BYTES2, VALUE_BYTES2);
|
|
259
|
+
return new DataView(bytes.buffer, bytes.byteOffset, VALUE_BYTES2).getUint32(0, true);
|
|
260
|
+
}
|
|
261
|
+
function createPagedIdTable(snapshot, ids, idOffsets, idOrder) {
|
|
262
|
+
const count = snapshot.documentCount;
|
|
263
|
+
const { idSampleIds, idSamplePositions, key } = snapshot;
|
|
264
|
+
let sampleBytes = idSamplePositions.byteLength;
|
|
265
|
+
for (const id of idSampleIds) sampleBytes += id.length;
|
|
266
|
+
function idAt(ordinal) {
|
|
267
|
+
const from = uint32At(idOffsets, ordinal);
|
|
268
|
+
const to = uint32At(idOffsets, ordinal + 1);
|
|
269
|
+
if (to < from || to > ids.length)
|
|
270
|
+
loadFailed(`The ID of document ${ordinal} of "${key}" leaves its list`, { ordinal });
|
|
271
|
+
try {
|
|
272
|
+
return decoder.decode(ids.bytesAt(from, to - from));
|
|
273
|
+
} catch {
|
|
274
|
+
return loadFailed(`The ID of document ${ordinal} of "${key}" is not valid UTF-8`, { ordinal });
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
function ordinalAtPosition(position) {
|
|
278
|
+
const ordinal = uint32At(idOrder, position);
|
|
279
|
+
if (ordinal >= count) loadFailed(`idOrder of "${key}" names document ${ordinal} of ${count}`, { position });
|
|
280
|
+
return ordinal;
|
|
281
|
+
}
|
|
282
|
+
function windowOf(docId) {
|
|
283
|
+
let low = 0;
|
|
284
|
+
let high = idSampleIds.length;
|
|
285
|
+
while (low < high) {
|
|
286
|
+
const middle = low + high >>> 1;
|
|
287
|
+
if (compareCodePoints(idSampleIds[middle], docId) <= 0) low = middle + 1;
|
|
288
|
+
else high = middle;
|
|
289
|
+
}
|
|
290
|
+
const from = low === 0 ? 0 : idSamplePositions[low - 1];
|
|
291
|
+
const to = low < idSamplePositions.length ? idSamplePositions[low] + 1 : count;
|
|
292
|
+
return { from, to: Math.min(to, count) };
|
|
293
|
+
}
|
|
294
|
+
return {
|
|
295
|
+
count,
|
|
296
|
+
bytes: sampleBytes,
|
|
297
|
+
idAt,
|
|
298
|
+
ordinalOf(docId) {
|
|
299
|
+
if (count === 0) return -1;
|
|
300
|
+
const window = windowOf(docId);
|
|
301
|
+
let low = window.from;
|
|
302
|
+
let high = window.to;
|
|
303
|
+
while (low < high) {
|
|
304
|
+
const middle = low + high >>> 1;
|
|
305
|
+
const ordinal = ordinalAtPosition(middle);
|
|
306
|
+
const order = compareCodePoints(idAt(ordinal), docId);
|
|
307
|
+
if (order === 0) return ordinal;
|
|
308
|
+
if (order < 0) low = middle + 1;
|
|
309
|
+
else high = middle;
|
|
310
|
+
}
|
|
311
|
+
return -1;
|
|
312
|
+
},
|
|
313
|
+
sortedOrdinalAt: ordinalAtPosition,
|
|
314
|
+
collectSortedIds(excluded) {
|
|
315
|
+
const kept = [];
|
|
316
|
+
for (let position = 0; position < count; position++) {
|
|
317
|
+
const ordinal = ordinalAtPosition(position);
|
|
318
|
+
if (!excluded(ordinal)) kept.push(idAt(ordinal));
|
|
319
|
+
}
|
|
320
|
+
return kept;
|
|
321
|
+
}
|
|
322
|
+
};
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/persistence/paged/lists.ts
|
|
326
|
+
var LITTLE_ENDIAN_PLATFORM = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
|
|
327
|
+
function swappedInPlace(bytes, width) {
|
|
328
|
+
for (let at = 0; at + width <= bytes.length; at += width) {
|
|
329
|
+
for (let low = 0, high = width - 1; low < high; low++, high--) {
|
|
330
|
+
const held = bytes[at + low];
|
|
331
|
+
bytes[at + low] = bytes[at + high];
|
|
332
|
+
bytes[at + high] = held;
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
function fromLittleEndian(bytes, width) {
|
|
337
|
+
if (!LITTLE_ENDIAN_PLATFORM) swappedInPlace(bytes, width);
|
|
338
|
+
}
|
|
339
|
+
function requireListRules(list, snapshot, token) {
|
|
340
|
+
const { docIds, fieldNameIndices } = list;
|
|
341
|
+
const documents = snapshot.documentCount;
|
|
342
|
+
const fields = snapshot.fieldNames.length;
|
|
343
|
+
for (let row = 0; row < docIds.length; row++) {
|
|
344
|
+
if (docIds[row] >= documents) {
|
|
345
|
+
loadFailed(`Token ${token} of "${snapshot.key}" names document ${docIds[row]} of ${documents}`, { token, row });
|
|
346
|
+
}
|
|
347
|
+
if (fieldNameIndices[row] >= fields) {
|
|
348
|
+
loadFailed(`Token ${token} of "${snapshot.key}" names field ${fieldNameIndices[row]} of ${fields}`, {
|
|
349
|
+
token,
|
|
350
|
+
row
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
if (row === 0) continue;
|
|
354
|
+
const before = docIds[row - 1];
|
|
355
|
+
if (docIds[row] < before || docIds[row] === before && fieldNameIndices[row] <= fieldNameIndices[row - 1]) {
|
|
356
|
+
loadFailed(`The list of token ${token} of "${snapshot.key}" descends at posting ${row}`, { token, row });
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
function createListReader(file, snapshot, sectionReader) {
|
|
361
|
+
const { postingOffsets } = snapshot;
|
|
362
|
+
const postings = sectionReader("postings");
|
|
363
|
+
const positionOffsets = sectionReader("positionOffsets");
|
|
364
|
+
const positions = sectionReader("positions");
|
|
365
|
+
const totals = sectionReader("tokenTotals");
|
|
366
|
+
const { cache, fileNumber } = file;
|
|
367
|
+
function listAt(token) {
|
|
368
|
+
const held = cache.get(fileNumber, CACHED_LIST, token);
|
|
369
|
+
if (held !== void 0) return held;
|
|
370
|
+
if (postings === null) return loadFailed(`The segment "${snapshot.key}" holds no postings`);
|
|
371
|
+
const start = postingOffsets[token];
|
|
372
|
+
const count = postingOffsets[token + 1] - start;
|
|
373
|
+
const buffer = new ArrayBuffer(count * POSTING_WIDTH_BYTES);
|
|
374
|
+
const bytes = new Uint8Array(buffer);
|
|
375
|
+
postings.read(start * POSTING_WIDTH_BYTES, bytes);
|
|
376
|
+
if (!LITTLE_ENDIAN_PLATFORM) {
|
|
377
|
+
swappedInPlace(bytes.subarray(0, count * 4), 4);
|
|
378
|
+
swappedInPlace(bytes.subarray(count * 4), 2);
|
|
379
|
+
}
|
|
380
|
+
const list = {
|
|
381
|
+
docIds: new Uint32Array(buffer, 0, count),
|
|
382
|
+
termFrequencies: new Uint16Array(buffer, count * 4, count),
|
|
383
|
+
fieldNameIndices: new Uint16Array(buffer, count * 6, count)
|
|
384
|
+
};
|
|
385
|
+
requireListRules(list, snapshot, token);
|
|
386
|
+
cache.put(fileNumber, CACHED_LIST, token, list, buffer.byteLength);
|
|
387
|
+
return list;
|
|
388
|
+
}
|
|
389
|
+
function positionsAt(token) {
|
|
390
|
+
if (positionOffsets === null || positions === null) return null;
|
|
391
|
+
const held = cache.get(fileNumber, CACHED_POSITIONS, token);
|
|
392
|
+
if (held !== void 0) return held;
|
|
393
|
+
const start = postingOffsets[token];
|
|
394
|
+
const count = postingOffsets[token + 1] - start;
|
|
395
|
+
const offsetBytes = new Uint8Array((count + 1) * POSITION_WIDTH_BYTES);
|
|
396
|
+
positionOffsets.read(start * POSITION_WIDTH_BYTES, offsetBytes);
|
|
397
|
+
fromLittleEndian(offsetBytes, POSITION_WIDTH_BYTES);
|
|
398
|
+
const absolute = new Uint32Array(offsetBytes.buffer);
|
|
399
|
+
const first = absolute[0];
|
|
400
|
+
const end = absolute[count];
|
|
401
|
+
const offsets = new Uint32Array(count + 1);
|
|
402
|
+
for (let row = 0; row <= count; row++) {
|
|
403
|
+
if (absolute[row] < first || absolute[row] > end || row > 0 && absolute[row] < absolute[row - 1]) {
|
|
404
|
+
loadFailed(`The position offsets of token ${token} of "${snapshot.key}" leave their list`, { token, row });
|
|
405
|
+
}
|
|
406
|
+
offsets[row] = absolute[row] - first;
|
|
407
|
+
}
|
|
408
|
+
const valueBytes = new Uint8Array((end - first) * POSITION_WIDTH_BYTES);
|
|
409
|
+
positions.read(first * POSITION_WIDTH_BYTES, valueBytes);
|
|
410
|
+
fromLittleEndian(valueBytes, POSITION_WIDTH_BYTES);
|
|
411
|
+
const loaded = { offsets, values: new Uint32Array(valueBytes.buffer) };
|
|
412
|
+
cache.put(fileNumber, CACHED_POSITIONS, token, loaded, offsetBytes.byteLength + valueBytes.byteLength);
|
|
413
|
+
return loaded;
|
|
414
|
+
}
|
|
415
|
+
function totalAt(token) {
|
|
416
|
+
if (totals === null) return loadFailed(`The segment "${snapshot.key}" holds no token totals`);
|
|
417
|
+
const bytes = totals.bytesAt(token * 8, 8);
|
|
418
|
+
return new DataView(bytes.buffer, bytes.byteOffset, 8).getFloat64(0, true);
|
|
419
|
+
}
|
|
420
|
+
function listedIndexOf(token) {
|
|
421
|
+
const { blockLists } = snapshot;
|
|
422
|
+
let low = 0;
|
|
423
|
+
let high = blockLists.length;
|
|
424
|
+
while (low < high) {
|
|
425
|
+
const middle = low + high >>> 1;
|
|
426
|
+
if (blockLists[middle] < token) low = middle + 1;
|
|
427
|
+
else high = middle;
|
|
428
|
+
}
|
|
429
|
+
return low < blockLists.length && blockLists[low] === token ? low : -1;
|
|
430
|
+
}
|
|
431
|
+
function storedBoundsAt(token) {
|
|
432
|
+
const listed = listedIndexOf(token);
|
|
433
|
+
if (listed < 0) return null;
|
|
434
|
+
const first = snapshot.blockListOffsets[listed];
|
|
435
|
+
const blockCount = snapshot.blockListOffsets[listed + 1] - first;
|
|
436
|
+
const { blockSummaries } = snapshot;
|
|
437
|
+
const view = new DataView(blockSummaries.buffer, blockSummaries.byteOffset, blockSummaries.byteLength);
|
|
438
|
+
const bounds = {
|
|
439
|
+
structureRevision: 0,
|
|
440
|
+
blockCount,
|
|
441
|
+
entryEnd: new Int32Array(blockCount),
|
|
442
|
+
maxTermFrequency: new Int32Array(blockCount),
|
|
443
|
+
minFieldLength: new Float64Array(blockCount),
|
|
444
|
+
maxEntriesPerDocument: new Int32Array(blockCount),
|
|
445
|
+
documentCount: new Int32Array(blockCount)
|
|
446
|
+
};
|
|
447
|
+
const length = postingOffsets[token + 1] - postingOffsets[token];
|
|
448
|
+
for (let block = 0; block < blockCount; block++) {
|
|
449
|
+
const place = (first + block) * BLOCK_SUMMARY_BYTES;
|
|
450
|
+
bounds.entryEnd[block] = view.getUint32(place, true);
|
|
451
|
+
bounds.maxTermFrequency[block] = view.getUint16(place + 4, true);
|
|
452
|
+
bounds.minFieldLength[block] = view.getFloat64(place + 6, true);
|
|
453
|
+
bounds.maxEntriesPerDocument[block] = view.getUint32(place + 14, true);
|
|
454
|
+
bounds.documentCount[block] = view.getUint32(place + 18, true);
|
|
455
|
+
const before = block === 0 ? 0 : bounds.entryEnd[block - 1];
|
|
456
|
+
if (bounds.entryEnd[block] <= before || bounds.entryEnd[block] > length) {
|
|
457
|
+
loadFailed(`Block ${block} of token ${token} of "${snapshot.key}" ends outside its list`, { token, block });
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
if (blockCount === 0 || bounds.entryEnd[blockCount - 1] !== length) {
|
|
461
|
+
loadFailed(`The blocks of token ${token} of "${snapshot.key}" fail to cover its list`, { token });
|
|
462
|
+
}
|
|
463
|
+
return bounds;
|
|
464
|
+
}
|
|
465
|
+
return { listAt, positionsAt, totalAt, storedBoundsAt };
|
|
466
|
+
}
|
|
467
|
+
function createPagedPostingViews(snapshot, lists, tombstones, counts) {
|
|
468
|
+
const { postingOffsets, tracksPositions } = snapshot;
|
|
469
|
+
const views = /* @__PURE__ */ new Map();
|
|
470
|
+
function buildView(token) {
|
|
471
|
+
const length = postingOffsets[token + 1] - postingOffsets[token];
|
|
472
|
+
return {
|
|
473
|
+
length,
|
|
474
|
+
get docIds() {
|
|
475
|
+
return lists.listAt(token).docIds;
|
|
476
|
+
},
|
|
477
|
+
get termFrequencies() {
|
|
478
|
+
return lists.listAt(token).termFrequencies;
|
|
479
|
+
},
|
|
480
|
+
get fieldNameIndices() {
|
|
481
|
+
return lists.listAt(token).fieldNameIndices;
|
|
482
|
+
},
|
|
483
|
+
get positions() {
|
|
484
|
+
const loaded = lists.positionsAt(token);
|
|
485
|
+
if (loaded === null) return null;
|
|
486
|
+
const rows = new Array(length);
|
|
487
|
+
for (let row = 0; row < length; row++) {
|
|
488
|
+
rows[row] = [...loaded.values.subarray(loaded.offsets[row], loaded.offsets[row + 1])];
|
|
489
|
+
}
|
|
490
|
+
return rows;
|
|
491
|
+
},
|
|
492
|
+
holdsPositions: tracksPositions,
|
|
493
|
+
positionCountAt(row) {
|
|
494
|
+
const loaded = lists.positionsAt(token);
|
|
495
|
+
return loaded === null ? 0 : loaded.offsets[row + 1] - loaded.offsets[row];
|
|
496
|
+
},
|
|
497
|
+
get positionStore() {
|
|
498
|
+
return lists.positionsAt(token)?.values ?? null;
|
|
499
|
+
},
|
|
500
|
+
positionStartAt(row) {
|
|
501
|
+
return lists.positionsAt(token)?.offsets[row] ?? 0;
|
|
502
|
+
},
|
|
503
|
+
docIdSet: {
|
|
504
|
+
get size() {
|
|
505
|
+
return counts.documentFrequency(token);
|
|
506
|
+
}
|
|
507
|
+
},
|
|
508
|
+
deletedDocs: tombstones,
|
|
509
|
+
get totalTermFrequency() {
|
|
510
|
+
return lists.totalAt(token) - counts.removedOccurrences(token);
|
|
511
|
+
},
|
|
512
|
+
get structureRevision() {
|
|
513
|
+
return tombstones.revision;
|
|
514
|
+
},
|
|
515
|
+
ordered: true,
|
|
516
|
+
storedBlockBounds() {
|
|
517
|
+
return tombstones.size === 0 ? lists.storedBoundsAt(token) : null;
|
|
518
|
+
}
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
return {
|
|
522
|
+
viewAt(payloadSlot) {
|
|
523
|
+
let view = views.get(payloadSlot);
|
|
524
|
+
if (view === void 0) {
|
|
525
|
+
if (views.size >= LIST_VIEWS_HELD_PER_SEGMENT) views.clear();
|
|
526
|
+
view = buildView(payloadSlot);
|
|
527
|
+
views.set(payloadSlot, view);
|
|
528
|
+
}
|
|
529
|
+
return view;
|
|
530
|
+
},
|
|
531
|
+
viewReadOnce(payloadSlot) {
|
|
532
|
+
return buildView(payloadSlot);
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
// src/persistence/paged/section-file.ts
|
|
538
|
+
var threadReader = null;
|
|
539
|
+
function fileReaderOfThisThread() {
|
|
540
|
+
threadReader ??= createVectorFileReader();
|
|
541
|
+
return threadReader;
|
|
542
|
+
}
|
|
543
|
+
var readersOfPath = /* @__PURE__ */ new Map();
|
|
544
|
+
function openPagedFile(path, cache) {
|
|
545
|
+
readersOfPath.set(path, (readersOfPath.get(path) ?? 0) + 1);
|
|
546
|
+
return { path, fileNumber: cache.fileNumber(), cache, closed: false };
|
|
547
|
+
}
|
|
548
|
+
function stopReadingPath(path) {
|
|
549
|
+
const readers = (readersOfPath.get(path) ?? 1) - 1;
|
|
550
|
+
if (readers > 0) {
|
|
551
|
+
readersOfPath.set(path, readers);
|
|
552
|
+
return;
|
|
553
|
+
}
|
|
554
|
+
readersOfPath.delete(path);
|
|
555
|
+
fileReaderOfThisThread().retainOnly(new Set(readersOfPath.keys()));
|
|
556
|
+
}
|
|
557
|
+
function closePagedFile(file) {
|
|
558
|
+
if (file.closed) return;
|
|
559
|
+
file.closed = true;
|
|
560
|
+
file.cache.releaseFile(file.fileNumber);
|
|
561
|
+
stopReadingPath(file.path);
|
|
562
|
+
}
|
|
563
|
+
function relocatePagedFile(file, path) {
|
|
564
|
+
if (path === file.path) return;
|
|
565
|
+
stopReadingPath(file.path);
|
|
566
|
+
readersOfPath.set(path, (readersOfPath.get(path) ?? 0) + 1);
|
|
567
|
+
file.path = path;
|
|
568
|
+
}
|
|
569
|
+
function unreadable(path, offset, target) {
|
|
570
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_LOAD_FAILED, `The file "${path}" cannot be read at byte ${offset}`, {
|
|
571
|
+
path,
|
|
572
|
+
offset,
|
|
573
|
+
length: target.byteLength
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
function readFileRange(path, offset, target) {
|
|
577
|
+
if (!fileReaderOfThisThread().readInto(path, offset, target)) unreadable(path, offset, target);
|
|
578
|
+
}
|
|
579
|
+
function readFileRangeLater(path, offset, target) {
|
|
580
|
+
return fileReaderOfThisThread().readLater(path, offset, target);
|
|
581
|
+
}
|
|
582
|
+
function checksumOfPage(section, page) {
|
|
583
|
+
const at = page * 4;
|
|
584
|
+
const { checksums } = section;
|
|
585
|
+
return (checksums[at] | checksums[at + 1] << 8 | checksums[at + 2] << 16 | checksums[at + 3] << 24) >>> 0;
|
|
586
|
+
}
|
|
587
|
+
function requirePageChecksum(path, section, page, bytes) {
|
|
588
|
+
if (crc32(bytes) === checksumOfPage(section, page)) return;
|
|
589
|
+
throw new NarsilError(
|
|
590
|
+
ErrorCodes.PERSISTENCE_CRC_MISMATCH,
|
|
591
|
+
`Page ${page} of section "${section.name}" of the file "${path}" does not match its checksum`,
|
|
592
|
+
{ path, section: section.name, page }
|
|
593
|
+
);
|
|
594
|
+
}
|
|
595
|
+
function createSectionReader(file, section) {
|
|
596
|
+
const { pageBytes, length } = section;
|
|
597
|
+
const start = HEADER_SIZE + section.offset;
|
|
598
|
+
function pageAt(page) {
|
|
599
|
+
const entry = start + page * pageBytes;
|
|
600
|
+
const held = file.cache.get(file.fileNumber, CACHED_PAGE, entry);
|
|
601
|
+
if (held !== void 0) return held;
|
|
602
|
+
const bytes = new Uint8Array(Math.min(pageBytes, length - page * pageBytes));
|
|
603
|
+
readFileRange(file.path, entry, bytes);
|
|
604
|
+
requirePageChecksum(file.path, section, page, bytes);
|
|
605
|
+
file.cache.put(file.fileNumber, CACHED_PAGE, entry, bytes, bytes.byteLength);
|
|
606
|
+
return bytes;
|
|
607
|
+
}
|
|
608
|
+
async function fetchPage(page) {
|
|
609
|
+
const entry = start + page * pageBytes;
|
|
610
|
+
const bytes = new Uint8Array(Math.min(pageBytes, length - page * pageBytes));
|
|
611
|
+
if (!await fileReaderOfThisThread().readLater(file.path, entry, bytes)) unreadable(file.path, entry, bytes);
|
|
612
|
+
requirePageChecksum(file.path, section, page, bytes);
|
|
613
|
+
if (!file.closed) file.cache.put(file.fileNumber, CACHED_PAGE, entry, bytes, bytes.byteLength);
|
|
614
|
+
}
|
|
615
|
+
function requireInside(position, size) {
|
|
616
|
+
if (position >= 0 && position + size <= length) return;
|
|
617
|
+
throw new NarsilError(
|
|
618
|
+
ErrorCodes.PERSISTENCE_LOAD_FAILED,
|
|
619
|
+
`A read of ${size} bytes at ${position} ends outside section "${section.name}" of "${file.path}"`,
|
|
620
|
+
{ path: file.path, section: section.name, position, size, length }
|
|
621
|
+
);
|
|
622
|
+
}
|
|
623
|
+
function read(position, target) {
|
|
624
|
+
requireInside(position, target.byteLength);
|
|
625
|
+
let written = 0;
|
|
626
|
+
while (written < target.byteLength) {
|
|
627
|
+
const at = position + written;
|
|
628
|
+
const page = Math.floor(at / pageBytes);
|
|
629
|
+
const from = at - page * pageBytes;
|
|
630
|
+
const bytes = pageAt(page);
|
|
631
|
+
const take = Math.min(bytes.byteLength - from, target.byteLength - written);
|
|
632
|
+
target.set(bytes.subarray(from, from + take), written);
|
|
633
|
+
written += take;
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
return {
|
|
637
|
+
length,
|
|
638
|
+
read,
|
|
639
|
+
fetchRows(positions, rowBytes) {
|
|
640
|
+
const wanted = /* @__PURE__ */ new Set();
|
|
641
|
+
for (const position of positions) {
|
|
642
|
+
requireInside(position, rowBytes);
|
|
643
|
+
const last = Math.floor((position + rowBytes - 1) / pageBytes);
|
|
644
|
+
for (let page = Math.floor(position / pageBytes); page <= last; page++) {
|
|
645
|
+
if (file.cache.get(file.fileNumber, CACHED_PAGE, start + page * pageBytes) === void 0) {
|
|
646
|
+
wanted.add(page);
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
if (wanted.size === 0) return null;
|
|
651
|
+
return Promise.all(Array.from(wanted, fetchPage));
|
|
652
|
+
},
|
|
653
|
+
bytesAt(position, size) {
|
|
654
|
+
requireInside(position, size);
|
|
655
|
+
if (size === 0) return new Uint8Array(0);
|
|
656
|
+
const page = Math.floor(position / pageBytes);
|
|
657
|
+
const from = position - page * pageBytes;
|
|
658
|
+
if (from + size <= pageBytes) return pageAt(page).subarray(from, from + size);
|
|
659
|
+
const target = new Uint8Array(size);
|
|
660
|
+
read(position, target);
|
|
661
|
+
return target;
|
|
662
|
+
},
|
|
663
|
+
whole() {
|
|
664
|
+
const target = new Uint8Array(length);
|
|
665
|
+
read(0, target);
|
|
666
|
+
return target;
|
|
667
|
+
}
|
|
668
|
+
};
|
|
669
|
+
}
|
|
670
|
+
async function checkEverySectionPage(path, section, stillWanted) {
|
|
671
|
+
const reader = fileReaderOfThisThread();
|
|
672
|
+
const start = HEADER_SIZE + section.offset;
|
|
673
|
+
for (let page = 0; page * section.pageBytes < section.length; page++) {
|
|
674
|
+
if (!stillWanted()) return;
|
|
675
|
+
const from = page * section.pageBytes;
|
|
676
|
+
const bytes = new Uint8Array(Math.min(section.pageBytes, section.length - from));
|
|
677
|
+
if (!await reader.readLater(path, start + from, bytes)) unreadable(path, start + from, bytes);
|
|
678
|
+
requirePageChecksum(path, section, page, bytes);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
function readWholeSectionChecked(path, section, shared) {
|
|
682
|
+
const target = new Uint8Array(shared ? new SharedArrayBuffer(section.length) : new ArrayBuffer(section.length));
|
|
683
|
+
const start = HEADER_SIZE + section.offset;
|
|
684
|
+
if (section.length > 0) readFileRange(path, start, target);
|
|
685
|
+
for (let page = 0; page * section.pageBytes < section.length; page++) {
|
|
686
|
+
const from = page * section.pageBytes;
|
|
687
|
+
const to = Math.min(from + section.pageBytes, section.length);
|
|
688
|
+
requirePageChecksum(path, section, page, target.subarray(from, to));
|
|
689
|
+
}
|
|
690
|
+
return target;
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// src/persistence/paged/resident.ts
|
|
694
|
+
var RESIDENT_SECTION_NAMES = /* @__PURE__ */ new Set([
|
|
695
|
+
"tokens",
|
|
696
|
+
"tokenOffsets",
|
|
697
|
+
"documentFrequencies",
|
|
698
|
+
"postingOffsets",
|
|
699
|
+
"blockLists",
|
|
700
|
+
"blockListOffsets",
|
|
701
|
+
"blockSummaries",
|
|
702
|
+
"documentBlocks",
|
|
703
|
+
"idSample",
|
|
704
|
+
"fieldLengths",
|
|
705
|
+
"numeric",
|
|
706
|
+
"sizes",
|
|
707
|
+
"boolean",
|
|
708
|
+
"enum",
|
|
709
|
+
"geopoint",
|
|
710
|
+
"pattern",
|
|
711
|
+
"partialWords"
|
|
712
|
+
]);
|
|
713
|
+
function sharedMemoryAvailable() {
|
|
714
|
+
return typeof SharedArrayBuffer === "function";
|
|
715
|
+
}
|
|
716
|
+
function identitySlots(count, shared) {
|
|
717
|
+
const slots = shared ? new Uint32Array(new SharedArrayBuffer(count * 4)) : new Uint32Array(count);
|
|
718
|
+
for (let at = 0; at < count; at++) slots[at] = at;
|
|
719
|
+
return slots;
|
|
720
|
+
}
|
|
721
|
+
function readPagedHead(listing) {
|
|
722
|
+
const { path, key, bytes, checksum } = listing;
|
|
723
|
+
const header = new Uint8Array(HEADER_SIZE + PAYLOAD_PREFIX_BYTES);
|
|
724
|
+
if (bytes < header.byteLength) loadFailed(`The segment "${key}" is too short to hold a head`, { key, bytes });
|
|
725
|
+
readFileRange(path, 0, header);
|
|
726
|
+
const opened = readHeader(header);
|
|
727
|
+
if (opened.envelopeFormatVersion !== PAGED_SEGMENT_ENVELOPE_VERSION) {
|
|
728
|
+
throw new NarsilError(
|
|
729
|
+
ErrorCodes.ENVELOPE_VERSION_MISMATCH,
|
|
730
|
+
`The segment "${key}" holds envelope format v${opened.envelopeFormatVersion}`,
|
|
731
|
+
{ key, version: opened.envelopeFormatVersion }
|
|
732
|
+
);
|
|
733
|
+
}
|
|
734
|
+
if (opened.flags.compressionEnabled) loadFailed(`The segment "${key}" is compressed, while its format stores it raw`);
|
|
735
|
+
if (HEADER_SIZE + opened.payloadLength !== bytes) {
|
|
736
|
+
loadFailed(
|
|
737
|
+
`The segment "${key}" holds ${bytes} bytes where its header gives ${HEADER_SIZE + opened.payloadLength}`,
|
|
738
|
+
{
|
|
739
|
+
key
|
|
740
|
+
}
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
if (opened.checksum !== checksum) {
|
|
744
|
+
loadFailed(`The segment "${key}" carries checksum ${opened.checksum} where the manifest lists ${checksum}`, { key });
|
|
745
|
+
}
|
|
746
|
+
const prefix = new DataView(header.buffer, HEADER_SIZE, PAYLOAD_PREFIX_BYTES);
|
|
747
|
+
const headLength = prefix.getUint32(0, true);
|
|
748
|
+
const headChecksum = prefix.getUint32(4, true);
|
|
749
|
+
if (PAYLOAD_PREFIX_BYTES + headLength > opened.payloadLength) {
|
|
750
|
+
loadFailed(`The head of the segment "${key}" ends outside its payload`, { key, headLength });
|
|
751
|
+
}
|
|
752
|
+
const head = new Uint8Array(headLength);
|
|
753
|
+
readFileRange(path, HEADER_SIZE + PAYLOAD_PREFIX_BYTES, head);
|
|
754
|
+
if (crc32(head) !== headChecksum) {
|
|
755
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_CRC_MISMATCH, `The head of the segment "${key}" fails its checksum`, {
|
|
756
|
+
key
|
|
757
|
+
});
|
|
758
|
+
}
|
|
759
|
+
return {
|
|
760
|
+
head: decodePagedSegmentHead(head, opened.payloadLength, `the segment "${key}"`),
|
|
761
|
+
payloadLength: opened.payloadLength
|
|
762
|
+
};
|
|
763
|
+
}
|
|
764
|
+
function requireSection(sections, name, key) {
|
|
765
|
+
const section = sectionNamed(sections, name);
|
|
766
|
+
if (section === void 0) loadFailed(`The segment "${key}" holds no section "${name}"`, { key, section: name });
|
|
767
|
+
return section;
|
|
768
|
+
}
|
|
769
|
+
function decodeResidentList(path, section) {
|
|
770
|
+
const bytes = readWholeSectionChecked(path, section, false);
|
|
771
|
+
try {
|
|
772
|
+
return decode(bytes);
|
|
773
|
+
} catch (error) {
|
|
774
|
+
return loadFailed(
|
|
775
|
+
`Section "${section.name}" of "${path}" is not MessagePack: ${error instanceof Error ? error.message : String(error)}`
|
|
776
|
+
);
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
function checkedUint32s(path, section, shared) {
|
|
780
|
+
if (section.length % 4 !== 0) loadFailed(`Section "${section.name}" of "${path}" is not a whole number of values`);
|
|
781
|
+
return uint32sFrom(readWholeSectionChecked(path, section, false), shared);
|
|
782
|
+
}
|
|
783
|
+
function readIdSample(path, section, shared) {
|
|
784
|
+
const raw = decodeResidentList(path, section);
|
|
785
|
+
if (!Array.isArray(raw)) loadFailed(`Section "idSample" of "${path}" is not a list`);
|
|
786
|
+
const ids = new Array(raw.length);
|
|
787
|
+
const positions = shared ? new Uint32Array(new SharedArrayBuffer(raw.length * 4)) : new Uint32Array(raw.length);
|
|
788
|
+
for (let at = 0; at < raw.length; at++) {
|
|
789
|
+
const entry = raw[at];
|
|
790
|
+
if (!Array.isArray(entry) || typeof entry[0] !== "string" || typeof entry[1] !== "number") {
|
|
791
|
+
loadFailed(`Section "idSample" of "${path}" holds an invalid entry at ${at}`, { at });
|
|
792
|
+
}
|
|
793
|
+
ids[at] = entry[0];
|
|
794
|
+
positions[at] = entry[1];
|
|
795
|
+
if (at > 0 && positions[at] <= positions[at - 1]) {
|
|
796
|
+
loadFailed(`Section "idSample" of "${path}" fails to ascend at entry ${at}`, { at });
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
return { ids, positions };
|
|
800
|
+
}
|
|
801
|
+
function readIdSampleOf(path, head) {
|
|
802
|
+
return readIdSample(path, requireSection(head.sections, "idSample", path), false);
|
|
803
|
+
}
|
|
804
|
+
function requireBlockLayout(lists, offsets, summaries, postingOffsets, tokenCount, key) {
|
|
805
|
+
if (summaries.byteLength % BLOCK_SUMMARY_BYTES !== 0) loadFailed(`The block summaries of "${key}" are cut short`);
|
|
806
|
+
if (offsets.length !== lists.length + 1) loadFailed(`The block list offsets of "${key}" have the wrong length`);
|
|
807
|
+
requireOffsets(offsets, summaries.byteLength / BLOCK_SUMMARY_BYTES, "blockListOffsets");
|
|
808
|
+
for (let at = 0; at < lists.length; at++) {
|
|
809
|
+
const token = lists[at];
|
|
810
|
+
if (at > 0 && token <= lists[at - 1]) loadFailed(`blockLists of "${key}" fails to ascend at entry ${at}`, { at });
|
|
811
|
+
if (token >= tokenCount) loadFailed(`blockLists of "${key}" holds token ${token} of ${tokenCount}`, { at });
|
|
812
|
+
if (postingOffsets[token + 1] - postingOffsets[token] <= SMALLEST_BLOCK_POSTINGS) {
|
|
813
|
+
loadFailed(`blockLists of "${key}" lists token ${token}, which holds too few postings`, { token });
|
|
814
|
+
}
|
|
815
|
+
}
|
|
816
|
+
}
|
|
817
|
+
function requireDocumentBlockLayout(blocks, documentCount, sectionLength, key) {
|
|
818
|
+
if (blocks.byteLength % DOCUMENT_BLOCK_BYTES !== 0) loadFailed(`The document blocks of "${key}" are cut short`);
|
|
819
|
+
const view = new DataView(blocks.buffer, blocks.byteOffset, blocks.byteLength);
|
|
820
|
+
const count = blocks.byteLength / DOCUMENT_BLOCK_BYTES;
|
|
821
|
+
let previousFirst = -1;
|
|
822
|
+
let previousEnd = 0;
|
|
823
|
+
for (let at = 0; at < count; at++) {
|
|
824
|
+
const place = at * DOCUMENT_BLOCK_BYTES;
|
|
825
|
+
const first = view.getUint32(place, true);
|
|
826
|
+
const offset = Number(view.getBigUint64(place + 4, true));
|
|
827
|
+
const length = view.getUint32(place + 12, true);
|
|
828
|
+
if (first <= previousFirst || first >= documentCount) loadFailed(`Document block ${at} of "${key}" starts wrongly`);
|
|
829
|
+
if (offset !== previousEnd || offset + length > sectionLength) {
|
|
830
|
+
loadFailed(`Document block ${at} of "${key}" lies outside its section`, { at });
|
|
831
|
+
}
|
|
832
|
+
previousFirst = first;
|
|
833
|
+
previousEnd = offset + length;
|
|
834
|
+
}
|
|
835
|
+
if (documentCount > 0 && (count === 0 || view.getUint32(0, true) !== 0)) {
|
|
836
|
+
loadFailed(`The document blocks of "${key}" fail to cover its documents`);
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
function vectorFieldsOfSchema(schema) {
|
|
840
|
+
return Object.keys(schema).filter((fieldPath) => VECTOR_PATTERN.test(schema[fieldPath]));
|
|
841
|
+
}
|
|
842
|
+
function residentSectionsOf(listing) {
|
|
843
|
+
return readPagedHead(listing).head.sections.filter((section) => RESIDENT_SECTION_NAMES.has(section.name));
|
|
844
|
+
}
|
|
845
|
+
function bytesOfResidentSections(listing) {
|
|
846
|
+
let bytes = 0;
|
|
847
|
+
for (const section of residentSectionsOf(listing)) bytes += section.length;
|
|
848
|
+
return bytes;
|
|
849
|
+
}
|
|
850
|
+
function openPagedSnapshot(listing) {
|
|
851
|
+
const { head } = readPagedHead(listing);
|
|
852
|
+
const { path, key } = listing;
|
|
853
|
+
const { sections, documentCount } = head;
|
|
854
|
+
const shared = sharedMemoryAvailable();
|
|
855
|
+
const context = { documentCount, shared };
|
|
856
|
+
const take = (name) => requireSection(sections, name, key);
|
|
857
|
+
const tokens = readWholeSectionChecked(path, take("tokens"), shared);
|
|
858
|
+
const tokenOffsets = checkedUint32s(path, take("tokenOffsets"), shared);
|
|
859
|
+
requireOffsets(tokenOffsets, tokens.length, "tokenOffsets");
|
|
860
|
+
requireUtf8Entries(tokens, tokenOffsets, "tokens");
|
|
861
|
+
requireEntriesAscend(tokens, tokenOffsets, null, "tokens");
|
|
862
|
+
const tokenCount = tokenOffsets.length - 1;
|
|
863
|
+
const documentFrequencies = checkedUint32s(path, take("documentFrequencies"), shared);
|
|
864
|
+
const postingOffsets = checkedUint32s(path, take("postingOffsets"), shared);
|
|
865
|
+
if (documentFrequencies.length !== tokenCount || postingOffsets.length !== tokenCount + 1) {
|
|
866
|
+
loadFailed(`The token columns of "${key}" disagree on the token count`, { key });
|
|
867
|
+
}
|
|
868
|
+
const postingBytes = requireSection(sections, "postings", key).length;
|
|
869
|
+
requireOffsets(postingOffsets, postingBytes / 8, "postingOffsets");
|
|
870
|
+
for (let token = 0; token < tokenCount; token++) {
|
|
871
|
+
const postings = postingOffsets[token + 1] - postingOffsets[token];
|
|
872
|
+
if (documentFrequencies[token] === 0 || documentFrequencies[token] > postings) {
|
|
873
|
+
loadFailed(`documentFrequencies of "${key}" gives token ${token} a count of ${documentFrequencies[token]}`, {
|
|
874
|
+
token
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
const blockLists = checkedUint32s(path, take("blockLists"), shared);
|
|
879
|
+
const blockListOffsets = checkedUint32s(path, take("blockListOffsets"), shared);
|
|
880
|
+
const blockSummaries = readWholeSectionChecked(path, take("blockSummaries"), shared);
|
|
881
|
+
requireBlockLayout(blockLists, blockListOffsets, blockSummaries, postingOffsets, tokenCount, key);
|
|
882
|
+
const documentBlocks = readWholeSectionChecked(path, take("documentBlocks"), shared);
|
|
883
|
+
requireDocumentBlockLayout(documentBlocks, documentCount, take("documents").length, key);
|
|
884
|
+
const idSample = readIdSample(path, take("idSample"), shared);
|
|
885
|
+
const lengths = decodeFieldLengths(decodeResidentList(path, take("fieldLengths")), context);
|
|
886
|
+
const positionOffsetsSection = sectionNamed(sections, "positionOffsets");
|
|
887
|
+
const positionsSection = sectionNamed(sections, "positions");
|
|
888
|
+
if (positionOffsetsSection === void 0 !== (positionsSection === void 0)) {
|
|
889
|
+
loadFailed(`The segment "${key}" holds one of positionOffsets and positions`, { key });
|
|
890
|
+
}
|
|
891
|
+
return {
|
|
892
|
+
segmentId: generateId(),
|
|
893
|
+
key,
|
|
894
|
+
path,
|
|
895
|
+
tracksPositions: positionOffsetsSection !== void 0,
|
|
896
|
+
schema: head.schema,
|
|
897
|
+
discardedFields: vectorFieldsOfSchema(head.schema),
|
|
898
|
+
sections,
|
|
899
|
+
documentCount,
|
|
900
|
+
fieldNames: head.fieldNames,
|
|
901
|
+
...lengths,
|
|
902
|
+
tokenTable: {
|
|
903
|
+
blob: tokens,
|
|
904
|
+
offsets: tokenOffsets,
|
|
905
|
+
payloadSlots: identitySlots(tokenCount, shared),
|
|
906
|
+
documentFrequencies
|
|
907
|
+
},
|
|
908
|
+
postingOffsets,
|
|
909
|
+
blockLists,
|
|
910
|
+
blockListOffsets,
|
|
911
|
+
blockSummaries,
|
|
912
|
+
documentBlocks,
|
|
913
|
+
idSampleIds: idSample.ids,
|
|
914
|
+
idSamplePositions: idSample.positions,
|
|
915
|
+
numeric: decodeValueColumns(decodeResidentList(path, take("numeric")), "numeric", context),
|
|
916
|
+
sizes: decodeValueColumns(decodeResidentList(path, take("sizes")), "sizes", context),
|
|
917
|
+
boolean: decodeBooleanColumns(decodeResidentList(path, take("boolean")), context),
|
|
918
|
+
enums: decodeEnumColumns(decodeResidentList(path, take("enum")), context),
|
|
919
|
+
geo: decodeGeopointColumns(decodeResidentList(path, take("geopoint")), context),
|
|
920
|
+
pattern: decodePatternColumns(decodeResidentList(path, take("pattern")), context),
|
|
921
|
+
partialWords: decodePartialWords(decodeResidentList(path, take("partialWords")), tokenCount, context),
|
|
922
|
+
tombstones: head.tombstones
|
|
923
|
+
};
|
|
924
|
+
}
|
|
925
|
+
function residentBytesOf(snapshot) {
|
|
926
|
+
const table = snapshot.tokenTable;
|
|
927
|
+
let bytes = table.blob.byteLength + table.offsets.byteLength;
|
|
928
|
+
bytes += table.payloadSlots.byteLength + table.documentFrequencies.byteLength;
|
|
929
|
+
bytes += snapshot.postingOffsets.byteLength + snapshot.blockLists.byteLength;
|
|
930
|
+
bytes += snapshot.blockListOffsets.byteLength + snapshot.blockSummaries.byteLength;
|
|
931
|
+
bytes += snapshot.documentBlocks.byteLength + snapshot.idSamplePositions.byteLength;
|
|
932
|
+
for (const id of snapshot.idSampleIds) bytes += id.length;
|
|
933
|
+
for (const id of snapshot.tombstones) bytes += id.length;
|
|
934
|
+
for (const column of snapshot.fieldLengthColumns) bytes += column.byteLength;
|
|
935
|
+
for (const entry of [...snapshot.numeric, ...snapshot.sizes])
|
|
936
|
+
bytes += entry.docIds.byteLength + entry.values.byteLength;
|
|
937
|
+
for (const entry of snapshot.boolean) bytes += entry.trueDocs.byteLength + entry.falseDocs.byteLength;
|
|
938
|
+
for (const entry of snapshot.enums) bytes += entry.offsets.byteLength + entry.docIds.byteLength;
|
|
939
|
+
for (const entry of snapshot.geo) {
|
|
940
|
+
bytes += entry.docIds.byteLength + entry.latitudes.byteLength + entry.longitudes.byteLength;
|
|
941
|
+
}
|
|
942
|
+
for (const entry of snapshot.pattern) {
|
|
943
|
+
bytes += entry.docIds.byteLength + entry.runs.byteLength + entry.offsets.byteLength + entry.postings.byteLength;
|
|
944
|
+
}
|
|
945
|
+
const partial = snapshot.partialWords;
|
|
946
|
+
if (partial !== null) {
|
|
947
|
+
bytes += partial.spellings.byteLength + partial.spellingOffsets.byteLength + partial.spellingTokens.byteLength;
|
|
948
|
+
bytes += partial.spellingCounts.byteLength + partial.runs.byteLength;
|
|
949
|
+
bytes += partial.runOffsets.byteLength + partial.runEntries.byteLength;
|
|
950
|
+
}
|
|
951
|
+
return bytes;
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
// src/persistence/paged/surface-candidates.ts
|
|
955
|
+
var surfaceDecoder = new TextDecoder();
|
|
956
|
+
var surfaceEncoder = new TextEncoder();
|
|
957
|
+
function surfaceCandidatesInTable(table, prefix) {
|
|
958
|
+
if (prefix.length === 0) return [];
|
|
959
|
+
const wanted = surfaceEncoder.encode(prefix);
|
|
960
|
+
const { blob, offsets, counts } = table;
|
|
961
|
+
const found = [];
|
|
962
|
+
for (let entry = 0; entry < counts.length; entry++) {
|
|
963
|
+
const start = offsets[2 * entry];
|
|
964
|
+
const tokenStart = offsets[2 * entry + 1];
|
|
965
|
+
if (tokenStart - start < wanted.length) continue;
|
|
966
|
+
let at = 0;
|
|
967
|
+
while (at < wanted.length && blob[start + at] === wanted[at]) at += 1;
|
|
968
|
+
if (at < wanted.length || counts[entry] === 0) continue;
|
|
969
|
+
found.push({
|
|
970
|
+
surface: surfaceDecoder.decode(blob.subarray(start, tokenStart)),
|
|
971
|
+
token: surfaceDecoder.decode(blob.subarray(tokenStart, offsets[2 * entry + 2])),
|
|
972
|
+
occurrences: counts[entry]
|
|
973
|
+
});
|
|
974
|
+
}
|
|
975
|
+
return found;
|
|
976
|
+
}
|
|
977
|
+
function createSurfaceTableReader(read) {
|
|
978
|
+
let table;
|
|
979
|
+
return {
|
|
980
|
+
candidatesForPrefix(prefix) {
|
|
981
|
+
if (table === void 0) table = read();
|
|
982
|
+
return table === null ? [] : surfaceCandidatesInTable(table, prefix);
|
|
983
|
+
},
|
|
984
|
+
heldBytes() {
|
|
985
|
+
if (table === void 0 || table === null) return 0;
|
|
986
|
+
return table.blob.byteLength + table.offsets.byteLength + table.counts.byteLength;
|
|
987
|
+
}
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
|
|
991
|
+
// src/persistence/paged/term-frequencies.ts
|
|
992
|
+
function termFrequenciesAcross(states) {
|
|
993
|
+
function countOf(token) {
|
|
994
|
+
if (typeof token !== "string") return 0;
|
|
995
|
+
let count = 0;
|
|
996
|
+
for (const state of states()) count += state.invertedIdx.documentFrequency(token);
|
|
997
|
+
return count;
|
|
998
|
+
}
|
|
999
|
+
return new Proxy(/* @__PURE__ */ Object.create(null), {
|
|
1000
|
+
get: (_target, token) => countOf(token) || void 0,
|
|
1001
|
+
has: (_target, token) => countOf(token) > 0,
|
|
1002
|
+
getOwnPropertyDescriptor(_target, token) {
|
|
1003
|
+
const value = countOf(token);
|
|
1004
|
+
return value > 0 ? { value, enumerable: true, configurable: true, writable: false } : void 0;
|
|
1005
|
+
},
|
|
1006
|
+
ownKeys: () => Object.keys(mergeDocFrequencies(states()))
|
|
1007
|
+
});
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
// src/persistence/paged/open-segment.ts
|
|
1011
|
+
function onlyMergesThroughTheFile(column) {
|
|
1012
|
+
throw new NarsilError(
|
|
1013
|
+
ErrorCodes.PARTITION_CORRUPTED,
|
|
1014
|
+
`A paged segment holds "${column}" in its file, so a caller must read it through the segment's paged access`,
|
|
1015
|
+
{ column }
|
|
1016
|
+
);
|
|
1017
|
+
}
|
|
1018
|
+
function readUint64(section, ordinal) {
|
|
1019
|
+
const bytes = section.bytesAt(ordinal * 8, 8);
|
|
1020
|
+
const value = new DataView(bytes.buffer, bytes.byteOffset, 8).getBigUint64(0, true);
|
|
1021
|
+
if (value > BigInt(Number.MAX_SAFE_INTEGER)) loadFailed(`A write record of document ${ordinal} lies above 2^53 - 1`);
|
|
1022
|
+
return Number(value);
|
|
1023
|
+
}
|
|
1024
|
+
function createPagedFrozenSegment(snapshot, cache = readCacheOfThisThread()) {
|
|
1025
|
+
const file = openPagedFile(snapshot.path, cache);
|
|
1026
|
+
const sections = snapshot.sections;
|
|
1027
|
+
function reader(name) {
|
|
1028
|
+
const section = sectionNamed(sections, name);
|
|
1029
|
+
return section === void 0 ? null : createSectionReader(file, section);
|
|
1030
|
+
}
|
|
1031
|
+
function required(name) {
|
|
1032
|
+
const section = reader(name);
|
|
1033
|
+
if (section === null) loadFailed(`The segment "${snapshot.key}" holds no section "${name}"`, { section: name });
|
|
1034
|
+
return section;
|
|
1035
|
+
}
|
|
1036
|
+
const tokenTable = wrapFrozenTokenTable(snapshot.tokenTable);
|
|
1037
|
+
const idTable = createPagedIdTable(snapshot, required("ids"), required("idOffsets"), required("idOrder"));
|
|
1038
|
+
const lists = createListReader(file, snapshot, reader);
|
|
1039
|
+
const documents = createDocumentBlockReader(snapshot, required("documents"), cache, file.fileNumber);
|
|
1040
|
+
const removalOffsets = required("removalOffsets");
|
|
1041
|
+
const removals = required("removals");
|
|
1042
|
+
const surfaceSection = reader("surfaceForms");
|
|
1043
|
+
const seqNos = reader("seqNos");
|
|
1044
|
+
const primaryTerms = reader("primaryTerms");
|
|
1045
|
+
const resident = residentBytesOf(snapshot);
|
|
1046
|
+
function readSurfaceTable() {
|
|
1047
|
+
if (surfaceSection === null) return null;
|
|
1048
|
+
let raw;
|
|
1049
|
+
try {
|
|
1050
|
+
raw = decode(surfaceSection.whole());
|
|
1051
|
+
} catch (error) {
|
|
1052
|
+
loadFailed(
|
|
1053
|
+
`Section "surfaceForms" of "${snapshot.key}" is not MessagePack: ${error instanceof Error ? error.message : String(error)}`
|
|
1054
|
+
);
|
|
1055
|
+
}
|
|
1056
|
+
return decodeSurfaceForms(raw, { documentCount: snapshot.documentCount, shared: false });
|
|
1057
|
+
}
|
|
1058
|
+
const access = {
|
|
1059
|
+
key: snapshot.key,
|
|
1060
|
+
snapshot,
|
|
1061
|
+
writeRecordsHeld: seqNos !== null && primaryTerms !== null,
|
|
1062
|
+
listAt: lists.listAt,
|
|
1063
|
+
positionsAt: lists.positionsAt,
|
|
1064
|
+
encodedDocumentAt: documents.encodedDocumentAt,
|
|
1065
|
+
writeRecordAt(ordinal) {
|
|
1066
|
+
if (seqNos === null || primaryTerms === null) return { seqNo: 0, primaryTerm: 0 };
|
|
1067
|
+
return { seqNo: readUint64(seqNos, ordinal), primaryTerm: readUint64(primaryTerms, ordinal) };
|
|
1068
|
+
},
|
|
1069
|
+
heldBytes(segment) {
|
|
1070
|
+
return resident + cache.bytesOfFile(file.fileNumber) + segment.removalBytes() + segment.surfaces.heldBytes() + (segment.vectorOrdinalMaps?.bytes ?? 0);
|
|
1071
|
+
},
|
|
1072
|
+
termFrequenciesAcross,
|
|
1073
|
+
close() {
|
|
1074
|
+
closePagedFile(file);
|
|
1075
|
+
},
|
|
1076
|
+
relocate(rewrite) {
|
|
1077
|
+
relocatePagedFile(file, rewrite(file.path));
|
|
1078
|
+
snapshot.path = file.path;
|
|
1079
|
+
}
|
|
1080
|
+
};
|
|
1081
|
+
const source = {
|
|
1082
|
+
documentCount: snapshot.documentCount,
|
|
1083
|
+
fieldNames: snapshot.fieldNames,
|
|
1084
|
+
fieldLengthNames: snapshot.fieldLengthNames,
|
|
1085
|
+
fieldLengthColumns: snapshot.fieldLengthColumns,
|
|
1086
|
+
totalFieldLengths: snapshot.totalFieldLengths,
|
|
1087
|
+
numeric: snapshot.numeric,
|
|
1088
|
+
sizes: snapshot.sizes,
|
|
1089
|
+
boolean: snapshot.boolean,
|
|
1090
|
+
enums: snapshot.enums,
|
|
1091
|
+
geo: snapshot.geo,
|
|
1092
|
+
pattern: snapshot.pattern,
|
|
1093
|
+
tokenTable,
|
|
1094
|
+
idTable,
|
|
1095
|
+
documentTable: null,
|
|
1096
|
+
postingOffsets: snapshot.postingOffsets,
|
|
1097
|
+
get postingDocIds() {
|
|
1098
|
+
return onlyMergesThroughTheFile("postingDocIds");
|
|
1099
|
+
},
|
|
1100
|
+
get postingFrequencies() {
|
|
1101
|
+
return onlyMergesThroughTheFile("postingFrequencies");
|
|
1102
|
+
},
|
|
1103
|
+
get postingFieldIndices() {
|
|
1104
|
+
return onlyMergesThroughTheFile("postingFieldIndices");
|
|
1105
|
+
},
|
|
1106
|
+
get positionOffsets() {
|
|
1107
|
+
return snapshot.tracksPositions ? onlyMergesThroughTheFile("positionOffsets") : null;
|
|
1108
|
+
},
|
|
1109
|
+
get positionValues() {
|
|
1110
|
+
return snapshot.tracksPositions ? onlyMergesThroughTheFile("positionValues") : null;
|
|
1111
|
+
},
|
|
1112
|
+
readSurfaceForms() {
|
|
1113
|
+
const table = readSurfaceTable();
|
|
1114
|
+
return table === null ? null : decodeSurfaceTable(table);
|
|
1115
|
+
},
|
|
1116
|
+
surfaceTableOnDisk: createSurfaceTableReader(readSurfaceTable),
|
|
1117
|
+
docFrequencies: () => {
|
|
1118
|
+
const frequencies = /* @__PURE__ */ Object.create(null);
|
|
1119
|
+
for (let at = 0; at < tokenTable.size; at++) {
|
|
1120
|
+
frequencies[tokenTable.tokenAt(at)] = tokenTable.documentFrequencyAt(at);
|
|
1121
|
+
}
|
|
1122
|
+
return frequencies;
|
|
1123
|
+
}
|
|
1124
|
+
};
|
|
1125
|
+
const documentSource = createEncodedDocumentSource(
|
|
1126
|
+
snapshot.documentCount,
|
|
1127
|
+
required("documents").length,
|
|
1128
|
+
documents.encodedDocumentAt,
|
|
1129
|
+
snapshot.discardedFields.length === 0 ? void 0 : snapshot.discardedFields
|
|
1130
|
+
);
|
|
1131
|
+
return assemblePagedFrozenSegment(snapshot.segmentId, source, documentSource, snapshot.partialWords, {
|
|
1132
|
+
access,
|
|
1133
|
+
createCounts: (tombstones) => createPagedLiveCounts(snapshot, tokenTable, tombstones, removalOffsets, removals),
|
|
1134
|
+
createViews: (tombstones, counts) => createPagedPostingViews(snapshot, lists, tombstones, counts)
|
|
1135
|
+
});
|
|
1136
|
+
}
|
|
1137
|
+
|
|
1138
|
+
// src/persistence/paged/verify.ts
|
|
1139
|
+
async function orLoadFailure(key, read) {
|
|
1140
|
+
try {
|
|
1141
|
+
return await read();
|
|
1142
|
+
} catch (error) {
|
|
1143
|
+
if (error instanceof NarsilError) throw error;
|
|
1144
|
+
return loadFailed(
|
|
1145
|
+
`The segment "${key}" cannot be read: ${error instanceof Error ? error.message : String(error)}`,
|
|
1146
|
+
{
|
|
1147
|
+
key
|
|
1148
|
+
}
|
|
1149
|
+
);
|
|
1150
|
+
}
|
|
1151
|
+
}
|
|
1152
|
+
async function envelopeVersionOf(directory, key) {
|
|
1153
|
+
const { readRange } = directory;
|
|
1154
|
+
if (readRange === void 0) return loadFailed(`The directory holding "${key}" reads no ranges`, { key });
|
|
1155
|
+
const bytes = await orLoadFailure(key, () => readRange.call(directory, key, 4, 1));
|
|
1156
|
+
return bytes[0];
|
|
1157
|
+
}
|
|
1158
|
+
async function isPagedSegmentFile(directory, key) {
|
|
1159
|
+
return await envelopeVersionOf(directory, key) === PAGED_SEGMENT_ENVELOPE_VERSION;
|
|
1160
|
+
}
|
|
1161
|
+
async function requirePagedPayloadChecksum(directory, listing) {
|
|
1162
|
+
const { readRange } = directory;
|
|
1163
|
+
const { key, bytes } = listing;
|
|
1164
|
+
if (readRange === void 0) return loadFailed(`The directory holding "${key}" reads no ranges`, { key });
|
|
1165
|
+
const header = readHeader(await readRange.call(directory, key, 0, HEADER_SIZE));
|
|
1166
|
+
if (HEADER_SIZE + header.payloadLength !== bytes) {
|
|
1167
|
+
loadFailed(`The segment "${key}" holds ${bytes} bytes where its header gives ${HEADER_SIZE + header.payloadLength}`);
|
|
1168
|
+
}
|
|
1169
|
+
let state = crc32Init();
|
|
1170
|
+
for (let at = HEADER_SIZE; at < bytes; at += FILE_ASSEMBLY_CHUNK_BYTES) {
|
|
1171
|
+
const size = Math.min(FILE_ASSEMBLY_CHUNK_BYTES, bytes - at);
|
|
1172
|
+
state = crc32Update(state, await readRange.call(directory, key, at, size));
|
|
1173
|
+
}
|
|
1174
|
+
const computed = crc32Final(state);
|
|
1175
|
+
if (computed !== header.checksum) {
|
|
1176
|
+
throw new NarsilError(
|
|
1177
|
+
ErrorCodes.PERSISTENCE_CRC_MISMATCH,
|
|
1178
|
+
`The checksum of the segment "${key}" does not match its payload`,
|
|
1179
|
+
{ key, expected: header.checksum, computed }
|
|
1180
|
+
);
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
async function pagedListingOf(directory, ref) {
|
|
1184
|
+
const { byteLength } = directory;
|
|
1185
|
+
const length = byteLength === void 0 ? ref.bytes : await byteLength.call(directory, ref.key);
|
|
1186
|
+
if (length === null) loadFailed(`The segment "${ref.key}" that the manifest lists is missing`, { key: ref.key });
|
|
1187
|
+
if (length !== ref.bytes) {
|
|
1188
|
+
loadFailed(`The segment "${ref.key}" holds ${length} bytes where the manifest lists ${ref.bytes}`, {
|
|
1189
|
+
key: ref.key,
|
|
1190
|
+
bytes: length,
|
|
1191
|
+
listed: ref.bytes
|
|
1192
|
+
});
|
|
1193
|
+
}
|
|
1194
|
+
return { key: ref.key, path: await directory.pathOf(ref.key), bytes: ref.bytes, checksum: ref.checksum };
|
|
1195
|
+
}
|
|
1196
|
+
|
|
1197
|
+
// src/persistence/paged/segment-scan.ts
|
|
1198
|
+
var REMOVAL_VALUE_BYTES = 4;
|
|
1199
|
+
async function openPagedSegmentScan(directory, ref) {
|
|
1200
|
+
const snapshot = openPagedSnapshot(await pagedListingOf(directory, ref));
|
|
1201
|
+
const cache = createReadCache(MERGE_READ_CACHE_BYTES);
|
|
1202
|
+
const segment = createPagedFrozenSegment(snapshot, cache);
|
|
1203
|
+
const { paged } = segment;
|
|
1204
|
+
if (paged === null) loadFailed(`The segment "${ref.key}" opened without its file`);
|
|
1205
|
+
const ids = segment.arrays.idTable;
|
|
1206
|
+
const tombstones = new Set(snapshot.tombstones);
|
|
1207
|
+
return {
|
|
1208
|
+
count: snapshot.documentCount,
|
|
1209
|
+
schema: snapshot.schema,
|
|
1210
|
+
tombstones,
|
|
1211
|
+
idAt: (ordinal) => ids.idAt(ordinal),
|
|
1212
|
+
holds: (docId) => ids.ordinalOf(docId) >= 0,
|
|
1213
|
+
storedDocumentAt: (ordinal) => decodeStoredDocument(paged.encodedDocumentAt(ordinal), snapshot.discardedFields),
|
|
1214
|
+
close: () => {
|
|
1215
|
+
paged.close();
|
|
1216
|
+
cache.clear();
|
|
1217
|
+
}
|
|
1218
|
+
};
|
|
1219
|
+
}
|
|
1220
|
+
function requireIdOrderAscending(ids, count, key) {
|
|
1221
|
+
let previous = null;
|
|
1222
|
+
for (let position = 0; position < count; position++) {
|
|
1223
|
+
const id = ids.idAt(ids.sortedOrdinalAt(position));
|
|
1224
|
+
if (previous !== null && compareCodePoints(previous, id) >= 0) {
|
|
1225
|
+
loadFailed(`The ID order of "${key}" does not ascend at position ${position}`, { position });
|
|
1226
|
+
}
|
|
1227
|
+
previous = id;
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
function requireRemovalEntry(offsets, removals, tokenCount, ordinal, key) {
|
|
1231
|
+
const bounds = offsets.bytesAt(ordinal * REMOVAL_VALUE_BYTES, REMOVAL_VALUE_BYTES * 2);
|
|
1232
|
+
const boundsView = new DataView(bounds.buffer, bounds.byteOffset, bounds.byteLength);
|
|
1233
|
+
const from = boundsView.getUint32(0, true);
|
|
1234
|
+
const to = boundsView.getUint32(REMOVAL_VALUE_BYTES, true);
|
|
1235
|
+
if (to < from || to * REMOVAL_VALUE_BYTES > removals.length || from % 2 !== 0 || to % 2 !== 0) {
|
|
1236
|
+
loadFailed(`The removal entry of document ${ordinal} of "${key}" leaves its list`, { ordinal });
|
|
1237
|
+
}
|
|
1238
|
+
const entry = removals.bytesAt(from * REMOVAL_VALUE_BYTES, (to - from) * REMOVAL_VALUE_BYTES);
|
|
1239
|
+
const view = new DataView(entry.buffer, entry.byteOffset, entry.byteLength);
|
|
1240
|
+
for (let at = 0; at < entry.byteLength; at += REMOVAL_VALUE_BYTES * 2) {
|
|
1241
|
+
const token = view.getUint32(at, true);
|
|
1242
|
+
if (token >= tokenCount) {
|
|
1243
|
+
loadFailed(`The removal entry of document ${ordinal} of "${key}" lists token ${token}`, { ordinal });
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
}
|
|
1247
|
+
function requireRemovalEntries(snapshot, cache, tokenCount) {
|
|
1248
|
+
const file = openPagedFile(snapshot.path, cache);
|
|
1249
|
+
try {
|
|
1250
|
+
const sections = snapshot.sections;
|
|
1251
|
+
const reader = (name) => {
|
|
1252
|
+
const section = sectionNamed(sections, name);
|
|
1253
|
+
if (section === void 0)
|
|
1254
|
+
loadFailed(`The segment "${snapshot.key}" holds no section "${name}"`, { section: name });
|
|
1255
|
+
return createSectionReader(file, section);
|
|
1256
|
+
};
|
|
1257
|
+
const offsets = reader("removalOffsets");
|
|
1258
|
+
const removals = reader("removals");
|
|
1259
|
+
for (let ordinal = 0; ordinal < snapshot.documentCount; ordinal++) {
|
|
1260
|
+
requireRemovalEntry(offsets, removals, tokenCount, ordinal, snapshot.key);
|
|
1261
|
+
}
|
|
1262
|
+
} finally {
|
|
1263
|
+
closePagedFile(file);
|
|
1264
|
+
}
|
|
1265
|
+
}
|
|
1266
|
+
async function checkPagedSegmentFully(directory, ref) {
|
|
1267
|
+
const listing = await pagedListingOf(directory, ref);
|
|
1268
|
+
await requirePagedPayloadChecksum(directory, listing);
|
|
1269
|
+
const snapshot = openPagedSnapshot(listing);
|
|
1270
|
+
const cache = createReadCache(MERGE_READ_CACHE_BYTES);
|
|
1271
|
+
const segment = createPagedFrozenSegment(snapshot, cache);
|
|
1272
|
+
const { paged } = segment;
|
|
1273
|
+
if (paged === null) loadFailed(`The segment "${ref.key}" opened without its file`);
|
|
1274
|
+
try {
|
|
1275
|
+
for (let ordinal = 0; ordinal < snapshot.documentCount; ordinal++) {
|
|
1276
|
+
decodeStoredDocument(paged.encodedDocumentAt(ordinal), snapshot.discardedFields);
|
|
1277
|
+
}
|
|
1278
|
+
requireIdOrderAscending(segment.arrays.idTable, snapshot.documentCount, ref.key);
|
|
1279
|
+
requireRemovalEntries(snapshot, cache, segment.arrays.tokenTable.size);
|
|
1280
|
+
return snapshot.schema;
|
|
1281
|
+
} finally {
|
|
1282
|
+
paged.close();
|
|
1283
|
+
cache.clear();
|
|
1284
|
+
}
|
|
1285
|
+
}
|
|
1286
|
+
async function bytesThatSegmentsHoldInMemory(directory, manifest) {
|
|
1287
|
+
let bytes = 0;
|
|
1288
|
+
for (const partition of manifest.partitions) {
|
|
1289
|
+
for (const ref of partition.segments) {
|
|
1290
|
+
const paged = await isPagedSegmentFile(directory, ref.key);
|
|
1291
|
+
bytes += paged ? bytesOfResidentSections(await pagedListingOf(directory, ref)) : ref.bytes;
|
|
1292
|
+
}
|
|
1293
|
+
}
|
|
1294
|
+
return bytes;
|
|
1295
|
+
}
|
|
1296
|
+
function countsAsLatestHolder(scans, at, docId) {
|
|
1297
|
+
for (let later = at + 1; later < scans.length; later++) {
|
|
1298
|
+
if (scans[later].tombstones.has(docId) || scans[later].holds(docId)) return false;
|
|
1299
|
+
}
|
|
1300
|
+
return true;
|
|
1301
|
+
}
|
|
1302
|
+
|
|
1303
|
+
// src/engine/export/paged-lines.ts
|
|
1304
|
+
async function coldFieldOf(checkpoint, field) {
|
|
1305
|
+
const { directory } = checkpoint;
|
|
1306
|
+
if (directory === void 0 || field.files.length === 0 || field.graphParts.length === 0) return null;
|
|
1307
|
+
if (![...field.files, ...field.graphParts].every((file) => file.headOffset !== void 0)) return null;
|
|
1308
|
+
const fieldType = checkpoint.manifest.schema[field.fieldPath];
|
|
1309
|
+
const dimension = fieldType === void 0 ? null : parseVectorDimension(fieldType);
|
|
1310
|
+
if (dimension === null) return null;
|
|
1311
|
+
const withPath = async (ref) => ({ ref, path: await directory.pathOf(ref.key) });
|
|
1312
|
+
const { openColdVectorField } = await import('./cold-module-4X7WJCFM.mjs');
|
|
1313
|
+
return openColdVectorField({
|
|
1314
|
+
fieldPath: field.fieldPath,
|
|
1315
|
+
dimension,
|
|
1316
|
+
metric: "cosine",
|
|
1317
|
+
codeBits: null,
|
|
1318
|
+
files: await Promise.all(field.files.map(withPath)),
|
|
1319
|
+
graphParts: await Promise.all(field.graphParts.map(withPath)),
|
|
1320
|
+
resolvePartition: () => void 0
|
|
1321
|
+
});
|
|
1322
|
+
}
|
|
1323
|
+
async function openFileVectorReads(checkpoint, wholeFileReads) {
|
|
1324
|
+
const fields = /* @__PURE__ */ new Map();
|
|
1325
|
+
for (const field of checkpoint.manifest.vectors) {
|
|
1326
|
+
const opened = await coldFieldOf(checkpoint, field);
|
|
1327
|
+
if (opened !== null) fields.set(field.fieldPath, opened);
|
|
1328
|
+
}
|
|
1329
|
+
const fallback = fields.size === checkpoint.manifest.vectors.length ? null : await wholeFileReads();
|
|
1330
|
+
return {
|
|
1331
|
+
async vectorOf(fieldPath, docId) {
|
|
1332
|
+
const field = fields.get(fieldPath);
|
|
1333
|
+
if (field === void 0) return fallback === null ? null : fallback.vectorOf(fieldPath, docId);
|
|
1334
|
+
const number = field.numberOf(docId);
|
|
1335
|
+
return number === void 0 ? null : Array.from(field.vectorOfNumber(number));
|
|
1336
|
+
},
|
|
1337
|
+
close() {
|
|
1338
|
+
for (const field of fields.values()) field.close();
|
|
1339
|
+
fallback?.close();
|
|
1340
|
+
}
|
|
1341
|
+
};
|
|
1342
|
+
}
|
|
1343
|
+
async function* pagedPartitionLines(checkpoint, partition, vectors, openWholeSegment) {
|
|
1344
|
+
const { directory } = checkpoint;
|
|
1345
|
+
if (directory === void 0) return;
|
|
1346
|
+
const scans = [];
|
|
1347
|
+
try {
|
|
1348
|
+
for (const ref of partition.segments) {
|
|
1349
|
+
const paged = await isPagedSegmentFile(directory, ref.key);
|
|
1350
|
+
scans.push(paged ? await openPagedSegmentScan(directory, ref) : await openWholeSegment(ref));
|
|
1351
|
+
}
|
|
1352
|
+
for (let at = 0; at < scans.length; at++) {
|
|
1353
|
+
const scan = scans[at];
|
|
1354
|
+
for (let ordinal = 0; ordinal < scan.count; ordinal++) {
|
|
1355
|
+
const docId = scan.idAt(ordinal);
|
|
1356
|
+
if (!countsAsLatestHolder(scans, at, docId)) continue;
|
|
1357
|
+
const document = { id: docId, ...scan.storedDocumentAt(ordinal) };
|
|
1358
|
+
document.id = docId;
|
|
1359
|
+
for (const field of checkpoint.manifest.vectors) {
|
|
1360
|
+
const vector = await vectors.vectorOf(field.fieldPath, docId);
|
|
1361
|
+
if (vector !== null) setNestedValue(document, field.fieldPath, vector);
|
|
1362
|
+
}
|
|
1363
|
+
yield `${encodeDocumentJson(document, docId)}
|
|
1364
|
+
`;
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
} finally {
|
|
1368
|
+
for (const scan of scans) scan.close();
|
|
1369
|
+
}
|
|
1370
|
+
}
|
|
1371
|
+
|
|
1372
|
+
// src/persistence/paged/adopt.ts
|
|
1373
|
+
function yieldToEventLoop() {
|
|
1374
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
1375
|
+
}
|
|
1376
|
+
function applyNewerTombstones(prepared) {
|
|
1377
|
+
for (let at = 0; at < prepared.length; at++) {
|
|
1378
|
+
if (!prepared[at].isNew) continue;
|
|
1379
|
+
for (let newer = at + 1; newer < prepared.length; newer++) {
|
|
1380
|
+
const access = prepared[newer].segment.paged;
|
|
1381
|
+
if (access === null) continue;
|
|
1382
|
+
for (const docId of access.snapshot.tombstones) prepared[at].segment.tombstoneDocument(docId);
|
|
1383
|
+
}
|
|
1384
|
+
}
|
|
1385
|
+
}
|
|
1386
|
+
async function applyNewerTombstonesInTurns(prepared) {
|
|
1387
|
+
let applied = 0;
|
|
1388
|
+
for (let at = 0; at < prepared.length; at++) {
|
|
1389
|
+
if (!prepared[at].isNew) continue;
|
|
1390
|
+
for (let newer = at + 1; newer < prepared.length; newer++) {
|
|
1391
|
+
const access = prepared[newer].segment.paged;
|
|
1392
|
+
if (access === null) continue;
|
|
1393
|
+
for (const docId of access.snapshot.tombstones) {
|
|
1394
|
+
prepared[at].segment.tombstoneDocument(docId);
|
|
1395
|
+
applied += 1;
|
|
1396
|
+
if (applied % TOMBSTONES_BETWEEN_YIELDS === 0) await yieldToEventLoop();
|
|
1397
|
+
}
|
|
1398
|
+
}
|
|
1399
|
+
}
|
|
1400
|
+
}
|
|
1401
|
+
async function prepareDiskAdoption(directory, manager, partitions) {
|
|
1402
|
+
for (const entry of partitions) {
|
|
1403
|
+
for (const ref of entry.segments) {
|
|
1404
|
+
if (!await isPagedSegmentFile(directory, ref.key)) return null;
|
|
1405
|
+
}
|
|
1406
|
+
}
|
|
1407
|
+
const prepared = [];
|
|
1408
|
+
const droppedKeys = [];
|
|
1409
|
+
for (const entry of partitions) {
|
|
1410
|
+
if (entry.partitionId >= manager.partitionCount) continue;
|
|
1411
|
+
const partition = manager.getPartition(entry.partitionId);
|
|
1412
|
+
const held = /* @__PURE__ */ new Map();
|
|
1413
|
+
if (isCompositePartition(partition)) {
|
|
1414
|
+
for (const segment of partition.frozenSegments()) {
|
|
1415
|
+
if (segment.paged !== null) held.set(segment.paged.key, segment);
|
|
1416
|
+
}
|
|
1417
|
+
}
|
|
1418
|
+
const list = [];
|
|
1419
|
+
for (const ref of entry.segments) {
|
|
1420
|
+
const existing = held.get(ref.key);
|
|
1421
|
+
if (existing !== void 0) {
|
|
1422
|
+
held.delete(ref.key);
|
|
1423
|
+
list.push({ segment: existing, isNew: false });
|
|
1424
|
+
continue;
|
|
1425
|
+
}
|
|
1426
|
+
const snapshot = openPagedSnapshot(await pagedListingOf(directory, ref));
|
|
1427
|
+
list.push({ segment: createPagedFrozenSegment(snapshot), isNew: true });
|
|
1428
|
+
}
|
|
1429
|
+
droppedKeys.push(...held.keys());
|
|
1430
|
+
await applyNewerTombstonesInTurns(list);
|
|
1431
|
+
prepared.push({ partitionId: entry.partitionId, prepared: list });
|
|
1432
|
+
}
|
|
1433
|
+
return { partitions: prepared, droppedKeys };
|
|
1434
|
+
}
|
|
1435
|
+
function keysThatSegmentsStillRead(manager) {
|
|
1436
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1437
|
+
for (const partition of manager.getAllPartitions()) {
|
|
1438
|
+
if (!isCompositePartition(partition)) continue;
|
|
1439
|
+
for (const segment of partition.frozenSegments()) {
|
|
1440
|
+
if (segment.paged !== null) keys.add(segment.paged.key);
|
|
1441
|
+
}
|
|
1442
|
+
}
|
|
1443
|
+
return keys;
|
|
1444
|
+
}
|
|
1445
|
+
function closeUnusedSegments(adoption) {
|
|
1446
|
+
for (const { prepared } of adoption.partitions) {
|
|
1447
|
+
for (const { segment, isNew } of prepared) {
|
|
1448
|
+
if (isNew) segment.paged?.close();
|
|
1449
|
+
}
|
|
1450
|
+
}
|
|
1451
|
+
}
|
|
1452
|
+
function actionPartitionsOf(adoption) {
|
|
1453
|
+
return adoption.partitions.map(({ partitionId, prepared }) => ({
|
|
1454
|
+
partitionId,
|
|
1455
|
+
segments: prepared.map(({ segment, isNew }) => {
|
|
1456
|
+
const access = segment.paged;
|
|
1457
|
+
if (access !== null && isNew) return { snapshot: access.snapshot };
|
|
1458
|
+
return { segmentId: segment.segmentId };
|
|
1459
|
+
})
|
|
1460
|
+
}));
|
|
1461
|
+
}
|
|
1462
|
+
function swapPreparedPartitions(manager, partitions, changed, onSwapped) {
|
|
1463
|
+
for (const { partitionId, prepared } of partitions) {
|
|
1464
|
+
const kept = [];
|
|
1465
|
+
for (const { segment } of prepared) {
|
|
1466
|
+
if (segment.arrays.documentCount > 0) kept.push(segment);
|
|
1467
|
+
else segment.paged?.close();
|
|
1468
|
+
}
|
|
1469
|
+
adoptDiskSegments(manager, partitionId, kept, changed);
|
|
1470
|
+
onSwapped?.(partitionId, kept);
|
|
1471
|
+
}
|
|
1472
|
+
}
|
|
1473
|
+
function adoptDiskSegments(manager, partitionId, segments, changed) {
|
|
1474
|
+
const { composite, docPartitionMap, insertOptions } = manager.diskSegmentAccess(partitionId);
|
|
1475
|
+
const before = composite.frozenSegments();
|
|
1476
|
+
const kept = [];
|
|
1477
|
+
for (const docId of changed) {
|
|
1478
|
+
if (docPartitionMap.get(docId) !== partitionId) continue;
|
|
1479
|
+
const document = composite.getRef(docId);
|
|
1480
|
+
if (document !== void 0) kept.push([docId, document]);
|
|
1481
|
+
}
|
|
1482
|
+
const dropped = before.filter((segment) => !segments.includes(segment));
|
|
1483
|
+
for (const segment of segments) {
|
|
1484
|
+
if (before.includes(segment)) continue;
|
|
1485
|
+
for (const docId of changed) segment.tombstoneDocument(docId);
|
|
1486
|
+
}
|
|
1487
|
+
composite.replaceFrozenSegments(segments);
|
|
1488
|
+
composite.live.clear();
|
|
1489
|
+
for (const [docId, pid] of docPartitionMap) {
|
|
1490
|
+
if (pid === partitionId) docPartitionMap.delete(docId);
|
|
1491
|
+
}
|
|
1492
|
+
composite.live.beginBatch();
|
|
1493
|
+
try {
|
|
1494
|
+
for (const [docId, document] of kept) {
|
|
1495
|
+
composite.live.insert(docId, document, manager.config.schema, manager.language, insertOptions);
|
|
1496
|
+
docPartitionMap.set(docId, partitionId);
|
|
1497
|
+
}
|
|
1498
|
+
} finally {
|
|
1499
|
+
composite.live.endBatch();
|
|
1500
|
+
}
|
|
1501
|
+
for (const segment of dropped) segment.paged?.close();
|
|
1502
|
+
}
|
|
1503
|
+
function adoptSegmentsOnWorker(manager, action) {
|
|
1504
|
+
const partitions = action.partitions.map(({ partitionId, segments }) => {
|
|
1505
|
+
const partition = manager.getPartition(partitionId);
|
|
1506
|
+
const held = isCompositePartition(partition) ? partition.frozenSegments() : [];
|
|
1507
|
+
const prepared = segments.map((item) => {
|
|
1508
|
+
if ("snapshot" in item) return { segment: createPagedFrozenSegment(item.snapshot), isNew: true };
|
|
1509
|
+
const existing = held.find((segment) => segment.segmentId === item.segmentId);
|
|
1510
|
+
if (existing === void 0) {
|
|
1511
|
+
throw new NarsilError(
|
|
1512
|
+
ErrorCodes.PARTITION_CORRUPTED,
|
|
1513
|
+
`Partition ${partitionId} of "${action.indexName}" holds no segment "${item.segmentId}" to keep`,
|
|
1514
|
+
{ indexName: action.indexName, partitionId, segmentId: item.segmentId }
|
|
1515
|
+
);
|
|
1516
|
+
}
|
|
1517
|
+
return { segment: existing, isNew: false };
|
|
1518
|
+
});
|
|
1519
|
+
applyNewerTombstones(prepared);
|
|
1520
|
+
return { partitionId, prepared };
|
|
1521
|
+
});
|
|
1522
|
+
swapPreparedPartitions(manager, partitions, new Set(action.changed));
|
|
1523
|
+
}
|
|
1524
|
+
|
|
1525
|
+
// src/persistence/paged/compact.ts
|
|
1526
|
+
async function openSource(input, ref, cache) {
|
|
1527
|
+
const { directory, store } = input;
|
|
1528
|
+
if (await isPagedSegmentFile(directory, ref.key)) {
|
|
1529
|
+
const listing = await pagedListingOf(directory, ref);
|
|
1530
|
+
await requirePagedPayloadChecksum(directory, listing);
|
|
1531
|
+
const snapshot = openPagedSnapshot(listing);
|
|
1532
|
+
const segment2 = createPagedFrozenSegment(snapshot, cache);
|
|
1533
|
+
const opened2 = {
|
|
1534
|
+
segment: segment2,
|
|
1535
|
+
replaces: snapshot.tombstones,
|
|
1536
|
+
close: () => segment2.paged?.close()
|
|
1537
|
+
};
|
|
1538
|
+
return opened2;
|
|
1539
|
+
}
|
|
1540
|
+
const contents = await readSegmentFile(store, ref);
|
|
1541
|
+
const segment = createSharedFrozenSegment(shareSegmentArrays(contents.arrays, null));
|
|
1542
|
+
const ids = segment.arrays.idTable;
|
|
1543
|
+
const replaces = [...contents.tombstones];
|
|
1544
|
+
for (let ordinal = 0; ordinal < ids.count; ordinal++) replaces.push(ids.idAt(ordinal));
|
|
1545
|
+
const opened = { segment, replaces, close: () => void 0 };
|
|
1546
|
+
if (contents.writeRecords !== void 0) opened.writeRecords = contents.writeRecords;
|
|
1547
|
+
return opened;
|
|
1548
|
+
}
|
|
1549
|
+
async function mergePagedWindow(input) {
|
|
1550
|
+
const cache = createReadCache(MERGE_READ_CACHE_BYTES);
|
|
1551
|
+
const sources = [];
|
|
1552
|
+
try {
|
|
1553
|
+
for (const ref of input.refs) sources.push(await openSource(input, ref, cache));
|
|
1554
|
+
for (let newer = 1; newer < sources.length; newer++) {
|
|
1555
|
+
for (const docId of sources[newer].replaces) {
|
|
1556
|
+
for (let older = 0; older < newer; older++) sources[older].segment.tombstoneDocument(docId);
|
|
1557
|
+
}
|
|
1558
|
+
}
|
|
1559
|
+
const tombstones = /* @__PURE__ */ new Set();
|
|
1560
|
+
if (!input.dropsEveryTombstone) {
|
|
1561
|
+
for (const source of sources) {
|
|
1562
|
+
for (const docId of source.replaces) tombstones.add(docId);
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
const writeRecords = /* @__PURE__ */ new Map();
|
|
1566
|
+
for (const source of sources) {
|
|
1567
|
+
if (source.writeRecords !== void 0) writeRecords.set(source.segment, source.writeRecords);
|
|
1568
|
+
}
|
|
1569
|
+
return await writeSegmentsOf({
|
|
1570
|
+
store: input.store,
|
|
1571
|
+
indexName: input.indexName,
|
|
1572
|
+
partitionId: input.partitionId,
|
|
1573
|
+
nextSegmentId: input.nextSegmentId,
|
|
1574
|
+
schema: input.schema,
|
|
1575
|
+
partialWordAnalysis: input.partialWordAnalysis,
|
|
1576
|
+
textAnalysis: input.textAnalysis,
|
|
1577
|
+
selections: sources.map((source) => ({ segment: source.segment })),
|
|
1578
|
+
tombstones: [...tombstones],
|
|
1579
|
+
mergesIntoOneFile: true,
|
|
1580
|
+
paged: true,
|
|
1581
|
+
...writeRecords.size === 0 ? {} : { writeRecords }
|
|
1582
|
+
});
|
|
1583
|
+
} finally {
|
|
1584
|
+
for (const source of sources) source.close();
|
|
1585
|
+
cache.clear();
|
|
1586
|
+
}
|
|
1587
|
+
}
|
|
1588
|
+
|
|
1589
|
+
// src/persistence/paged/older-holders.ts
|
|
1590
|
+
function everyDocumentId(segments) {
|
|
1591
|
+
const ids = [];
|
|
1592
|
+
for (const segment of segments) {
|
|
1593
|
+
const table = segment.arrays.idTable;
|
|
1594
|
+
for (let ordinal = 0; ordinal < table.count; ordinal++) ids.push(table.idAt(ordinal));
|
|
1595
|
+
}
|
|
1596
|
+
return ids;
|
|
1597
|
+
}
|
|
1598
|
+
async function pagedTombstonesFor(directory, older, written, deleted) {
|
|
1599
|
+
const tombstones = new Set(deleted);
|
|
1600
|
+
if (older.length === 0) return [...tombstones];
|
|
1601
|
+
const ids = everyDocumentId(written);
|
|
1602
|
+
for (const ref of older) {
|
|
1603
|
+
if (!await isPagedSegmentFile(directory, ref.key)) return [.../* @__PURE__ */ new Set([...deleted, ...ids])];
|
|
1604
|
+
}
|
|
1605
|
+
const cache = createReadCache(MERGE_READ_CACHE_BYTES);
|
|
1606
|
+
try {
|
|
1607
|
+
for (const ref of older) {
|
|
1608
|
+
const listing = await pagedListingOf(directory, ref);
|
|
1609
|
+
const { head } = readPagedHead(listing);
|
|
1610
|
+
const sample = readIdSampleOf(listing.path, head);
|
|
1611
|
+
const file = openPagedFile(listing.path, cache);
|
|
1612
|
+
try {
|
|
1613
|
+
const reader = (name) => {
|
|
1614
|
+
const section = sectionNamed(head.sections, name);
|
|
1615
|
+
if (section === void 0) loadFailed(`The segment "${ref.key}" holds no section "${name}"`);
|
|
1616
|
+
return createSectionReader(file, section);
|
|
1617
|
+
};
|
|
1618
|
+
const table = createPagedIdTable(
|
|
1619
|
+
{
|
|
1620
|
+
documentCount: head.documentCount,
|
|
1621
|
+
key: ref.key,
|
|
1622
|
+
idSampleIds: sample.ids,
|
|
1623
|
+
idSamplePositions: sample.positions
|
|
1624
|
+
},
|
|
1625
|
+
reader("ids"),
|
|
1626
|
+
reader("idOffsets"),
|
|
1627
|
+
reader("idOrder")
|
|
1628
|
+
);
|
|
1629
|
+
for (const docId of ids) {
|
|
1630
|
+
if (!tombstones.has(docId) && table.ordinalOf(docId) >= 0) tombstones.add(docId);
|
|
1631
|
+
}
|
|
1632
|
+
} finally {
|
|
1633
|
+
closePagedFile(file);
|
|
1634
|
+
}
|
|
1635
|
+
}
|
|
1636
|
+
} finally {
|
|
1637
|
+
cache.clear();
|
|
1638
|
+
}
|
|
1639
|
+
return [...tombstones];
|
|
1640
|
+
}
|
|
1641
|
+
var FILE_PAGE_SIZES = /* @__PURE__ */ new Set([SEGMENT_PAGE_BYTES, RECORD_PAGE_BYTES]);
|
|
1642
|
+
function rangeOf(path, section) {
|
|
1643
|
+
return { path, offset: HEADER_SIZE + section.offset, length: section.length };
|
|
1644
|
+
}
|
|
1645
|
+
async function readRangesAhead(ranges) {
|
|
1646
|
+
let rangeAt = 0;
|
|
1647
|
+
let doneInRange = 0;
|
|
1648
|
+
function nextChunk() {
|
|
1649
|
+
while (rangeAt < ranges.length && doneInRange >= ranges[rangeAt].length) {
|
|
1650
|
+
rangeAt += 1;
|
|
1651
|
+
doneInRange = 0;
|
|
1652
|
+
}
|
|
1653
|
+
if (rangeAt >= ranges.length) return null;
|
|
1654
|
+
const { path, offset, length } = ranges[rangeAt];
|
|
1655
|
+
const chunk = { path, offset: offset + doneInRange, length: Math.min(READ_AHEAD_CHUNK_BYTES, length - doneInRange) };
|
|
1656
|
+
doneInRange += chunk.length;
|
|
1657
|
+
return chunk;
|
|
1658
|
+
}
|
|
1659
|
+
const scratch = new Uint8Array(READ_AHEAD_CHUNK_BYTES);
|
|
1660
|
+
async function readChunks() {
|
|
1661
|
+
for (let chunk = nextChunk(); chunk !== null; chunk = nextChunk()) {
|
|
1662
|
+
const target = scratch.subarray(0, chunk.length);
|
|
1663
|
+
if (!await readFileRangeLater(chunk.path, chunk.offset, target)) return;
|
|
1664
|
+
}
|
|
1665
|
+
}
|
|
1666
|
+
await Promise.all(Array.from({ length: READS_AHEAD_AT_ONCE }, readChunks));
|
|
1667
|
+
}
|
|
1668
|
+
async function headOf(path, ref) {
|
|
1669
|
+
const { headOffset, headLength } = ref;
|
|
1670
|
+
if (headOffset === void 0 || headLength === void 0) return null;
|
|
1671
|
+
const head = new Uint8Array(headLength);
|
|
1672
|
+
if (!await readFileRangeLater(path, HEADER_SIZE + headOffset, head)) return null;
|
|
1673
|
+
return decode(head);
|
|
1674
|
+
}
|
|
1675
|
+
async function levelsOfPart(directory, ref) {
|
|
1676
|
+
const path = await directory.pathOf(ref.key);
|
|
1677
|
+
const head = await headOf(path, ref);
|
|
1678
|
+
const graphs = head?.graphs;
|
|
1679
|
+
if (!Array.isArray(graphs) || graphs.length !== 1) return [];
|
|
1680
|
+
const { levels } = graphs[0];
|
|
1681
|
+
return decodeSections([levels], ref.bytes - HEADER_SIZE, `head of "${ref.key}"`, FILE_PAGE_SIZES).map(
|
|
1682
|
+
(section) => rangeOf(path, section)
|
|
1683
|
+
);
|
|
1684
|
+
}
|
|
1685
|
+
async function residentSectionsOfSegment(directory, ref) {
|
|
1686
|
+
if (!await isPagedSegmentFile(directory, ref.key)) return [];
|
|
1687
|
+
const listing = await pagedListingOf(directory, ref);
|
|
1688
|
+
return residentSectionsOf(listing).map((section) => rangeOf(listing.path, section));
|
|
1689
|
+
}
|
|
1690
|
+
async function startReadingOpeningSectionsAhead(directory, manifest) {
|
|
1691
|
+
try {
|
|
1692
|
+
const groups = await Promise.all([
|
|
1693
|
+
...manifest.partitions.flatMap(
|
|
1694
|
+
(partition) => partition.segments.map((ref) => residentSectionsOfSegment(directory, ref))
|
|
1695
|
+
),
|
|
1696
|
+
...manifest.vectors.flatMap((field) => field.graphParts.map((ref) => levelsOfPart(directory, ref)))
|
|
1697
|
+
]);
|
|
1698
|
+
void readRangesAhead(groups.flat());
|
|
1699
|
+
} catch {
|
|
1700
|
+
return;
|
|
1701
|
+
}
|
|
1702
|
+
}
|
|
1703
|
+
|
|
1704
|
+
// src/persistence/paged/removal-sort.ts
|
|
1705
|
+
var TRIPLE_VALUES = 3;
|
|
1706
|
+
var VALUE_BYTES3 = 4;
|
|
1707
|
+
var TRIPLE_BYTES = TRIPLE_VALUES * VALUE_BYTES3;
|
|
1708
|
+
function bucketCountFor(upperBoundTriples) {
|
|
1709
|
+
const wanted = Math.ceil(upperBoundTriples * TRIPLE_BYTES / REMOVAL_BUCKET_TARGET_BYTES);
|
|
1710
|
+
return Math.max(1, Math.min(LARGEST_REMOVAL_BUCKET_COUNT, wanted));
|
|
1711
|
+
}
|
|
1712
|
+
async function openRemovalWriter(directory, keyPrefix, documentCount, upperBoundTriples) {
|
|
1713
|
+
const bucketCount = bucketCountFor(upperBoundTriples);
|
|
1714
|
+
const ordinalsPerBucket = Math.max(1, Math.ceil(documentCount / bucketCount));
|
|
1715
|
+
const heldValues = Math.floor(REMOVAL_BUCKET_WRITE_BUFFER_BYTES / TRIPLE_BYTES) * TRIPLE_VALUES;
|
|
1716
|
+
const buckets = [];
|
|
1717
|
+
for (let at = 0; at < bucketCount; at++) {
|
|
1718
|
+
const key = `${keyPrefix}.part-removals-${at}`;
|
|
1719
|
+
await directory.remove(key);
|
|
1720
|
+
buckets.push({
|
|
1721
|
+
key,
|
|
1722
|
+
handle: await directory.appendHandle(key),
|
|
1723
|
+
held: new Uint32Array(heldValues),
|
|
1724
|
+
heldValues: 0,
|
|
1725
|
+
triples: 0
|
|
1726
|
+
});
|
|
1727
|
+
}
|
|
1728
|
+
async function flush(bucket) {
|
|
1729
|
+
if (bucket.heldValues === 0) return;
|
|
1730
|
+
await bucket.handle.append(new Uint8Array(bucket.held.buffer, 0, bucket.heldValues * VALUE_BYTES3));
|
|
1731
|
+
bucket.heldValues = 0;
|
|
1732
|
+
}
|
|
1733
|
+
async function drainBucket(bucket, firstOrdinal, endOrdinal, firstValue, emit) {
|
|
1734
|
+
await flush(bucket);
|
|
1735
|
+
await bucket.handle.close();
|
|
1736
|
+
const stored = await directory.read(bucket.key);
|
|
1737
|
+
if (stored === null || stored.byteLength !== bucket.triples * TRIPLE_BYTES) {
|
|
1738
|
+
loadFailed(`The removal bucket "${bucket.key}" holds the wrong number of bytes`, { key: bucket.key });
|
|
1739
|
+
}
|
|
1740
|
+
const triples = new Uint32Array(bucket.triples * TRIPLE_VALUES);
|
|
1741
|
+
new Uint8Array(triples.buffer).set(stored);
|
|
1742
|
+
const span = endOrdinal - firstOrdinal;
|
|
1743
|
+
const counts = new Uint32Array(span + 1);
|
|
1744
|
+
for (let at = 0; at < triples.length; at += TRIPLE_VALUES) counts[triples[at] - firstOrdinal + 1] += 2;
|
|
1745
|
+
for (let at = 0; at < span; at++) counts[at + 1] += counts[at];
|
|
1746
|
+
const values = new Uint32Array(bucket.triples * 2);
|
|
1747
|
+
const cursors = counts.slice(0, span);
|
|
1748
|
+
for (let at = 0; at < triples.length; at += TRIPLE_VALUES) {
|
|
1749
|
+
const row = triples[at] - firstOrdinal;
|
|
1750
|
+
values[cursors[row]] = triples[at + 1];
|
|
1751
|
+
values[cursors[row] + 1] = triples[at + 2];
|
|
1752
|
+
cursors[row] += 2;
|
|
1753
|
+
}
|
|
1754
|
+
const offsets = new Uint32Array(span);
|
|
1755
|
+
for (let at = 0; at < span; at++) offsets[at] = firstValue + counts[at];
|
|
1756
|
+
await emit(offsets, values);
|
|
1757
|
+
await directory.remove(bucket.key);
|
|
1758
|
+
return firstValue + values.length;
|
|
1759
|
+
}
|
|
1760
|
+
return {
|
|
1761
|
+
async add(ordinal, token, occurrences) {
|
|
1762
|
+
const bucket = buckets[Math.floor(ordinal / ordinalsPerBucket)];
|
|
1763
|
+
bucket.held[bucket.heldValues] = ordinal;
|
|
1764
|
+
bucket.held[bucket.heldValues + 1] = token;
|
|
1765
|
+
bucket.held[bucket.heldValues + 2] = occurrences;
|
|
1766
|
+
bucket.heldValues += TRIPLE_VALUES;
|
|
1767
|
+
bucket.triples += 1;
|
|
1768
|
+
if (bucket.heldValues === bucket.held.length) await flush(bucket);
|
|
1769
|
+
},
|
|
1770
|
+
async drain(emit) {
|
|
1771
|
+
let nextValue = 0;
|
|
1772
|
+
for (let at = 0; at < buckets.length; at++) {
|
|
1773
|
+
const first = at * ordinalsPerBucket;
|
|
1774
|
+
const end = Math.min(documentCount, first + ordinalsPerBucket);
|
|
1775
|
+
nextValue = await drainBucket(buckets[at], first, Math.max(first, end), nextValue, emit);
|
|
1776
|
+
}
|
|
1777
|
+
await emit(Uint32Array.of(nextValue), new Uint32Array(0));
|
|
1778
|
+
},
|
|
1779
|
+
async abandon() {
|
|
1780
|
+
for (const bucket of buckets) {
|
|
1781
|
+
await bucket.handle.close().catch(() => void 0);
|
|
1782
|
+
await directory.remove(bucket.key).catch(() => void 0);
|
|
1783
|
+
}
|
|
1784
|
+
}
|
|
1785
|
+
};
|
|
1786
|
+
}
|
|
1787
|
+
|
|
1788
|
+
// src/persistence/paged/sink.ts
|
|
1789
|
+
async function openSectionSink(directory, temporaryKey) {
|
|
1790
|
+
await directory.remove(temporaryKey);
|
|
1791
|
+
const handle = await directory.appendHandle(temporaryKey);
|
|
1792
|
+
const buffer = new Uint8Array(SECTION_WRITE_BUFFER_BYTES);
|
|
1793
|
+
const page = new Uint8Array(SEGMENT_PAGE_BYTES);
|
|
1794
|
+
const checksums = [];
|
|
1795
|
+
let buffered = 0;
|
|
1796
|
+
let pageFilled = 0;
|
|
1797
|
+
let length = 0;
|
|
1798
|
+
let closed = false;
|
|
1799
|
+
function checksumPage() {
|
|
1800
|
+
checksums.push(crc32(page.subarray(0, pageFilled)));
|
|
1801
|
+
pageFilled = 0;
|
|
1802
|
+
}
|
|
1803
|
+
function addToPage(bytes) {
|
|
1804
|
+
let at = 0;
|
|
1805
|
+
while (at < bytes.byteLength) {
|
|
1806
|
+
const take = Math.min(SEGMENT_PAGE_BYTES - pageFilled, bytes.byteLength - at);
|
|
1807
|
+
page.set(bytes.subarray(at, at + take), pageFilled);
|
|
1808
|
+
pageFilled += take;
|
|
1809
|
+
at += take;
|
|
1810
|
+
if (pageFilled === SEGMENT_PAGE_BYTES) checksumPage();
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
async function flush() {
|
|
1814
|
+
if (buffered === 0) return;
|
|
1815
|
+
await handle.append(buffer.subarray(0, buffered));
|
|
1816
|
+
buffered = 0;
|
|
1817
|
+
}
|
|
1818
|
+
async function close() {
|
|
1819
|
+
if (closed) return;
|
|
1820
|
+
closed = true;
|
|
1821
|
+
await handle.close();
|
|
1822
|
+
}
|
|
1823
|
+
return {
|
|
1824
|
+
get length() {
|
|
1825
|
+
return length;
|
|
1826
|
+
},
|
|
1827
|
+
async write(bytes) {
|
|
1828
|
+
addToPage(bytes);
|
|
1829
|
+
length += bytes.byteLength;
|
|
1830
|
+
let at = 0;
|
|
1831
|
+
while (at < bytes.byteLength) {
|
|
1832
|
+
const take = Math.min(buffer.byteLength - buffered, bytes.byteLength - at);
|
|
1833
|
+
buffer.set(bytes.subarray(at, at + take), buffered);
|
|
1834
|
+
buffered += take;
|
|
1835
|
+
at += take;
|
|
1836
|
+
if (buffered === buffer.byteLength) await flush();
|
|
1837
|
+
}
|
|
1838
|
+
},
|
|
1839
|
+
async finish() {
|
|
1840
|
+
await flush();
|
|
1841
|
+
await close();
|
|
1842
|
+
if (pageFilled > 0) checksumPage();
|
|
1843
|
+
const bytes = new Uint8Array(checksums.length * 4);
|
|
1844
|
+
const view = new DataView(bytes.buffer);
|
|
1845
|
+
for (let at = 0; at < checksums.length; at++) view.setUint32(at * 4, checksums[at], true);
|
|
1846
|
+
return { temporaryKey, length, checksums: bytes };
|
|
1847
|
+
},
|
|
1848
|
+
async abandon() {
|
|
1849
|
+
await close();
|
|
1850
|
+
await directory.remove(temporaryKey);
|
|
1851
|
+
}
|
|
1852
|
+
};
|
|
1853
|
+
}
|
|
1854
|
+
var encoder = new TextEncoder();
|
|
1855
|
+
function yieldToEventLoop2() {
|
|
1856
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
1857
|
+
}
|
|
1858
|
+
function encodedDocumentOf(segment, ordinal) {
|
|
1859
|
+
if (segment.paged !== null) return segment.paged.encodedDocumentAt(ordinal);
|
|
1860
|
+
const table = segment.arrays.documentTable;
|
|
1861
|
+
if (table === null) return encodeDocument(segment.documentSource.docAt(ordinal));
|
|
1862
|
+
const stored = table.blob.subarray(table.offsets[ordinal], table.offsets[ordinal + 1]);
|
|
1863
|
+
const { discardedFields } = table;
|
|
1864
|
+
if (discardedFields === void 0 || !holdsADiscardedField(stored, discardedFields)) return stored;
|
|
1865
|
+
return encodeDocument(segment.documentSource.docAt(ordinal));
|
|
1866
|
+
}
|
|
1867
|
+
async function writeDocumentRows(inputs, sinks, writeRecords) {
|
|
1868
|
+
const blocks = [];
|
|
1869
|
+
let pending = [];
|
|
1870
|
+
let pendingBytes = 0;
|
|
1871
|
+
let firstOrdinal = 0;
|
|
1872
|
+
let compressedAt = 0;
|
|
1873
|
+
let idBytes = 0;
|
|
1874
|
+
const single = new Uint32Array(1);
|
|
1875
|
+
const seqNo = new Float64Array(1);
|
|
1876
|
+
const primaryTerm = new Float64Array(1);
|
|
1877
|
+
async function closeBlock() {
|
|
1878
|
+
if (pendingBytes === 0) return;
|
|
1879
|
+
const raw = new Uint8Array(pendingBytes);
|
|
1880
|
+
let at = 0;
|
|
1881
|
+
for (const chunk of pending) {
|
|
1882
|
+
raw.set(chunk, at);
|
|
1883
|
+
at += chunk.byteLength;
|
|
1884
|
+
}
|
|
1885
|
+
const compressed = deflateBlock(raw);
|
|
1886
|
+
const entry = new Uint8Array(DOCUMENT_BLOCK_BYTES);
|
|
1887
|
+
const view = new DataView(entry.buffer);
|
|
1888
|
+
view.setUint32(0, firstOrdinal, true);
|
|
1889
|
+
view.setBigUint64(4, BigInt(compressedAt), true);
|
|
1890
|
+
view.setUint32(12, compressed.byteLength, true);
|
|
1891
|
+
view.setUint32(16, raw.byteLength, true);
|
|
1892
|
+
blocks.push(entry);
|
|
1893
|
+
await sinks.documents.write(compressed);
|
|
1894
|
+
compressedAt += compressed.byteLength;
|
|
1895
|
+
pending = [];
|
|
1896
|
+
pendingBytes = 0;
|
|
1897
|
+
await yieldToEventLoop2();
|
|
1898
|
+
}
|
|
1899
|
+
single[0] = 0;
|
|
1900
|
+
await sinks.idOffsets.write(littleEndianBytes(single));
|
|
1901
|
+
for (const input of inputs) {
|
|
1902
|
+
const { segment, remap } = input;
|
|
1903
|
+
const records = writeRecords?.get(segment);
|
|
1904
|
+
for (let ordinal = 0; ordinal < remap.length; ordinal++) {
|
|
1905
|
+
const target = remap[ordinal];
|
|
1906
|
+
if (target < 0) continue;
|
|
1907
|
+
if (pendingBytes === 0) firstOrdinal = target;
|
|
1908
|
+
const encoded = encodedDocumentOf(segment, ordinal);
|
|
1909
|
+
pending.push(encoded.slice());
|
|
1910
|
+
pendingBytes += encoded.byteLength;
|
|
1911
|
+
if (pendingBytes >= DOCUMENT_BLOCK_MIN_RAW_BYTES) await closeBlock();
|
|
1912
|
+
const id = encoder.encode(segment.arrays.idTable.idAt(ordinal));
|
|
1913
|
+
await sinks.ids.write(id);
|
|
1914
|
+
idBytes += id.byteLength;
|
|
1915
|
+
single[0] = idBytes;
|
|
1916
|
+
await sinks.idOffsets.write(littleEndianBytes(single));
|
|
1917
|
+
if (sinks.seqNos !== null && sinks.primaryTerms !== null) {
|
|
1918
|
+
if (segment.paged !== null) {
|
|
1919
|
+
const held = segment.paged.writeRecordAt(ordinal);
|
|
1920
|
+
seqNo[0] = held.seqNo;
|
|
1921
|
+
primaryTerm[0] = held.primaryTerm;
|
|
1922
|
+
} else {
|
|
1923
|
+
seqNo[0] = records?.seqNos[ordinal] ?? 0;
|
|
1924
|
+
primaryTerm[0] = records?.primaryTerms[ordinal] ?? 0;
|
|
1925
|
+
}
|
|
1926
|
+
await sinks.seqNos.write(uint64Bytes(seqNo));
|
|
1927
|
+
await sinks.primaryTerms.write(uint64Bytes(primaryTerm));
|
|
1928
|
+
}
|
|
1929
|
+
}
|
|
1930
|
+
}
|
|
1931
|
+
await closeBlock();
|
|
1932
|
+
const blockBytes = new Uint8Array(blocks.length * DOCUMENT_BLOCK_BYTES);
|
|
1933
|
+
for (let at = 0; at < blocks.length; at++) blockBytes.set(blocks[at], at * DOCUMENT_BLOCK_BYTES);
|
|
1934
|
+
return blockBytes;
|
|
1935
|
+
}
|
|
1936
|
+
function advance(cursor) {
|
|
1937
|
+
const table = cursor.input.segment.arrays.idTable;
|
|
1938
|
+
for (; ; ) {
|
|
1939
|
+
cursor.position += 1;
|
|
1940
|
+
if (cursor.position >= table.count) return false;
|
|
1941
|
+
const ordinal = table.sortedOrdinalAt(cursor.position);
|
|
1942
|
+
if (cursor.input.remap[ordinal] < 0) continue;
|
|
1943
|
+
cursor.ordinal = ordinal;
|
|
1944
|
+
cursor.id = table.idAt(ordinal);
|
|
1945
|
+
return true;
|
|
1946
|
+
}
|
|
1947
|
+
}
|
|
1948
|
+
async function writeIdOrder(inputs, idOrder) {
|
|
1949
|
+
const cursors = [];
|
|
1950
|
+
for (const input of inputs) {
|
|
1951
|
+
const cursor = { input, position: -1, ordinal: 0, id: "" };
|
|
1952
|
+
if (advance(cursor)) cursors.push(cursor);
|
|
1953
|
+
}
|
|
1954
|
+
const sample = [];
|
|
1955
|
+
const single = new Uint32Array(1);
|
|
1956
|
+
let position = 0;
|
|
1957
|
+
while (cursors.length > 0) {
|
|
1958
|
+
let least = 0;
|
|
1959
|
+
for (let at = 1; at < cursors.length; at++) {
|
|
1960
|
+
if (compareCodePoints(cursors[at].id, cursors[least].id) < 0) least = at;
|
|
1961
|
+
}
|
|
1962
|
+
const cursor = cursors[least];
|
|
1963
|
+
single[0] = cursor.input.remap[cursor.ordinal];
|
|
1964
|
+
await idOrder.write(littleEndianBytes(single));
|
|
1965
|
+
if (position % ID_SAMPLE_INTERVAL === 0) sample.push([cursor.id, position]);
|
|
1966
|
+
position += 1;
|
|
1967
|
+
if (!advance(cursor)) cursors.splice(least, 1);
|
|
1968
|
+
if (position % DOCUMENT_BLOCK_MIN_RAW_BYTES === 0) await yieldToEventLoop2();
|
|
1969
|
+
}
|
|
1970
|
+
return encode(sample);
|
|
1971
|
+
}
|
|
1972
|
+
|
|
1973
|
+
// src/persistence/paged/write-lists.ts
|
|
1974
|
+
var NO_DELETED_DOCUMENTS = { size: 0, has: () => false };
|
|
1975
|
+
function compareBytes(left, right) {
|
|
1976
|
+
const shared = Math.min(left.byteLength, right.byteLength);
|
|
1977
|
+
for (let at = 0; at < shared; at++) {
|
|
1978
|
+
if (left[at] !== right[at]) return left[at] - right[at];
|
|
1979
|
+
}
|
|
1980
|
+
return left.byteLength - right.byteLength;
|
|
1981
|
+
}
|
|
1982
|
+
function rowsOf(input, sortedIndex) {
|
|
1983
|
+
const { segment } = input;
|
|
1984
|
+
const { arrays, paged } = segment;
|
|
1985
|
+
const slot = arrays.tokenTable.payloadSlot(sortedIndex);
|
|
1986
|
+
if (paged !== null) {
|
|
1987
|
+
const list = paged.listAt(slot);
|
|
1988
|
+
const positions = paged.positionsAt(slot);
|
|
1989
|
+
return {
|
|
1990
|
+
start: 0,
|
|
1991
|
+
end: list.docIds.length,
|
|
1992
|
+
docIds: list.docIds,
|
|
1993
|
+
termFrequencies: list.termFrequencies,
|
|
1994
|
+
fieldNameIndices: list.fieldNameIndices,
|
|
1995
|
+
positionOffsets: positions?.offsets ?? null,
|
|
1996
|
+
positionValues: positions?.values ?? null
|
|
1997
|
+
};
|
|
1998
|
+
}
|
|
1999
|
+
return {
|
|
2000
|
+
start: arrays.postingOffsets[slot],
|
|
2001
|
+
end: arrays.postingOffsets[slot + 1],
|
|
2002
|
+
docIds: arrays.postingDocIds,
|
|
2003
|
+
termFrequencies: arrays.postingFrequencies,
|
|
2004
|
+
fieldNameIndices: arrays.postingFieldIndices,
|
|
2005
|
+
positionOffsets: arrays.positionOffsets,
|
|
2006
|
+
positionValues: arrays.positionValues
|
|
2007
|
+
};
|
|
2008
|
+
}
|
|
2009
|
+
var GrowingBytes = class {
|
|
2010
|
+
bytes = new Uint8Array(65536);
|
|
2011
|
+
length = 0;
|
|
2012
|
+
add(chunk) {
|
|
2013
|
+
if (this.length + chunk.byteLength > this.bytes.byteLength) {
|
|
2014
|
+
const next = new Uint8Array(Math.max(this.bytes.byteLength * 2, this.length + chunk.byteLength));
|
|
2015
|
+
next.set(this.bytes.subarray(0, this.length));
|
|
2016
|
+
this.bytes = next;
|
|
2017
|
+
}
|
|
2018
|
+
this.bytes.set(chunk, this.length);
|
|
2019
|
+
this.length += chunk.byteLength;
|
|
2020
|
+
}
|
|
2021
|
+
finished(shared) {
|
|
2022
|
+
const target = new Uint8Array(shared ? new SharedArrayBuffer(this.length) : new ArrayBuffer(this.length));
|
|
2023
|
+
target.set(this.bytes.subarray(0, this.length));
|
|
2024
|
+
return target;
|
|
2025
|
+
}
|
|
2026
|
+
};
|
|
2027
|
+
var GrowingUint32s = class {
|
|
2028
|
+
values = new Uint32Array(4096);
|
|
2029
|
+
length = 0;
|
|
2030
|
+
add(value) {
|
|
2031
|
+
if (this.length === this.values.length) {
|
|
2032
|
+
const next = new Uint32Array(this.values.length * 2);
|
|
2033
|
+
next.set(this.values);
|
|
2034
|
+
this.values = next;
|
|
2035
|
+
}
|
|
2036
|
+
this.values[this.length] = value;
|
|
2037
|
+
this.length += 1;
|
|
2038
|
+
}
|
|
2039
|
+
finished(shared) {
|
|
2040
|
+
const target = new Uint32Array(shared ? new SharedArrayBuffer(this.length * 4) : new ArrayBuffer(this.length * 4));
|
|
2041
|
+
target.set(this.values.subarray(0, this.length));
|
|
2042
|
+
return target;
|
|
2043
|
+
}
|
|
2044
|
+
};
|
|
2045
|
+
var TokenRows = class {
|
|
2046
|
+
docIds = new Uint32Array(1024);
|
|
2047
|
+
frequencies = new Uint16Array(1024);
|
|
2048
|
+
fields = new Uint16Array(1024);
|
|
2049
|
+
positionStarts = new Uint32Array(1025);
|
|
2050
|
+
positions = new Uint32Array(4096);
|
|
2051
|
+
count = 0;
|
|
2052
|
+
positionCount = 0;
|
|
2053
|
+
ensureRows(wanted) {
|
|
2054
|
+
if (wanted <= this.docIds.length) return;
|
|
2055
|
+
const capacity = Math.max(wanted, this.docIds.length * 2);
|
|
2056
|
+
const docIds = new Uint32Array(capacity);
|
|
2057
|
+
docIds.set(this.docIds.subarray(0, this.count));
|
|
2058
|
+
this.docIds = docIds;
|
|
2059
|
+
const frequencies = new Uint16Array(capacity);
|
|
2060
|
+
frequencies.set(this.frequencies.subarray(0, this.count));
|
|
2061
|
+
this.frequencies = frequencies;
|
|
2062
|
+
const fields = new Uint16Array(capacity);
|
|
2063
|
+
fields.set(this.fields.subarray(0, this.count));
|
|
2064
|
+
this.fields = fields;
|
|
2065
|
+
const starts = new Uint32Array(capacity + 1);
|
|
2066
|
+
starts.set(this.positionStarts.subarray(0, this.count + 1));
|
|
2067
|
+
this.positionStarts = starts;
|
|
2068
|
+
}
|
|
2069
|
+
ensurePositions(wanted) {
|
|
2070
|
+
if (wanted <= this.positions.length) return;
|
|
2071
|
+
const positions = new Uint32Array(Math.max(wanted, this.positions.length * 2));
|
|
2072
|
+
positions.set(this.positions.subarray(0, this.positionCount));
|
|
2073
|
+
this.positions = positions;
|
|
2074
|
+
}
|
|
2075
|
+
clear() {
|
|
2076
|
+
this.count = 0;
|
|
2077
|
+
this.positionCount = 0;
|
|
2078
|
+
}
|
|
2079
|
+
appendFrom(input, rows) {
|
|
2080
|
+
const { remap, fieldIndexRemap } = input;
|
|
2081
|
+
this.ensureRows(this.count + rows.end - rows.start);
|
|
2082
|
+
for (let row = rows.start; row < rows.end; row++) {
|
|
2083
|
+
const target = remap[rows.docIds[row]];
|
|
2084
|
+
if (target < 0) continue;
|
|
2085
|
+
const at = this.count;
|
|
2086
|
+
this.docIds[at] = target;
|
|
2087
|
+
this.frequencies[at] = rows.termFrequencies[row];
|
|
2088
|
+
this.fields[at] = fieldIndexRemap[rows.fieldNameIndices[row]];
|
|
2089
|
+
this.positionStarts[at] = this.positionCount;
|
|
2090
|
+
if (rows.positionOffsets !== null && rows.positionValues !== null) {
|
|
2091
|
+
const from = rows.positionOffsets[row];
|
|
2092
|
+
const to = rows.positionOffsets[row + 1];
|
|
2093
|
+
this.ensurePositions(this.positionCount + to - from);
|
|
2094
|
+
this.positions.set(rows.positionValues.subarray(from, to), this.positionCount);
|
|
2095
|
+
this.positionCount += to - from;
|
|
2096
|
+
}
|
|
2097
|
+
this.count += 1;
|
|
2098
|
+
}
|
|
2099
|
+
this.positionStarts[this.count] = this.positionCount;
|
|
2100
|
+
}
|
|
2101
|
+
isOrdered() {
|
|
2102
|
+
for (let at = 1; at < this.count; at++) {
|
|
2103
|
+
const before = this.docIds[at - 1];
|
|
2104
|
+
if (this.docIds[at] < before) return false;
|
|
2105
|
+
if (this.docIds[at] === before && this.fields[at] <= this.fields[at - 1]) return false;
|
|
2106
|
+
}
|
|
2107
|
+
return true;
|
|
2108
|
+
}
|
|
2109
|
+
sort() {
|
|
2110
|
+
const order = Array.from({ length: this.count }, (_, at) => at);
|
|
2111
|
+
order.sort((left, right) => this.docIds[left] - this.docIds[right] || this.fields[left] - this.fields[right]);
|
|
2112
|
+
const docIds = new Uint32Array(this.count);
|
|
2113
|
+
const frequencies = new Uint16Array(this.count);
|
|
2114
|
+
const fields = new Uint16Array(this.count);
|
|
2115
|
+
const starts = new Uint32Array(this.count + 1);
|
|
2116
|
+
const positions = new Uint32Array(this.positionCount);
|
|
2117
|
+
let cursor = 0;
|
|
2118
|
+
for (let at = 0; at < this.count; at++) {
|
|
2119
|
+
const from = order[at];
|
|
2120
|
+
docIds[at] = this.docIds[from];
|
|
2121
|
+
frequencies[at] = this.frequencies[from];
|
|
2122
|
+
fields[at] = this.fields[from];
|
|
2123
|
+
starts[at] = cursor;
|
|
2124
|
+
const held = this.positions.subarray(this.positionStarts[from], this.positionStarts[from + 1]);
|
|
2125
|
+
positions.set(held, cursor);
|
|
2126
|
+
cursor += held.length;
|
|
2127
|
+
}
|
|
2128
|
+
starts[this.count] = cursor;
|
|
2129
|
+
this.docIds.set(docIds);
|
|
2130
|
+
this.frequencies.set(frequencies);
|
|
2131
|
+
this.fields.set(fields);
|
|
2132
|
+
this.positionStarts.set(starts);
|
|
2133
|
+
this.positions.set(positions);
|
|
2134
|
+
}
|
|
2135
|
+
distinctDocuments() {
|
|
2136
|
+
let documents = 0;
|
|
2137
|
+
for (let at = 0; at < this.count; at++) {
|
|
2138
|
+
if (at === 0 || this.docIds[at] !== this.docIds[at - 1]) documents += 1;
|
|
2139
|
+
}
|
|
2140
|
+
return documents;
|
|
2141
|
+
}
|
|
2142
|
+
occurrences() {
|
|
2143
|
+
let total = 0;
|
|
2144
|
+
for (let at = 0; at < this.count; at++) total += this.frequencies[at];
|
|
2145
|
+
return total;
|
|
2146
|
+
}
|
|
2147
|
+
};
|
|
2148
|
+
function openCursors(inputs) {
|
|
2149
|
+
const cursors = [];
|
|
2150
|
+
for (const input of inputs) {
|
|
2151
|
+
const table = input.segment.arrays.tokenTable;
|
|
2152
|
+
if (table.size > 0) cursors.push({ input, at: 0, bytes: table.tokenBytesAt(0) });
|
|
2153
|
+
}
|
|
2154
|
+
return cursors;
|
|
2155
|
+
}
|
|
2156
|
+
function blockSummaryBytesOf(rows, columns) {
|
|
2157
|
+
const list = {
|
|
2158
|
+
length: rows.count,
|
|
2159
|
+
docIds: rows.docIds,
|
|
2160
|
+
termFrequencies: rows.frequencies,
|
|
2161
|
+
fieldNameIndices: rows.fields,
|
|
2162
|
+
positions: null,
|
|
2163
|
+
docIdSet: { size: 0 },
|
|
2164
|
+
deletedDocs: NO_DELETED_DOCUMENTS,
|
|
2165
|
+
totalTermFrequency: 0,
|
|
2166
|
+
structureRevision: 0,
|
|
2167
|
+
ordered: true
|
|
2168
|
+
};
|
|
2169
|
+
const bounds = scanPostingBlocks(list, columns, null, 0);
|
|
2170
|
+
const bytes = new Uint8Array(bounds.blockCount * BLOCK_SUMMARY_BYTES);
|
|
2171
|
+
const view = new DataView(bytes.buffer);
|
|
2172
|
+
for (let block = 0; block < bounds.blockCount; block++) {
|
|
2173
|
+
const place = block * BLOCK_SUMMARY_BYTES;
|
|
2174
|
+
view.setUint32(place, bounds.entryEnd[block], true);
|
|
2175
|
+
view.setUint16(place + 4, bounds.maxTermFrequency[block], true);
|
|
2176
|
+
view.setFloat64(place + 6, bounds.minFieldLength[block], true);
|
|
2177
|
+
view.setUint32(place + 14, bounds.maxEntriesPerDocument[block], true);
|
|
2178
|
+
view.setUint32(place + 18, bounds.documentCount[block], true);
|
|
2179
|
+
}
|
|
2180
|
+
return bytes;
|
|
2181
|
+
}
|
|
2182
|
+
async function addRemovalsOf(rows, token, removals) {
|
|
2183
|
+
let at = 0;
|
|
2184
|
+
while (at < rows.count) {
|
|
2185
|
+
const ordinal = rows.docIds[at];
|
|
2186
|
+
let occurrences = 0;
|
|
2187
|
+
while (at < rows.count && rows.docIds[at] === ordinal) {
|
|
2188
|
+
occurrences += rows.frequencies[at];
|
|
2189
|
+
at += 1;
|
|
2190
|
+
}
|
|
2191
|
+
await removals.add(ordinal, token, occurrences);
|
|
2192
|
+
}
|
|
2193
|
+
}
|
|
2194
|
+
function yieldToEventLoop3() {
|
|
2195
|
+
return new Promise((resolve) => setTimeout(resolve, 0));
|
|
2196
|
+
}
|
|
2197
|
+
async function writeMergedLists(inputs, lengthColumnsByField, sinks, removals, shared) {
|
|
2198
|
+
const tokens = new GrowingBytes();
|
|
2199
|
+
const tokenOffsets = new GrowingUint32s();
|
|
2200
|
+
const frequencies = new GrowingUint32s();
|
|
2201
|
+
const postingOffsets = new GrowingUint32s();
|
|
2202
|
+
const blockLists = new GrowingUint32s();
|
|
2203
|
+
const blockListOffsets = new GrowingUint32s();
|
|
2204
|
+
const blockSummaries = new GrowingBytes();
|
|
2205
|
+
tokenOffsets.add(0);
|
|
2206
|
+
postingOffsets.add(0);
|
|
2207
|
+
blockListOffsets.add(0);
|
|
2208
|
+
const rows = new TokenRows();
|
|
2209
|
+
const total = new Float64Array(1);
|
|
2210
|
+
const offsetScratch = new Uint32Array(1);
|
|
2211
|
+
const cursors = openCursors(inputs);
|
|
2212
|
+
let postingsWritten = 0;
|
|
2213
|
+
let positionsWritten = 0;
|
|
2214
|
+
let tokensWalked = 0;
|
|
2215
|
+
while (cursors.length > 0) {
|
|
2216
|
+
let smallest = cursors[0].bytes;
|
|
2217
|
+
for (let at = 1; at < cursors.length; at++) {
|
|
2218
|
+
if (compareBytes(cursors[at].bytes, smallest) < 0) smallest = cursors[at].bytes;
|
|
2219
|
+
}
|
|
2220
|
+
rows.clear();
|
|
2221
|
+
for (let at = 0; at < cursors.length; at++) {
|
|
2222
|
+
const cursor = cursors[at];
|
|
2223
|
+
if (compareBytes(cursor.bytes, smallest) !== 0) continue;
|
|
2224
|
+
rows.appendFrom(cursor.input, rowsOf(cursor.input, cursor.at));
|
|
2225
|
+
cursor.at += 1;
|
|
2226
|
+
}
|
|
2227
|
+
const held = smallest;
|
|
2228
|
+
for (let at = cursors.length - 1; at >= 0; at--) {
|
|
2229
|
+
const cursor = cursors[at];
|
|
2230
|
+
if (cursor.bytes !== held && compareBytes(cursor.bytes, held) !== 0) continue;
|
|
2231
|
+
const table = cursor.input.segment.arrays.tokenTable;
|
|
2232
|
+
if (cursor.at >= table.size) cursors.splice(at, 1);
|
|
2233
|
+
else cursor.bytes = table.tokenBytesAt(cursor.at);
|
|
2234
|
+
}
|
|
2235
|
+
tokensWalked += 1;
|
|
2236
|
+
if (tokensWalked % WRITER_YIELD_INTERVAL_TOKENS === 0) await yieldToEventLoop3();
|
|
2237
|
+
if (rows.count === 0) continue;
|
|
2238
|
+
if (!rows.isOrdered()) rows.sort();
|
|
2239
|
+
const token = frequencies.length;
|
|
2240
|
+
tokens.add(held);
|
|
2241
|
+
tokenOffsets.add(tokens.length);
|
|
2242
|
+
frequencies.add(rows.distinctDocuments());
|
|
2243
|
+
await sinks.postings.write(littleEndianBytes(rows.docIds.subarray(0, rows.count)));
|
|
2244
|
+
await sinks.postings.write(littleEndianBytes(rows.frequencies.subarray(0, rows.count)));
|
|
2245
|
+
await sinks.postings.write(littleEndianBytes(rows.fields.subarray(0, rows.count)));
|
|
2246
|
+
total[0] = rows.occurrences();
|
|
2247
|
+
await sinks.tokenTotals.write(littleEndianBytes(total));
|
|
2248
|
+
if (sinks.positionOffsets !== null && sinks.positions !== null) {
|
|
2249
|
+
for (let at = 0; at < rows.count; at++) {
|
|
2250
|
+
offsetScratch[0] = positionsWritten + rows.positionStarts[at];
|
|
2251
|
+
await sinks.positionOffsets.write(littleEndianBytes(offsetScratch));
|
|
2252
|
+
}
|
|
2253
|
+
await sinks.positions.write(littleEndianBytes(rows.positions.subarray(0, rows.positionCount)));
|
|
2254
|
+
positionsWritten += rows.positionCount;
|
|
2255
|
+
}
|
|
2256
|
+
if (rows.count > SMALLEST_BLOCK_POSTINGS) {
|
|
2257
|
+
blockLists.add(token);
|
|
2258
|
+
const summaries = blockSummaryBytesOf(rows, lengthColumnsByField);
|
|
2259
|
+
blockSummaries.add(summaries);
|
|
2260
|
+
blockListOffsets.add(blockSummaries.length / BLOCK_SUMMARY_BYTES);
|
|
2261
|
+
}
|
|
2262
|
+
await addRemovalsOf(rows, token, removals);
|
|
2263
|
+
postingsWritten += rows.count;
|
|
2264
|
+
postingOffsets.add(postingsWritten);
|
|
2265
|
+
}
|
|
2266
|
+
if (sinks.positionOffsets !== null) {
|
|
2267
|
+
offsetScratch[0] = positionsWritten;
|
|
2268
|
+
await sinks.positionOffsets.write(littleEndianBytes(offsetScratch));
|
|
2269
|
+
}
|
|
2270
|
+
const tokenCount = frequencies.length;
|
|
2271
|
+
const slots = new Uint32Array(new ArrayBuffer(tokenCount * 4));
|
|
2272
|
+
for (let at = 0; at < tokenCount; at++) slots[at] = at;
|
|
2273
|
+
return {
|
|
2274
|
+
tokenTable: {
|
|
2275
|
+
blob: tokens.finished(shared),
|
|
2276
|
+
offsets: tokenOffsets.finished(shared),
|
|
2277
|
+
payloadSlots: slots,
|
|
2278
|
+
documentFrequencies: frequencies.finished(shared)
|
|
2279
|
+
},
|
|
2280
|
+
postingOffsets: postingOffsets.finished(shared),
|
|
2281
|
+
blockLists: blockLists.finished(shared),
|
|
2282
|
+
blockListOffsets: blockListOffsets.finished(shared),
|
|
2283
|
+
blockSummaries: blockSummaries.finished(shared)
|
|
2284
|
+
};
|
|
2285
|
+
}
|
|
2286
|
+
|
|
2287
|
+
// src/persistence/paged/write-segment.ts
|
|
2288
|
+
var RESERVED_BYTES = 14;
|
|
2289
|
+
var RESIDENT_SECTION_ORDER = [
|
|
2290
|
+
"tokens",
|
|
2291
|
+
"tokenOffsets",
|
|
2292
|
+
"documentFrequencies",
|
|
2293
|
+
"postingOffsets",
|
|
2294
|
+
"blockLists",
|
|
2295
|
+
"blockListOffsets",
|
|
2296
|
+
"blockSummaries",
|
|
2297
|
+
"fieldLengths",
|
|
2298
|
+
"numeric",
|
|
2299
|
+
"sizes",
|
|
2300
|
+
"boolean",
|
|
2301
|
+
"enum",
|
|
2302
|
+
"geopoint",
|
|
2303
|
+
"pattern",
|
|
2304
|
+
"partialWords",
|
|
2305
|
+
"documentBlocks",
|
|
2306
|
+
"idSample"
|
|
2307
|
+
];
|
|
2308
|
+
function buildInputs(selections, fieldNames) {
|
|
2309
|
+
let documentCount = 0;
|
|
2310
|
+
const inputs = selections.map((selection) => {
|
|
2311
|
+
const count = selection.segment.arrays.documentCount;
|
|
2312
|
+
const remap = new Int32Array(count).fill(-1);
|
|
2313
|
+
for (let ordinal = 0; ordinal < count; ordinal++) {
|
|
2314
|
+
if (selectionKeeps(selection, ordinal)) {
|
|
2315
|
+
remap[ordinal] = documentCount;
|
|
2316
|
+
documentCount += 1;
|
|
2317
|
+
}
|
|
2318
|
+
}
|
|
2319
|
+
return { segment: selection.segment, remap, fieldIndexRemap: fieldIndexRemapOf(selection.segment, fieldNames) };
|
|
2320
|
+
});
|
|
2321
|
+
return { inputs, documentCount };
|
|
2322
|
+
}
|
|
2323
|
+
function postingUpperBound(inputs) {
|
|
2324
|
+
let total = 0;
|
|
2325
|
+
for (const { segment } of inputs) {
|
|
2326
|
+
const { postingOffsets, tokenTable } = segment.arrays;
|
|
2327
|
+
if (tokenTable.size > 0) {
|
|
2328
|
+
let largest = 0;
|
|
2329
|
+
for (let at = 0; at < postingOffsets.length; at++) {
|
|
2330
|
+
if (postingOffsets[at] > largest) largest = postingOffsets[at];
|
|
2331
|
+
}
|
|
2332
|
+
total += largest;
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
return total;
|
|
2336
|
+
}
|
|
2337
|
+
function onlyBytes(out) {
|
|
2338
|
+
const joined = new Uint8Array(out.byteLength);
|
|
2339
|
+
let at = 0;
|
|
2340
|
+
for (const chunk of out.chunks) {
|
|
2341
|
+
joined.set(chunk, at);
|
|
2342
|
+
at += chunk.byteLength;
|
|
2343
|
+
}
|
|
2344
|
+
return joined;
|
|
2345
|
+
}
|
|
2346
|
+
function keepsRecordsOf(request, inputs) {
|
|
2347
|
+
if (request.keepsWriteRecords === true || request.writeRecords !== void 0) return true;
|
|
2348
|
+
return inputs.length > 0 && inputs.every((input) => input.segment.paged?.writeRecordsHeld === true);
|
|
2349
|
+
}
|
|
2350
|
+
async function writePagedSegment(request) {
|
|
2351
|
+
const { directory, key } = request;
|
|
2352
|
+
const fieldNames = [];
|
|
2353
|
+
const { inputs, documentCount } = buildInputs(request.selections, fieldNames);
|
|
2354
|
+
const lengths = mergeFieldLengths(inputs, documentCount);
|
|
2355
|
+
const keepsRecords = keepsRecordsOf(request, inputs);
|
|
2356
|
+
const sinks = [];
|
|
2357
|
+
const finished = /* @__PURE__ */ new Map();
|
|
2358
|
+
const residentBytes = /* @__PURE__ */ new Map();
|
|
2359
|
+
const temporaries = [];
|
|
2360
|
+
async function sink(name) {
|
|
2361
|
+
const temporaryKey = `${key}.part-${name}`;
|
|
2362
|
+
temporaries.push(temporaryKey);
|
|
2363
|
+
const opened = await openSectionSink(directory, temporaryKey);
|
|
2364
|
+
sinks.push(opened);
|
|
2365
|
+
return opened;
|
|
2366
|
+
}
|
|
2367
|
+
const postings = await sink("postings");
|
|
2368
|
+
const tokenTotals = await sink("tokenTotals");
|
|
2369
|
+
const tracksPositions = inputs.some(
|
|
2370
|
+
(input) => input.segment.paged?.snapshot.tracksPositions ?? input.segment.arrays.positionOffsets !== null
|
|
2371
|
+
);
|
|
2372
|
+
const positionOffsets = tracksPositions ? await sink("positionOffsets") : null;
|
|
2373
|
+
const positions = tracksPositions ? await sink("positions") : null;
|
|
2374
|
+
const documents = await sink("documents");
|
|
2375
|
+
const ids = await sink("ids");
|
|
2376
|
+
const idOffsets = await sink("idOffsets");
|
|
2377
|
+
const idOrder = await sink("idOrder");
|
|
2378
|
+
const removalOffsets = await sink("removalOffsets");
|
|
2379
|
+
const removalValues = await sink("removals");
|
|
2380
|
+
const seqNos = keepsRecords ? await sink("seqNos") : null;
|
|
2381
|
+
const primaryTerms = keepsRecords ? await sink("primaryTerms") : null;
|
|
2382
|
+
const removals = await openRemovalWriter(directory, key, documentCount, postingUpperBound(inputs));
|
|
2383
|
+
const temporaryAssembly = `${key}.part-assembled`;
|
|
2384
|
+
try {
|
|
2385
|
+
const lengthByName = /* @__PURE__ */ new Map();
|
|
2386
|
+
for (let at = 0; at < lengths.fieldLengthNames.length; at++) {
|
|
2387
|
+
lengthByName.set(lengths.fieldLengthNames[at], lengths.fieldLengthColumns[at]);
|
|
2388
|
+
}
|
|
2389
|
+
const columnsByField = fieldNames.map((name) => lengthByName.get(name) ?? null);
|
|
2390
|
+
const columns = await writeMergedLists(
|
|
2391
|
+
inputs,
|
|
2392
|
+
columnsByField,
|
|
2393
|
+
{ postings, tokenTotals, positionOffsets, positions },
|
|
2394
|
+
removals,
|
|
2395
|
+
false
|
|
2396
|
+
);
|
|
2397
|
+
await removals.drain(async (offsets, values) => {
|
|
2398
|
+
await removalOffsets.write(littleEndianBytes(offsets));
|
|
2399
|
+
await removalValues.write(littleEndianBytes(values));
|
|
2400
|
+
});
|
|
2401
|
+
const documentBlocks = await writeDocumentRows(
|
|
2402
|
+
inputs,
|
|
2403
|
+
{ documents, ids, idOffsets, seqNos, primaryTerms },
|
|
2404
|
+
request.writeRecords
|
|
2405
|
+
);
|
|
2406
|
+
const idSample = await writeIdOrder(inputs, idOrder);
|
|
2407
|
+
const surfaces = mergeSurfaceForms(inputs, request.textAnalysis);
|
|
2408
|
+
const surfaceTable = surfaces === null ? null : encodeSurfaceTable(surfaces);
|
|
2409
|
+
if (surfaceTable !== null && surfaceTable.counts.length > 0) {
|
|
2410
|
+
const surfaceChunks = new MessagePackChunks();
|
|
2411
|
+
writeSurfaceForms(surfaceChunks, { surfaceTable });
|
|
2412
|
+
const surfaceForms = await sink("surfaceForms");
|
|
2413
|
+
await surfaceForms.write(onlyBytes(surfaceChunks));
|
|
2414
|
+
}
|
|
2415
|
+
const tokenCount = columns.tokenTable.documentFrequencies.length;
|
|
2416
|
+
const partial = mergePartialWords(inputs, tokenCount, request.partialWordAnalysis);
|
|
2417
|
+
const partialTable = partial.reused ?? (partial.spellings === null ? null : buildPartialWordTableFrom(partial.spellings, wrapFrozenTokenTable(columns.tokenTable)));
|
|
2418
|
+
const fields = mergeFieldIndexes(inputs);
|
|
2419
|
+
const resident = (name, write) => {
|
|
2420
|
+
const out = new MessagePackChunks();
|
|
2421
|
+
write(out);
|
|
2422
|
+
residentBytes.set(name, onlyBytes(out));
|
|
2423
|
+
};
|
|
2424
|
+
residentBytes.set("tokens", columns.tokenTable.blob);
|
|
2425
|
+
residentBytes.set("tokenOffsets", littleEndianBytes(columns.tokenTable.offsets));
|
|
2426
|
+
residentBytes.set("documentFrequencies", littleEndianBytes(columns.tokenTable.documentFrequencies));
|
|
2427
|
+
residentBytes.set("postingOffsets", littleEndianBytes(columns.postingOffsets));
|
|
2428
|
+
residentBytes.set("blockLists", littleEndianBytes(columns.blockLists));
|
|
2429
|
+
residentBytes.set("blockListOffsets", littleEndianBytes(columns.blockListOffsets));
|
|
2430
|
+
residentBytes.set("blockSummaries", columns.blockSummaries);
|
|
2431
|
+
resident("fieldLengths", (out) => writeFieldLengths(out, lengths));
|
|
2432
|
+
resident("numeric", (out) => writeValueColumns(out, fields.numeric));
|
|
2433
|
+
resident("sizes", (out) => writeValueColumns(out, fields.sizes));
|
|
2434
|
+
resident("boolean", (out) => writeBooleanColumns(out, fields.boolean));
|
|
2435
|
+
resident("enum", (out) => writeEnumColumns(out, fields.enums));
|
|
2436
|
+
resident("geopoint", (out) => writeGeopointColumns(out, fields.geo));
|
|
2437
|
+
resident("pattern", (out) => writePatternColumns(out, fields.pattern));
|
|
2438
|
+
resident("partialWords", (out) => writePartialWords(out, { partialWords: partialTable }));
|
|
2439
|
+
residentBytes.set("documentBlocks", documentBlocks);
|
|
2440
|
+
residentBytes.set("idSample", idSample);
|
|
2441
|
+
for (const opened of sinks) {
|
|
2442
|
+
const done = await opened.finish();
|
|
2443
|
+
const name = done.temporaryKey.slice(`${key}.part-`.length);
|
|
2444
|
+
finished.set(name, done);
|
|
2445
|
+
}
|
|
2446
|
+
const planned = [];
|
|
2447
|
+
for (const name of RESIDENT_SECTION_ORDER) {
|
|
2448
|
+
const bytes = residentBytes.get(name);
|
|
2449
|
+
if (bytes === void 0) continue;
|
|
2450
|
+
planned.push({ name, length: bytes.byteLength, checksums: checksumsOfPages(bytes) });
|
|
2451
|
+
}
|
|
2452
|
+
for (const [name, done] of finished) planned.push({ name, length: done.length, checksums: done.checksums });
|
|
2453
|
+
const encoded = encodePagedSegmentHead(
|
|
2454
|
+
{ schema: request.schema, tombstones: [...request.tombstones], documentCount, fieldNames },
|
|
2455
|
+
planned
|
|
2456
|
+
);
|
|
2457
|
+
requireWithinFileLimit(HEADER_SIZE + encoded.payloadLength, `segment "${key}"`);
|
|
2458
|
+
await directory.remove(temporaryAssembly);
|
|
2459
|
+
temporaries.push(temporaryAssembly);
|
|
2460
|
+
const assembly = await directory.appendHandle(temporaryAssembly);
|
|
2461
|
+
let state = crc32Init();
|
|
2462
|
+
let cursor = 0;
|
|
2463
|
+
const appendPayload = async (bytes) => {
|
|
2464
|
+
state = crc32Update(state, bytes);
|
|
2465
|
+
await assembly.append(bytes);
|
|
2466
|
+
cursor += bytes.byteLength;
|
|
2467
|
+
};
|
|
2468
|
+
const alignTo = async (offset) => {
|
|
2469
|
+
if (offset < cursor || offset - cursor >= SECTION_ALIGNMENT_BYTES) {
|
|
2470
|
+
throw new NarsilError(
|
|
2471
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2472
|
+
`Section offset ${offset} of "${key}" does not follow byte ${cursor}`,
|
|
2473
|
+
{ key, offset, cursor }
|
|
2474
|
+
);
|
|
2475
|
+
}
|
|
2476
|
+
if (offset > cursor) await appendPayload(new Uint8Array(offset - cursor));
|
|
2477
|
+
};
|
|
2478
|
+
try {
|
|
2479
|
+
await assembly.append(new Uint8Array(HEADER_SIZE));
|
|
2480
|
+
await appendPayload(encoded.prefix);
|
|
2481
|
+
await appendPayload(encoded.head);
|
|
2482
|
+
for (const section of encoded.sections) {
|
|
2483
|
+
await alignTo(section.offset);
|
|
2484
|
+
const name = section.name;
|
|
2485
|
+
const heldBytes = residentBytes.get(name);
|
|
2486
|
+
if (heldBytes !== void 0) {
|
|
2487
|
+
await appendPayload(heldBytes);
|
|
2488
|
+
continue;
|
|
2489
|
+
}
|
|
2490
|
+
const done = finished.get(name);
|
|
2491
|
+
if (done === void 0) {
|
|
2492
|
+
throw new NarsilError(
|
|
2493
|
+
ErrorCodes.PERSISTENCE_SAVE_FAILED,
|
|
2494
|
+
`Section "${name}" of "${key}" holds no written bytes`,
|
|
2495
|
+
{ key, section: name }
|
|
2496
|
+
);
|
|
2497
|
+
}
|
|
2498
|
+
await copySection(directory, done, appendPayload);
|
|
2499
|
+
}
|
|
2500
|
+
await assembly.sync();
|
|
2501
|
+
} finally {
|
|
2502
|
+
await assembly.close();
|
|
2503
|
+
}
|
|
2504
|
+
const checksum = crc32Final(state);
|
|
2505
|
+
const [major, minor, patch] = engineVersionParts();
|
|
2506
|
+
const header = writeHeader({
|
|
2507
|
+
magic: "NRSL",
|
|
2508
|
+
envelopeFormatVersion: PAGED_SEGMENT_ENVELOPE_VERSION,
|
|
2509
|
+
engineVersionMajor: major,
|
|
2510
|
+
engineVersionMinor: minor,
|
|
2511
|
+
engineVersionPatch: patch,
|
|
2512
|
+
payloadLength: encoded.payloadLength,
|
|
2513
|
+
flags: {
|
|
2514
|
+
compressionEnabled: false,
|
|
2515
|
+
compressionAlgorithm: "none",
|
|
2516
|
+
checksumPresent: true,
|
|
2517
|
+
encryptionEnabled: false
|
|
2518
|
+
},
|
|
2519
|
+
checksum,
|
|
2520
|
+
reserved: new Uint8Array(RESERVED_BYTES)
|
|
2521
|
+
});
|
|
2522
|
+
const patched = await directory.markerHandle(temporaryAssembly);
|
|
2523
|
+
try {
|
|
2524
|
+
await patched.writeSlot(0, header);
|
|
2525
|
+
} finally {
|
|
2526
|
+
await patched.close();
|
|
2527
|
+
}
|
|
2528
|
+
await directory.move(temporaryAssembly, key);
|
|
2529
|
+
await directory.syncDirectoryOf(key);
|
|
2530
|
+
return {
|
|
2531
|
+
bytes: HEADER_SIZE + encoded.payloadLength,
|
|
2532
|
+
checksum,
|
|
2533
|
+
documentCount,
|
|
2534
|
+
tombstoneCount: request.tombstones.length
|
|
2535
|
+
};
|
|
2536
|
+
} catch (error) {
|
|
2537
|
+
await removals.abandon();
|
|
2538
|
+
throw error;
|
|
2539
|
+
} finally {
|
|
2540
|
+
for (const opened of sinks) await opened.abandon().catch(() => void 0);
|
|
2541
|
+
for (const temporaryKey of temporaries) await directory.remove(temporaryKey).catch(() => void 0);
|
|
2542
|
+
}
|
|
2543
|
+
}
|
|
2544
|
+
function checksumsOfPages(bytes) {
|
|
2545
|
+
const pages = Math.ceil(bytes.byteLength / SEGMENT_PAGE_BYTES);
|
|
2546
|
+
const checksums = new Uint8Array(pages * 4);
|
|
2547
|
+
const view = new DataView(checksums.buffer);
|
|
2548
|
+
for (let page = 0; page < pages; page++) {
|
|
2549
|
+
const from = page * SEGMENT_PAGE_BYTES;
|
|
2550
|
+
view.setUint32(page * 4, crc32(bytes.subarray(from, Math.min(from + SEGMENT_PAGE_BYTES, bytes.byteLength))), true);
|
|
2551
|
+
}
|
|
2552
|
+
return checksums;
|
|
2553
|
+
}
|
|
2554
|
+
async function copySection(directory, section, append) {
|
|
2555
|
+
const { readRange } = directory;
|
|
2556
|
+
if (readRange === void 0) {
|
|
2557
|
+
throw new NarsilError(ErrorCodes.PERSISTENCE_SAVE_FAILED, "A paged segment requires a directory that reads ranges");
|
|
2558
|
+
}
|
|
2559
|
+
for (let at = 0; at < section.length; at += FILE_ASSEMBLY_CHUNK_BYTES) {
|
|
2560
|
+
const size = Math.min(FILE_ASSEMBLY_CHUNK_BYTES, section.length - at);
|
|
2561
|
+
await append(await readRange.call(directory, section.temporaryKey, at, size));
|
|
2562
|
+
}
|
|
2563
|
+
}
|
|
2564
|
+
|
|
2565
|
+
// src/persistence/paged/rewrite-spill.ts
|
|
2566
|
+
function openRewriteSpill(storage, manager, target) {
|
|
2567
|
+
const config = target?.config ?? manager.config;
|
|
2568
|
+
return createRewriteSpill(storage, manager.indexName, {
|
|
2569
|
+
schema: flattenSchema(config.schema),
|
|
2570
|
+
trackPositions: config.trackPositions ?? true,
|
|
2571
|
+
partialWordAnalysis: target === void 0 ? manager.partialWordAnalysis : target.partialWordAnalysis,
|
|
2572
|
+
textAnalysis: {
|
|
2573
|
+
language: target?.language ?? manager.language,
|
|
2574
|
+
options: resolvePartitionInsertOptions(config, target?.analysis ?? manager.analysis)
|
|
2575
|
+
}
|
|
2576
|
+
});
|
|
2577
|
+
}
|
|
2578
|
+
function createRewriteSpill(storage, indexName, analysis) {
|
|
2579
|
+
const { directory, writeRecords } = storage;
|
|
2580
|
+
const prefix = `${indexName}/rebuilt/${generateId()}/`;
|
|
2581
|
+
const opened = [];
|
|
2582
|
+
let nextFile = 0;
|
|
2583
|
+
async function removeFiles() {
|
|
2584
|
+
for (const key of await directory.list(prefix)) await directory.remove(key);
|
|
2585
|
+
}
|
|
2586
|
+
function freezeLive(partition) {
|
|
2587
|
+
if (!isCompositePartition(partition)) return null;
|
|
2588
|
+
return partition.freezeLiveTail((payload, documents) => {
|
|
2589
|
+
const snapshot = freezeSegmentShared(payload, documents, void 0, analysis.partialWordAnalysis);
|
|
2590
|
+
return snapshot === null ? createFrozenSegment(payload, documents) : createSharedFrozenSegment(snapshot);
|
|
2591
|
+
});
|
|
2592
|
+
}
|
|
2593
|
+
return {
|
|
2594
|
+
newPartition(partitionId) {
|
|
2595
|
+
const live = createPartitionIndex(partitionId, analysis.trackPositions);
|
|
2596
|
+
return createCompositePartition(partitionId, analysis.trackPositions, live, analysis.textAnalysis);
|
|
2597
|
+
},
|
|
2598
|
+
async spillIfFull(partition, partitionId) {
|
|
2599
|
+
if (!isCompositePartition(partition)) return;
|
|
2600
|
+
if (partition.live.estimateMemoryBytes() < REWRITE_SPILL_BYTES) return;
|
|
2601
|
+
const frozen = freezeLive(partition);
|
|
2602
|
+
if (frozen === null) return;
|
|
2603
|
+
const key = `${prefix}${nextFile}`;
|
|
2604
|
+
nextFile += 1;
|
|
2605
|
+
const written = await writePagedSegment({
|
|
2606
|
+
directory,
|
|
2607
|
+
key,
|
|
2608
|
+
schema: analysis.schema,
|
|
2609
|
+
tombstones: [],
|
|
2610
|
+
selections: [{ segment: frozen, keep: (ordinal) => !frozen.isTombstoned(ordinal) }],
|
|
2611
|
+
partialWordAnalysis: analysis.partialWordAnalysis,
|
|
2612
|
+
textAnalysis: analysis.textAnalysis,
|
|
2613
|
+
...writeRecords === null ? {} : { writeRecords: writeRecords.captureSegments(indexName, partitionId, [frozen]) }
|
|
2614
|
+
});
|
|
2615
|
+
const listing = await pagedListingOf(directory, { key, bytes: written.bytes, checksum: written.checksum });
|
|
2616
|
+
const segment = createPagedFrozenSegment(openPagedSnapshot(listing));
|
|
2617
|
+
opened.push(segment);
|
|
2618
|
+
partition.swapFrozenSegments([frozen.segmentId], segment);
|
|
2619
|
+
},
|
|
2620
|
+
async abandon() {
|
|
2621
|
+
for (const segment of opened.splice(0)) segment.paged?.close();
|
|
2622
|
+
await removeFiles();
|
|
2623
|
+
},
|
|
2624
|
+
async removeUnread(manager) {
|
|
2625
|
+
for (const partition of manager.getAllPartitions()) {
|
|
2626
|
+
if (!isCompositePartition(partition)) continue;
|
|
2627
|
+
for (const segment of partition.frozenSegments()) {
|
|
2628
|
+
if (segment.paged?.key.startsWith(prefix)) return;
|
|
2629
|
+
}
|
|
2630
|
+
}
|
|
2631
|
+
for (const segment of opened.splice(0)) segment.paged?.close();
|
|
2632
|
+
await removeFiles();
|
|
2633
|
+
}
|
|
2634
|
+
};
|
|
2635
|
+
}
|
|
2636
|
+
|
|
2637
|
+
export { actionPartitionsOf, adoptSegmentsOnWorker, bytesThatSegmentsHoldInMemory, checkEverySectionPage, checkPagedSegmentFully, closePagedFile, closeUnusedSegments, countsAsLatestHolder, createPagedFrozenSegment, createSectionReader, isPagedSegmentFile, keysThatSegmentsStillRead, mergePagedWindow, openFileVectorReads, openPagedFile, openPagedSegmentScan, openPagedSnapshot, openRewriteSpill, pagedListingOf, pagedPartitionLines, pagedTombstonesFor, prepareDiskAdoption, readCacheOfThisThread, readFileRange, readWholeSectionChecked, startReadingOpeningSectionsAhead, swapPreparedPartitions, writePagedSegment };
|