@based/db 0.0.1
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 +43 -0
- package/dist/lib/darwin_aarch64/include/cdefs.h +317 -0
- package/dist/lib/darwin_aarch64/include/libdeflate.h +322 -0
- package/dist/lib/darwin_aarch64/include/libdeflate_strings.h +35 -0
- package/dist/lib/darwin_aarch64/include/linker_set.h +109 -0
- package/dist/lib/darwin_aarch64/include/queue.h +627 -0
- package/dist/lib/darwin_aarch64/include/selva/_export.h +7 -0
- package/dist/lib/darwin_aarch64/include/selva/align.h +9 -0
- package/dist/lib/darwin_aarch64/include/selva/backoff_timeout.h +29 -0
- package/dist/lib/darwin_aarch64/include/selva/base64.h +59 -0
- package/dist/lib/darwin_aarch64/include/selva/base64url.h +59 -0
- package/dist/lib/darwin_aarch64/include/selva/bitmap.h +95 -0
- package/dist/lib/darwin_aarch64/include/selva/crc32c.h +17 -0
- package/dist/lib/darwin_aarch64/include/selva/ctime.h +135 -0
- package/dist/lib/darwin_aarch64/include/selva/db.h +284 -0
- package/dist/lib/darwin_aarch64/include/selva/endian.h +301 -0
- package/dist/lib/darwin_aarch64/include/selva/fast_linear_search.h +23 -0
- package/dist/lib/darwin_aarch64/include/selva/fast_memmem.h +11 -0
- package/dist/lib/darwin_aarch64/include/selva/fast_parsei.h +36 -0
- package/dist/lib/darwin_aarch64/include/selva/fields.h +338 -0
- package/dist/lib/darwin_aarch64/include/selva/find.h +47 -0
- package/dist/lib/darwin_aarch64/include/selva/lpf.h +28 -0
- package/dist/lib/darwin_aarch64/include/selva/node_id_set.h +43 -0
- package/dist/lib/darwin_aarch64/include/selva/poptop.h +114 -0
- package/dist/lib/darwin_aarch64/include/selva/queue_r.h +190 -0
- package/dist/lib/darwin_aarch64/include/selva/selva_hash128.h +39 -0
- package/dist/lib/darwin_aarch64/include/selva/selva_lang.h +105 -0
- package/dist/lib/darwin_aarch64/include/selva/selva_math.h +37 -0
- package/dist/lib/darwin_aarch64/include/selva/selva_string.h +672 -0
- package/dist/lib/darwin_aarch64/include/selva/sort.h +119 -0
- package/dist/lib/darwin_aarch64/include/selva/strsearch.h +43 -0
- package/dist/lib/darwin_aarch64/include/selva/timestamp.h +25 -0
- package/dist/lib/darwin_aarch64/include/selva/traverse.h +65 -0
- package/dist/lib/darwin_aarch64/include/selva/types.h +110 -0
- package/dist/lib/darwin_aarch64/include/selva/vector.h +14 -0
- package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +13 -0
- package/dist/lib/darwin_aarch64/include/selva_error.h +137 -0
- package/dist/lib/darwin_aarch64/include/selva_lang_code.h +157 -0
- package/dist/lib/darwin_aarch64/include/tree.h +852 -0
- 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-v20.11.1.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v22.8.0.node +0 -0
- package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
- package/dist/lib/darwin_aarch64/libxxhash.0.8.2.dylib +0 -0
- package/dist/lib/darwin_aarch64/libxxhash.0.dylib +0 -0
- package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
- package/dist/lib/linux_aarch64/include/cdefs.h +317 -0
- package/dist/lib/linux_aarch64/include/libdeflate.h +322 -0
- package/dist/lib/linux_aarch64/include/libdeflate_strings.h +35 -0
- package/dist/lib/linux_aarch64/include/linker_set.h +109 -0
- package/dist/lib/linux_aarch64/include/queue.h +627 -0
- package/dist/lib/linux_aarch64/include/selva/_export.h +7 -0
- package/dist/lib/linux_aarch64/include/selva/align.h +9 -0
- package/dist/lib/linux_aarch64/include/selva/backoff_timeout.h +29 -0
- package/dist/lib/linux_aarch64/include/selva/base64.h +59 -0
- package/dist/lib/linux_aarch64/include/selva/base64url.h +59 -0
- package/dist/lib/linux_aarch64/include/selva/bitmap.h +95 -0
- package/dist/lib/linux_aarch64/include/selva/crc32c.h +17 -0
- package/dist/lib/linux_aarch64/include/selva/ctime.h +135 -0
- package/dist/lib/linux_aarch64/include/selva/db.h +284 -0
- package/dist/lib/linux_aarch64/include/selva/endian.h +301 -0
- package/dist/lib/linux_aarch64/include/selva/fast_linear_search.h +23 -0
- package/dist/lib/linux_aarch64/include/selva/fast_memmem.h +11 -0
- package/dist/lib/linux_aarch64/include/selva/fast_parsei.h +36 -0
- package/dist/lib/linux_aarch64/include/selva/fields.h +338 -0
- package/dist/lib/linux_aarch64/include/selva/find.h +47 -0
- package/dist/lib/linux_aarch64/include/selva/lpf.h +28 -0
- package/dist/lib/linux_aarch64/include/selva/node_id_set.h +43 -0
- package/dist/lib/linux_aarch64/include/selva/poptop.h +114 -0
- package/dist/lib/linux_aarch64/include/selva/queue_r.h +190 -0
- package/dist/lib/linux_aarch64/include/selva/selva_hash128.h +39 -0
- package/dist/lib/linux_aarch64/include/selva/selva_lang.h +105 -0
- package/dist/lib/linux_aarch64/include/selva/selva_math.h +37 -0
- package/dist/lib/linux_aarch64/include/selva/selva_string.h +672 -0
- package/dist/lib/linux_aarch64/include/selva/sort.h +119 -0
- package/dist/lib/linux_aarch64/include/selva/strsearch.h +43 -0
- package/dist/lib/linux_aarch64/include/selva/timestamp.h +25 -0
- package/dist/lib/linux_aarch64/include/selva/traverse.h +65 -0
- package/dist/lib/linux_aarch64/include/selva/types.h +110 -0
- package/dist/lib/linux_aarch64/include/selva/vector.h +14 -0
- package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +13 -0
- package/dist/lib/linux_aarch64/include/selva_error.h +137 -0
- package/dist/lib/linux_aarch64/include/selva_lang_code.h +157 -0
- package/dist/lib/linux_aarch64/include/tree.h +852 -0
- package/dist/lib/linux_aarch64/libdeflate.so +0 -0
- package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
- package/dist/lib/linux_aarch64/libselva.so +0 -0
- package/dist/lib/linux_aarch64/libxxhash.so +0 -0
- package/dist/lib/linux_aarch64/libxxhash.so.0 +0 -0
- package/dist/lib/linux_aarch64/libxxhash.so.0.8.2 +0 -0
- package/dist/lib/linux_x86_64/include/cdefs.h +317 -0
- package/dist/lib/linux_x86_64/include/libdeflate.h +322 -0
- package/dist/lib/linux_x86_64/include/libdeflate_strings.h +35 -0
- package/dist/lib/linux_x86_64/include/linker_set.h +109 -0
- package/dist/lib/linux_x86_64/include/queue.h +627 -0
- package/dist/lib/linux_x86_64/include/selva/_export.h +7 -0
- package/dist/lib/linux_x86_64/include/selva/align.h +9 -0
- package/dist/lib/linux_x86_64/include/selva/backoff_timeout.h +29 -0
- package/dist/lib/linux_x86_64/include/selva/base64.h +59 -0
- package/dist/lib/linux_x86_64/include/selva/base64url.h +59 -0
- package/dist/lib/linux_x86_64/include/selva/bitmap.h +95 -0
- package/dist/lib/linux_x86_64/include/selva/crc32c.h +17 -0
- package/dist/lib/linux_x86_64/include/selva/ctime.h +135 -0
- package/dist/lib/linux_x86_64/include/selva/db.h +284 -0
- package/dist/lib/linux_x86_64/include/selva/endian.h +301 -0
- package/dist/lib/linux_x86_64/include/selva/fast_linear_search.h +23 -0
- package/dist/lib/linux_x86_64/include/selva/fast_memmem.h +11 -0
- package/dist/lib/linux_x86_64/include/selva/fast_parsei.h +36 -0
- package/dist/lib/linux_x86_64/include/selva/fields.h +338 -0
- package/dist/lib/linux_x86_64/include/selva/find.h +47 -0
- package/dist/lib/linux_x86_64/include/selva/lpf.h +28 -0
- package/dist/lib/linux_x86_64/include/selva/node_id_set.h +43 -0
- package/dist/lib/linux_x86_64/include/selva/poptop.h +114 -0
- package/dist/lib/linux_x86_64/include/selva/queue_r.h +190 -0
- package/dist/lib/linux_x86_64/include/selva/selva_hash128.h +39 -0
- package/dist/lib/linux_x86_64/include/selva/selva_lang.h +105 -0
- package/dist/lib/linux_x86_64/include/selva/selva_math.h +37 -0
- package/dist/lib/linux_x86_64/include/selva/selva_string.h +672 -0
- package/dist/lib/linux_x86_64/include/selva/sort.h +119 -0
- package/dist/lib/linux_x86_64/include/selva/strsearch.h +43 -0
- package/dist/lib/linux_x86_64/include/selva/timestamp.h +25 -0
- package/dist/lib/linux_x86_64/include/selva/traverse.h +65 -0
- package/dist/lib/linux_x86_64/include/selva/types.h +110 -0
- package/dist/lib/linux_x86_64/include/selva/vector.h +14 -0
- package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +13 -0
- package/dist/lib/linux_x86_64/include/selva_error.h +137 -0
- package/dist/lib/linux_x86_64/include/selva_lang_code.h +157 -0
- package/dist/lib/linux_x86_64/include/tree.h +852 -0
- package/dist/lib/linux_x86_64/libdeflate.so +0 -0
- package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
- package/dist/lib/linux_x86_64/libselva.so +0 -0
- package/dist/lib/linux_x86_64/libxxhash.so +0 -0
- package/dist/lib/linux_x86_64/libxxhash.so.0 +0 -0
- package/dist/lib/linux_x86_64/libxxhash.so.0.8.2 +0 -0
- package/dist/src/client/crc32.d.ts +1 -0
- package/dist/src/client/crc32.js +281 -0
- package/dist/src/client/index.d.ts +58 -0
- package/dist/src/client/index.js +120 -0
- package/dist/src/client/modify/ModifyRes.d.ts +26 -0
- package/dist/src/client/modify/ModifyRes.js +98 -0
- package/dist/src/client/modify/alias.d.ts +4 -0
- package/dist/src/client/modify/alias.js +47 -0
- package/dist/src/client/modify/binary.d.ts +6 -0
- package/dist/src/client/modify/binary.js +61 -0
- package/dist/src/client/modify/create.d.ts +4 -0
- package/dist/src/client/modify/create.js +108 -0
- package/dist/src/client/modify/fixed.d.ts +5 -0
- package/dist/src/client/modify/fixed.js +118 -0
- package/dist/src/client/modify/hll.d.ts +4 -0
- package/dist/src/client/modify/hll.js +58 -0
- package/dist/src/client/modify/index.d.ts +4 -0
- package/dist/src/client/modify/index.js +4 -0
- package/dist/src/client/modify/modify.d.ts +5 -0
- package/dist/src/client/modify/modify.js +126 -0
- package/dist/src/client/modify/references/edge.d.ts +6 -0
- package/dist/src/client/modify/references/edge.js +158 -0
- package/dist/src/client/modify/references/reference.d.ts +11 -0
- package/dist/src/client/modify/references/reference.js +92 -0
- package/dist/src/client/modify/references/references.d.ts +17 -0
- package/dist/src/client/modify/references/references.js +271 -0
- package/dist/src/client/modify/remove.d.ts +2 -0
- package/dist/src/client/modify/remove.js +36 -0
- package/dist/src/client/modify/setCursor.d.ts +4 -0
- package/dist/src/client/modify/setCursor.js +36 -0
- package/dist/src/client/modify/string.d.ts +5 -0
- package/dist/src/client/modify/string.js +56 -0
- package/dist/src/client/modify/text.d.ts +5 -0
- package/dist/src/client/modify/text.js +14 -0
- package/dist/src/client/modify/types.d.ts +14 -0
- package/dist/src/client/modify/types.js +12 -0
- package/dist/src/client/modify/update.d.ts +5 -0
- package/dist/src/client/modify/update.js +80 -0
- package/dist/src/client/modify/upsert.d.ts +2 -0
- package/dist/src/client/modify/upsert.js +40 -0
- package/dist/src/client/operations.d.ts +31 -0
- package/dist/src/client/operations.js +110 -0
- package/dist/src/client/query/BasedDbQuery.d.ts +40 -0
- package/dist/src/client/query/BasedDbQuery.js +235 -0
- package/dist/src/client/query/BasedIterable.d.ts +26 -0
- package/dist/src/client/query/BasedIterable.js +113 -0
- package/dist/src/client/query/debug.d.ts +3 -0
- package/dist/src/client/query/debug.js +117 -0
- package/dist/src/client/query/display.d.ts +5 -0
- package/dist/src/client/query/display.js +233 -0
- package/dist/src/client/query/filter/FilterBranch.d.ts +13 -0
- package/dist/src/client/query/filter/FilterBranch.js +29 -0
- package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +3 -0
- package/dist/src/client/query/filter/createFixedFilterBuffer.js +101 -0
- package/dist/src/client/query/filter/createReferenceFilter.d.ts +2 -0
- package/dist/src/client/query/filter/createReferenceFilter.js +24 -0
- package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -0
- package/dist/src/client/query/filter/createVariableFilterBuffer.js +81 -0
- package/dist/src/client/query/filter/filter.d.ts +10 -0
- package/dist/src/client/query/filter/filter.js +134 -0
- package/dist/src/client/query/filter/operators.d.ts +6 -0
- package/dist/src/client/query/filter/operators.js +98 -0
- package/dist/src/client/query/filter/parseFilterValue.d.ts +2 -0
- package/dist/src/client/query/filter/parseFilterValue.js +99 -0
- package/dist/src/client/query/filter/primitiveFilter.d.ts +4 -0
- package/dist/src/client/query/filter/primitiveFilter.js +47 -0
- package/dist/src/client/query/filter/toBuffer.d.ts +3 -0
- package/dist/src/client/query/filter/toBuffer.js +100 -0
- package/dist/src/client/query/filter/types.d.ts +6 -0
- package/dist/src/client/query/filter/types.js +7 -0
- package/dist/src/client/query/include/props.d.ts +6 -0
- package/dist/src/client/query/include/props.js +44 -0
- package/dist/src/client/query/include/toBuffer.d.ts +3 -0
- package/dist/src/client/query/include/toBuffer.js +82 -0
- package/dist/src/client/query/include/utils.d.ts +6 -0
- package/dist/src/client/query/include/utils.js +41 -0
- package/dist/src/client/query/include/walk.d.ts +3 -0
- package/dist/src/client/query/include/walk.js +70 -0
- package/dist/src/client/query/query.d.ts +10 -0
- package/dist/src/client/query/query.js +11 -0
- package/dist/src/client/query/queryDef.d.ts +3 -0
- package/dist/src/client/query/queryDef.js +54 -0
- package/dist/src/client/query/read/read.d.ts +8 -0
- package/dist/src/client/query/read/read.js +303 -0
- package/dist/src/client/query/registerQuery.d.ts +2 -0
- package/dist/src/client/query/registerQuery.js +31 -0
- package/dist/src/client/query/search/index.d.ts +6 -0
- package/dist/src/client/query/search/index.js +82 -0
- package/dist/src/client/query/sort.d.ts +3 -0
- package/dist/src/client/query/sort.js +18 -0
- package/dist/src/client/query/subscription/index.d.ts +5 -0
- package/dist/src/client/query/subscription/index.js +52 -0
- package/dist/src/client/query/subscription/markers.d.ts +10 -0
- package/dist/src/client/query/subscription/markers.js +213 -0
- package/dist/src/client/query/subscription/run.d.ts +5 -0
- package/dist/src/client/query/subscription/run.js +69 -0
- package/dist/src/client/query/subscription/types.d.ts +33 -0
- package/dist/src/client/query/subscription/types.js +3 -0
- package/dist/src/client/query/thresholds.d.ts +6 -0
- package/dist/src/client/query/thresholds.js +16 -0
- package/dist/src/client/query/toBuffer.d.ts +3 -0
- package/dist/src/client/query/toBuffer.js +229 -0
- package/dist/src/client/query/types.d.ts +88 -0
- package/dist/src/client/query/types.js +15 -0
- package/dist/src/client/query/validation.d.ts +15 -0
- package/dist/src/client/query/validation.js +59 -0
- package/dist/src/client/string.d.ts +5 -0
- package/dist/src/client/string.js +73 -0
- package/dist/src/client/tree.d.ts +1 -0
- package/dist/src/client/tree.js +5 -0
- package/dist/src/index.d.ts +31 -0
- package/dist/src/index.js +85 -0
- package/dist/src/native.d.ts +28 -0
- package/dist/src/native.js +96 -0
- package/dist/src/server/csmt/draw-dot.d.ts +2 -0
- package/dist/src/server/csmt/draw-dot.js +32 -0
- package/dist/src/server/csmt/index.d.ts +4 -0
- package/dist/src/server/csmt/index.js +5 -0
- package/dist/src/server/csmt/match.d.ts +7 -0
- package/dist/src/server/csmt/match.js +10 -0
- package/dist/src/server/csmt/memebership-proof.d.ts +7 -0
- package/dist/src/server/csmt/memebership-proof.js +122 -0
- package/dist/src/server/csmt/tree-utils.d.ts +6 -0
- package/dist/src/server/csmt/tree-utils.js +33 -0
- package/dist/src/server/csmt/tree.d.ts +2 -0
- package/dist/src/server/csmt/tree.js +209 -0
- package/dist/src/server/csmt/types.d.ts +40 -0
- package/dist/src/server/csmt/types.js +2 -0
- package/dist/src/server/index.d.ts +85 -0
- package/dist/src/server/index.js +407 -0
- package/dist/src/server/migrate/index.d.ts +7 -0
- package/dist/src/server/migrate/index.js +108 -0
- package/dist/src/server/migrate/worker.d.ts +1 -0
- package/dist/src/server/migrate/worker.js +81 -0
- package/dist/src/server/save.d.ts +2 -0
- package/dist/src/server/save.js +66 -0
- package/dist/src/server/schema/schema.d.ts +3 -0
- package/dist/src/server/schema/schema.js +4 -0
- package/dist/src/server/schema/selvaBuffer.d.ts +4 -0
- package/dist/src/server/schema/selvaBuffer.js +102 -0
- package/dist/src/server/schema/typeDef.d.ts +6 -0
- package/dist/src/server/schema/typeDef.js +315 -0
- package/dist/src/server/schema/types.d.ts +127 -0
- package/dist/src/server/schema/types.js +113 -0
- package/dist/src/server/schema/utils.d.ts +6 -0
- package/dist/src/server/schema/utils.js +21 -0
- package/dist/src/server/start.d.ts +6 -0
- package/dist/src/server/start.js +86 -0
- package/dist/src/server/tree.d.ts +13 -0
- package/dist/src/server/tree.js +33 -0
- package/dist/src/server/worker.d.ts +1 -0
- package/dist/src/server/worker.js +35 -0
- package/package.json +48 -0
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { includeFields } from '../query.js';
|
|
2
|
+
import { registerQuery } from '../registerQuery.js';
|
|
3
|
+
import { runSubscription } from './run.js';
|
|
4
|
+
import { addSubscriptionMarkers, removeSubscriptionMarkers } from './markers.js';
|
|
5
|
+
export * from './types.js';
|
|
6
|
+
export * from './markers.js';
|
|
7
|
+
export const subscribe = (q, onData, onError) => {
|
|
8
|
+
let closed = false;
|
|
9
|
+
if (!q.def.include.stringFields.size && !q.def.references.size) {
|
|
10
|
+
includeFields(q.def, ['*']);
|
|
11
|
+
}
|
|
12
|
+
registerQuery(q);
|
|
13
|
+
if (!q.db.subscriptions.has(q.id)) {
|
|
14
|
+
const subscription = {
|
|
15
|
+
query: q,
|
|
16
|
+
subs: new Set(),
|
|
17
|
+
inProgress: false,
|
|
18
|
+
closed: false,
|
|
19
|
+
};
|
|
20
|
+
q.db.subscriptions.set(q.id, subscription);
|
|
21
|
+
addSubscriptionMarkers(q, subscription);
|
|
22
|
+
}
|
|
23
|
+
const fn = (res, err) => {
|
|
24
|
+
if (!closed) {
|
|
25
|
+
if (err) {
|
|
26
|
+
onError(err);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
onData(res);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
const sub = q.db.subscriptions.get(q.id);
|
|
34
|
+
const close = () => {
|
|
35
|
+
sub.subs.delete(fn);
|
|
36
|
+
if (sub.subs.size === 0) {
|
|
37
|
+
q.db.subscriptions.delete(q.id);
|
|
38
|
+
removeSubscriptionMarkers(q);
|
|
39
|
+
}
|
|
40
|
+
closed = true;
|
|
41
|
+
return q;
|
|
42
|
+
};
|
|
43
|
+
sub.subs.add(fn);
|
|
44
|
+
if (!sub.inProgress) {
|
|
45
|
+
runSubscription(sub);
|
|
46
|
+
}
|
|
47
|
+
else if (sub.res) {
|
|
48
|
+
onData(sub.res);
|
|
49
|
+
}
|
|
50
|
+
return close;
|
|
51
|
+
};
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { PropDef, PropDefEdge } from '../../../server/schema/types.js';
|
|
2
|
+
import { Subscription, SubscriptionMarkerMap, SubscriptionMarkersCheck } from './types.js';
|
|
3
|
+
import { BasedDbQuery } from '../BasedDbQuery.js';
|
|
4
|
+
import { DbClient } from '../../index.js';
|
|
5
|
+
export declare const getSubscriptionMarkers: (db: DbClient, typeId: number, id: number, isCreate: boolean) => SubscriptionMarkersCheck | false;
|
|
6
|
+
export declare const checkSubscriptionMarkers: (db: DbClient, m: SubscriptionMarkersCheck, prop: PropDef | PropDefEdge) => void;
|
|
7
|
+
export declare const createSubscriptionMarkerMap: () => SubscriptionMarkerMap;
|
|
8
|
+
export declare const addSubscriptionMarkers: (q: BasedDbQuery, subscription: Subscription) => void;
|
|
9
|
+
export declare const resetSubscriptionMarkers: (db: DbClient) => void;
|
|
10
|
+
export declare const removeSubscriptionMarkers: (q: BasedDbQuery) => void;
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { startSubscription } from './run.js';
|
|
2
|
+
export const getSubscriptionMarkers = (db, typeId, id, isCreate) => {
|
|
3
|
+
if (!(typeId in db.subscriptionMarkers)) {
|
|
4
|
+
return false;
|
|
5
|
+
}
|
|
6
|
+
const t = db.subscriptionMarkers[typeId];
|
|
7
|
+
let subMarkersCheck = false;
|
|
8
|
+
if (!isCreate) {
|
|
9
|
+
if (t.ids.has(id)) {
|
|
10
|
+
const idMarkers = t.ids.get(id);
|
|
11
|
+
subMarkersCheck = { ids: idMarkers, collection: false };
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
if (t.collection) {
|
|
15
|
+
if (!subMarkersCheck) {
|
|
16
|
+
subMarkersCheck = { ids: false, collection: t.collection };
|
|
17
|
+
}
|
|
18
|
+
else {
|
|
19
|
+
subMarkersCheck.collection = t.collection;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return subMarkersCheck;
|
|
23
|
+
};
|
|
24
|
+
export const checkSubscriptionMarkers = (db, m, prop) => {
|
|
25
|
+
let newSub = false;
|
|
26
|
+
if (m.ids) {
|
|
27
|
+
const markers = m.ids;
|
|
28
|
+
if (prop.separate) {
|
|
29
|
+
const propSubs = markers.props[prop.prop];
|
|
30
|
+
if (propSubs) {
|
|
31
|
+
for (const s of propSubs) {
|
|
32
|
+
if (!s.inProgress) {
|
|
33
|
+
newSub = true;
|
|
34
|
+
db.subscriptionsToRun.push(s);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
else {
|
|
40
|
+
const propSubs = markers.main[prop.start];
|
|
41
|
+
if (propSubs) {
|
|
42
|
+
for (const s of propSubs) {
|
|
43
|
+
if (!s.inProgress) {
|
|
44
|
+
newSub = true;
|
|
45
|
+
db.subscriptionsToRun.push(s);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (m.collection) {
|
|
52
|
+
const markers = m.collection;
|
|
53
|
+
if (prop.separate) {
|
|
54
|
+
const propSubs = markers.props[prop.prop];
|
|
55
|
+
// @ts-ignore
|
|
56
|
+
if (propSubs && !propSubs.__handled) {
|
|
57
|
+
// @ts-ignore
|
|
58
|
+
propSubs.__handled = true;
|
|
59
|
+
for (const s of propSubs) {
|
|
60
|
+
if (!s.inProgress) {
|
|
61
|
+
newSub = true;
|
|
62
|
+
db.subscriptionsToRun.push(s);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
const propSubs = markers.main[prop.start];
|
|
69
|
+
// @ts-ignore
|
|
70
|
+
if (propSubs && !propSubs.__handled) {
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
propSubs.__handled = true;
|
|
73
|
+
for (const s of propSubs) {
|
|
74
|
+
if (!s.inProgress) {
|
|
75
|
+
newSub = true;
|
|
76
|
+
db.subscriptionsToRun.push(s);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (newSub && !db.subscriptionsInProgress) {
|
|
83
|
+
startSubscription(db);
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
export const createSubscriptionMarkerMap = () => {
|
|
87
|
+
return {};
|
|
88
|
+
};
|
|
89
|
+
const getFilterFields = (filter, results = {
|
|
90
|
+
main: new Set(),
|
|
91
|
+
props: new Set(),
|
|
92
|
+
}) => {
|
|
93
|
+
const conditions = filter.conditions;
|
|
94
|
+
if (conditions) {
|
|
95
|
+
conditions.forEach((v, k) => {
|
|
96
|
+
if (k === 0) {
|
|
97
|
+
for (const buf of v) {
|
|
98
|
+
results.main.add(buf.readUint16LE(4));
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
results.props.add(k);
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
if (filter.or) {
|
|
107
|
+
getFilterFields(filter.or, results);
|
|
108
|
+
}
|
|
109
|
+
return results;
|
|
110
|
+
};
|
|
111
|
+
// resetting subs is a copy for now
|
|
112
|
+
export const addSubscriptionMarkers = (q, subscription) => {
|
|
113
|
+
const typeId = q.def.schema.id;
|
|
114
|
+
if (!q.db.subscriptionMarkers[typeId]) {
|
|
115
|
+
q.db.subscriptionMarkers[typeId] = {
|
|
116
|
+
ids: new Map(),
|
|
117
|
+
collection: {
|
|
118
|
+
main: {},
|
|
119
|
+
props: {},
|
|
120
|
+
},
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
const markerType = q.db.subscriptionMarkers[typeId];
|
|
124
|
+
if ('id' in q.def.target) {
|
|
125
|
+
const id = q.def.target.id;
|
|
126
|
+
if (!markerType.ids.has(id)) {
|
|
127
|
+
markerType.ids.set(id, {
|
|
128
|
+
main: {},
|
|
129
|
+
props: {},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
const marker = markerType.ids.get(id);
|
|
133
|
+
const props = q.def.include.props;
|
|
134
|
+
const main = q.def.include.main;
|
|
135
|
+
for (const p of props) {
|
|
136
|
+
if (!(p in marker.props)) {
|
|
137
|
+
marker.props[p] = [];
|
|
138
|
+
}
|
|
139
|
+
const markerProps = marker.props[p];
|
|
140
|
+
markerProps.push(subscription);
|
|
141
|
+
}
|
|
142
|
+
for (const p in main.include) {
|
|
143
|
+
if (!(p in marker.main)) {
|
|
144
|
+
marker.main[p] = [];
|
|
145
|
+
}
|
|
146
|
+
const markerProps = marker.main[p];
|
|
147
|
+
markerProps.push(subscription);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else if ('alias' in q.def.target) {
|
|
151
|
+
// later
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
const props = q.def.include.props;
|
|
155
|
+
const main = q.def.include.main;
|
|
156
|
+
const marker = markerType.collection;
|
|
157
|
+
for (const p of props) {
|
|
158
|
+
if (!(p in marker.props)) {
|
|
159
|
+
marker.props[p] = [];
|
|
160
|
+
}
|
|
161
|
+
marker.props[p].push(subscription);
|
|
162
|
+
}
|
|
163
|
+
for (const p in main.include) {
|
|
164
|
+
if (!(p in marker.main)) {
|
|
165
|
+
marker.main[p] = [];
|
|
166
|
+
}
|
|
167
|
+
marker.main[p].push(subscription);
|
|
168
|
+
}
|
|
169
|
+
if (q.def.filter) {
|
|
170
|
+
const r = getFilterFields(q.def.filter);
|
|
171
|
+
r.main.forEach((k) => {
|
|
172
|
+
const p = String(k);
|
|
173
|
+
if (!(p in marker.main)) {
|
|
174
|
+
marker.main[p] = [];
|
|
175
|
+
// @ts-ignore
|
|
176
|
+
marker.main[p].__handled = false;
|
|
177
|
+
}
|
|
178
|
+
if (!main.include[p]) {
|
|
179
|
+
marker.main[p].push(subscription);
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
r.props.forEach((k) => {
|
|
183
|
+
const p = String(k);
|
|
184
|
+
if (!(p in marker.props)) {
|
|
185
|
+
marker.props[p] = [];
|
|
186
|
+
// @ts-ignore
|
|
187
|
+
marker.props[p].__handled = false;
|
|
188
|
+
}
|
|
189
|
+
if (!props.has(k)) {
|
|
190
|
+
marker.props[p].push(subscription);
|
|
191
|
+
}
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
export const resetSubscriptionMarkers = (db) => {
|
|
197
|
+
for (const typeId in db.subscriptionMarkers) {
|
|
198
|
+
const t = db.subscriptionMarkers[typeId];
|
|
199
|
+
for (const k in t.collection.main) {
|
|
200
|
+
// @ts-ignore
|
|
201
|
+
t.collection.main[k].__handled = false;
|
|
202
|
+
}
|
|
203
|
+
for (const k in t.collection.props) {
|
|
204
|
+
// @ts-ignore
|
|
205
|
+
t.collection.props[k].__handled = false;
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
export const removeSubscriptionMarkers = (q) => {
|
|
210
|
+
// derp
|
|
211
|
+
console.log('remove markers!');
|
|
212
|
+
};
|
|
213
|
+
//# sourceMappingURL=markers.js.map
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { Subscription } from './types.js';
|
|
2
|
+
import { DbClient } from '../../index.js';
|
|
3
|
+
export declare const resultsAreEqual: (a: Buffer, b: Buffer) => boolean;
|
|
4
|
+
export declare const runSubscription: (subscription: Subscription) => void;
|
|
5
|
+
export declare const startSubscription: (db: DbClient) => void;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { BasedQueryResponse } from '../BasedIterable.js';
|
|
2
|
+
import { resetSubscriptionMarkers } from './markers.js';
|
|
3
|
+
export const resultsAreEqual = (a, b) => {
|
|
4
|
+
const aLen = a.byteLength;
|
|
5
|
+
const bLen = b.byteLength;
|
|
6
|
+
if (aLen != bLen) {
|
|
7
|
+
return false;
|
|
8
|
+
}
|
|
9
|
+
if (a[aLen - 4] != b[bLen - 4])
|
|
10
|
+
return false;
|
|
11
|
+
if (a[aLen - 3] != b[bLen - 3])
|
|
12
|
+
return false;
|
|
13
|
+
if (a[aLen - 2] != b[bLen - 2])
|
|
14
|
+
return false;
|
|
15
|
+
if (a[aLen - 1] != b[bLen - 1])
|
|
16
|
+
return false;
|
|
17
|
+
return true;
|
|
18
|
+
};
|
|
19
|
+
export const runSubscription = (subscription) => {
|
|
20
|
+
if (!subscription.inProgress) {
|
|
21
|
+
subscription.inProgress = true;
|
|
22
|
+
const q = subscription.query;
|
|
23
|
+
const buf = q.buffer;
|
|
24
|
+
const d = performance.now();
|
|
25
|
+
q.db.hooks
|
|
26
|
+
.getQueryBuf(buf)
|
|
27
|
+
.then((res) => {
|
|
28
|
+
if (subscription.closed) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
subscription.inProgress = false;
|
|
32
|
+
const buf = Buffer.from(res);
|
|
33
|
+
if (subscription.res) {
|
|
34
|
+
if (resultsAreEqual(subscription.res.result, buf)) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
subscription.res.execTime = performance.now() - d;
|
|
38
|
+
subscription.res.result = buf;
|
|
39
|
+
subscription.res.end = buf.byteLength;
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
subscription.res = new BasedQueryResponse(q.id, q.def, buf, performance.now() - d);
|
|
43
|
+
}
|
|
44
|
+
subscription.subs.forEach((fn) => {
|
|
45
|
+
fn(subscription.res);
|
|
46
|
+
});
|
|
47
|
+
})
|
|
48
|
+
.catch((err) => {
|
|
49
|
+
subscription.inProgress = false;
|
|
50
|
+
console.error('Subscription getQuery errors', err);
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
export const startSubscription = (db) => {
|
|
55
|
+
console.log('start subscription');
|
|
56
|
+
if (!db.subscriptionsInProgress) {
|
|
57
|
+
db.subscriptionsInProgress = true;
|
|
58
|
+
setTimeout(() => {
|
|
59
|
+
console.log('run it');
|
|
60
|
+
db.subscriptionsToRun.forEach((s) => {
|
|
61
|
+
runSubscription(s);
|
|
62
|
+
});
|
|
63
|
+
db.subscriptionsToRun = [];
|
|
64
|
+
resetSubscriptionMarkers(db);
|
|
65
|
+
db.subscriptionsInProgress = false;
|
|
66
|
+
}, db.subscriptonThrottleMs);
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
//# sourceMappingURL=run.js.map
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { BasedQueryResponse } from '../BasedIterable.js';
|
|
2
|
+
import { BasedDbQuery } from '../BasedDbQuery.js';
|
|
3
|
+
export type OnData = (res: BasedQueryResponse) => any;
|
|
4
|
+
export type OnError = (err: Error) => any;
|
|
5
|
+
export type OnClose = () => BasedDbQuery;
|
|
6
|
+
export type OnSubscription = (res: any, err?: Error) => void;
|
|
7
|
+
export type Subscription = {
|
|
8
|
+
query: BasedDbQuery;
|
|
9
|
+
subs: Set<OnSubscription>;
|
|
10
|
+
res?: BasedQueryResponse;
|
|
11
|
+
closed: boolean;
|
|
12
|
+
inProgress: boolean;
|
|
13
|
+
};
|
|
14
|
+
export type SubscriptionsMap = Map<number, Subscription>;
|
|
15
|
+
export type SubscriptionsToRun = Subscription[];
|
|
16
|
+
export type SubscriptionMarkers = {
|
|
17
|
+
main: {
|
|
18
|
+
[start: string]: Subscription[];
|
|
19
|
+
};
|
|
20
|
+
props: {
|
|
21
|
+
[prop: string]: Subscription[];
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
export type SubscriptionMarkerMap = {
|
|
25
|
+
[typeID: string]: {
|
|
26
|
+
ids: Map<number, SubscriptionMarkers>;
|
|
27
|
+
collection: SubscriptionMarkers;
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
export type SubscriptionMarkersCheck = {
|
|
31
|
+
collection: SubscriptionMarkers | false;
|
|
32
|
+
ids: SubscriptionMarkers | false;
|
|
33
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export declare const MAX_IDS_PER_QUERY = 1000000;
|
|
2
|
+
export declare const MAX_BUFFER_SIZE: number;
|
|
3
|
+
export declare const MAX_RANGE_PROP_LIMIT = 1000;
|
|
4
|
+
export declare const MAX_RANGE_REF_LIMIT = 10000;
|
|
5
|
+
export declare const MIN_ID_VALUE = 1;
|
|
6
|
+
export declare const MAX_ID_VALUE: number;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Query thresholds
|
|
2
|
+
export const MAX_IDS_PER_QUERY = 1e6; // Max number of IDs that can be queried at once
|
|
3
|
+
export const MAX_BUFFER_SIZE = 5 * 1024 * 1024; // 5MB (max buffer size)
|
|
4
|
+
export const MAX_RANGE_PROP_LIMIT = 1e3;
|
|
5
|
+
export const MAX_RANGE_REF_LIMIT = 1e4;
|
|
6
|
+
// Data validation thresholds
|
|
7
|
+
export const MIN_ID_VALUE = 1; // Minimum allowed value for an ID (id array stared)
|
|
8
|
+
export const MAX_ID_VALUE = 2 ** 32 - 1; /// Maximum allowed value for an ID (Uint32)
|
|
9
|
+
// Performance-related thresholds
|
|
10
|
+
// <Just making room to further threshoulds e.g. MAX_MEMORY_BLABLABLA>
|
|
11
|
+
// Network-related thresholds
|
|
12
|
+
// <Just making room to further threshoulds e.g. MAX_NETWORK_RETRIES, NETWORK_TIMEOUT, etc.>
|
|
13
|
+
// Security-related thresholds
|
|
14
|
+
// <Just making room to further threshoulds that could make sense such. e.g.
|
|
15
|
+
// MAX_INPUT_LENGTH (strings/json to prevent DDoS attacks) or MAX_API_DUMP_LENGTH to avoid undesired dumps>
|
|
16
|
+
//# sourceMappingURL=thresholds.js.map
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { createSortBuffer } from './sort.js';
|
|
2
|
+
import { QueryDefType } from './types.js';
|
|
3
|
+
import { includeToBuffer } from './include/toBuffer.js';
|
|
4
|
+
import { filterToBuffer } from './query.js';
|
|
5
|
+
import { searchToBuffer } from './search/index.js';
|
|
6
|
+
import { ALIAS } from '../../server/schema/types.js';
|
|
7
|
+
const byteSize = (arr) => {
|
|
8
|
+
return arr.reduce((a, b) => {
|
|
9
|
+
return a + b.byteLength;
|
|
10
|
+
}, 0);
|
|
11
|
+
};
|
|
12
|
+
const getAliasPropdef = (alias, path, def) => {
|
|
13
|
+
const schema = def.schema;
|
|
14
|
+
for (const k in alias) {
|
|
15
|
+
if (typeof alias[k] === 'string') {
|
|
16
|
+
const p = path.join('.') + k;
|
|
17
|
+
const prop = schema.props[p];
|
|
18
|
+
if (prop.typeIndex === ALIAS) {
|
|
19
|
+
return { def: prop, value: alias[k] };
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
else if (typeof alias[k] === 'object') {
|
|
23
|
+
const propDef = getAliasPropdef(alias[k], [...path, k], def);
|
|
24
|
+
if (propDef) {
|
|
25
|
+
return propDef;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
export function defToBuffer(db, def) {
|
|
31
|
+
const result = [];
|
|
32
|
+
const include = includeToBuffer(db, def);
|
|
33
|
+
def.references.forEach((ref) => {
|
|
34
|
+
include.push(...defToBuffer(db, ref));
|
|
35
|
+
});
|
|
36
|
+
let edges;
|
|
37
|
+
let edgesSize = 0;
|
|
38
|
+
if (def.edges) {
|
|
39
|
+
edges = includeToBuffer(db, def.edges);
|
|
40
|
+
def.edges.references.forEach((ref) => {
|
|
41
|
+
edges.push(...defToBuffer(db, ref));
|
|
42
|
+
});
|
|
43
|
+
edgesSize = byteSize(edges);
|
|
44
|
+
}
|
|
45
|
+
const size = (edges ? edgesSize + 3 : 0) + byteSize(include);
|
|
46
|
+
if (def.type === QueryDefType.Root) {
|
|
47
|
+
let filter;
|
|
48
|
+
let filterSize = 0;
|
|
49
|
+
let search;
|
|
50
|
+
let searchSize = 0;
|
|
51
|
+
if (def.search) {
|
|
52
|
+
search = searchToBuffer(def.search);
|
|
53
|
+
searchSize = def.search.size;
|
|
54
|
+
}
|
|
55
|
+
if (def.filter.size) {
|
|
56
|
+
filter = filterToBuffer(def.filter);
|
|
57
|
+
filterSize = filter.byteLength;
|
|
58
|
+
}
|
|
59
|
+
// [type,type]
|
|
60
|
+
// [q type] 0 == id, 1 === ids, 2 === type only
|
|
61
|
+
if (def.target.alias) {
|
|
62
|
+
// put this somehwere else at some point
|
|
63
|
+
try {
|
|
64
|
+
const alias = getAliasPropdef(def.target.alias, [], def);
|
|
65
|
+
if (!alias) {
|
|
66
|
+
throw new Error(`Cannot find valid alias type in "${JSON.stringify(def.target.alias)}"`);
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
const s = Buffer.byteLength(alias.value);
|
|
70
|
+
// filter is nice for things like access
|
|
71
|
+
const buf = Buffer.allocUnsafe(8 + filterSize + s);
|
|
72
|
+
buf[0] = 3;
|
|
73
|
+
buf[1] = def.schema.idUint8[0];
|
|
74
|
+
buf[2] = def.schema.idUint8[1];
|
|
75
|
+
buf[3] = alias.def.prop;
|
|
76
|
+
buf.writeUint16LE(s, 4);
|
|
77
|
+
buf.write(alias.value, 6);
|
|
78
|
+
buf.writeUint16LE(filterSize, s + 6);
|
|
79
|
+
if (filterSize) {
|
|
80
|
+
buf.set(filter, 8 + s);
|
|
81
|
+
}
|
|
82
|
+
result.push(buf);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
catch (err) {
|
|
86
|
+
throw new Error(`Cannot parse alias object query for type ${def.schema.type}`);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
else if (def.target.id) {
|
|
90
|
+
// type 0
|
|
91
|
+
// 0: 4 [id]
|
|
92
|
+
// 0: 2 [filterSize]
|
|
93
|
+
const buf = Buffer.allocUnsafe(9 + filterSize);
|
|
94
|
+
buf[0] = 0;
|
|
95
|
+
buf[1] = def.schema.idUint8[0];
|
|
96
|
+
buf[2] = def.schema.idUint8[1];
|
|
97
|
+
buf.writeUInt32LE(def.target.id, 3);
|
|
98
|
+
buf.writeUint16LE(filterSize, 7);
|
|
99
|
+
if (filterSize) {
|
|
100
|
+
buf.set(filter, 9);
|
|
101
|
+
}
|
|
102
|
+
result.push(buf);
|
|
103
|
+
}
|
|
104
|
+
else {
|
|
105
|
+
let sort;
|
|
106
|
+
let sortSize = 0;
|
|
107
|
+
if (def.sort) {
|
|
108
|
+
sort = createSortBuffer(def.sort);
|
|
109
|
+
sortSize = sort.byteLength;
|
|
110
|
+
}
|
|
111
|
+
if (def.target.ids) {
|
|
112
|
+
// type 1
|
|
113
|
+
// 1: 4 + ids * 4 [ids len] [id,id,id]
|
|
114
|
+
// 1: 8 [offset, limit]
|
|
115
|
+
// 1: 2 [filter size]
|
|
116
|
+
// ?filter
|
|
117
|
+
// 1: 2 [sort size]
|
|
118
|
+
// ?sort
|
|
119
|
+
if (!sortSize &&
|
|
120
|
+
(def.range.offset || def.range.limit < def.target.ids.length)) {
|
|
121
|
+
def.target.ids = def.target.ids.slice(def.range.offset, def.range.offset + def.range.limit);
|
|
122
|
+
}
|
|
123
|
+
const idsSize = def.target.ids.length * 4;
|
|
124
|
+
const buf = Buffer.allocUnsafe(21 + idsSize + filterSize + sortSize + searchSize);
|
|
125
|
+
buf[0] = 1;
|
|
126
|
+
buf[1] = def.schema.idUint8[0];
|
|
127
|
+
buf[2] = def.schema.idUint8[1];
|
|
128
|
+
buf.writeUint32LE(idsSize, 3);
|
|
129
|
+
buf.set(new Uint8Array(def.target.ids.buffer), 7);
|
|
130
|
+
buf.writeUint32LE(def.range.offset, idsSize + 7);
|
|
131
|
+
buf.writeUint32LE(def.range.limit, idsSize + 11);
|
|
132
|
+
buf.writeUint16LE(filterSize, idsSize + 15);
|
|
133
|
+
if (filterSize) {
|
|
134
|
+
buf.set(filter, idsSize + 17);
|
|
135
|
+
}
|
|
136
|
+
buf.writeUint16LE(sortSize, 17 + filterSize + idsSize);
|
|
137
|
+
if (sortSize) {
|
|
138
|
+
buf.set(sort, 19 + filterSize + idsSize);
|
|
139
|
+
}
|
|
140
|
+
buf.writeUint16LE(searchSize, 19 + filterSize + idsSize + sortSize);
|
|
141
|
+
if (searchSize) {
|
|
142
|
+
buf.set(search, 21 + filterSize + idsSize + sortSize);
|
|
143
|
+
}
|
|
144
|
+
// ----------
|
|
145
|
+
result.push(buf);
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
// type 2
|
|
149
|
+
// 2: 0
|
|
150
|
+
// 2: 8 [offset, limit]
|
|
151
|
+
// 2: 2 [filter size]
|
|
152
|
+
// ?filter
|
|
153
|
+
// 2: 2 [sort size]
|
|
154
|
+
// ?sort
|
|
155
|
+
const buf = Buffer.allocUnsafe(17 + filterSize + sortSize + searchSize);
|
|
156
|
+
buf[0] = 2;
|
|
157
|
+
buf[1] = def.schema.idUint8[0];
|
|
158
|
+
buf[2] = def.schema.idUint8[1];
|
|
159
|
+
buf.writeUint32LE(def.range.offset, 3);
|
|
160
|
+
buf.writeUint32LE(def.range.limit, 7);
|
|
161
|
+
buf.writeUint16LE(filterSize, 11);
|
|
162
|
+
if (filterSize) {
|
|
163
|
+
buf.set(filter, 13);
|
|
164
|
+
}
|
|
165
|
+
buf.writeUint16LE(sortSize, 13 + filterSize);
|
|
166
|
+
if (sortSize) {
|
|
167
|
+
buf.set(sort, 15 + filterSize);
|
|
168
|
+
}
|
|
169
|
+
buf.writeUint16LE(searchSize, 15 + filterSize + sortSize);
|
|
170
|
+
if (searchSize) {
|
|
171
|
+
buf.set(search, 17 + filterSize + sortSize);
|
|
172
|
+
}
|
|
173
|
+
result.push(buf);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
else if (def.type === QueryDefType.References) {
|
|
178
|
+
// TODO filter edge
|
|
179
|
+
let filter;
|
|
180
|
+
if (def.filter.size) {
|
|
181
|
+
filter = filterToBuffer(def.filter);
|
|
182
|
+
}
|
|
183
|
+
let sort;
|
|
184
|
+
if (def.sort) {
|
|
185
|
+
sort = createSortBuffer(def.sort);
|
|
186
|
+
}
|
|
187
|
+
// ADD IT
|
|
188
|
+
// TODO: ADD RANGE [offset,limit] (8 bytes)
|
|
189
|
+
const sortSize = sort?.byteLength ?? 0;
|
|
190
|
+
const filterSize = filter?.byteLength ?? 0;
|
|
191
|
+
const modsSize = filterSize + sortSize;
|
|
192
|
+
const meta = Buffer.allocUnsafe(modsSize + 10 + 8);
|
|
193
|
+
meta[0] = 254;
|
|
194
|
+
meta.writeUint16LE(size + 7 + modsSize + 8, 1);
|
|
195
|
+
meta.writeUint16LE(filterSize, 3);
|
|
196
|
+
meta.writeUint16LE(sortSize, 5);
|
|
197
|
+
meta.writeUint32LE(def.range.offset, 7);
|
|
198
|
+
meta.writeUint32LE(def.range.limit, 7 + 4);
|
|
199
|
+
if (filter) {
|
|
200
|
+
meta.set(filter, 15);
|
|
201
|
+
}
|
|
202
|
+
if (sort) {
|
|
203
|
+
meta.set(sort, 15 + filterSize);
|
|
204
|
+
}
|
|
205
|
+
meta[15 + modsSize] = def.schema.idUint8[0];
|
|
206
|
+
meta[15 + 1 + modsSize] = def.schema.idUint8[1];
|
|
207
|
+
meta[15 + 2 + modsSize] = def.target.propDef.prop;
|
|
208
|
+
result.push(meta);
|
|
209
|
+
}
|
|
210
|
+
else if (def.type === QueryDefType.Reference) {
|
|
211
|
+
const meta = Buffer.allocUnsafe(6);
|
|
212
|
+
meta[0] = 255;
|
|
213
|
+
meta.writeUint16LE(size + 3, 1);
|
|
214
|
+
meta[3] = def.schema.idUint8[0];
|
|
215
|
+
meta[4] = def.schema.idUint8[1];
|
|
216
|
+
meta[5] = def.target.propDef.prop;
|
|
217
|
+
result.push(meta);
|
|
218
|
+
}
|
|
219
|
+
result.push(...include);
|
|
220
|
+
if (edges) {
|
|
221
|
+
const metaEdgeBuffer = Buffer.allocUnsafe(3);
|
|
222
|
+
metaEdgeBuffer[0] = 252;
|
|
223
|
+
metaEdgeBuffer.writeUint16LE(edgesSize, 1);
|
|
224
|
+
result.push(metaEdgeBuffer, ...edges);
|
|
225
|
+
}
|
|
226
|
+
// 8 byte crc 32
|
|
227
|
+
return result;
|
|
228
|
+
}
|
|
229
|
+
//# sourceMappingURL=toBuffer.js.map
|