hyperdb-wasm 0.0.26282-dev.wasm.g959538a5 → 0.0.26297-dev.wasm.g08f2873d
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/api.d.ts +31 -2
- package/dist/browser.js +341 -5
- package/dist/browser_worker.js +105 -6
- package/dist/client.d.ts +10 -0
- package/dist/hyperdb-wasm.cjs +1 -1
- package/dist/hyperdb-wasm.js +1 -1
- package/dist/hyperdb-wasm.wasm.br +0 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.node.d.ts +1 -0
- package/dist/node.d.ts +1 -0
- package/dist/node.js +176 -7
- package/dist/node_worker.js +102 -7
- package/dist/protocol.d.ts +48 -0
- package/dist/raw.browser.js +89 -2
- package/dist/raw.node.js +90 -3
- package/dist/wasm_bindings.d.ts +17 -0
- package/dist/worker_setup.d.ts +4 -1
- package/examples/browser-attached-databases.html +71 -0
- package/examples/browser.html +7 -7
- package/examples/node-attached-databases.mjs +36 -0
- package/examples/node.mjs +33 -8
- package/package.json +1 -1
package/dist/api.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Table } from 'apache-arrow';
|
|
2
|
-
import type { AsyncRecordBatchStreamReader,
|
|
1
|
+
import { DataType, Table } from 'apache-arrow';
|
|
2
|
+
import type { AsyncRecordBatchStreamReader, TypeMap } from 'apache-arrow';
|
|
3
3
|
import type { HyperDBEngineClient } from './client.js';
|
|
4
4
|
import type { BrowserClientOptions } from './browser.js';
|
|
5
5
|
import type { WasmResult } from './protocol.js';
|
|
@@ -7,23 +7,47 @@ export interface HyperDBClient {
|
|
|
7
7
|
ready(): Promise<void>;
|
|
8
8
|
connect(): Promise<WasmResult>;
|
|
9
9
|
disconnect(connection: number): Promise<WasmResult>;
|
|
10
|
+
createDatabase(databaseName: string, persistent: boolean): Promise<WasmResult>;
|
|
11
|
+
openDatabase(databaseName: string): Promise<WasmResult>;
|
|
12
|
+
checkpointDatabase(databaseName: string): Promise<WasmResult>;
|
|
13
|
+
dropDatabase(databaseName: string): Promise<WasmResult>;
|
|
14
|
+
attachDatabase(connection: number, databaseName: string, alias: string): Promise<WasmResult>;
|
|
15
|
+
detachDatabase(connection: number, alias: string): Promise<WasmResult>;
|
|
10
16
|
startQuery(connection: number, sql: string): Promise<WasmResult>;
|
|
17
|
+
insertArrowIPCFromPath(connection: number, path: string, name: string, schema: string | null, create: boolean, internal?: boolean): Promise<WasmResult>;
|
|
18
|
+
createTemporaryFile(bytes: Uint8Array): Promise<WasmResult>;
|
|
19
|
+
removeFile(path: string): Promise<WasmResult>;
|
|
11
20
|
pollQuery(query: number): Promise<WasmResult>;
|
|
12
21
|
cancelQuery(query: number): Promise<WasmResult>;
|
|
13
22
|
releaseQuery(query: number): Promise<WasmResult>;
|
|
23
|
+
shutdown(): Promise<WasmResult>;
|
|
14
24
|
terminate(): Promise<void>;
|
|
15
25
|
}
|
|
26
|
+
export interface ArrowInsertOptions {
|
|
27
|
+
readonly name: string;
|
|
28
|
+
readonly schema?: string;
|
|
29
|
+
readonly create?: boolean;
|
|
30
|
+
}
|
|
31
|
+
export interface DatabaseCreateOptions {
|
|
32
|
+
readonly storage?: 'memory' | 'persistent';
|
|
33
|
+
}
|
|
16
34
|
export declare class HyperDB {
|
|
17
35
|
private readonly client;
|
|
18
36
|
private terminated;
|
|
19
37
|
private termination;
|
|
38
|
+
private activeDatabaseOperations;
|
|
20
39
|
private readonly connections;
|
|
21
40
|
constructor(client: HyperDBClient);
|
|
22
41
|
static create(options?: BrowserClientOptions): Promise<HyperDB>;
|
|
23
42
|
ready(): Promise<void>;
|
|
24
43
|
connect(): Promise<HyperDBConnection>;
|
|
44
|
+
createDatabase(databaseName: string, options?: DatabaseCreateOptions): Promise<void>;
|
|
45
|
+
openDatabase(databaseName: string): Promise<void>;
|
|
46
|
+
checkpointDatabase(databaseName: string): Promise<void>;
|
|
47
|
+
dropDatabase(databaseName: string): Promise<void>;
|
|
25
48
|
terminate(): Promise<void>;
|
|
26
49
|
private ensureRunning;
|
|
50
|
+
private runDatabaseOperation;
|
|
27
51
|
}
|
|
28
52
|
export declare class HyperDBConnection {
|
|
29
53
|
private readonly client;
|
|
@@ -34,6 +58,8 @@ export declare class HyperDBConnection {
|
|
|
34
58
|
private closing;
|
|
35
59
|
constructor(client: HyperDBClient, connection: number, onClose?: () => void);
|
|
36
60
|
close(): Promise<void>;
|
|
61
|
+
attachDatabase(databaseName: string, alias?: string): Promise<void>;
|
|
62
|
+
detachDatabase(alias: string): Promise<void>;
|
|
37
63
|
sendIPC(sql: string): AsyncIterable<Uint8Array>;
|
|
38
64
|
queryIPC(sql: string): Promise<Uint8Array>;
|
|
39
65
|
send<T extends TypeMap = {
|
|
@@ -42,6 +68,9 @@ export declare class HyperDBConnection {
|
|
|
42
68
|
query<T extends TypeMap = {
|
|
43
69
|
[key: string]: DataType;
|
|
44
70
|
}>(sql: string): Promise<Table<T>>;
|
|
71
|
+
insertArrowTable(table: Table, options: ArrowInsertOptions): Promise<void>;
|
|
72
|
+
insertArrowIPCFromPath(path: string, options: ArrowInsertOptions): Promise<void>;
|
|
73
|
+
private insertArrowIPCFromPathInternal;
|
|
45
74
|
private ensureAvailable;
|
|
46
75
|
private release;
|
|
47
76
|
closeForTermination(): Promise<void>;
|
package/dist/browser.js
CHANGED
|
@@ -95,9 +95,40 @@ var init_client = __esm({
|
|
|
95
95
|
disconnect(connection) {
|
|
96
96
|
return this.request({ id: this.nextId++, type: "disconnect", connection });
|
|
97
97
|
}
|
|
98
|
+
createDatabase(databaseName, persistent) {
|
|
99
|
+
return this.request({ id: this.nextId++, type: "createDatabase", databaseName, persistent });
|
|
100
|
+
}
|
|
101
|
+
openDatabase(databaseName) {
|
|
102
|
+
return this.request({ id: this.nextId++, type: "openDatabase", databaseName });
|
|
103
|
+
}
|
|
104
|
+
checkpointDatabase(databaseName) {
|
|
105
|
+
return this.request({ id: this.nextId++, type: "checkpointDatabase", databaseName });
|
|
106
|
+
}
|
|
107
|
+
dropDatabase(databaseName) {
|
|
108
|
+
return this.request({ id: this.nextId++, type: "dropDatabase", databaseName });
|
|
109
|
+
}
|
|
110
|
+
attachDatabase(connection, databaseName, alias) {
|
|
111
|
+
return this.request({ id: this.nextId++, type: "attachDatabase", connection, databaseName, alias });
|
|
112
|
+
}
|
|
113
|
+
detachDatabase(connection, alias) {
|
|
114
|
+
return this.request({ id: this.nextId++, type: "detachDatabase", connection, alias });
|
|
115
|
+
}
|
|
116
|
+
shutdown() {
|
|
117
|
+
return this.request({ id: this.nextId++, type: "shutdown" });
|
|
118
|
+
}
|
|
98
119
|
startQuery(connection, sql) {
|
|
99
120
|
return this.request({ id: this.nextId++, type: "startQuery", connection, sql });
|
|
100
121
|
}
|
|
122
|
+
insertArrowIPCFromPath(connection, path, name, schema, create, internal = false) {
|
|
123
|
+
return this.request({ id: this.nextId++, type: "insertArrowIPCFromPath", connection, path, name, schema, create, internal });
|
|
124
|
+
}
|
|
125
|
+
createTemporaryFile(bytes) {
|
|
126
|
+
const payload = bytes.slice();
|
|
127
|
+
return this.request({ id: this.nextId++, type: "createTemporaryFile", bytes: payload }, [payload.buffer]);
|
|
128
|
+
}
|
|
129
|
+
removeFile(path) {
|
|
130
|
+
return this.request({ id: this.nextId++, type: "removeFile", path });
|
|
131
|
+
}
|
|
101
132
|
pollQuery(query) {
|
|
102
133
|
return this.request({ id: this.nextId++, type: "pollQuery", query });
|
|
103
134
|
}
|
|
@@ -111,12 +142,12 @@ var init_client = __esm({
|
|
|
111
142
|
this.fail(new Error("engine worker terminated"));
|
|
112
143
|
await this.transport.terminate();
|
|
113
144
|
}
|
|
114
|
-
request(request) {
|
|
145
|
+
request(request, transfer = []) {
|
|
115
146
|
if (this.failed) return Promise.reject(this.failed);
|
|
116
147
|
return new Promise((resolve, reject) => {
|
|
117
148
|
this.pending.set(request.id, { resolve, reject });
|
|
118
149
|
try {
|
|
119
|
-
this.transport.postMessage(request);
|
|
150
|
+
this.transport.postMessage(request, transfer);
|
|
120
151
|
} catch (error) {
|
|
121
152
|
this.pending.delete(request.id);
|
|
122
153
|
reject(error instanceof Error ? error : new Error(String(error)));
|
|
@@ -3858,6 +3889,11 @@ Timestamp_[_o] = ((proto) => {
|
|
|
3858
3889
|
proto.ArrayType = BigInt64Array;
|
|
3859
3890
|
return proto[Symbol.toStringTag] = "Timestamp";
|
|
3860
3891
|
})(Timestamp_.prototype);
|
|
3892
|
+
var TimestampMillisecond = class extends Timestamp_ {
|
|
3893
|
+
constructor(timezone) {
|
|
3894
|
+
super(TimeUnit.MILLISECOND, timezone);
|
|
3895
|
+
}
|
|
3896
|
+
};
|
|
3861
3897
|
var Interval_ = class extends DataType {
|
|
3862
3898
|
constructor(unit) {
|
|
3863
3899
|
super(Type2.Interval);
|
|
@@ -6397,6 +6433,63 @@ var MemoizedVector = class _MemoizedVector extends Vector {
|
|
|
6397
6433
|
});
|
|
6398
6434
|
}
|
|
6399
6435
|
};
|
|
6436
|
+
function makeVector(init) {
|
|
6437
|
+
if (init) {
|
|
6438
|
+
if (init instanceof Data) {
|
|
6439
|
+
return new Vector([init]);
|
|
6440
|
+
}
|
|
6441
|
+
if (init instanceof Vector) {
|
|
6442
|
+
return new Vector(init.data);
|
|
6443
|
+
}
|
|
6444
|
+
if (init.type instanceof DataType) {
|
|
6445
|
+
return new Vector([makeData(init)]);
|
|
6446
|
+
}
|
|
6447
|
+
if (Array.isArray(init)) {
|
|
6448
|
+
return new Vector(init.flatMap((v) => unwrapInputs(v)));
|
|
6449
|
+
}
|
|
6450
|
+
if (ArrayBuffer.isView(init)) {
|
|
6451
|
+
if (init instanceof DataView) {
|
|
6452
|
+
init = new Uint8Array(init.buffer);
|
|
6453
|
+
}
|
|
6454
|
+
const props = { offset: 0, length: init.length, nullCount: -1, data: init };
|
|
6455
|
+
if (init instanceof Int8Array) {
|
|
6456
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Int8() }))]);
|
|
6457
|
+
}
|
|
6458
|
+
if (init instanceof Int16Array) {
|
|
6459
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Int16() }))]);
|
|
6460
|
+
}
|
|
6461
|
+
if (init instanceof Int32Array) {
|
|
6462
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Int32() }))]);
|
|
6463
|
+
}
|
|
6464
|
+
if (init instanceof BigInt64Array) {
|
|
6465
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Int64() }))]);
|
|
6466
|
+
}
|
|
6467
|
+
if (init instanceof Uint8Array || init instanceof Uint8ClampedArray) {
|
|
6468
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Uint8() }))]);
|
|
6469
|
+
}
|
|
6470
|
+
if (init instanceof Uint16Array) {
|
|
6471
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Uint16() }))]);
|
|
6472
|
+
}
|
|
6473
|
+
if (init instanceof Uint32Array) {
|
|
6474
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Uint32() }))]);
|
|
6475
|
+
}
|
|
6476
|
+
if (init instanceof BigUint64Array) {
|
|
6477
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Uint64() }))]);
|
|
6478
|
+
}
|
|
6479
|
+
if (init instanceof Float32Array) {
|
|
6480
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Float32() }))]);
|
|
6481
|
+
}
|
|
6482
|
+
if (init instanceof Float64Array) {
|
|
6483
|
+
return new Vector([makeData(Object.assign(Object.assign({}, props), { type: new Float64() }))]);
|
|
6484
|
+
}
|
|
6485
|
+
throw new Error("Unrecognized input");
|
|
6486
|
+
}
|
|
6487
|
+
}
|
|
6488
|
+
throw new Error("Unrecognized input");
|
|
6489
|
+
}
|
|
6490
|
+
function unwrapInputs(x) {
|
|
6491
|
+
return x instanceof Data ? [x] : x instanceof Vector ? x.data : makeVector(x).data;
|
|
6492
|
+
}
|
|
6400
6493
|
|
|
6401
6494
|
// ../node_modules/apache-arrow/builder/valid.mjs
|
|
6402
6495
|
function createIsValidFunction(nullValues) {
|
|
@@ -8872,6 +8965,106 @@ function makeBuilder(options) {
|
|
|
8872
8965
|
}
|
|
8873
8966
|
return builder;
|
|
8874
8967
|
}
|
|
8968
|
+
function vectorFromArray(init, type) {
|
|
8969
|
+
if (init instanceof Data || init instanceof Vector || init.type instanceof DataType || ArrayBuffer.isView(init)) {
|
|
8970
|
+
return makeVector(init);
|
|
8971
|
+
}
|
|
8972
|
+
const options = { type: type !== null && type !== void 0 ? type : inferType(init), nullValues: [null] };
|
|
8973
|
+
const chunks = [...builderThroughIterable(options)(init)];
|
|
8974
|
+
const vector = chunks.length === 1 ? chunks[0] : chunks.reduce((a, b) => a.concat(b));
|
|
8975
|
+
if (DataType.isDictionary(vector.type)) {
|
|
8976
|
+
return vector.memoize();
|
|
8977
|
+
}
|
|
8978
|
+
return vector;
|
|
8979
|
+
}
|
|
8980
|
+
function inferType(value) {
|
|
8981
|
+
if (value.length === 0) {
|
|
8982
|
+
return new Null2();
|
|
8983
|
+
}
|
|
8984
|
+
let nullsCount = 0;
|
|
8985
|
+
let arraysCount = 0;
|
|
8986
|
+
let objectsCount = 0;
|
|
8987
|
+
let numbersCount = 0;
|
|
8988
|
+
let stringsCount = 0;
|
|
8989
|
+
let bigintsCount = 0;
|
|
8990
|
+
let booleansCount = 0;
|
|
8991
|
+
let datesCount = 0;
|
|
8992
|
+
for (const val of value) {
|
|
8993
|
+
if (val == null) {
|
|
8994
|
+
++nullsCount;
|
|
8995
|
+
continue;
|
|
8996
|
+
}
|
|
8997
|
+
switch (typeof val) {
|
|
8998
|
+
case "bigint":
|
|
8999
|
+
++bigintsCount;
|
|
9000
|
+
continue;
|
|
9001
|
+
case "boolean":
|
|
9002
|
+
++booleansCount;
|
|
9003
|
+
continue;
|
|
9004
|
+
case "number":
|
|
9005
|
+
++numbersCount;
|
|
9006
|
+
continue;
|
|
9007
|
+
case "string":
|
|
9008
|
+
++stringsCount;
|
|
9009
|
+
continue;
|
|
9010
|
+
case "object":
|
|
9011
|
+
if (Array.isArray(val)) {
|
|
9012
|
+
++arraysCount;
|
|
9013
|
+
} else if (Object.prototype.toString.call(val) === "[object Date]") {
|
|
9014
|
+
++datesCount;
|
|
9015
|
+
} else {
|
|
9016
|
+
++objectsCount;
|
|
9017
|
+
}
|
|
9018
|
+
continue;
|
|
9019
|
+
}
|
|
9020
|
+
throw new TypeError("Unable to infer Vector type from input values, explicit type declaration expected.");
|
|
9021
|
+
}
|
|
9022
|
+
if (numbersCount + nullsCount === value.length) {
|
|
9023
|
+
return new Float64();
|
|
9024
|
+
} else if (stringsCount + nullsCount === value.length) {
|
|
9025
|
+
return new Dictionary(new Utf82(), new Int32());
|
|
9026
|
+
} else if (bigintsCount + nullsCount === value.length) {
|
|
9027
|
+
return new Int64();
|
|
9028
|
+
} else if (booleansCount + nullsCount === value.length) {
|
|
9029
|
+
return new Bool2();
|
|
9030
|
+
} else if (datesCount + nullsCount === value.length) {
|
|
9031
|
+
return new TimestampMillisecond();
|
|
9032
|
+
} else if (arraysCount + nullsCount === value.length) {
|
|
9033
|
+
const array = value;
|
|
9034
|
+
const childType = inferType(array[array.findIndex((ary) => ary != null)]);
|
|
9035
|
+
if (array.every((ary) => ary == null || compareTypes(childType, inferType(ary)))) {
|
|
9036
|
+
return new List2(new Field2("", childType, true));
|
|
9037
|
+
}
|
|
9038
|
+
} else if (objectsCount + nullsCount === value.length) {
|
|
9039
|
+
const fields = /* @__PURE__ */ new Map();
|
|
9040
|
+
for (const row of value) {
|
|
9041
|
+
for (const key of Object.keys(row)) {
|
|
9042
|
+
if (!fields.has(key) && row[key] != null) {
|
|
9043
|
+
fields.set(key, new Field2(key, inferType([row[key]]), true));
|
|
9044
|
+
}
|
|
9045
|
+
}
|
|
9046
|
+
}
|
|
9047
|
+
return new Struct([...fields.values()]);
|
|
9048
|
+
}
|
|
9049
|
+
throw new TypeError("Unable to infer Vector type from input values, explicit type declaration expected.");
|
|
9050
|
+
}
|
|
9051
|
+
function builderThroughIterable(options) {
|
|
9052
|
+
const { ["queueingStrategy"]: queueingStrategy = "count" } = options;
|
|
9053
|
+
const { ["highWaterMark"]: highWaterMark = queueingStrategy !== "bytes" ? Number.POSITIVE_INFINITY : Math.pow(2, 14) } = options;
|
|
9054
|
+
const sizeProperty = queueingStrategy !== "bytes" ? "length" : "byteLength";
|
|
9055
|
+
return function* (source) {
|
|
9056
|
+
let numChunks = 0;
|
|
9057
|
+
const builder = makeBuilder(options);
|
|
9058
|
+
for (const value of source) {
|
|
9059
|
+
if (builder.append(value)[sizeProperty] >= highWaterMark) {
|
|
9060
|
+
++numChunks && (yield builder.toVector());
|
|
9061
|
+
}
|
|
9062
|
+
}
|
|
9063
|
+
if (builder.finish().length > 0 || numChunks === 0) {
|
|
9064
|
+
yield builder.toVector();
|
|
9065
|
+
}
|
|
9066
|
+
};
|
|
9067
|
+
}
|
|
8875
9068
|
|
|
8876
9069
|
// ../node_modules/apache-arrow/util/recordbatch.mjs
|
|
8877
9070
|
function distributeVectorsIntoRecordBatches(schema, vecs) {
|
|
@@ -11858,6 +12051,11 @@ function recordBatchWriterThroughDOMStream(writableStrategy, readableStrategy) {
|
|
|
11858
12051
|
}
|
|
11859
12052
|
}
|
|
11860
12053
|
|
|
12054
|
+
// ../node_modules/apache-arrow/ipc/serialization.mjs
|
|
12055
|
+
function tableToIPC(table, type = "stream") {
|
|
12056
|
+
return (type === "stream" ? RecordBatchStreamWriter : RecordBatchFileWriter).writeAll(table).toUint8Array(true);
|
|
12057
|
+
}
|
|
12058
|
+
|
|
11861
12059
|
// ../node_modules/apache-arrow/Arrow.mjs
|
|
11862
12060
|
var util = Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign(Object.assign({}, bn_exports), int_exports), bit_exports), math_exports), buffer_exports), vector_exports), pretty_exports), {
|
|
11863
12061
|
compareSchemas,
|
|
@@ -11876,6 +12074,36 @@ RecordBatchFileWriter["throughDOM"] = recordBatchWriterThroughDOMStream;
|
|
|
11876
12074
|
RecordBatchStreamWriter["throughDOM"] = recordBatchWriterThroughDOMStream;
|
|
11877
12075
|
|
|
11878
12076
|
// api.ts
|
|
12077
|
+
function normalizeArrowInsertOptions(options) {
|
|
12078
|
+
if (!options.name) throw new Error("Arrow insert table name must not be empty");
|
|
12079
|
+
return { name: options.name, schema: options.schema ?? null, create: options.create ?? true };
|
|
12080
|
+
}
|
|
12081
|
+
function validateDatabaseName(databaseName) {
|
|
12082
|
+
if (!databaseName) throw new Error("Database name must not be empty");
|
|
12083
|
+
if (databaseName.includes("\0")) throw new Error("Database name must not contain NUL");
|
|
12084
|
+
}
|
|
12085
|
+
function validateDatabaseAlias(alias) {
|
|
12086
|
+
if (!alias) throw new Error("Database alias must not be empty");
|
|
12087
|
+
if (alias.includes("\0")) throw new Error("Database alias must not contain NUL");
|
|
12088
|
+
}
|
|
12089
|
+
function materializeDictionaryColumns(table) {
|
|
12090
|
+
const dictionaryColumns = table.schema.fields.map((field, index) => DataType.isDictionary(field.type) ? index : -1).filter((index) => index !== -1);
|
|
12091
|
+
if (dictionaryColumns.length === 0) return table;
|
|
12092
|
+
const fields = table.schema.fields.map((field) => DataType.isDictionary(field.type) ? field.clone({ type: field.type.dictionary }) : field);
|
|
12093
|
+
const batches = table.batches.map((batch) => {
|
|
12094
|
+
let result = batch;
|
|
12095
|
+
for (const index of dictionaryColumns) {
|
|
12096
|
+
const column = result.getChildAt(index);
|
|
12097
|
+
const sourceField = table.schema.fields[index];
|
|
12098
|
+
if (!column || !sourceField) throw new Error(`Arrow table is missing column ${index}`);
|
|
12099
|
+
const type = sourceField.type;
|
|
12100
|
+
const materialized = vectorFromArray([...column], type.dictionary);
|
|
12101
|
+
result = result.setChildAt(index, materialized);
|
|
12102
|
+
}
|
|
12103
|
+
return result;
|
|
12104
|
+
});
|
|
12105
|
+
return new Table(new Schema2(fields, table.schema.metadata, null, table.schema.metadataVersion), batches);
|
|
12106
|
+
}
|
|
11879
12107
|
function readHandleFromResult(result, operation) {
|
|
11880
12108
|
if (result.state !== "ok") throw readErrorFromResult(result, operation);
|
|
11881
12109
|
if (result.payload.byteLength !== 4) throw new Error(`${operation} returned an invalid handle`);
|
|
@@ -11898,6 +12126,7 @@ var HyperDB = class _HyperDB {
|
|
|
11898
12126
|
client;
|
|
11899
12127
|
terminated = false;
|
|
11900
12128
|
termination;
|
|
12129
|
+
activeDatabaseOperations = 0;
|
|
11901
12130
|
connections = /* @__PURE__ */ new Set();
|
|
11902
12131
|
static async create(options = {}) {
|
|
11903
12132
|
const { createBrowserClient: createBrowserClient2 } = await Promise.resolve().then(() => (init_browser(), browser_exports));
|
|
@@ -11916,8 +12145,36 @@ var HyperDB = class _HyperDB {
|
|
|
11916
12145
|
this.connections.add(result);
|
|
11917
12146
|
return result;
|
|
11918
12147
|
}
|
|
12148
|
+
async createDatabase(databaseName, options = {}) {
|
|
12149
|
+
validateDatabaseName(databaseName);
|
|
12150
|
+
if (options.storage !== void 0 && options.storage !== "memory" && options.storage !== "persistent") {
|
|
12151
|
+
throw new Error(`Unsupported database storage: ${String(options.storage)}`);
|
|
12152
|
+
}
|
|
12153
|
+
await this.runDatabaseOperation(async () => {
|
|
12154
|
+
expectOk(await this.client.createDatabase(databaseName, options.storage === "persistent"), "create database");
|
|
12155
|
+
});
|
|
12156
|
+
}
|
|
12157
|
+
async openDatabase(databaseName) {
|
|
12158
|
+
validateDatabaseName(databaseName);
|
|
12159
|
+
await this.runDatabaseOperation(async () => {
|
|
12160
|
+
expectOk(await this.client.openDatabase(databaseName), "open database");
|
|
12161
|
+
});
|
|
12162
|
+
}
|
|
12163
|
+
async checkpointDatabase(databaseName) {
|
|
12164
|
+
validateDatabaseName(databaseName);
|
|
12165
|
+
await this.runDatabaseOperation(async () => {
|
|
12166
|
+
expectOk(await this.client.checkpointDatabase(databaseName), "checkpoint database");
|
|
12167
|
+
});
|
|
12168
|
+
}
|
|
12169
|
+
async dropDatabase(databaseName) {
|
|
12170
|
+
validateDatabaseName(databaseName);
|
|
12171
|
+
await this.runDatabaseOperation(async () => {
|
|
12172
|
+
expectOk(await this.client.dropDatabase(databaseName), "drop database");
|
|
12173
|
+
});
|
|
12174
|
+
}
|
|
11919
12175
|
async terminate() {
|
|
11920
12176
|
if (this.terminated) return;
|
|
12177
|
+
if (this.activeDatabaseOperations !== 0) throw new Error("database has an active operation");
|
|
11921
12178
|
if (!this.termination) {
|
|
11922
12179
|
this.termination = (async () => {
|
|
11923
12180
|
try {
|
|
@@ -11926,6 +12183,12 @@ var HyperDB = class _HyperDB {
|
|
|
11926
12183
|
this.termination = void 0;
|
|
11927
12184
|
throw error;
|
|
11928
12185
|
}
|
|
12186
|
+
try {
|
|
12187
|
+
expectOk(await this.client.shutdown(), "shutdown");
|
|
12188
|
+
} catch (error) {
|
|
12189
|
+
this.termination = void 0;
|
|
12190
|
+
throw error;
|
|
12191
|
+
}
|
|
11929
12192
|
this.terminated = true;
|
|
11930
12193
|
await this.client.terminate();
|
|
11931
12194
|
})();
|
|
@@ -11934,6 +12197,16 @@ var HyperDB = class _HyperDB {
|
|
|
11934
12197
|
}
|
|
11935
12198
|
ensureRunning() {
|
|
11936
12199
|
if (this.terminated) throw new Error("database is terminated");
|
|
12200
|
+
if (this.termination) throw new Error("database is terminating");
|
|
12201
|
+
}
|
|
12202
|
+
async runDatabaseOperation(operation) {
|
|
12203
|
+
this.ensureRunning();
|
|
12204
|
+
++this.activeDatabaseOperations;
|
|
12205
|
+
try {
|
|
12206
|
+
await operation();
|
|
12207
|
+
} finally {
|
|
12208
|
+
--this.activeDatabaseOperations;
|
|
12209
|
+
}
|
|
11937
12210
|
}
|
|
11938
12211
|
};
|
|
11939
12212
|
var HyperDBConnection = class {
|
|
@@ -11950,7 +12223,7 @@ var HyperDBConnection = class {
|
|
|
11950
12223
|
closing;
|
|
11951
12224
|
async close() {
|
|
11952
12225
|
if (this.closed) return;
|
|
11953
|
-
if (this.active) throw new Error("connection has an active
|
|
12226
|
+
if (this.active) throw new Error("connection has an active operation");
|
|
11954
12227
|
if (!this.closing) {
|
|
11955
12228
|
this.closing = (async () => {
|
|
11956
12229
|
try {
|
|
@@ -11965,6 +12238,25 @@ var HyperDBConnection = class {
|
|
|
11965
12238
|
}
|
|
11966
12239
|
await this.closing;
|
|
11967
12240
|
}
|
|
12241
|
+
async attachDatabase(databaseName, alias = databaseName) {
|
|
12242
|
+
validateDatabaseName(databaseName);
|
|
12243
|
+
validateDatabaseAlias(alias);
|
|
12244
|
+
this.ensureAvailable();
|
|
12245
|
+
try {
|
|
12246
|
+
expectOk(await this.client.attachDatabase(this.connection, databaseName, alias), "attach database");
|
|
12247
|
+
} finally {
|
|
12248
|
+
this.active = false;
|
|
12249
|
+
}
|
|
12250
|
+
}
|
|
12251
|
+
async detachDatabase(alias) {
|
|
12252
|
+
validateDatabaseAlias(alias);
|
|
12253
|
+
this.ensureAvailable();
|
|
12254
|
+
try {
|
|
12255
|
+
expectOk(await this.client.detachDatabase(this.connection, alias), "detach database");
|
|
12256
|
+
} finally {
|
|
12257
|
+
this.active = false;
|
|
12258
|
+
}
|
|
12259
|
+
}
|
|
11968
12260
|
sendIPC(sql) {
|
|
11969
12261
|
const connection = this;
|
|
11970
12262
|
return {
|
|
@@ -12051,11 +12343,55 @@ var HyperDBConnection = class {
|
|
|
12051
12343
|
for await (const batch of reader) batches.push(batch);
|
|
12052
12344
|
return new Table(reader.schema, batches);
|
|
12053
12345
|
}
|
|
12346
|
+
async insertArrowTable(table, options) {
|
|
12347
|
+
this.ensureAvailable();
|
|
12348
|
+
let path;
|
|
12349
|
+
try {
|
|
12350
|
+
const result = await this.client.createTemporaryFile(tableToIPC(materializeDictionaryColumns(table), "stream"));
|
|
12351
|
+
expectOk(result, "create temporary Arrow IPC file");
|
|
12352
|
+
path = new TextDecoder().decode(result.payload);
|
|
12353
|
+
await this.insertArrowIPCFromPathInternal(path, options, true, false);
|
|
12354
|
+
} finally {
|
|
12355
|
+
try {
|
|
12356
|
+
if (path !== void 0) expectOk(await this.client.removeFile(path), "remove temporary Arrow IPC file");
|
|
12357
|
+
} finally {
|
|
12358
|
+
this.active = false;
|
|
12359
|
+
}
|
|
12360
|
+
}
|
|
12361
|
+
}
|
|
12362
|
+
async insertArrowIPCFromPath(path, options) {
|
|
12363
|
+
await this.insertArrowIPCFromPathInternal(path, options, false);
|
|
12364
|
+
}
|
|
12365
|
+
async insertArrowIPCFromPathInternal(path, options, internal, acquire = true) {
|
|
12366
|
+
if (!path) throw new Error("Arrow IPC input path must not be empty");
|
|
12367
|
+
const normalized = normalizeArrowInsertOptions(options);
|
|
12368
|
+
if (acquire) this.ensureAvailable();
|
|
12369
|
+
let query;
|
|
12370
|
+
try {
|
|
12371
|
+
query = readHandleFromResult(await this.client.insertArrowIPCFromPath(this.connection, path, normalized.name, normalized.schema, normalized.create, internal), "insert Arrow IPC from path");
|
|
12372
|
+
for (; ; ) {
|
|
12373
|
+
let result = await this.client.pollQuery(query);
|
|
12374
|
+
while (result.state === "pending") {
|
|
12375
|
+
await yieldToEventLoop();
|
|
12376
|
+
result = await this.client.pollQuery(query);
|
|
12377
|
+
}
|
|
12378
|
+
if (result.state === "done") return;
|
|
12379
|
+
if (result.state === "chunk") continue;
|
|
12380
|
+
throw readErrorFromResult(result, "insert Arrow IPC from path");
|
|
12381
|
+
}
|
|
12382
|
+
} finally {
|
|
12383
|
+
try {
|
|
12384
|
+
if (query !== void 0) await this.release(query);
|
|
12385
|
+
} finally {
|
|
12386
|
+
if (acquire) this.active = false;
|
|
12387
|
+
}
|
|
12388
|
+
}
|
|
12389
|
+
}
|
|
12054
12390
|
ensureAvailable() {
|
|
12055
12391
|
if (this.closed) throw new Error("connection is closed");
|
|
12056
12392
|
if (this.closing) throw new Error("connection is closing");
|
|
12057
12393
|
if (this.active) {
|
|
12058
|
-
throw new Error("connection has an active
|
|
12394
|
+
throw new Error("connection has an active operation");
|
|
12059
12395
|
}
|
|
12060
12396
|
this.active = true;
|
|
12061
12397
|
}
|
|
@@ -12070,7 +12406,7 @@ var HyperDBConnection = class {
|
|
|
12070
12406
|
}
|
|
12071
12407
|
}
|
|
12072
12408
|
async closeForTermination() {
|
|
12073
|
-
if (this.active) throw new Error("cannot terminate while a connection has an active
|
|
12409
|
+
if (this.active) throw new Error("cannot terminate while a connection has an active operation");
|
|
12074
12410
|
await this.close();
|
|
12075
12411
|
}
|
|
12076
12412
|
};
|