datalevin-node 0.10.18 → 1.0.2

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/src/llm.js ADDED
@@ -0,0 +1,114 @@
1
+ import { _BINDINGS } from "./interop.js";
2
+ import { ResourceWrapper } from "./resource.js";
3
+ import { toJsResult } from "./result.js";
4
+
5
+ function numberList(values) {
6
+ return values.map((value) => Number(value));
7
+ }
8
+
9
+ export class LlamaEmbedder extends ResourceWrapper {
10
+ constructor(handle) {
11
+ super(
12
+ handle,
13
+ (rawHandle) => _BINDINGS.closeLlamaEmbedder(rawHandle),
14
+ (rawHandle) => _BINDINGS.llamaEmbedderClosed(rawHandle),
15
+ "llama embedder"
16
+ );
17
+ }
18
+
19
+ async modelPath() {
20
+ return toJsResult(await _BINDINGS.llamaEmbedderModelPath(this.rawHandle()));
21
+ }
22
+
23
+ async gpuLayers() {
24
+ return Number(await toJsResult(await _BINDINGS.llamaEmbedderGpuLayers(this.rawHandle())));
25
+ }
26
+
27
+ async ctxSize() {
28
+ return Number(await toJsResult(await _BINDINGS.llamaEmbedderCtxSize(this.rawHandle())));
29
+ }
30
+
31
+ async contextSize() {
32
+ return Number(await toJsResult(await _BINDINGS.llamaEmbedderContextSize(this.rawHandle())));
33
+ }
34
+
35
+ async batchSize() {
36
+ return Number(await toJsResult(await _BINDINGS.llamaEmbedderBatchSize(this.rawHandle())));
37
+ }
38
+
39
+ async threads() {
40
+ return Number(await toJsResult(await _BINDINGS.llamaEmbedderThreads(this.rawHandle())));
41
+ }
42
+
43
+ async dimensions() {
44
+ return Number(await toJsResult(await _BINDINGS.llamaEmbedderDimensions(this.rawHandle())));
45
+ }
46
+
47
+ async embed(text) {
48
+ return numberList(await toJsResult(await _BINDINGS.llamaEmbedderEmbed(this.rawHandle(), text)));
49
+ }
50
+
51
+ async embedAll(texts) {
52
+ const vectors = await toJsResult(await _BINDINGS.llamaEmbedderEmbedAll(this.rawHandle(), texts));
53
+ return vectors.map(numberList);
54
+ }
55
+
56
+ async tokenCount(text) {
57
+ return Number(await toJsResult(await _BINDINGS.llamaEmbedderTokenCount(this.rawHandle(), text)));
58
+ }
59
+
60
+ async tokenize(text) {
61
+ return numberList(await toJsResult(await _BINDINGS.llamaEmbedderTokenize(this.rawHandle(), text)));
62
+ }
63
+
64
+ async detokenize(tokens) {
65
+ return toJsResult(await _BINDINGS.llamaEmbedderDetokenize(this.rawHandle(), tokens));
66
+ }
67
+
68
+ async truncateText(text, maxTokens) {
69
+ return toJsResult(await _BINDINGS.llamaEmbedderTruncateText(this.rawHandle(), text, maxTokens));
70
+ }
71
+ }
72
+
73
+ export class LlamaGenerator extends ResourceWrapper {
74
+ constructor(handle) {
75
+ super(
76
+ handle,
77
+ (rawHandle) => _BINDINGS.closeLlamaGenerator(rawHandle),
78
+ (rawHandle) => _BINDINGS.llamaGeneratorClosed(rawHandle),
79
+ "llama generator"
80
+ );
81
+ }
82
+
83
+ async modelPath() {
84
+ return toJsResult(await _BINDINGS.llamaGeneratorModelPath(this.rawHandle()));
85
+ }
86
+
87
+ async gpuLayers() {
88
+ return Number(await toJsResult(await _BINDINGS.llamaGeneratorGpuLayers(this.rawHandle())));
89
+ }
90
+
91
+ async ctxSize() {
92
+ return Number(await toJsResult(await _BINDINGS.llamaGeneratorCtxSize(this.rawHandle())));
93
+ }
94
+
95
+ async contextSize() {
96
+ return Number(await toJsResult(await _BINDINGS.llamaGeneratorContextSize(this.rawHandle())));
97
+ }
98
+
99
+ async threads() {
100
+ return Number(await toJsResult(await _BINDINGS.llamaGeneratorThreads(this.rawHandle())));
101
+ }
102
+
103
+ async tokenCount(text) {
104
+ return Number(await toJsResult(await _BINDINGS.llamaGeneratorTokenCount(this.rawHandle(), text)));
105
+ }
106
+
107
+ async generate(prompt, maxTokens) {
108
+ return toJsResult(await _BINDINGS.llamaGeneratorGenerate(this.rawHandle(), prompt, maxTokens));
109
+ }
110
+
111
+ async summarize(text, maxTokens) {
112
+ return toJsResult(await _BINDINGS.llamaGeneratorSummarize(this.rawHandle(), text, maxTokens));
113
+ }
114
+ }
package/src/raw.js CHANGED
@@ -1,5 +1,30 @@
1
+ import {
2
+ CONNECTION_WITH_TRANSACTION_UNSUPPORTED,
3
+ _BINDINGS,
4
+ datomA,
5
+ datomAdded,
6
+ datomE,
7
+ datomIs,
8
+ datomTx,
9
+ datomV
10
+ } from "./interop.js";
11
+ import { toJava } from "./convert.js";
1
12
  import { DatalevinError } from "./errors.js";
