datalevin-node 1.0.2 → 1.2.10

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/connection.js CHANGED
@@ -1,9 +1,20 @@
1
- import { toEdnForm, toJava, toJs, toQueryInput } from "./convert.js";
1
+ import { randomUUID } from "node:crypto";
2
+
3
+ import {
4
+ toEdnForm,
5
+ toJava,
6
+ toJs,
7
+ toJsQueryResult,
8
+ toQueryInput
9
+ } from "./convert.js";
2
10
  import { txReportToJs } from "./database.js";
11
+ import { Form } from "./form.js";
12
+ import { preparePull, prepareQuery } from "./prepared.js";
3
13
  import { _BINDINGS } from "./interop.js";
4
14
  import { callJavaMethod, javaBridgeModule } from "./jvm.js";
5
15
  import { ResourceWrapper } from "./resource.js";
6
16
  import { toJsResult } from "./result.js";
17
+ import { bindNativeCallback, nativeMethods } from "./native.js";
7
18
 
8
19
  async function pullSelector(value) {
9
20
  if (typeof value === "string") {
@@ -60,37 +71,24 @@ function fulltextOpts(opts = null, { limit = null, offset = 0, pagingCachePages
60
71
  return (mapOpts ? merged.size === 0 : Object.keys(merged).length === 0) ? null : merged;
61
72
  }
62
73
 
63
- let listenerProxyEventLoopDepth = 0;
64
- let listenerProxyEventLoopPrevious = false;
65
-
66
- async function retainListenerProxyEventLoop() {
67
- const bridge = await javaBridgeModule();
68
- if (listenerProxyEventLoopDepth === 0) {
69
- listenerProxyEventLoopPrevious = bridge.config.runEventLoopWhenInterfaceProxyIsActive;
70
- bridge.config.runEventLoopWhenInterfaceProxyIsActive = true;
71
- }
72
- listenerProxyEventLoopDepth += 1;
73
- return () => {
74
- listenerProxyEventLoopDepth -= 1;
75
- if (listenerProxyEventLoopDepth === 0) {
76
- bridge.config.runEventLoopWhenInterfaceProxyIsActive = listenerProxyEventLoopPrevious;
77
- }
78
- };
79
- }
80
-
81
74
  async function createConsumerProxy(fn) {
82
75
  if (typeof fn !== "function") {
83
76
  throw new TypeError("callback must be a function.");
84
77
  }
85
78
  const { newProxy } = await javaBridgeModule();
86
79
  return newProxy("java.util.function.Consumer", {
87
- accept: async (value) => {
80
+ accept: bindNativeCallback(async (value) => {
88
81
  await fn(await toJs(value));
89
- }
82
+ })
90
83
  });
91
84
  }
92
85
 
93
86
  export class Connection extends ResourceWrapper {
87
+ /** Register a database-wide type for use as an attribute's :db/valueType. */
88
+ async registerType(typeName, definition) {
89
+ return toJs(await _BINDINGS.registerType(this.rawHandle(), typeName, definition));
90
+ }
91
+
94
92
  constructor(handle, { owned = true } = {}) {
95
93
  super(
96
94
  handle,
@@ -99,25 +97,43 @@ export class Connection extends ResourceWrapper {
99
97
  "connection"
100
98
  );
101
99
  this._listenerProxies = new Map();
100
+ this._dbListenerProxies = new Map();
102
101
  }
103
102
 
104
103
  async close() {
105
- this._resetListenerProxies();
106
104
  await super.close();
105
+ this._resetListenerProxies();
107
106
  }
108
107
 
109
108
  _resetListenerProxies() {
110
- for (const { proxy, release } of this._listenerProxies.values()) {
111
- proxy.reset?.();
112
- release?.();
109
+ for (const listeners of [this._listenerProxies, this._dbListenerProxies]) {
110
+ for (const { proxy } of listeners.values()) {
111
+ proxy.reset?.();
112
+ }
113
+ listeners.clear();
113
114
  }
114
- this._listenerProxies.clear();
115
115
  }
116
116
 
117
117
  async schema() {
118
118
  return toJsResult(await _BINDINGS.coreInvoke("schema", [this.rawHandle()]));
119
119
  }
120
120
 
121
+ async indexAttr(attr) {
122
+ return toJsResult(await _BINDINGS.connectionIndexAttr(this.rawHandle(), attr), { bridge: true });
123
+ }
124
+
125
+ async preparePull(selector, opts = null) {
126
+ return preparePull(this, selector, opts);
127
+ }
128
+
129
+ async prepareQuery(query) {
130
+ return prepareQuery(this, query);
131
+ }
132
+
133
+ async transactAck(txData, txMeta = null) {
134
+ return toJsResult(await _BINDINGS.connectionTransactAck(this.rawHandle(), txData, txMeta));
135
+ }
136
+
121
137
  async opts() {
122
138
  return toJsResult(await _BINDINGS.coreInvoke("opts", [this.rawHandle()]));
123
139
  }
@@ -195,26 +211,33 @@ export class Connection extends ResourceWrapper {
195
211
  }
196
212
 
197
213
  async query(query, ...inputs) {
214
+ const convertInput = query instanceof Form ? toJava : toQueryInput;
198
215
  const normalizedInputs = [];
199
216
  for (const input of inputs) {
200
- normalizedInputs.push(await toQueryInput(input));
217
+ normalizedInputs.push(await convertInput(input));
201
218
  }
202
219
  if (typeof query === "string") {
203
- return toJsResult(
204
- await callJavaMethod(this.rawHandle(), "query", query, await toJava(normalizedInputs)),
205
- { bridge: true }
220
+ return toJsQueryResult(
221
+ await _BINDINGS.connectionQuery(
222
+ this.rawHandle(), query, await toJava(normalizedInputs)
223
+ )
206
224
  );
207
225
  }
208
- return toJsResult(
209
- await callJavaMethod(this.rawHandle(), "queryForm", await queryForm(query), await toJava(normalizedInputs)),
210
- { bridge: true }
226
+ return toJsQueryResult(
227
+ await _BINDINGS.connectionQuery(
228
+ this.rawHandle(),
229
+ await queryForm(query),
230
+ await toJava(normalizedInputs),
231
+ { form: true }
232
+ )
211
233
  );
212
234
  }
213
235
 
214
236
  async explain(query, { inputs = [], optsEdn = null } = {}) {
237
+ const convertInput = query instanceof Form ? toJava : toQueryInput;
215
238
  const normalizedInputs = [];
216
239
  for (const input of inputs) {
217
- normalizedInputs.push(await toQueryInput(input));
240
+ normalizedInputs.push(await convertInput(input));
218
241
  }
219
242
  if (optsEdn !== null && optsEdn !== undefined) {
220
243
  if (typeof query === "string") {
@@ -285,11 +308,9 @@ export class Connection extends ResourceWrapper {
285
308
  }
286
309
  const previous = this._listenerProxies.get(registeredKey);
287
310
  previous?.proxy.reset?.();
288
- previous?.release?.();
289
- this._listenerProxies.set(registeredKey, {
290
- proxy,
291
- release: await retainListenerProxyEventLoop()
292
- });
311
+ // Transactions use async Java calls, so callbacks need no reentrant event
312
+ // loop. Enabling java-bridge's experimental loop can race proxy cleanup.
313
+ this._listenerProxies.set(registeredKey, { proxy });
293
314
  return registeredKey;
294
315
  }
295
316
 
@@ -297,10 +318,34 @@ export class Connection extends ResourceWrapper {
297
318
  await _BINDINGS.connectionUnlisten(this.rawHandle(), key);
298
319
  const existing = this._listenerProxies.get(key);
299
320
  existing?.proxy.reset?.();
300
- existing?.release?.();
301
321
  this._listenerProxies.delete(key);
302
322
  }
303
323
 
324
+ /** Subscribe to remote database commits; events may coalesce and callbacks may query. */
325
+ async listenDb(callback, key = null) {
326
+ const proxy = await createConsumerProxy(callback);
327
+ let registeredKey;
328
+ try {
329
+ // Generate string keys here so unlistenDb receives the same type as registration.
330
+ registeredKey = await toJsResult(
331
+ await _BINDINGS.connectionListenDb(this.rawHandle(), key ?? randomUUID(), proxy),
332
+ { bridge: true }
333
+ );
334
+ } catch (error) {
335
+ proxy.reset?.();
336
+ throw error;
337
+ }
338
+ this._dbListenerProxies.get(registeredKey)?.proxy.reset?.();
339
+ this._dbListenerProxies.set(registeredKey, { proxy });
340
+ return registeredKey;
341
+ }
342
+
343
+ async unlistenDb(key) {
344
+ await _BINDINGS.connectionUnlistenDb(this.rawHandle(), key);
345
+ this._dbListenerProxies.get(key)?.proxy.reset?.();
346
+ this._dbListenerProxies.delete(key);
347
+ }
348
+
304
349
  async fillDb(datoms) {
305
350
  await callJavaMethod(this.rawHandle(), "fillDb", await toJava(datoms));
306
351
  return this;
@@ -443,3 +488,5 @@ export class Connection extends ResourceWrapper {
443
488
  return toJsResult(await callJavaMethod(this.rawHandle(), "gcTxLogSegments"), { bridge: true });
444
489
  }
445
490
  }
491
+
492
+ nativeMethods(Connection);
package/src/convert.js CHANGED
@@ -1,4 +1,10 @@
1
1
  import { classes } from "./jvm.js";
2
+ import { DatalogSymbol, EdnList, Form, Keyword, Uuid } from "./form.js";
3
+ import { currentNativeBindings, nativeCodecFor, nativeToJs } from "./native.js";
4
+
5
+ function nativeBindingsActive() {
6
+ return Boolean(currentNativeBindings()?.types.size);
7
+ }
2
8
 
3
9
  const INT64_MIN = -(2n ** 63n);
4
10
  const INT64_MAX = 2n ** 63n - 1n;
@@ -23,7 +29,15 @@ function toBufferView(value) {
23
29
  return Buffer.from(value.buffer, value.byteOffset, value.byteLength);
24
30
  }
25
31
 
26
- function materializeJavaCollection(collection) {
32
+ async function materializeJavaCollection(collection) {
33
+ if (nativeBindingsActive()) {
34
+ // Reading spilled collections can reconstruct maps/sets and call native
35
+ // equality. Leave the event loop available for those JVM callbacks.
36
+ const items = [];
37
+ const iterator = await collection.iterator();
38
+ while (await iterator.hasNext()) items.push(await iterator.next());
39
+ return items;
40
+ }
27
41
  if (typeof collection?.toArraySync === "function") {
28
42
  try {
29
43
  const items = collection.toArraySync();
@@ -57,7 +71,9 @@ async function toJavaMap(entries, converter) {
57
71
  const cls = await classes();
58
72
  const jmap = new cls.linkedHashMap();
59
73
  for (const [key, item] of entries) {
60
- jmap.putSync(await converter(key), await converter(item));
74
+ const k = await converter(key), v = await converter(item);
75
+ if (nativeBindingsActive()) await jmap.put(k, v);
76
+ else jmap.putSync(k, v);
61
77
  }
62
78
  return jmap;
63
79
  }
@@ -66,7 +82,9 @@ async function toJavaSet(items, converter) {
66
82
  const cls = await classes();
67
83
  const jset = new cls.linkedHashSet();
68
84
  for (const item of items) {
69
- jset.addSync(await converter(item));
85
+ const value = await converter(item);
86
+ if (nativeBindingsActive()) await jset.add(value);
87
+ else jset.addSync(value);
70
88
  }
71
89
  return jset;
72
90
  }
@@ -87,6 +105,8 @@ export function isJavaObject(value) {
87
105
  }
88
106
 
89
107
  export async function toJava(value) {
108
+ const native = nativeCodecFor(value);
109
+ if (native) return native.wrap(value);
90
110
  if (value === null || typeof value === "boolean" || typeof value === "string") {
91
111
  return value;
92
112
  }
@@ -114,6 +134,34 @@ export async function toJava(value) {
114
134
  return value;
115
135
  }
116
136
 
137
+ if (value instanceof Keyword) {
138
+ const cls = await classes();
139
+ return cls.interop.keywordSync(value.toString());
140
+ }
141
+
142
+ if (value instanceof DatalogSymbol) {
143
+ const cls = await classes();
144
+ return cls.interop.symbolSync(value.toString());
145
+ }
146
+
147
+ if (value instanceof Uuid) {
148
+ const cls = await classes();
149
+ return cls.uuid.fromStringSync(value.toString());
150
+ }
151
+
152
+ if (value instanceof EdnList) {
153
+ const cls = await classes();
154
+ const items = new cls.arrayList();
155
+ for (const item of value) {
156
+ items.addSync(await toJava(item));
157
+ }
158
+ return nativeBindingsActive() ? cls.interop.ednList(items) : cls.interop.ednListSync(items);
159
+ }
160
+
161
+ if (value instanceof Form) {
162
+ return toJava(value.toForm());
163
+ }
164
+
117
165
  if (typeof value?.rawHandle === "function") {
118
166
  return value.rawHandle();
119
167
  }
@@ -178,6 +226,11 @@ export async function toJs(value) {
178
226
 
179
227
  const cls = await classes();
180
228
 
229
+ if (typeof value.getClassSync === "function"
230
+ && value.getClassSync().getNameSync() === "datalevin.NativeValue") {
231
+ return nativeToJs(value);
232
+ }
233
+
181
234
  if (instanceOf(value, cls.uuid)) {
182
235
  return value.toStringSync();
183
236
  }
@@ -204,7 +257,8 @@ export async function toJs(value) {
204
257
 
205
258
  if (instanceOf(value, cls.mapType) || typeof value.entrySetSync === "function") {
206
259
  const entries = [];
207
- for (const entry of materializeJavaCollection(value.entrySetSync())) {
260
+ const entrySet = nativeBindingsActive() ? await value.entrySet() : value.entrySetSync();
261
+ for (const entry of await materializeJavaCollection(entrySet)) {
208
262
  entries.push([await toJs(entry.getKeySync()), await toJs(entry.getValueSync())]);
209
263
  }
210
264
 
@@ -216,7 +270,7 @@ export async function toJs(value) {
216
270
 
217
271
  if (instanceOf(value, cls.setType)) {
218
272
  const items = [];
219
- for (const item of materializeJavaCollection(value)) {
273
+ for (const item of await materializeJavaCollection(value)) {
220
274
  items.push(await toJs(item));
221
275
  }
222
276
  return new Set(items);
@@ -228,7 +282,7 @@ export async function toJs(value) {
228
282
  || typeof value.iteratorSync === "function"
229
283
  ) {
230
284
  const items = [];
231
- for (const item of materializeJavaCollection(value)) {
285
+ for (const item of await materializeJavaCollection(value)) {
232
286
  items.push(await toJs(item));
233
287
  }
234
288
  return items;
@@ -237,20 +291,54 @@ export async function toJs(value) {
237
291
  return value;
238
292
  }
239
293
 
294
+ export async function toJsQueryResult(value) {
295
+ if (isJavaObject(value)) {
296
+ const cls = await classes();
297
+ if (instanceOf(value, cls.setType)) {
298
+ const rows = [];
299
+ for (const item of await materializeJavaCollection(value)) {
300
+ rows.push(await toJs(item));
301
+ }
302
+ return rows;
303
+ }
304
+ }
305
+ return toJs(value);
306
+ }
307
+
240
308
  async function toJavaFormMap(entries, converter) {
241
309
  const cls = await classes();
242
310
  const jmap = new cls.linkedHashMap();
243
311
  for (const [key, item] of entries) {
244
- jmap.putSync(await converter(key), await converter(item));
312
+ const k = await converter(key), v = await converter(item);
313
+ if (nativeBindingsActive()) await jmap.put(k, v);
314
+ else jmap.putSync(k, v);
245
315
  }
246
316
  return jmap;
247
317
  }
248
318
 
249
319
  export async function toEdnForm(value) {
320
+ const native = nativeCodecFor(value);
321
+ if (native) return native.wrap(value);
250
322
  if (value === null || isJavaObject(value)) {
251
323
  return value;
252
324
  }
253
325
 
326
+ if (value instanceof Keyword) {
327
+ const cls = await classes();
328
+ return cls.interop.keywordSync(value.toString());
329
+ }
330
+
331
+ if (value instanceof DatalogSymbol) {
332
+ const cls = await classes();
333
+ return cls.interop.symbolSync(value.toString());
334
+ }
335
+
336
+ if (value instanceof Form) {
337
+ // Builder forms carry explicit Keyword/DatalogSymbol nodes. Lower through
338
+ // toJava so strings such as "?literal" remain ordinary string constants.
339
+ return toJava(value);
340
+ }
341
+
254
342
  if (typeof value?.rawHandle === "function") {
255
343
  return value.rawHandle();
256
344
  }
@@ -286,10 +374,26 @@ export async function toEdnForm(value) {
286
374
  }
287
375
 
288
376
  export async function toQueryInput(value) {
377
+ const native = nativeCodecFor(value);
378
+ if (native) return native.wrap(value);
289
379
  if (value === null || isJavaObject(value)) {
290
380
  return value;
291
381
  }
292
382
 
383
+ if (value instanceof Keyword) {
384
+ const cls = await classes();
385
+ return cls.interop.keywordSync(value.toString());
386
+ }
387
+
388
+ if (value instanceof DatalogSymbol) {
389
+ const cls = await classes();
390
+ return cls.interop.symbolSync(value.toString());
391
+ }
392
+
393
+ if (value instanceof Form) {
394
+ return toJava(value);
395
+ }
396
+
293
397
  if (typeof value?.rawHandle === "function") {
294
398
  return value.rawHandle();
295
399
  }
package/src/database.js CHANGED
@@ -1,7 +1,9 @@
1
1
  import { toEdnForm, toJava } from "./convert.js";
2
2
  import { _BINDINGS } from "./interop.js";
3
+ import { preparePull, prepareQuery } from "./prepared.js";
3
4
  import { callJavaMethod } from "./jvm.js";
4
5
  import { toJsResult } from "./result.js";
6
+ import { currentNativeRegistry, nativeMethods } from "./native.js";
5
7
 
6
8
  async function pullSelector(value) {
7
9
  if (typeof value === "string") {
@@ -20,6 +22,7 @@ async function mapGet(map, key) {
20
22
 
21
23
  export class Database {
22
24
  constructor(handle) {
25
+ this._nativeRegistry = currentNativeRegistry();
23
26
  this._handle = handle;
24
27
  }
25
28
 
@@ -27,6 +30,14 @@ export class Database {
27
30
  return this._handle;
28
31
  }
29
32
 
33
+ async preparePull(selector, opts = null) {
34
+ return preparePull(this, selector, opts);
35
+ }
36
+
37
+ async prepareQuery(query) {
38
+ return prepareQuery(this, query);
39
+ }
40
+
30
41
  toString() {
31
42
  return "<Database>";
32
43
  }
@@ -83,3 +94,5 @@ export async function txReportToJs(report) {
83
94
  }
84
95
  return result;
85
96
  }
97
+
98
+ nativeMethods(Database);
package/src/entity.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { isJavaObject, toJs } from "./convert.js";
2
2
  import { _BINDINGS } from "./interop.js";
3
3
  import { classes } from "./jvm.js";
4
+ import { currentNativeRegistry, nativeMethods } from "./native.js";
4
5
 
5
6
  function materializeJavaCollection(collection) {
6
7
  if (typeof collection?.toArraySync === "function") {
@@ -123,6 +124,7 @@ async function entityValue(value) {
123
124
 
124
125
  export class Entity {
125
126
  constructor(handle) {
127
+ this._nativeRegistry = currentNativeRegistry();
126
128
  this.handle = handle;
127
129
  }
128
130
 
@@ -169,3 +171,5 @@ export class Entity {
169
171
  return "<Entity lazy>";
170
172
  }
171
173
  }
174
+
175
+ nativeMethods(Entity);