@based/db 0.1.5 → 0.2.0
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/lib/darwin_aarch64/include/cdefs.h +4 -0
- package/dist/lib/darwin_aarch64/include/selva/db.h +25 -5
- package/dist/lib/darwin_aarch64/include/selva/fields.h +34 -72
- package/dist/lib/darwin_aarch64/include/selva/selva_lang.h +7 -0
- package/dist/lib/darwin_aarch64/include/selva/types.h +9 -13
- package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
- package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
- package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v25.node +0 -0
- package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
- package/dist/lib/linux_aarch64/include/cdefs.h +4 -0
- package/dist/lib/linux_aarch64/include/selva/db.h +25 -5
- package/dist/lib/linux_aarch64/include/selva/fields.h +34 -72
- package/dist/lib/linux_aarch64/include/selva/selva_lang.h +7 -0
- package/dist/lib/linux_aarch64/include/selva/types.h +9 -13
- package/dist/lib/linux_aarch64/libdeflate.so +0 -0
- package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v25.node +0 -0
- package/dist/lib/linux_aarch64/libselva.so +0 -0
- package/dist/lib/linux_x86_64/include/cdefs.h +4 -0
- package/dist/lib/linux_x86_64/include/selva/db.h +25 -5
- package/dist/lib/linux_x86_64/include/selva/fields.h +34 -72
- package/dist/lib/linux_x86_64/include/selva/selva_lang.h +7 -0
- package/dist/lib/linux_x86_64/include/selva/types.h +9 -13
- package/dist/lib/linux_x86_64/libdeflate.so +0 -0
- package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v25.node +0 -0
- package/dist/lib/linux_x86_64/libselva.so +0 -0
- package/dist/src/client/modify/Tmp.d.ts +0 -1
- package/dist/src/client/modify/Tmp.js +14 -9
- package/dist/src/client/modify/create/index.js +6 -5
- package/dist/src/client/modify/drain.js +6 -2
- package/dist/src/client/modify/edges/binary.js +3 -2
- package/dist/src/client/modify/edges/reference.js +2 -3
- package/dist/src/client/modify/edges/references.js +2 -3
- package/dist/src/client/modify/edges/separate.js +4 -1
- package/dist/src/client/modify/edges/string.js +2 -3
- package/dist/src/client/modify/error.d.ts +29 -0
- package/dist/src/client/modify/error.js +10 -0
- package/dist/src/client/modify/props/alias.js +1 -1
- package/dist/src/client/modify/props/binary.js +1 -1
- package/dist/src/client/modify/props/cardinality.js +1 -1
- package/dist/src/client/modify/props/fixed.js +13 -14
- package/dist/src/client/modify/props/reference.js +2 -2
- package/dist/src/client/modify/props/references.js +7 -7
- package/dist/src/client/modify/props/string.js +1 -1
- package/dist/src/client/modify/props/vector.js +1 -11
- package/dist/src/client/modify/types.d.ts +1 -0
- package/dist/src/client/modify/types.js +1 -0
- package/dist/src/client/modify/validate.d.ts +1 -1
- package/dist/src/client/modify/validate.js +4 -3
- package/dist/src/client/query/BasedDbQuery.d.ts +4 -4
- package/dist/src/client/query/BasedDbQuery.js +23 -12
- package/dist/src/client/query/BasedQueryResponse.d.ts +0 -1
- package/dist/src/client/query/BasedQueryResponse.js +0 -3
- package/dist/src/client/query/aggregates/aggregation.d.ts +1 -1
- package/dist/src/client/query/aggregates/aggregation.js +123 -51
- package/dist/src/client/query/display.js +14 -8
- package/dist/src/client/query/filter/createFixedFilterBuffer.js +59 -22
- package/dist/src/client/query/filter/createReferenceFilter.js +13 -13
- package/dist/src/client/query/filter/createVariableFilterBuffer.js +6 -3
- package/dist/src/client/query/filter/filter.js +8 -0
- package/dist/src/client/query/filter/primitiveFilter.js +4 -1
- package/dist/src/client/query/filter/{toBuffer.d.ts → toByteCode.d.ts} +3 -2
- package/dist/src/client/query/filter/{toBuffer.js → toByteCode.js} +46 -12
- package/dist/src/client/query/filter/types.d.ts +1 -0
- package/dist/src/client/query/filter/types.js +33 -5
- package/dist/src/client/query/include/toByteCode.d.ts +2 -2
- package/dist/src/client/query/include/toByteCode.js +1 -1
- package/dist/src/client/query/query.d.ts +1 -1
- package/dist/src/client/query/query.js +1 -1
- package/dist/src/client/query/queryDef.js +6 -2
- package/dist/src/client/query/registerQuery.js +5 -9
- package/dist/src/client/query/subscription/index.js +5 -0
- package/dist/src/client/query/subscription/toByteCode.d.ts +6 -0
- package/dist/src/client/query/subscription/toByteCode.js +139 -0
- package/dist/src/client/query/subscription/types.d.ts +6 -0
- package/dist/src/client/query/subscription/types.js +7 -9
- package/dist/src/client/query/toByteCode/aggregates.d.ts +2 -0
- package/dist/src/client/query/toByteCode/aggregates.js +60 -0
- package/dist/src/client/query/toByteCode/alias.d.ts +2 -0
- package/dist/src/client/query/toByteCode/alias.js +24 -0
- package/dist/src/client/query/toByteCode/default.d.ts +2 -2
- package/dist/src/client/query/toByteCode/default.js +21 -24
- package/dist/src/client/query/toByteCode/id.d.ts +2 -0
- package/dist/src/client/query/toByteCode/id.js +17 -0
- package/dist/src/client/query/toByteCode/ids.d.ts +2 -0
- package/dist/src/client/query/toByteCode/ids.js +52 -0
- package/dist/src/client/query/toByteCode/offsets.d.ts +35 -0
- package/dist/src/client/query/toByteCode/offsets.js +36 -0
- package/dist/src/client/query/toByteCode/reference.d.ts +2 -0
- package/dist/src/client/query/toByteCode/reference.js +12 -0
- package/dist/src/client/query/toByteCode/references.d.ts +2 -0
- package/dist/src/client/query/toByteCode/references.js +35 -0
- package/dist/src/client/query/toByteCode/toByteCode.d.ts +4 -2
- package/dist/src/client/query/toByteCode/toByteCode.js +62 -189
- package/dist/src/client/query/types.d.ts +24 -5
- package/dist/src/client/query/validation.d.ts +5 -1
- package/dist/src/client/query/validation.js +15 -3
- package/dist/src/client/string.js +1 -1
- package/dist/src/hooks.js +2 -29
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.js +1 -0
- package/dist/src/native.d.ts +8 -1
- package/dist/src/native.js +23 -2
- package/dist/src/server/index.d.ts +2 -0
- package/dist/src/server/index.js +11 -2
- package/dist/src/server/schema.js +3 -2
- package/dist/src/server/schemaSelvaBuffer.d.ts +4 -0
- package/dist/src/server/schemaSelvaBuffer.js +182 -0
- package/dist/src/server/subscription.d.ts +32 -0
- package/dist/src/server/subscription.js +275 -0
- package/dist/src/utils.js +2 -3
- package/package.json +5 -6
package/dist/src/server/index.js
CHANGED
|
@@ -5,7 +5,6 @@ import { ID_FIELD_DEF } from '@based/schema/def';
|
|
|
5
5
|
import { start } from './start.js';
|
|
6
6
|
import { destructureTreeKey, makeTreeKeyFromNodeId } from './tree.js';
|
|
7
7
|
import { save } from './save.js';
|
|
8
|
-
import { setTimeout } from 'node:timers/promises';
|
|
9
8
|
import { migrate } from './migrate/index.js';
|
|
10
9
|
import { debugServer } from '../utils.js';
|
|
11
10
|
import { readUint16, readUint64 } from '@based/utils';
|
|
@@ -27,6 +26,15 @@ class SortIndex {
|
|
|
27
26
|
export class DbServer extends DbShared {
|
|
28
27
|
modifyDirtyRanges;
|
|
29
28
|
dbCtxExternal; // pointer to zig dbCtx
|
|
29
|
+
subscriptions = {
|
|
30
|
+
subInterval: 200,
|
|
31
|
+
active: 0,
|
|
32
|
+
updateHandler: null,
|
|
33
|
+
ids: new Map(),
|
|
34
|
+
fullType: new Map(),
|
|
35
|
+
updateId: 1,
|
|
36
|
+
now: { listeners: new Set(), lastUpdated: 1 },
|
|
37
|
+
};
|
|
30
38
|
migrating = null;
|
|
31
39
|
saveInProgress = false;
|
|
32
40
|
fileSystemPath;
|
|
@@ -382,6 +390,8 @@ export class DbServer extends DbShared {
|
|
|
382
390
|
if (this.stopped) {
|
|
383
391
|
return;
|
|
384
392
|
}
|
|
393
|
+
clearTimeout(this.subscriptions.updateHandler);
|
|
394
|
+
this.subscriptions.updateHandler = null;
|
|
385
395
|
this.stopped = true;
|
|
386
396
|
this.unlistenExit();
|
|
387
397
|
if (this.cleanupTimer) {
|
|
@@ -402,7 +412,6 @@ export class DbServer extends DbShared {
|
|
|
402
412
|
this.workers = [];
|
|
403
413
|
native.stop(this.dbCtxExternal);
|
|
404
414
|
this.dbCtxExternal = null;
|
|
405
|
-
await setTimeout(100);
|
|
406
415
|
}
|
|
407
416
|
catch (e) {
|
|
408
417
|
this.stopped = false;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { updateTypeDefs } from '@based/schema/def';
|
|
2
2
|
import { serialize } from '@based/schema';
|
|
3
3
|
import { join } from 'node:path';
|
|
4
4
|
import { writeFile } from 'node:fs/promises';
|
|
@@ -8,6 +8,7 @@ import { saveSync } from './save.js';
|
|
|
8
8
|
import { writeCreate } from '../client/modify/create/index.js';
|
|
9
9
|
import { Ctx } from '../client/modify/Ctx.js';
|
|
10
10
|
import { consume } from '../client/modify/drain.js';
|
|
11
|
+
import { schemaToSelvaBuffer } from './schemaSelvaBuffer.js';
|
|
11
12
|
export const setSchemaOnServer = (server, schema) => {
|
|
12
13
|
const { schemaTypesParsed, schemaTypesParsedById } = updateTypeDefs(schema);
|
|
13
14
|
server.schema = schema;
|
|
@@ -40,7 +41,7 @@ export const setNativeSchema = (server, schema) => {
|
|
|
40
41
|
const type = server.schemaTypesParsed[types[i]];
|
|
41
42
|
maxTid = Math.max(maxTid, type.id);
|
|
42
43
|
try {
|
|
43
|
-
native.setSchemaType(type.id, new Uint8Array(s[i])
|
|
44
|
+
native.setSchemaType(server.dbCtxExternal, type.id, new Uint8Array(s[i]));
|
|
44
45
|
}
|
|
45
46
|
catch (err) {
|
|
46
47
|
throw new Error(`Cannot update schema on selva (native) ${type.type} ${err.message}`);
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { ENCODER, writeDoubleLE, writeUint16, writeUint32, writeUint64 } from '@based/utils';
|
|
2
|
+
import { ALIAS, ALIASES, BINARY, EMPTY_MICRO_BUFFER, CARDINALITY, MICRO_BUFFER, REFERENCE, REFERENCES, STRING, TEXT, VECTOR, JSON, COLVEC, VECTOR_BASE_TYPE_SIZE_MAP, INT8, UINT8, BOOLEAN, INT16, UINT16, INT32, UINT32, NUMBER, TIMESTAMP, ENUM, } from '@based/schema/def';
|
|
3
|
+
import { NOT_COMPRESSED } from '@based/protocol';
|
|
4
|
+
import native from '../native.js';
|
|
5
|
+
const selvaFieldType = {
|
|
6
|
+
NULL: 0,
|
|
7
|
+
MICRO_BUFFER: 1,
|
|
8
|
+
STRING: 2,
|
|
9
|
+
TEXT: 3,
|
|
10
|
+
REFERENCE: 4,
|
|
11
|
+
REFERENCES: 5,
|
|
12
|
+
ALIAS: 8,
|
|
13
|
+
ALIASES: 9,
|
|
14
|
+
COLVEC: 10,
|
|
15
|
+
};
|
|
16
|
+
const selvaTypeMap = new Uint8Array(32); // 1.2x faster than JS array
|
|
17
|
+
selvaTypeMap[MICRO_BUFFER] = selvaFieldType.MICRO_BUFFER;
|
|
18
|
+
selvaTypeMap[VECTOR] = selvaFieldType.MICRO_BUFFER;
|
|
19
|
+
selvaTypeMap[BINARY] = selvaFieldType.STRING;
|
|
20
|
+
selvaTypeMap[CARDINALITY] = selvaFieldType.STRING;
|
|
21
|
+
selvaTypeMap[JSON] = selvaFieldType.STRING;
|
|
22
|
+
selvaTypeMap[STRING] = selvaFieldType.STRING;
|
|
23
|
+
selvaTypeMap[TEXT] = selvaFieldType.TEXT;
|
|
24
|
+
selvaTypeMap[REFERENCE] = selvaFieldType.REFERENCE;
|
|
25
|
+
selvaTypeMap[REFERENCES] = selvaFieldType.REFERENCES;
|
|
26
|
+
selvaTypeMap[ALIAS] = selvaFieldType.ALIAS;
|
|
27
|
+
selvaTypeMap[ALIASES] = selvaFieldType.ALIASES;
|
|
28
|
+
selvaTypeMap[COLVEC] = selvaFieldType.COLVEC;
|
|
29
|
+
const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
|
|
30
|
+
function blockCapacity(blockCapacity) {
|
|
31
|
+
const buf = new Uint8Array(Uint32Array.BYTES_PER_ELEMENT);
|
|
32
|
+
const view = new DataView(buf.buffer);
|
|
33
|
+
view.setUint32(0, blockCapacity, true);
|
|
34
|
+
return buf;
|
|
35
|
+
}
|
|
36
|
+
function sepPropCount(props) {
|
|
37
|
+
return props.filter((prop) => prop.separate).length;
|
|
38
|
+
}
|
|
39
|
+
function makeEdgeConstraintFlags(prop) {
|
|
40
|
+
let flags = 0;
|
|
41
|
+
flags |= prop.dependent ? EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT : 0x00;
|
|
42
|
+
return flags;
|
|
43
|
+
}
|
|
44
|
+
const propDefBuffer = (schema, prop) => {
|
|
45
|
+
const type = prop.typeIndex;
|
|
46
|
+
const selvaType = selvaTypeMap[type];
|
|
47
|
+
if (prop.len && (type === MICRO_BUFFER || type === VECTOR)) {
|
|
48
|
+
const buf = new Uint8Array(4);
|
|
49
|
+
const view = new DataView(buf.buffer);
|
|
50
|
+
buf[0] = selvaType;
|
|
51
|
+
view.setUint16(1, prop.len, true);
|
|
52
|
+
if (prop.default) {
|
|
53
|
+
buf[3] = 1; // has default
|
|
54
|
+
return [...buf, ...prop.default];
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
buf[3] = 0; // has default
|
|
58
|
+
return [...buf];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
else if (prop.len && type === COLVEC) {
|
|
62
|
+
const buf = new Uint8Array(5);
|
|
63
|
+
const view = new DataView(buf.buffer);
|
|
64
|
+
buf[0] = selvaType;
|
|
65
|
+
const baseSize = VECTOR_BASE_TYPE_SIZE_MAP[prop.vectorBaseType];
|
|
66
|
+
view.setUint16(1, prop.len / baseSize, true); // elements
|
|
67
|
+
view.setUint16(3, baseSize, true); // element size
|
|
68
|
+
return [...buf];
|
|
69
|
+
}
|
|
70
|
+
else if (type === REFERENCE || type === REFERENCES) {
|
|
71
|
+
const buf = new Uint8Array(7);
|
|
72
|
+
const view = new DataView(buf.buffer);
|
|
73
|
+
const dstType = schema[prop.inverseTypeName];
|
|
74
|
+
buf[0] = selvaType; // field type
|
|
75
|
+
buf[1] = makeEdgeConstraintFlags(prop); // flags
|
|
76
|
+
view.setUint16(2, dstType.id, true); // dst_node_type
|
|
77
|
+
buf[4] = prop.inversePropNumber; // inverse_field
|
|
78
|
+
view.setUint16(5, prop.edgeNodeTypeId ?? 0, true); // meta_node_type
|
|
79
|
+
return [...buf];
|
|
80
|
+
}
|
|
81
|
+
else if (type === STRING ||
|
|
82
|
+
type === BINARY ||
|
|
83
|
+
type === CARDINALITY ||
|
|
84
|
+
type === JSON) {
|
|
85
|
+
return [selvaType, prop.len < 50 ? prop.len : 0];
|
|
86
|
+
}
|
|
87
|
+
{
|
|
88
|
+
return [selvaType];
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
// TODO rewrite
|
|
92
|
+
export function schemaToSelvaBuffer(schema) {
|
|
93
|
+
return Object.values(schema).map((t) => {
|
|
94
|
+
const props = Object.values(t.props);
|
|
95
|
+
const rest = [];
|
|
96
|
+
const nrFields = 1 + sepPropCount(props);
|
|
97
|
+
let refFields = 0;
|
|
98
|
+
let virtualFields = 0;
|
|
99
|
+
if (nrFields >= 250) {
|
|
100
|
+
throw new Error('Too many fields');
|
|
101
|
+
}
|
|
102
|
+
const main = {
|
|
103
|
+
...EMPTY_MICRO_BUFFER,
|
|
104
|
+
len: t.mainLen === 0 ? 1 : t.mainLen,
|
|
105
|
+
};
|
|
106
|
+
for (const f of props) {
|
|
107
|
+
if (f.separate) {
|
|
108
|
+
if (f.typeIndex === REFERENCE || f.typeIndex === REFERENCES) {
|
|
109
|
+
refFields++;
|
|
110
|
+
}
|
|
111
|
+
else if (f.typeIndex === ALIAS ||
|
|
112
|
+
f.typeIndex === ALIASES ||
|
|
113
|
+
f.typeIndex === COLVEC) {
|
|
114
|
+
// We assume that these are always the last props!
|
|
115
|
+
virtualFields++;
|
|
116
|
+
}
|
|
117
|
+
rest.push(f);
|
|
118
|
+
}
|
|
119
|
+
else {
|
|
120
|
+
if (f.default) {
|
|
121
|
+
if (!main.default) {
|
|
122
|
+
main.default = new Uint8Array(main.len);
|
|
123
|
+
}
|
|
124
|
+
const buf = main.default;
|
|
125
|
+
switch (f.typeIndex) {
|
|
126
|
+
case INT8:
|
|
127
|
+
case UINT8:
|
|
128
|
+
case BOOLEAN:
|
|
129
|
+
case ENUM:
|
|
130
|
+
main.default[f.start] = f.default;
|
|
131
|
+
break;
|
|
132
|
+
case INT16:
|
|
133
|
+
case UINT16:
|
|
134
|
+
writeUint16(buf, f.default, f.start);
|
|
135
|
+
break;
|
|
136
|
+
case INT32:
|
|
137
|
+
case UINT32:
|
|
138
|
+
writeUint32(buf, f.default, f.start);
|
|
139
|
+
break;
|
|
140
|
+
case NUMBER:
|
|
141
|
+
writeDoubleLE(buf, f.default, f.start);
|
|
142
|
+
//const view = new DataView(
|
|
143
|
+
// buf.buffer,
|
|
144
|
+
// f.start,
|
|
145
|
+
// 8,
|
|
146
|
+
//)
|
|
147
|
+
//view.setFloat64(0, f.default, true)
|
|
148
|
+
break;
|
|
149
|
+
case TIMESTAMP:
|
|
150
|
+
writeUint64(buf, f.default, f.start);
|
|
151
|
+
break;
|
|
152
|
+
case BINARY:
|
|
153
|
+
case STRING:
|
|
154
|
+
if (f.default instanceof Uint8Array) {
|
|
155
|
+
buf.set(f.default, f.start);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
const value = f.default.normalize('NFKD');
|
|
159
|
+
buf[f.start] = 0; // lang
|
|
160
|
+
buf[f.start + 1] = NOT_COMPRESSED;
|
|
161
|
+
const { written: l } = ENCODER.encodeInto(value, buf.subarray(f.start + 2));
|
|
162
|
+
let crc = native.crc32(buf.subarray(f.start + 2, f.start + 2 + l));
|
|
163
|
+
writeUint32(buf, crc, f.start + 2 + l);
|
|
164
|
+
}
|
|
165
|
+
break;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
rest.sort((a, b) => a.prop - b.prop);
|
|
171
|
+
return Uint8Array.from([
|
|
172
|
+
...blockCapacity(t.blockCapacity), // u32 blockCapacity
|
|
173
|
+
nrFields, // u8 nrFields
|
|
174
|
+
1 + refFields, // u8 nrFixedFields
|
|
175
|
+
virtualFields, // u8 nrVirtualFields
|
|
176
|
+
6, // u8 version (generally follows the sdb version)
|
|
177
|
+
...propDefBuffer(schema, main),
|
|
178
|
+
...rest.map((f) => propDefBuffer(schema, f)).flat(1),
|
|
179
|
+
]).buffer;
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
//# sourceMappingURL=schemaSelvaBuffer.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { OnError } from '../client/query/subscription/types.js';
|
|
2
|
+
import { DbServer } from '../index.js';
|
|
3
|
+
type OnData = (res: Uint8Array) => void;
|
|
4
|
+
export type SubscriptionFullType = {
|
|
5
|
+
listeners: Set<() => void>;
|
|
6
|
+
};
|
|
7
|
+
export type SubscriptionNow = {
|
|
8
|
+
lastEval: number;
|
|
9
|
+
next: number;
|
|
10
|
+
listeners: Set<() => void>;
|
|
11
|
+
};
|
|
12
|
+
export type SubscriptionId = {
|
|
13
|
+
types?: Uint16Array;
|
|
14
|
+
ids: Map<number, Set<() => void>>;
|
|
15
|
+
typesListener?: () => void;
|
|
16
|
+
nowListener?: () => void;
|
|
17
|
+
};
|
|
18
|
+
export type Subscriptions = {
|
|
19
|
+
updateId: number;
|
|
20
|
+
active: number;
|
|
21
|
+
updateHandler: ReturnType<typeof setTimeout>;
|
|
22
|
+
ids: Map<number, SubscriptionId>;
|
|
23
|
+
fullType: Map<number, SubscriptionFullType>;
|
|
24
|
+
now: {
|
|
25
|
+
listeners: Set<() => void>;
|
|
26
|
+
lastUpdated: number;
|
|
27
|
+
};
|
|
28
|
+
subInterval: number;
|
|
29
|
+
};
|
|
30
|
+
export declare const startUpdateHandler: (server: DbServer) => void;
|
|
31
|
+
export declare const registerSubscription: (server: DbServer, query: Uint8Array, sub: Uint8Array, onData: OnData, onError: OnError, subInterval?: number) => () => void;
|
|
32
|
+
export {};
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { readInt64, readUint16, readUint32, writeInt64 } from '@based/utils';
|
|
2
|
+
import { SubscriptionType, } from '../client/query/subscription/types.js';
|
|
3
|
+
import native from '../native.js';
|
|
4
|
+
import { MAX_ID } from '@based/schema';
|
|
5
|
+
import { styleText } from 'util';
|
|
6
|
+
export const startUpdateHandler = (server) => {
|
|
7
|
+
// skip next if queries are sitll in progress can add a number for each staged sub
|
|
8
|
+
// combine this with handled modify
|
|
9
|
+
const scheduleUpdate = () => {
|
|
10
|
+
if (server.stopped) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
server.subscriptions.updateId++;
|
|
14
|
+
if (server.subscriptions.updateId > MAX_ID) {
|
|
15
|
+
server.subscriptions.updateId = 1;
|
|
16
|
+
}
|
|
17
|
+
// can do seperate timing for id / type
|
|
18
|
+
// scince multi queries are much heavier ofc
|
|
19
|
+
const markedIdSubs = native.getMarkedIdSubscriptions(server.dbCtxExternal);
|
|
20
|
+
if (markedIdSubs) {
|
|
21
|
+
const buffer = new Uint8Array(markedIdSubs);
|
|
22
|
+
for (let i = 0; i < buffer.byteLength; i += 8) {
|
|
23
|
+
const subId = readUint32(buffer, i);
|
|
24
|
+
const id = readUint32(buffer, i + 4);
|
|
25
|
+
const subContainer = server.subscriptions.ids.get(subId);
|
|
26
|
+
const ids = subContainer.ids.get(id);
|
|
27
|
+
if (ids) {
|
|
28
|
+
for (const fn of ids) {
|
|
29
|
+
fn();
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
const markedMultiSubs = native.getMarkedMultiSubscriptions(server.dbCtxExternal);
|
|
35
|
+
if (markedMultiSubs) {
|
|
36
|
+
const buffer = new Uint8Array(markedMultiSubs);
|
|
37
|
+
for (let i = 0; i < buffer.byteLength; i += 2) {
|
|
38
|
+
const typeId = readUint16(buffer, i);
|
|
39
|
+
const subs = server.subscriptions.fullType.get(typeId);
|
|
40
|
+
if (subs) {
|
|
41
|
+
for (const fn of subs.listeners) {
|
|
42
|
+
fn();
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (server.subscriptions.updateId - server.subscriptions.now.lastUpdated >
|
|
48
|
+
Math.max(1000 / server.subscriptions.subInterval, 1) // 1 time per second
|
|
49
|
+
) {
|
|
50
|
+
server.subscriptions.now.lastUpdated = server.subscriptions.updateId;
|
|
51
|
+
for (const fn of server.subscriptions.now.listeners) {
|
|
52
|
+
fn();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
server.subscriptions.updateHandler = setTimeout(scheduleUpdate, server.subscriptions.subInterval);
|
|
56
|
+
};
|
|
57
|
+
server.subscriptions.updateHandler = setTimeout(scheduleUpdate, server.subscriptions.subInterval);
|
|
58
|
+
};
|
|
59
|
+
const addToMultiSub = (server, typeId, runQuery) => {
|
|
60
|
+
let fullType;
|
|
61
|
+
let listeners;
|
|
62
|
+
if (!server.subscriptions.fullType.has(typeId)) {
|
|
63
|
+
listeners = new Set();
|
|
64
|
+
fullType = {
|
|
65
|
+
listeners,
|
|
66
|
+
};
|
|
67
|
+
server.subscriptions.fullType.set(typeId, fullType);
|
|
68
|
+
native.addMultiSubscription(server.dbCtxExternal, typeId);
|
|
69
|
+
}
|
|
70
|
+
else {
|
|
71
|
+
fullType = server.subscriptions.fullType.get(typeId);
|
|
72
|
+
listeners = fullType.listeners;
|
|
73
|
+
}
|
|
74
|
+
listeners.add(runQuery);
|
|
75
|
+
};
|
|
76
|
+
const removeFromMultiSub = (server, typeId, runQuery) => {
|
|
77
|
+
const typeSub = server.subscriptions.fullType.get(typeId);
|
|
78
|
+
if (!typeSub) {
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
typeSub.listeners.delete(runQuery);
|
|
82
|
+
if (typeSub.listeners.size === 0) {
|
|
83
|
+
native.removeMultiSubscription(server.dbCtxExternal, typeId);
|
|
84
|
+
server.subscriptions.fullType.delete(typeId);
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
const replaceNowValues = (query, now) => {
|
|
88
|
+
const dateNow = Date.now();
|
|
89
|
+
for (let i = 0; i < now.byteLength; i += 16) {
|
|
90
|
+
const offset = readInt64(now, i + 4);
|
|
91
|
+
const byteIndex = readUint32(now, i + 12);
|
|
92
|
+
writeInt64(query, dateNow + offset, byteIndex);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
export const registerSubscription = (server, query, sub, onData, onError, subInterval) => {
|
|
96
|
+
// this can change dynamicly
|
|
97
|
+
if (subInterval) {
|
|
98
|
+
server.subscriptions.subInterval = subInterval;
|
|
99
|
+
}
|
|
100
|
+
let killed = false;
|
|
101
|
+
// now maybe just once per second? (for now)
|
|
102
|
+
if (server.subscriptions.active === 0) {
|
|
103
|
+
startUpdateHandler(server);
|
|
104
|
+
}
|
|
105
|
+
let lastUpdated = 0;
|
|
106
|
+
let now;
|
|
107
|
+
const runQuery = () => {
|
|
108
|
+
if (lastUpdated !== server.subscriptions.updateId) {
|
|
109
|
+
lastUpdated = server.subscriptions.updateId;
|
|
110
|
+
if (now) {
|
|
111
|
+
replaceNowValues(query, now);
|
|
112
|
+
}
|
|
113
|
+
server.getQueryBuf(query).then((res) => {
|
|
114
|
+
if (killed) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (res.byteLength >= 4) {
|
|
118
|
+
onData(res);
|
|
119
|
+
}
|
|
120
|
+
else if (res.byteLength === 1 && res[0] === 0) {
|
|
121
|
+
server.emit('info', `Subscribe schema mismatch - should resolve after update`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
let name = styleText('red', `QueryError[]\n`);
|
|
126
|
+
name += ` Incorrect buffer received in subscription (maybe server not started ${res.byteLength}) bytes\n`;
|
|
127
|
+
onError(new Error(name));
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
console.log('Allready fired block');
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
server.subscriptions.active++;
|
|
136
|
+
if (sub[0] === SubscriptionType.singleId) {
|
|
137
|
+
const subId = readUint32(sub, 1);
|
|
138
|
+
const id = readUint32(sub, 7);
|
|
139
|
+
const headerLen = 16;
|
|
140
|
+
let subContainer;
|
|
141
|
+
let listeners;
|
|
142
|
+
if (!server.subscriptions.ids.get(subId)) {
|
|
143
|
+
subContainer = { ids: new Map() };
|
|
144
|
+
server.subscriptions.ids.set(subId, subContainer);
|
|
145
|
+
const typesLen = readUint16(sub, 12);
|
|
146
|
+
const nowLen = readUint16(sub, 14);
|
|
147
|
+
const fLen = sub[11];
|
|
148
|
+
if (typesLen != 0) {
|
|
149
|
+
// double check if this is alignment correct with the byteOffset else copy
|
|
150
|
+
const byteOffset = sub.byteOffset + headerLen + fLen;
|
|
151
|
+
if (byteOffset % 2 === 0) {
|
|
152
|
+
subContainer.types = new Uint16Array(sub.buffer, byteOffset, typesLen);
|
|
153
|
+
}
|
|
154
|
+
else {
|
|
155
|
+
subContainer.types = new Uint16Array(sub.slice(headerLen + fLen, headerLen + fLen + typesLen * 2));
|
|
156
|
+
}
|
|
157
|
+
subContainer.typesListener = () => {
|
|
158
|
+
for (const set of subContainer.ids.values()) {
|
|
159
|
+
for (const fn of set) {
|
|
160
|
+
fn();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
for (const typeId of subContainer.types) {
|
|
165
|
+
addToMultiSub(server, typeId, subContainer.typesListener);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
if (nowLen != 0) {
|
|
169
|
+
// when this is the case do a completely different strategy
|
|
170
|
+
// keep track of last update on sub id container
|
|
171
|
+
// and get the date allways (as a seperate query)
|
|
172
|
+
// when getting the date mark next in line
|
|
173
|
+
// have to make a copy (subArray is weak)
|
|
174
|
+
now = sub.slice(headerLen + fLen + typesLen * 2);
|
|
175
|
+
subContainer.nowListener = () => {
|
|
176
|
+
// per id want to have a last eval and needs next eval
|
|
177
|
+
for (const set of subContainer.ids.values()) {
|
|
178
|
+
for (const fn of set) {
|
|
179
|
+
fn();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
};
|
|
183
|
+
server.subscriptions.now.listeners.add(subContainer.nowListener);
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
subContainer = server.subscriptions.ids.get(subId);
|
|
188
|
+
}
|
|
189
|
+
if (!subContainer.ids.has(id)) {
|
|
190
|
+
listeners = new Set();
|
|
191
|
+
subContainer.ids.set(id, listeners);
|
|
192
|
+
native.addIdSubscription(server.dbCtxExternal, sub);
|
|
193
|
+
}
|
|
194
|
+
else {
|
|
195
|
+
listeners = subContainer.ids.get(id);
|
|
196
|
+
}
|
|
197
|
+
listeners.add(runQuery);
|
|
198
|
+
process.nextTick(() => {
|
|
199
|
+
runQuery();
|
|
200
|
+
});
|
|
201
|
+
return () => {
|
|
202
|
+
killed = true;
|
|
203
|
+
listeners.delete(runQuery);
|
|
204
|
+
if (listeners.size === 0) {
|
|
205
|
+
native.removeIdSubscription(server.dbCtxExternal, sub);
|
|
206
|
+
subContainer.ids.delete(id);
|
|
207
|
+
}
|
|
208
|
+
if (subContainer.ids.size === 0) {
|
|
209
|
+
if (subContainer.types) {
|
|
210
|
+
for (const typeId of subContainer.types) {
|
|
211
|
+
removeFromMultiSub(server, typeId, subContainer.typesListener);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
if (now) {
|
|
215
|
+
server.subscriptions.now.listeners.delete(subContainer.nowListener);
|
|
216
|
+
}
|
|
217
|
+
server.subscriptions.ids.delete(subId);
|
|
218
|
+
}
|
|
219
|
+
server.subscriptions.active--;
|
|
220
|
+
if (server.subscriptions.active === 0) {
|
|
221
|
+
clearTimeout(server.subscriptions.updateHandler);
|
|
222
|
+
server.subscriptions.updateHandler = null;
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
else if (sub[0] === SubscriptionType.fullType) {
|
|
227
|
+
const headerLen = 8;
|
|
228
|
+
const typeId = readUint16(sub, 1);
|
|
229
|
+
addToMultiSub(server, typeId, runQuery);
|
|
230
|
+
const typesLen = readUint16(sub, 3);
|
|
231
|
+
let types;
|
|
232
|
+
if (typesLen != 0) {
|
|
233
|
+
// double check if this is alignment correct with the byteOffset else copy
|
|
234
|
+
const byteOffset = sub.byteOffset + headerLen;
|
|
235
|
+
if (byteOffset % 2 === 0) {
|
|
236
|
+
types = new Uint16Array(sub.buffer, byteOffset, typesLen);
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
types = new Uint16Array(sub.slice(headerLen, headerLen + typesLen * 2));
|
|
240
|
+
}
|
|
241
|
+
for (const typeId of types) {
|
|
242
|
+
addToMultiSub(server, typeId, runQuery);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
if (readUint16(sub, 5) != 0) {
|
|
246
|
+
// have to make a copy (subArray is weak)
|
|
247
|
+
now = sub.slice(headerLen + typesLen * 2);
|
|
248
|
+
server.subscriptions.now.listeners.add(runQuery);
|
|
249
|
+
}
|
|
250
|
+
process.nextTick(() => {
|
|
251
|
+
runQuery();
|
|
252
|
+
});
|
|
253
|
+
return () => {
|
|
254
|
+
killed = true;
|
|
255
|
+
if (now) {
|
|
256
|
+
server.subscriptions.now.listeners.delete(runQuery);
|
|
257
|
+
}
|
|
258
|
+
removeFromMultiSub(server, typeId, runQuery);
|
|
259
|
+
if (types) {
|
|
260
|
+
for (const typeId of types) {
|
|
261
|
+
removeFromMultiSub(server, typeId, runQuery);
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
server.subscriptions.active--;
|
|
265
|
+
if (server.subscriptions.active === 0) {
|
|
266
|
+
clearTimeout(server.subscriptions.updateHandler);
|
|
267
|
+
server.subscriptions.updateHandler = null;
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
else {
|
|
272
|
+
throw new Error('Unhandled subscription!');
|
|
273
|
+
}
|
|
274
|
+
};
|
|
275
|
+
//# sourceMappingURL=subscription.js.map
|
package/dist/src/utils.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { inspect } from 'node:util';
|
|
2
|
-
import picocolors from 'picocolors';
|
|
1
|
+
import { inspect, styleText } from 'node:util';
|
|
3
2
|
export const debugMode = (target, getInfo = null) => {
|
|
4
3
|
const opts = { showHidden: false, depth: null, colors: true };
|
|
5
4
|
const info = (v) => (typeof v === 'object' ? inspect(v, opts) : v);
|
|
@@ -29,7 +28,7 @@ export const debugMode = (target, getInfo = null) => {
|
|
|
29
28
|
let cnt = 0;
|
|
30
29
|
const color = colors[colorKey++ % colors.length];
|
|
31
30
|
target[key] = function () {
|
|
32
|
-
const arr = [
|
|
31
|
+
const arr = [styleText(color, `[${key}:${++cnt}]`)];
|
|
33
32
|
const add = getInfo?.(key);
|
|
34
33
|
if (add)
|
|
35
34
|
arr.push(add);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/db",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "./dist/src/index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -42,11 +42,10 @@
|
|
|
42
42
|
],
|
|
43
43
|
"dependencies": {
|
|
44
44
|
"@based/hash": "1.1.0",
|
|
45
|
-
"@based/schema": "5.0
|
|
46
|
-
"@based/utils": "1.
|
|
47
|
-
"@based/protocol": "0.0
|
|
48
|
-
"exit-hook": "^4.0.0"
|
|
49
|
-
"picocolors": "^1.1.0"
|
|
45
|
+
"@based/schema": "5.1.0",
|
|
46
|
+
"@based/utils": "1.2.0",
|
|
47
|
+
"@based/protocol": "0.1.0",
|
|
48
|
+
"exit-hook": "^4.0.0"
|
|
50
49
|
},
|
|
51
50
|
"optionalDependencies": {
|
|
52
51
|
"@based/locale-x86-64-gnu": "*"
|