2
- import { _BINDINGS } from "./interop.js";
13
+ import { callJavaMethod } from "./jvm.js";
14
+ import { toJsResult } from "./result.js";
15
+
16
+ function datomTuple(e, attr, value, tx = undefined, added = undefined) {
17
+ if (added !== undefined && tx === undefined) {
18
+ throw new TypeError("tx is required when added is provided");
19
+ }
20
+ if (added !== undefined) {
21
+ return [e, attr, value, tx, added];
22
+ }
23
+ if (tx !== undefined) {
24
+ return [e, attr, value, tx];
25
+ }
26
+ return [e, attr, value];
27
+ }
3
28
 
4
29
  function resourceHandle(value) {
5
30
  if (typeof value?.rawHandle === "function") {
@@ -8,6 +33,10 @@ function resourceHandle(value) {
8
33
  return value;
9
34
  }
10
35
 
36
+ function hasValue(value) {
37
+ return value !== null && value !== undefined;
38
+ }
39
+
11
40
  class RawInterop {
12
41
  async apiInfo() {
13
42
  return _BINDINGS.apiInfoRaw();
@@ -29,6 +58,14 @@ class RawInterop {
29
58
  return _BINDINGS.createConnection(dir, schema, opts, { shared });
30
59
  }
31
60
 
61
+ async initDb(datoms, dir = null, schema = null, opts = null) {
62
+ return _BINDINGS.initDb(datoms, dir, schema, opts);
63
+ }
64
+
65
+ async fillDb(conn, datoms) {
66
+ return _BINDINGS.fillDb(conn, datoms);
67
+ }
68
+
32
69
  async closeConnection(handle) {
33
70
  await _BINDINGS.closeConnection(resourceHandle(handle));
34
71
  }
@@ -38,11 +75,212 @@ class RawInterop {
38
75
  }
39
76
 
40
77
  async connectionDb(handle) {
41
- throw new DatalevinError(
42
- "Raw database values are not exposed by the Node binding. Use Connection query/pull/entity methods instead."
78
+ const { Database } = await import("./database.js");
79
+ return new Database(await _BINDINGS.connectionDb(resourceHandle(handle)));
80
+ }
81
+
82
+ async connectionEntity(handle, eid) {
83
+ return _BINDINGS.connectionEntity(resourceHandle(handle), eid);
84
+ }
85
+
86
+ async databaseEntid(db, eid) {
87
+ return _BINDINGS.databaseEntid(resourceHandle(db), eid);
88
+ }
89
+
90
+ async databaseEntity(db, eid) {
91
+ return _BINDINGS.databaseEntity(resourceHandle(db), eid);
92
+ }
93
+
94
+ async databaseEntityMap(db, eid) {
95
+ return toJsResult(await _BINDINGS.databaseEntityMap(resourceHandle(db), eid), { bridge: true });
96
+ }
97
+
98
+ async databasePull(db, selector, eid) {
99
+ return toJsResult(await _BINDINGS.databasePull(resourceHandle(db), selector, eid), { bridge: true });
100
+ }
101
+
102
+ async databasePullMany(db, selector, eids) {
103
+ return toJsResult(await _BINDINGS.databasePullMany(resourceHandle(db), selector, eids), { bridge: true });
104
+ }
105
+
106
+ async databaseCardinality(db, attr) {
107
+ return toJsResult(await _BINDINGS.databaseCardinality(resourceHandle(db), attr), { bridge: true });
108
+ }
109
+
110
+ async databaseAnalyze(db, attr = null) {
111
+ return toJsResult(await _BINDINGS.databaseAnalyze(resourceHandle(db), attr), { bridge: true });
112
+ }
113
+
114
+ async connectionTxDataToSimulatedReport(handle, txData) {
115
+ return _BINDINGS.connectionTxDataToSimulatedReport(resourceHandle(handle), txData);
116
+ }
117
+
118
+ async entityIs(value) {
119
+ return _BINDINGS.entityIs(value);
120
+ }
121
+
122
+ async entityId(entity) {
123
+ return _BINDINGS.entityId(resourceHandle(entity));
124
+ }
125
+
126
+ async entityGet(entity, attr) {
127
+ return _BINDINGS.entityGet(resourceHandle(entity), attr);
128
+ }
129
+
130
+ async entityContains(entity, attr) {
131
+ return _BINDINGS.entityContains(resourceHandle(entity), attr);
132
+ }
133
+
134
+ async entityTouch(entity) {
135
+ return toJsResult(await _BINDINGS.entityTouch(resourceHandle(entity)), { bridge: true });
136
+ }
137
+
138
+ async connectionDatalogKv(handle) {
139
+ return callJavaMethod(resourceHandle(handle), "datalogKV");
140
+ }
141
+
142
+ async connectionTransact(handle, txData, txMeta = null) {
143
+ return toJsResult(
144
+ await _BINDINGS.connectionTransact(resourceHandle(handle), txData, txMeta)
145
+ );
146
+ }
147
+
148
+ async connectionTransactAsync(handle, txData, txMeta = null) {
149
+ const future = await _BINDINGS.connectionTransactAsync(resourceHandle(handle), txData, txMeta);
150
+ return toJsResult(await callJavaMethod(future, "get"), { bridge: true });
151
+ }
152
+
153
+ async connectionAbortTransact(handle) {
154
+ return toJsResult(await _BINDINGS.connectionAbortTransact(resourceHandle(handle)));
155
+ }
156
+
157
+ async connectionDatoms(handle, index, c1 = null, c2 = null, c3 = null, limit = null) {
158
+ const args = [resourceHandle(handle), "datoms", await toJava(index), await toJava(c1), await toJava(c2), await toJava(c3)];
159
+ if (hasValue(limit)) {
160
+ args.push(await toJava(limit));
161
+ }
162
+ return toJsResult(await callJavaMethod(...args), { bridge: true });
163
+ }
164
+
165
+ async connectionSearchDatoms(handle, e = null, attr = null, value = null) {
166
+ return toJsResult(
167
+ await callJavaMethod(
168
+ resourceHandle(handle),
169
+ "searchDatoms",
170
+ await toJava(e),
171
+ await toJava(attr),
172
+ await toJava(value)
173
+ ),
174
+ { bridge: true }
175
+ );
176
+ }
177
+
178
+ async connectionCountDatoms(handle, e = null, attr = null, value = null) {
179
+ return toJsResult(
180
+ await callJavaMethod(
181
+ resourceHandle(handle),
182
+ "countDatoms",
183
+ await toJava(e),
184
+ await toJava(attr),
185
+ await toJava(value)
186
+ )
187
+ );
188
+ }
189
+
190
+ async connectionSeekDatoms(handle, index, c1 = null, c2 = null, c3 = null, limit = null) {
191
+ const args = [
192
+ resourceHandle(handle),
193
+ "seekDatoms",
194
+ await toJava(index),
195
+ await toJava(c1),
196
+ await toJava(c2),
197
+ await toJava(c3)
198
+ ];
199
+ if (hasValue(limit)) {
200
+ args.push(await toJava(limit));
201
+ }
202
+ return toJsResult(await callJavaMethod(...args), { bridge: true });
203
+ }
204
+
205
+ async connectionRseekDatoms(handle, index, c1 = null, c2 = null, c3 = null, limit = null) {
206
+ const args = [
207
+ resourceHandle(handle),
208
+ "rseekDatoms",
209
+ await toJava(index),
210
+ await toJava(c1),
211
+ await toJava(c2),
212
+ await toJava(c3)
213
+ ];
214
+ if (hasValue(limit)) {
215
+ args.push(await toJava(limit));
216
+ }
217
+ return toJsResult(await callJavaMethod(...args), { bridge: true });
218
+ }
219
+
220
+ async connectionIndexRange(handle, attr, start, end) {
221
+ return toJsResult(
222
+ await callJavaMethod(
223
+ resourceHandle(handle),
224
+ "indexRange",
225
+ await toJava(attr),
226
+ await toJava(start),
227
+ await toJava(end)
228
+ ),
229
+ { bridge: true }
43
230
  );
44
231
  }
45
232
 
233
+ async connectionFulltextDatoms(handle, query, opts = null) {
234
+ const result = hasValue(opts)
235
+ ? await callJavaMethod(resourceHandle(handle), "fulltextDatoms", query, await toJava(opts))
236
+ : await callJavaMethod(resourceHandle(handle), "fulltextDatoms", query);
237
+ return toJsResult(result, { bridge: true });
238
+ }
239
+
240
+ async connectionCopy(handle, dest, compact = null) {
241
+ if (hasValue(compact)) {
242
+ return callJavaMethod(resourceHandle(handle), "copy", dest, Boolean(compact));
243
+ }
244
+ return callJavaMethod(resourceHandle(handle), "copy", dest);
245
+ }
246
+
247
+ async connectionTxLogWatermarks(handle) {
248
+ return toJsResult(await callJavaMethod(resourceHandle(handle), "txLogWatermarks"), { bridge: true });
249
+ }
250
+
251
+ async connectionOpenTxLog(handle, fromLsn, uptoLsn = null) {
252
+ const result = hasValue(uptoLsn)
253
+ ? await callJavaMethod(resourceHandle(handle), "openTxLog", await toJava(fromLsn), await toJava(uptoLsn))
254
+ : await callJavaMethod(resourceHandle(handle), "openTxLog", await toJava(fromLsn));
255
+ return toJsResult(result, { bridge: true });
256
+ }
257
+
258
+ async connectionCreateSnapshot(handle) {
259
+ return toJsResult(await callJavaMethod(resourceHandle(handle), "createSnapshot"), { bridge: true });
260
+ }
261
+
262
+ async connectionListSnapshots(handle) {
263
+ return toJsResult(await callJavaMethod(resourceHandle(handle), "listSnapshots"), { bridge: true });
264
+ }
265
+
266
+ async connectionGcTxLogSegments(handle, retainFloorLsn = null) {
267
+ if (hasValue(retainFloorLsn)) {
268
+ return toJsResult(
269
+ await callJavaMethod(resourceHandle(handle), "gcTxLogSegments", await toJava(retainFloorLsn)),
270
+ { bridge: true }
271
+ );
272
+ }
273
+ return toJsResult(await callJavaMethod(resourceHandle(handle), "gcTxLogSegments"), { bridge: true });
274
+ }
275
+
276
+ async connectionWithTransaction(handle, fn) {
277
+ throw new DatalevinError(CONNECTION_WITH_TRANSACTION_UNSUPPORTED);
278
+ }
279
+
280
+ async connectionReIndex(handle, schema = null, opts = null) {
281
+ return _BINDINGS.connectionReIndex(resourceHandle(handle), schema, opts);
282
+ }
283
+
46
284
  async openKeyValue(dir, opts = null) {
47
285
  return _BINDINGS.openKeyValue(dir, opts);
48
286
  }
@@ -55,6 +293,118 @@ class RawInterop {
55
293
  return _BINDINGS.keyValueClosed(resourceHandle(handle));
56
294
  }
57
295
 
296
+ async keyValueBeginTransaction(handle) {
297
+ return _BINDINGS.keyValueBeginTransaction(resourceHandle(handle));
298
+ }
299
+
300
+ async keyValueCommitTransaction(tx) {
301
+ return toJsResult(await _BINDINGS.keyValueCommitTransaction(resourceHandle(tx)));
302
+ }
303
+
304
+ async keyValueAbortTransaction(tx) {
305
+ return toJsResult(await _BINDINGS.keyValueAbortTransaction(resourceHandle(tx)));
306
+ }
307
+
308
+ async keyValueWithTransaction(handle, fn) {
309
+ return toJsResult(await _BINDINGS.keyValueWithTransaction(resourceHandle(handle), fn), { bridge: true });
310
+ }
311
+
312
+ async keyValueReIndex(handle, opts = null) {
313
+ return _BINDINGS.keyValueReIndex(resourceHandle(handle), opts);
314
+ }
315
+
316
+ async newSearchEngine(kv, opts = null) {
317
+ return _BINDINGS.newSearchEngine(resourceHandle(kv), opts);
318
+ }
319
+
320
+ async searchAddDoc(search, docRef, docText, checkExist = null) {
321
+ return _BINDINGS.searchAddDoc(resourceHandle(search), docRef, docText, checkExist);
322
+ }
323
+
324
+ async searchRemoveDoc(search, docRef) {
325
+ return _BINDINGS.searchRemoveDoc(resourceHandle(search), docRef);
326
+ }
327
+
328
+ async searchClearDocs(search) {
329
+ return _BINDINGS.searchClearDocs(resourceHandle(search));
330
+ }
331
+
332
+ async searchDocIndexed(search, docRef) {
333
+ return _BINDINGS.searchDocIndexed(resourceHandle(search), docRef);
334
+ }
335
+
336
+ async searchDocCount(search) {
337
+ return toJsResult(await _BINDINGS.searchDocCount(resourceHandle(search)));
338
+ }
339
+
340
+ async search(search, query, opts = null) {
341
+ return toJsResult(await _BINDINGS.search(resourceHandle(search), query, opts));
342
+ }
343
+
344
+ async searchReIndex(search, opts = null) {
345
+ return _BINDINGS.searchReIndex(resourceHandle(search), opts);
346
+ }
347
+
348
+ async searchIndexWriter(kv, opts = null) {
349
+ return _BINDINGS.searchIndexWriter(resourceHandle(kv), opts);
350
+ }
351
+
352
+ async searchWrite(writer, docRef, docText) {
353
+ return _BINDINGS.searchWrite(resourceHandle(writer), docRef, docText);
354
+ }
355
+
356
+ async searchCommit(writer) {
357
+ return toJsResult(await _BINDINGS.searchCommit(resourceHandle(writer)));
358
+ }
359
+
360
+ async newVectorIndex(kv, opts) {
361
+ return _BINDINGS.newVectorIndex(resourceHandle(kv), opts);
362
+ }
363
+
364
+ async closeVectorIndex(index) {
365
+ await _BINDINGS.closeVectorIndex(resourceHandle(index));
366
+ }
367
+
368
+ async vectorIndexClosed(index) {
369
+ return _BINDINGS.vectorIndexClosed(resourceHandle(index));
370
+ }
371
+
372
+ async vectorAddVec(index, vecRef, vecData) {
373
+ return toJsResult(await _BINDINGS.vectorAddVec(resourceHandle(index), vecRef, vecData));
374
+ }
375
+
376
+ async vectorRemoveVec(index, vecRef) {
377
+ return toJsResult(await _BINDINGS.vectorRemoveVec(resourceHandle(index), vecRef));
378
+ }
379
+
380
+ async vectorIndexed(index, vecRef) {
381
+ return _BINDINGS.vectorIndexed(resourceHandle(index), vecRef);
382
+ }
383
+
384
+ async vectorSearch(index, queryVec, opts = null) {
385
+ return toJsResult(await _BINDINGS.vectorSearch(resourceHandle(index), queryVec, opts));
386
+ }
387
+
388
+ async vectorReIndex(index, opts = null) {
389
+ return _BINDINGS.vectorReIndex(resourceHandle(index), opts);
390
+ }
391
+
392
+ async vectorClear(index) {
393
+ return toJsResult(await _BINDINGS.vectorClear(resourceHandle(index)));
394
+ }
395
+
396
+ async vectorForceCheckpoint(index) {
397
+ return toJsResult(await _BINDINGS.vectorForceCheckpoint(resourceHandle(index)));
398
+ }
399
+
400
+ async vectorInfo(index) {
401
+ return toJsResult(await _BINDINGS.vectorInfo(resourceHandle(index)));
402
+ }
403
+
404
+ async vectorCheckpointState(index) {
405
+ return toJsResult(await _BINDINGS.vectorCheckpointState(resourceHandle(index)));
406
+ }
407
+
58
408
  async newClient(uri, opts = null) {
59
409
  return _BINDINGS.newClient(uri, opts);
60
410
  }
@@ -71,6 +421,10 @@ class RawInterop {
71
421
  return _BINDINGS.readEdn(edn);
72
422
  }
73
423
 
424
+ async writeEdn(value) {
425
+ return _BINDINGS.writeEdn(value);
426
+ }
427
+
74
428
  async keyword(value) {
75
429
  return _BINDINGS.keyword(value);
76
430
  }
@@ -119,6 +473,34 @@ class RawInterop {
119
473
  return _BINDINGS.lookupRef(value);
120
474
  }
121
475
 
476
+ async datom(e, attr, value, tx = undefined, added = undefined) {
477
+ return datomTuple(e, attr, value, tx, added);
478
+ }
479
+
480
+ async datomIs(value) {
481
+ return datomIs(value);
482
+ }
483
+
484
+ async datomE(value) {
485
+ return datomE(value);
486
+ }
487
+
488
+ async datomA(value) {
489
+ return datomA(value);
490
+ }
491
+
492
+ async datomV(value) {
493
+ return datomV(value);
494
+ }
495
+
496
+ async datomTx(value) {
497
+ return datomTx(value);
498
+ }
499
+
500
+ async datomAdded(value) {
501
+ return datomAdded(value);
502
+ }
503
+
122
504
  async txData(txData) {
123
505
  return _BINDINGS.txData(txData);
124
506
  }
package/src/search.js ADDED
@@ -0,0 +1,173 @@
1
+ import util from "node:util";
2
+
3
+ import { _BINDINGS } from "./interop.js";
4
+ import { toJsResult } from "./result.js";
5
+
6
+ export async function createAnalyzer(options = {}) {
7
+ const {
8
+ tokenizer = null,
9
+ tokenFilters = null,
10
+ extra = null,
11
+ ...opts
12
+ } = options ?? {};
13
+ const merged = { ...opts };
14
+ if (tokenizer !== null && tokenizer !== undefined) {
15
+ merged[":tokenizer"] = tokenizer;
16
+ }
17
+ if (tokenFilters !== null && tokenFilters !== undefined) {
18
+ merged[":token-filters"] = [...tokenFilters];
19
+ }
20
+ if (extra !== null && extra !== undefined) {
21
+ Object.assign(merged, extra);
22
+ }
23
+ return _BINDINGS.searchUtilsCreateAnalyzer(merged);
24
+ }
25
+
26
+ export async function lowerCaseTokenFilter() {
27
+ return _BINDINGS.searchUtilsLowerCaseTokenFilter();
28
+ }
29
+
30
+ export async function unaccentTokenFilter() {
31
+ return _BINDINGS.searchUtilsUnaccentTokenFilter();
32
+ }
33
+
34
+ export async function createStopWordsTokenFilter(stopWordsOrPredicate) {
35
+ return _BINDINGS.searchUtilsCreateStopWordsTokenFilter(stopWordsOrPredicate);
36
+ }
37
+
38
+ export async function enStopWordsTokenFilter() {
39
+ return _BINDINGS.searchUtilsEnStopWordsTokenFilter();
40
+ }
41
+
42
+ export async function prefixTokenFilter() {
43
+ return _BINDINGS.searchUtilsPrefixTokenFilter();
44
+ }
45
+
46
+ export async function createNgramTokenFilter(minGramSize, maxGramSize = null) {
47
+ return _BINDINGS.searchUtilsCreateNgramTokenFilter(minGramSize, maxGramSize);
48
+ }
49
+
50
+ export async function createMinLengthTokenFilter(minLength) {
51
+ return _BINDINGS.searchUtilsCreateMinLengthTokenFilter(minLength);
52
+ }
53
+
54
+ export async function createMaxLengthTokenFilter(maxLength) {
55
+ return _BINDINGS.searchUtilsCreateMaxLengthTokenFilter(maxLength);
56
+ }
57
+
58
+ export async function createStemmingTokenFilter(language) {
59
+ return _BINDINGS.searchUtilsCreateStemmingTokenFilter(language);
60
+ }
61
+
62
+ export async function createRegexpTokenizer(pattern) {
63
+ return _BINDINGS.searchUtilsCreateRegexpTokenizer(pattern);
64
+ }
65
+
66
+ export class SearchEngine {
67
+ constructor(handle) {
68
+ this._handle = handle;
69
+ }
70
+
71
+ rawHandle() {
72
+ if (this._handle === null) {
73
+ throw new Error("search engine is closed.");
74
+ }
75
+ return this._handle;
76
+ }
77
+
78
+ async closed() {
79
+ return this._handle === null;
80
+ }
81
+
82
+ async close() {
83
+ this._handle = null;
84
+ }
85
+
86
+ async addDoc(docRef, docText, { checkExist = null } = {}) {
87
+ await _BINDINGS.searchAddDoc(this.rawHandle(), docRef, docText, checkExist);
88
+ return this;
89
+ }
90
+
91
+ async removeDoc(docRef) {
92
+ await _BINDINGS.searchRemoveDoc(this.rawHandle(), docRef);
93
+ return this;
94
+ }
95
+
96
+ async clearDocs() {
97
+ await _BINDINGS.searchClearDocs(this.rawHandle());
98
+ return this;
99
+ }
100
+
101
+ async docIndexed(docRef) {
102
+ return Boolean(await _BINDINGS.searchDocIndexed(this.rawHandle(), docRef));
103
+ }
104
+
105
+ async docCount() {
106
+ return toJsResult(await _BINDINGS.searchDocCount(this.rawHandle()));
107
+ }
108
+
109
+ async search(query, opts = null) {
110
+ return toJsResult(await _BINDINGS.search(this.rawHandle(), query, opts));
111
+ }
112
+
113
+ async reIndex(opts = null) {
114
+ this._handle = await _BINDINGS.searchReIndex(this.rawHandle(), opts);
115
+ return this;
116
+ }
117
+
118
+ toString() {
119
+ return `<${this.constructor.name} ${this._handle === null ? "closed" : "open"}>`;
120
+ }
121
+
122
+ [util.inspect.custom]() {
123
+ return this.toString();
124
+ }
125
+
126
+ async [Symbol.asyncDispose]() {
127
+ await this.close();
128
+ }
129
+ }
130
+
131
+ export class SearchIndexWriter {
132
+ constructor(handle) {
133
+ this._handle = handle;
134
+ }
135
+
136
+ rawHandle() {
137
+ if (this._handle === null) {
138
+ throw new Error("search index writer is closed.");
139
+ }
140
+ return this._handle;
141
+ }
142
+
143
+ async closed() {
144
+ return this._handle === null;
145
+ }
146
+
147
+ async close() {
148
+ this._handle = null;
149
+ }
150
+
151
+ async write(docRef, docText) {
152
+ await _BINDINGS.searchWrite(this.rawHandle(), docRef, docText);
153
+ return this;
154
+ }
155
+
156
+ async commit() {
157
+ const result = await toJsResult(await _BINDINGS.searchCommit(this.rawHandle()));
158
+ this._handle = null;
159
+ return result;
160
+ }
161
+
162
+ toString() {
163
+ return `<${this.constructor.name} ${this._handle === null ? "closed" : "open"}>`;
164
+ }
165
+
166
+ [util.inspect.custom]() {
167
+ return this.toString();
168
+ }
169
+
170
+ async [Symbol.asyncDispose]() {
171
+ await this.close();
172
+ }
173
+ }