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/README.md +470 -31
- package/jars/datalevin-runtime-1.2.10.jar +4 -0
- package/package.json +1 -1
- package/src/client.js +5 -3
- package/src/connection.js +87 -40
- package/src/convert.js +111 -7
- package/src/database.js +13 -0
- package/src/entity.js +4 -0
- package/src/form.js +408 -0
- package/src/index.js +38 -1
- package/src/interop.js +119 -7
- package/src/kv.js +27 -1
- package/src/native.js +172 -0
- package/src/prepared.js +88 -0
- package/src/query.js +1352 -0
- package/src/resource.js +2 -0
- package/src/transaction.js +312 -0
- package/src/udf-value.js +267 -0
- package/src/udf.js +128 -76
- package/jars/datalevin-runtime-1.0.2.jar +0 -4
package/src/udf.js
CHANGED
|
@@ -1,21 +1,14 @@
|
|
|
1
1
|
import { toJava, toJs } from "./convert.js";
|
|
2
|
+
import { Database } from "./database.js";
|
|
2
3
|
import { _BINDINGS } from "./interop.js";
|
|
3
4
|
import { javaBridgeModule } from "./jvm.js";
|
|
5
|
+
import { UdfDescriptor, descriptorData } from "./udf-value.js";
|
|
6
|
+
import { NativeCodec, createNativeEqualityProxy, descriptorKey, withNativeScope } from "./native.js";
|
|
4
7
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
function descriptorKey(descriptor) {
|
|
11
|
-
const normalized = udfDescriptor(descriptor);
|
|
12
|
-
return [
|
|
13
|
-
normalized[":udf/lang"],
|
|
14
|
-
normalized[":udf/kind"],
|
|
15
|
-
normalized[":udf/id"],
|
|
16
|
-
normalized[":udf/version"] ?? ""
|
|
17
|
-
].join("\u0000");
|
|
18
|
-
}
|
|
8
|
+
const DATABASE_CLASSES = new Set([
|
|
9
|
+
"datalevin.db.DB",
|
|
10
|
+
"datalevin.DatabaseValue"
|
|
11
|
+
]);
|
|
19
12
|
|
|
20
13
|
function materializeJavaArgs(args) {
|
|
21
14
|
if (Array.isArray(args)) {
|
|
@@ -38,53 +31,73 @@ function materializeJavaArgs(args) {
|
|
|
38
31
|
return items;
|
|
39
32
|
}
|
|
40
33
|
|
|
41
|
-
|
|
34
|
+
function materializeUdfArgs(args) {
|
|
42
35
|
if (Array.isArray(args)) {
|
|
43
|
-
|
|
44
|
-
return args;
|
|
45
|
-
}
|
|
46
|
-
const convertedTail = await Promise.all(args.slice(1).map((value) => toJs(value)));
|
|
47
|
-
return [args[0], ...convertedTail];
|
|
36
|
+
return args;
|
|
48
37
|
}
|
|
49
|
-
|
|
50
38
|
if (typeof args?.sizeSync === "function" && typeof args?.getSync === "function") {
|
|
39
|
+
const values = [];
|
|
51
40
|
const size = Number(args.sizeSync());
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
41
|
+
for (let index = 0; index < size; index += 1) {
|
|
42
|
+
try {
|
|
43
|
+
values.push(args.getSync(index));
|
|
44
|
+
} catch (error) {
|
|
45
|
+
const message = String(error?.message ?? error);
|
|
46
|
+
if (index === 0 && message.includes("ClassNotFoundException: datalevin.db.DB")) {
|
|
47
|
+
// Runtime jars before the DatabaseValue UDF bridge could not expose
|
|
48
|
+
// the generated Clojure DB class to Node. Preserve their historical
|
|
49
|
+
// null argument while newer runtimes provide an idiomatic Database.
|
|
50
|
+
values.push(null);
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
throw error;
|
|
54
|
+
}
|
|
59
55
|
}
|
|
60
56
|
return values;
|
|
61
57
|
}
|
|
62
|
-
|
|
63
|
-
const values = materializeJavaArgs(args);
|
|
64
|
-
if (values.length === 0) {
|
|
65
|
-
return values;
|
|
66
|
-
}
|
|
67
|
-
const convertedTail = await Promise.all(values.slice(1).map((value) => toJs(value)));
|
|
68
|
-
return [values[0], ...convertedTail];
|
|
58
|
+
return materializeJavaArgs(args);
|
|
69
59
|
}
|
|
70
60
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
61
|
+
function javaClassName(value) {
|
|
62
|
+
try {
|
|
63
|
+
return value.getClassSync().getNameSync();
|
|
64
|
+
} catch {
|
|
65
|
+
return null;
|
|
75
66
|
}
|
|
67
|
+
}
|
|
76
68
|
|
|
77
|
-
|
|
69
|
+
async function udfArgsToJs(args) {
|
|
70
|
+
const values = materializeUdfArgs(args);
|
|
71
|
+
return Promise.all(values.map(async (value) => {
|
|
72
|
+
if (DATABASE_CLASSES.has(javaClassName(value))) {
|
|
73
|
+
return new Database(value);
|
|
74
|
+
}
|
|
75
|
+
return toJs(value);
|
|
76
|
+
}));
|
|
78
77
|
}
|
|
79
78
|
|
|
80
|
-
async function createProxy(fn, descriptor) {
|
|
79
|
+
async function createProxy(fn, descriptor, bindings) {
|
|
81
80
|
const { newProxy } = await javaBridgeModule();
|
|
82
81
|
return newProxy("datalevin.UdfFunction", {
|
|
83
|
-
invoke: async (
|
|
84
|
-
const values = await udfArgsToJs(args
|
|
82
|
+
invoke: (args) => withNativeScope(bindings, async () => {
|
|
83
|
+
const values = await udfArgsToJs(args);
|
|
85
84
|
const result = await fn(...values);
|
|
85
|
+
if (descriptor.kind === ":serializer"
|
|
86
|
+
&& !Buffer.isBuffer(result) && !(result instanceof Uint8Array)) {
|
|
87
|
+
throw new TypeError("Custom serializer must return a byte array (Buffer or Uint8Array).");
|
|
88
|
+
}
|
|
89
|
+
if (descriptor.kind === ":deserializer") {
|
|
90
|
+
let expected;
|
|
91
|
+
for (const codec of bindings.types.values()) {
|
|
92
|
+
if (descriptorKey(codec.deserialize) === descriptorKey(descriptor)) {
|
|
93
|
+
expected ??= codec;
|
|
94
|
+
if (codec.accepts(result)) return codec.wrapPayload(values[0]);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
expected?.checkValue(result);
|
|
98
|
+
}
|
|
86
99
|
return toJava(result === undefined ? null : result);
|
|
87
|
-
}
|
|
100
|
+
})
|
|
88
101
|
});
|
|
89
102
|
}
|
|
90
103
|
|
|
@@ -93,35 +106,15 @@ export function udfDescriptor(idOrDescriptor, {
|
|
|
93
106
|
lang = ":java",
|
|
94
107
|
version = null
|
|
95
108
|
} = {}) {
|
|
96
|
-
|
|
97
|
-
const descriptor = idOrDescriptor;
|
|
98
|
-
const id = descriptor[":udf/id"] ?? descriptor.id ?? descriptor.udfId;
|
|
99
|
-
return udfDescriptor(id, {
|
|
100
|
-
kind: descriptor[":udf/kind"] ?? descriptor.kind ?? kind,
|
|
101
|
-
lang: descriptor[":udf/lang"] ?? descriptor.lang ?? lang,
|
|
102
|
-
version: descriptor[":udf/version"] ?? descriptor.version ?? version
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
if (idOrDescriptor === null || idOrDescriptor === undefined) {
|
|
107
|
-
throw new TypeError("udf id is required");
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
const descriptor = {
|
|
111
|
-
":udf/lang": keywordString(lang),
|
|
112
|
-
":udf/kind": keywordString(kind),
|
|
113
|
-
":udf/id": keywordString(idOrDescriptor)
|
|
114
|
-
};
|
|
115
|
-
if (version !== null && version !== undefined) {
|
|
116
|
-
descriptor[":udf/version"] = version;
|
|
117
|
-
}
|
|
118
|
-
return descriptor;
|
|
109
|
+
return descriptorData(idOrDescriptor, { kind, lang, version });
|
|
119
110
|
}
|
|
120
111
|
|
|
121
112
|
export class UdfRegistry {
|
|
122
113
|
constructor(handle) {
|
|
123
114
|
this._handle = handle;
|
|
124
115
|
this._proxies = new Map();
|
|
116
|
+
this._nativeBindings = { types: new Map(), functions: new Map(), ownerRef: new WeakRef(this) };
|
|
117
|
+
this._nativeEqualityProxyPromise = null;
|
|
125
118
|
}
|
|
126
119
|
|
|
127
120
|
rawHandle() {
|
|
@@ -133,17 +126,19 @@ export class UdfRegistry {
|
|
|
133
126
|
throw new TypeError("fn must be a function");
|
|
134
127
|
}
|
|
135
128
|
|
|
136
|
-
const normalized =
|
|
137
|
-
const proxy = await createProxy(fn, normalized);
|
|
129
|
+
const normalized = UdfDescriptor.from(descriptor, { defaultLang: "javascript" });
|
|
130
|
+
const proxy = await createProxy(fn, normalized, this._nativeBindings);
|
|
138
131
|
await _BINDINGS.registerUdf(this._handle, normalized, proxy);
|
|
139
132
|
this._proxies.set(descriptorKey(normalized), proxy);
|
|
133
|
+
this._nativeBindings.functions.set(descriptorKey(normalized), fn);
|
|
140
134
|
return fn;
|
|
141
135
|
}
|
|
142
136
|
|
|
143
137
|
async unregister(descriptor) {
|
|
144
|
-
const normalized =
|
|
138
|
+
const normalized = UdfDescriptor.from(descriptor, { defaultLang: "javascript" });
|
|
145
139
|
await _BINDINGS.unregisterUdf(this._handle, normalized);
|
|
146
140
|
const key = descriptorKey(normalized);
|
|
141
|
+
this._nativeBindings.functions.delete(key);
|
|
147
142
|
const proxy = this._proxies.get(key);
|
|
148
143
|
if (proxy !== undefined) {
|
|
149
144
|
proxy.reset();
|
|
@@ -152,30 +147,87 @@ export class UdfRegistry {
|
|
|
152
147
|
}
|
|
153
148
|
|
|
154
149
|
async registered(descriptor) {
|
|
155
|
-
return _BINDINGS.registeredUdf(
|
|
150
|
+
return _BINDINGS.registeredUdf(
|
|
151
|
+
this._handle,
|
|
152
|
+
UdfDescriptor.from(descriptor, { defaultLang: "javascript" })
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** Bind an exact native class to payload UDFs before opening a database.
|
|
157
|
+
* Equality defaults to node:util.isDeepStrictEqual; classes with private state
|
|
158
|
+
* can supply { equals: (left, right) => boolean }. This binding is not persisted.
|
|
159
|
+
*/
|
|
160
|
+
async bindNativeType(typeName, nativeType, definition, options = {}) {
|
|
161
|
+
const codec = new NativeCodec(this._nativeBindings, typeName, nativeType, definition, options);
|
|
162
|
+
const existing = this._nativeBindings.types.get(nativeType.prototype);
|
|
163
|
+
if (existing) {
|
|
164
|
+
if (!existing.sameBinding(codec)) throw new TypeError("JavaScript class already has a different native type binding");
|
|
165
|
+
return this;
|
|
166
|
+
}
|
|
167
|
+
if ([...this._nativeBindings.types.values()].some((c) => c.typeName === codec.typeName)) {
|
|
168
|
+
throw new TypeError("Custom type already has a different JavaScript class binding");
|
|
169
|
+
}
|
|
170
|
+
this._nativeEqualityProxyPromise ??= createNativeEqualityProxy();
|
|
171
|
+
this._nativeBindings.equalityProxyRef = new WeakRef(await this._nativeEqualityProxyPromise);
|
|
172
|
+
// Recheck after the asynchronous proxy creation: concurrent conflicting
|
|
173
|
+
// bindings must not overwrite a class installed by another call.
|
|
174
|
+
const installed = this._nativeBindings.types.get(nativeType.prototype);
|
|
175
|
+
if (installed) {
|
|
176
|
+
if (!installed.sameBinding(codec)) throw new TypeError("JavaScript class already has a different native type binding");
|
|
177
|
+
return this;
|
|
178
|
+
}
|
|
179
|
+
if ([...this._nativeBindings.types.values()].some((c) => c.typeName === codec.typeName)) {
|
|
180
|
+
throw new TypeError("Custom type already has a different JavaScript class binding");
|
|
181
|
+
}
|
|
182
|
+
await _BINDINGS.bindNativeType(this._handle, codec.typeName, codec.deserialize);
|
|
183
|
+
const concurrent = this._nativeBindings.types.get(nativeType.prototype);
|
|
184
|
+
if (concurrent) {
|
|
185
|
+
if (!concurrent.sameBinding(codec)) throw new TypeError("JavaScript class already has a different native type binding");
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
if ([...this._nativeBindings.types.values()].some((c) => c.typeName === codec.typeName)) {
|
|
189
|
+
throw new TypeError("Custom type already has a different JavaScript class binding");
|
|
190
|
+
}
|
|
191
|
+
this._nativeBindings.types.set(nativeType.prototype, codec);
|
|
192
|
+
codec.install();
|
|
193
|
+
return this;
|
|
156
194
|
}
|
|
157
195
|
|
|
158
196
|
async queryUdf(id, fn, options = {}) {
|
|
159
|
-
return this.register(
|
|
197
|
+
return this.register(UdfDescriptor.queryFn(id, options), fn);
|
|
160
198
|
}
|
|
161
199
|
|
|
162
200
|
async predicateUdf(id, fn, options = {}) {
|
|
163
|
-
return this.register(
|
|
201
|
+
return this.register(UdfDescriptor.predicate(id, options), fn);
|
|
164
202
|
}
|
|
165
203
|
|
|
166
204
|
async txUdf(id, fn, options = {}) {
|
|
167
|
-
return this.register(
|
|
205
|
+
return this.register(UdfDescriptor.txFn(id, options), fn);
|
|
168
206
|
}
|
|
169
207
|
|
|
170
208
|
async analyzerUdf(id, fn, options = {}) {
|
|
171
|
-
return this.register(
|
|
209
|
+
return this.register(UdfDescriptor.analyzer(id, options), fn);
|
|
172
210
|
}
|
|
173
211
|
|
|
174
212
|
async queryAnalyzerUdf(id, fn, options = {}) {
|
|
175
|
-
return this.register(
|
|
213
|
+
return this.register(UdfDescriptor.queryAnalyzer(id, options), fn);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
async orderUdf(id, fn, options = {}) {
|
|
217
|
+
return this.register(UdfDescriptor.orderFn(id, options), fn);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
async serializerUdf(id, fn, options = {}) {
|
|
221
|
+
return this.register(UdfDescriptor.serializer(id, options), fn);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
async deserializerUdf(id, fn, options = {}) {
|
|
225
|
+
return this.register(UdfDescriptor.deserializer(id, options), fn);
|
|
176
226
|
}
|
|
177
227
|
}
|
|
178
228
|
|
|
179
229
|
export async function createUdfRegistry() {
|
|
180
230
|
return new UdfRegistry(await _BINDINGS.createUdfRegistry());
|
|
181
231
|
}
|
|
232
|
+
|
|
233
|
+
export { UdfDescriptor };
|