@based/db 0.0.21 → 0.0.23
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 +10 -10
- package/dist/lib/darwin_aarch64/include/selva/types.h +9 -18
- package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v21.node +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/libselva.dylib +0 -0
- package/dist/lib/linux_aarch64/include/selva/types.h +9 -18
- package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v21.node +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/libselva.so +0 -0
- package/dist/lib/linux_x86_64/include/selva/types.h +9 -18
- package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v21.node +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/libselva.so +0 -0
- package/dist/src/client/bitWise.d.ts +0 -1
- package/dist/src/client/bitWise.js +0 -10
- package/dist/src/client/{operations.d.ts → flushModify.d.ts} +4 -4
- package/dist/src/client/{operations.js → flushModify.js} +39 -15
- package/dist/src/client/index.d.ts +10 -6
- package/dist/src/client/index.js +12 -4
- package/dist/src/client/modify/ModifyRes.d.ts +2 -8
- package/dist/src/client/modify/ModifyRes.js +23 -29
- package/dist/src/client/modify/alias.d.ts +1 -1
- package/dist/src/client/modify/alias.js +5 -2
- package/dist/src/client/modify/binary.d.ts +1 -1
- package/dist/src/client/modify/binary.js +1 -1
- package/dist/src/client/modify/cardinality.d.ts +2 -2
- package/dist/src/client/modify/cardinality.js +7 -5
- package/dist/src/client/modify/create.js +10 -5
- package/dist/src/client/modify/delete.d.ts +3 -1
- package/dist/src/client/modify/delete.js +12 -5
- package/dist/src/client/modify/expire.js +1 -1
- package/dist/src/client/modify/fixed.js +11 -4
- package/dist/src/client/modify/references/appendRefs.d.ts +4 -0
- package/dist/src/client/modify/references/appendRefs.js +27 -0
- package/dist/src/client/modify/references/edge.d.ts +0 -1
- package/dist/src/client/modify/references/edge.js +191 -71
- package/dist/src/client/modify/references/getEdgeSize.d.ts +3 -0
- package/dist/src/client/modify/references/getEdgeSize.js +27 -0
- package/dist/src/client/modify/references/reference.js +40 -26
- package/dist/src/client/modify/references/references.js +18 -6
- package/dist/src/client/modify/string.js +2 -1
- package/dist/src/client/modify/types.d.ts +1 -0
- package/dist/src/client/modify/types.js +1 -0
- package/dist/src/client/modify/update.js +3 -4
- package/dist/src/client/modify/upsert.js +1 -0
- package/dist/src/client/modify/vector.js +0 -2
- package/dist/src/client/query/BasedDbQuery.d.ts +4 -3
- package/dist/src/client/query/BasedDbQuery.js +12 -3
- package/dist/src/client/query/aggregationFn.d.ts +3 -0
- package/dist/src/client/query/aggregationFn.js +9 -0
- package/dist/src/client/query/debug.js +2 -6
- package/dist/src/client/query/display.js +5 -2
- package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +2 -2
- package/dist/src/client/query/filter/createFixedFilterBuffer.js +34 -26
- package/dist/src/client/query/filter/createReferenceFilter.d.ts +1 -1
- package/dist/src/client/query/filter/createReferenceFilter.js +17 -7
- package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +1 -1
- package/dist/src/client/query/filter/createVariableFilterBuffer.js +45 -22
- package/dist/src/client/query/filter/filter.d.ts +1 -1
- package/dist/src/client/query/filter/filter.js +5 -0
- package/dist/src/client/query/filter/parseFilterValue.js +1 -1
- package/dist/src/client/query/filter/toBuffer.d.ts +2 -2
- package/dist/src/client/query/filter/toBuffer.js +16 -9
- package/dist/src/client/query/include/props.js +18 -1
- package/dist/src/client/query/include/toBuffer.d.ts +1 -1
- package/dist/src/client/query/include/toBuffer.js +22 -12
- package/dist/src/client/query/include/walk.js +2 -1
- package/dist/src/client/query/query.d.ts +1 -0
- package/dist/src/client/query/query.js +1 -0
- package/dist/src/client/query/queryDef.js +2 -1
- package/dist/src/client/query/read/read.d.ts +2 -1
- package/dist/src/client/query/read/read.js +95 -62
- package/dist/src/client/query/registerQuery.d.ts +1 -1
- package/dist/src/client/query/registerQuery.js +2 -1
- package/dist/src/client/query/search/index.d.ts +1 -1
- package/dist/src/client/query/search/index.js +56 -24
- package/dist/src/client/query/subscription/markers.js +2 -1
- package/dist/src/client/query/toBuffer.d.ts +1 -1
- package/dist/src/client/query/toBuffer.js +80 -32
- package/dist/src/client/query/types.d.ts +25 -3
- package/dist/src/client/query/types.js +6 -0
- package/dist/src/client/query/validation.js +1 -1
- package/dist/src/client/string.d.ts +1 -1
- package/dist/src/client/string.js +1 -2
- package/dist/src/client/xxHash64.d.ts +1 -1
- package/dist/src/index.d.ts +4 -3
- package/dist/src/index.js +20 -11
- package/dist/src/native.d.ts +8 -13
- package/dist/src/native.js +9 -31
- package/dist/src/server/dbHash.d.ts +5 -0
- package/dist/src/server/dbHash.js +27 -0
- package/dist/src/server/index.d.ts +10 -10
- package/dist/src/server/index.js +54 -35
- package/dist/src/server/migrate/index.js +1 -1
- package/dist/src/server/migrate/worker.js +2 -2
- package/dist/src/server/save.d.ts +1 -1
- package/dist/src/server/save.js +10 -10
- package/dist/src/server/start.js +4 -2
- package/dist/src/server/tree.d.ts +1 -1
- package/dist/src/server/tree.js +1 -1
- package/dist/src/utils.d.ts +3 -0
- package/dist/src/utils.js +23 -5
- package/package.json +3 -2
- package/dist/src/client/query/read/types.d.ts +0 -4
- package/dist/src/client/query/read/types.js +0 -5
|
@@ -2,9 +2,11 @@ import { ALIAS, TEXT, VECTOR } from '@based/schema/def';
|
|
|
2
2
|
import { EQUAL, EQUAL_CRC32, HAS, HAS_TO_LOWER_CASE, LIKE, MODE_DEFAULT_VAR, MODE_OR_VAR, getVectorFn, } from './types.js';
|
|
3
3
|
import { createFixedFilterBuffer } from './createFixedFilterBuffer.js';
|
|
4
4
|
import { crc32 } from '../../crc32.js';
|
|
5
|
+
import { ENCODER, concatUint8Arr } from '../../../utils.js';
|
|
5
6
|
const DEFAULT_SCORE = new Uint8Array(new Float32Array([0.5]).buffer);
|
|
6
7
|
const parseValue = (value, prop, ctx, lang) => {
|
|
7
8
|
let val = value;
|
|
9
|
+
// TODO should we do .normalize('NFKD') for all strings?
|
|
8
10
|
if (ctx.operation === HAS_TO_LOWER_CASE && typeof val === 'string') {
|
|
9
11
|
val = val.toLowerCase();
|
|
10
12
|
}
|
|
@@ -28,25 +30,28 @@ const parseValue = (value, prop, ctx, lang) => {
|
|
|
28
30
|
typeof value === 'string' ||
|
|
29
31
|
!prop.separate ||
|
|
30
32
|
ctx.operation !== EQUAL) {
|
|
31
|
-
if (
|
|
32
|
-
|
|
33
|
-
val = Buffer.concat([Buffer.from(val), Buffer.from([lang])]);
|
|
33
|
+
if (typeof val === 'string') {
|
|
34
|
+
val = ENCODER.encode(val);
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
if (prop.typeIndex === TEXT) {
|
|
37
|
+
const tmp = new Uint8Array(val.byteLength + 1);
|
|
38
|
+
tmp.set(val);
|
|
39
|
+
tmp[tmp.byteLength - 1] = lang;
|
|
40
|
+
val = tmp;
|
|
37
41
|
}
|
|
38
42
|
}
|
|
39
43
|
if (val?.BYTES_PER_ELEMENT > 1) {
|
|
40
44
|
val = val.buffer;
|
|
41
45
|
}
|
|
42
|
-
if (!(val instanceof
|
|
46
|
+
if (!(val instanceof Uint8Array || val instanceof ArrayBuffer)) {
|
|
43
47
|
throw new Error(`Incorrect value for filter: ${prop.path}`);
|
|
44
48
|
}
|
|
45
49
|
if (ctx.operation === LIKE && prop.typeIndex !== VECTOR) {
|
|
46
|
-
|
|
47
|
-
val
|
|
50
|
+
const tmp = new Uint8Array(val.byteLength + 1);
|
|
51
|
+
tmp.set((val instanceof ArrayBuffer) ? new Uint8Array(val) : val);
|
|
52
|
+
tmp[tmp.byteLength - 1] = ctx.opts.score ?? 2;
|
|
53
|
+
val = tmp;
|
|
48
54
|
}
|
|
49
|
-
// @ts-ignore
|
|
50
55
|
return val;
|
|
51
56
|
};
|
|
52
57
|
export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
|
|
@@ -59,11 +64,12 @@ export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
|
|
|
59
64
|
const x = [];
|
|
60
65
|
for (const v of value) {
|
|
61
66
|
const a = parseValue(v, prop, ctx, lang);
|
|
62
|
-
const size =
|
|
63
|
-
size
|
|
67
|
+
const size = new Uint8Array(2);
|
|
68
|
+
size[0] = a.byteLength;
|
|
69
|
+
size[1] = a.byteLength >>> 8;
|
|
64
70
|
x.push(size, a);
|
|
65
71
|
}
|
|
66
|
-
val =
|
|
72
|
+
val = concatUint8Arr(x);
|
|
67
73
|
}
|
|
68
74
|
else {
|
|
69
75
|
const x = [];
|
|
@@ -85,21 +91,33 @@ export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
|
|
|
85
91
|
prop.typeIndex !== ALIAS &&
|
|
86
92
|
prop.typeIndex !== VECTOR) {
|
|
87
93
|
if (prop.typeIndex === TEXT) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
]
|
|
94
|
+
const crc = crc32(val.slice(0, -1));
|
|
95
|
+
const len = val.byteLength - 1;
|
|
96
|
+
const v = new Uint8Array(9);
|
|
97
|
+
v[0] = crc;
|
|
98
|
+
v[1] = crc >>> 8;
|
|
99
|
+
v[2] = crc >>> 16;
|
|
100
|
+
v[3] = crc >>> 24;
|
|
101
|
+
v[4] = len;
|
|
102
|
+
v[5] = len >>> 8;
|
|
103
|
+
v[6] = len >>> 16;
|
|
104
|
+
v[7] = len >>> 24;
|
|
105
|
+
v[8] = val[val.length - 1];
|
|
106
|
+
buf = writeVarFilter(mode, v, ctx, prop, 0, 0);
|
|
93
107
|
}
|
|
94
108
|
else {
|
|
95
109
|
buf = createFixedFilterBuffer(prop, 8, { operation: EQUAL_CRC32, type: ctx.type, opts: ctx.opts }, val, false);
|
|
96
110
|
}
|
|
97
111
|
}
|
|
98
112
|
else {
|
|
113
|
+
if (val instanceof ArrayBuffer) {
|
|
114
|
+
val = new Uint8Array(val);
|
|
115
|
+
}
|
|
99
116
|
buf = writeVarFilter(mode, val, ctx, prop, 0, 0);
|
|
100
117
|
}
|
|
101
118
|
}
|
|
102
119
|
else {
|
|
120
|
+
// RFE is val always an Uint8Array?
|
|
103
121
|
buf = writeVarFilter(mode, val, ctx, prop, prop.start, prop.len);
|
|
104
122
|
}
|
|
105
123
|
}
|
|
@@ -107,17 +125,22 @@ export const createVariableFilterBuffer = (value, prop, ctx, lang) => {
|
|
|
107
125
|
};
|
|
108
126
|
function writeVarFilter(mode, val, ctx, prop, start, len) {
|
|
109
127
|
const size = val.byteLength;
|
|
110
|
-
const buf =
|
|
128
|
+
const buf = new Uint8Array(12 + size);
|
|
111
129
|
buf[0] = ctx.type;
|
|
112
130
|
buf[1] = mode;
|
|
113
131
|
buf[2] = prop.typeIndex;
|
|
114
|
-
buf
|
|
115
|
-
buf
|
|
116
|
-
buf
|
|
132
|
+
buf[3] = start;
|
|
133
|
+
buf[4] = start >>> 8;
|
|
134
|
+
buf[5] = len;
|
|
135
|
+
buf[6] = len >>> 8;
|
|
136
|
+
buf[7] = size;
|
|
137
|
+
buf[8] = size >>> 8;
|
|
138
|
+
buf[9] = size >>> 16;
|
|
139
|
+
buf[10] = size >>> 24;
|
|
117
140
|
buf[11] = ctx.operation;
|
|
118
141
|
// need to pas LANG FROM QUERY
|
|
119
142
|
// need to set on 12 if TEXT
|
|
120
|
-
buf.set(
|
|
143
|
+
buf.set(val, 12);
|
|
121
144
|
return buf;
|
|
122
145
|
}
|
|
123
146
|
//# sourceMappingURL=createVariableFilterBuffer.js.map
|
|
@@ -6,4 +6,4 @@ import { DbClient } from '../../index.js';
|
|
|
6
6
|
export { Operator, Filter };
|
|
7
7
|
export declare const filterRaw: (db: DbClient, filter: Filter, schema: SchemaTypeDef, conditions: QueryDefFilter, def: QueryDef) => number;
|
|
8
8
|
export declare const filter: (db: DbClient, def: QueryDef, filterAst: FilterAst, conditions: QueryDefFilter) => void;
|
|
9
|
-
export declare const filterOr: (db: DbClient, def: QueryDef, filterAst: FilterAst[], conditions: QueryDefFilter) =>
|
|
9
|
+
export declare const filterOr: (db: DbClient, def: QueryDef, filterAst: FilterAst[], conditions: QueryDefFilter) => any;
|
|
@@ -100,6 +100,11 @@ export const filterOr = (db, def, filterAst, conditions) => {
|
|
|
100
100
|
conditions: new Map(),
|
|
101
101
|
};
|
|
102
102
|
}
|
|
103
|
+
else {
|
|
104
|
+
const r = filterOr(db, def, filterAst, conditions.or);
|
|
105
|
+
conditions.size += r.size + 7;
|
|
106
|
+
return r;
|
|
107
|
+
}
|
|
103
108
|
filter(db, def, filterAst, conditions.or);
|
|
104
109
|
conditions.size += conditions.or.size;
|
|
105
110
|
return conditions.or;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { TIMESTAMP, CREATED, UPDATED, ENUM, BOOLEAN, STRING, BINARY, TEXT, } from '@based/schema/def';
|
|
2
2
|
import { crc32 } from '../../crc32.js';
|
|
3
3
|
import { convertToTimestamp } from '../../timestamp.js';
|
|
4
|
-
|
|
4
|
+
import { ENCODER } from '../../../utils.js';
|
|
5
5
|
// -------------------------------------------
|
|
6
6
|
// conditions normal
|
|
7
7
|
// field, [size 2]
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import { QueryDefFilter } from '../types.js';
|
|
2
|
-
export declare const fillConditionsBuffer: (result:
|
|
3
|
-
export declare const filterToBuffer: (conditions: QueryDefFilter) =>
|
|
2
|
+
export declare const fillConditionsBuffer: (result: Uint8Array, conditions: QueryDefFilter, offset: number) => number;
|
|
3
|
+
export declare const filterToBuffer: (conditions: QueryDefFilter) => Uint8Array;
|
|
@@ -26,7 +26,8 @@ const writeConditions = (result, k, offset, conditions) => {
|
|
|
26
26
|
result.set(condition, lastWritten);
|
|
27
27
|
lastWritten += condition.byteLength;
|
|
28
28
|
}
|
|
29
|
-
result
|
|
29
|
+
result[sizeIndex] = conditionSize;
|
|
30
|
+
result[sizeIndex + 1] = conditionSize >>> 8;
|
|
30
31
|
return lastWritten - offset;
|
|
31
32
|
};
|
|
32
33
|
export const fillConditionsBuffer = (result, conditions, offset) => {
|
|
@@ -48,12 +49,14 @@ export const fillConditionsBuffer = (result, conditions, offset) => {
|
|
|
48
49
|
lastWritten++;
|
|
49
50
|
result[lastWritten] = refField;
|
|
50
51
|
lastWritten++;
|
|
51
|
-
result
|
|
52
|
+
result[lastWritten] = refConditions.schema.id;
|
|
53
|
+
result[lastWritten + 1] = refConditions.schema.id >>> 8;
|
|
52
54
|
lastWritten += 2;
|
|
53
55
|
const sizeIndex = lastWritten;
|
|
54
56
|
lastWritten += 2;
|
|
55
57
|
const size = fillConditionsBuffer(result, refConditions, lastWritten);
|
|
56
|
-
result
|
|
58
|
+
result[sizeIndex] = size;
|
|
59
|
+
result[sizeIndex + 1] = size >>> 8;
|
|
57
60
|
lastWritten += size;
|
|
58
61
|
}
|
|
59
62
|
}
|
|
@@ -65,13 +68,18 @@ export const fillConditionsBuffer = (result, conditions, offset) => {
|
|
|
65
68
|
lastWritten += 2;
|
|
66
69
|
const size = writeConditions(result, k, lastWritten, v);
|
|
67
70
|
lastWritten += size;
|
|
68
|
-
result
|
|
71
|
+
result[sizeIndex] = size;
|
|
72
|
+
result[sizeIndex + 1] = size >>> 8;
|
|
69
73
|
});
|
|
70
74
|
}
|
|
71
75
|
if (conditions.or) {
|
|
72
76
|
const size = fillConditionsBuffer(result, conditions.or, lastWritten);
|
|
73
|
-
result
|
|
74
|
-
result
|
|
77
|
+
result[orJumpIndex] = size;
|
|
78
|
+
result[orJumpIndex + 1] = size >>> 8;
|
|
79
|
+
result[orJumpIndex + 2] = lastWritten;
|
|
80
|
+
result[orJumpIndex + 3] = lastWritten >>> 8;
|
|
81
|
+
result[orJumpIndex + 4] = lastWritten >>> 16;
|
|
82
|
+
result[orJumpIndex + 5] = lastWritten >>> 24;
|
|
75
83
|
lastWritten += size;
|
|
76
84
|
}
|
|
77
85
|
if (conditions.exists) {
|
|
@@ -88,15 +96,14 @@ export const fillConditionsBuffer = (result, conditions, offset) => {
|
|
|
88
96
|
}
|
|
89
97
|
return lastWritten - offset;
|
|
90
98
|
};
|
|
91
|
-
// TODO convert to UINT8ARRAY
|
|
92
99
|
export const filterToBuffer = (conditions) => {
|
|
93
100
|
let result;
|
|
94
101
|
if (conditions.size > 0) {
|
|
95
|
-
result =
|
|
102
|
+
result = new Uint8Array(conditions.size);
|
|
96
103
|
fillConditionsBuffer(result, conditions, 0);
|
|
97
104
|
}
|
|
98
105
|
else {
|
|
99
|
-
result =
|
|
106
|
+
result = new Uint8Array(0);
|
|
100
107
|
}
|
|
101
108
|
return result;
|
|
102
109
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { REFERENCE, REFERENCES, TEXT, } from '@based/schema/def';
|
|
2
|
+
import { QueryDefType } from '../types.js';
|
|
2
3
|
export const getAll = (props) => {
|
|
3
4
|
const fields = [];
|
|
4
5
|
for (const key in props) {
|
|
@@ -14,13 +15,29 @@ export const getAllRefs = (props, affix = '') => {
|
|
|
14
15
|
for (const key in props) {
|
|
15
16
|
const prop = props[key];
|
|
16
17
|
if (prop.typeIndex === REFERENCE || prop.typeIndex === REFERENCES) {
|
|
17
|
-
|
|
18
|
+
const refPath = prop.path.join('.') + affix;
|
|
19
|
+
fields.push(refPath);
|
|
20
|
+
if (prop.edges) {
|
|
21
|
+
for (const edge in prop.edges) {
|
|
22
|
+
fields.push(refPath + '.' + edge);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
18
25
|
}
|
|
19
26
|
}
|
|
20
27
|
return fields;
|
|
21
28
|
};
|
|
22
29
|
export const includeField = (def, field) => {
|
|
23
30
|
if (field === '*') {
|
|
31
|
+
if (def.type === QueryDefType.Reference ||
|
|
32
|
+
def.type === QueryDefType.References) {
|
|
33
|
+
const fields = [];
|
|
34
|
+
if (def.target.propDef.edges) {
|
|
35
|
+
for (const edge in def.target.propDef.edges) {
|
|
36
|
+
fields.push(edge);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
includeFields(def, fields);
|
|
40
|
+
}
|
|
24
41
|
includeFields(def, getAll(def.props));
|
|
25
42
|
}
|
|
26
43
|
else if (field === '**') {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import { QueryDefType } from '../types.js';
|
|
1
2
|
import { walkDefs } from './walk.js';
|
|
2
|
-
const EMPTY_BUFFER =
|
|
3
|
+
const EMPTY_BUFFER = new Uint8Array(0);
|
|
3
4
|
export const includeToBuffer = (db, def) => {
|
|
4
5
|
const result = [];
|
|
5
6
|
if (!def.include.stringFields.size &&
|
|
@@ -17,8 +18,13 @@ export const includeToBuffer = (db, def) => {
|
|
|
17
18
|
walkDefs(db, def, f);
|
|
18
19
|
}
|
|
19
20
|
}
|
|
21
|
+
// main
|
|
20
22
|
if (def.include.main.len > 0) {
|
|
21
|
-
if (def.
|
|
23
|
+
// if (def.target.)
|
|
24
|
+
const len = def.type === QueryDefType.Edge
|
|
25
|
+
? def.target.ref.edgeMainLen
|
|
26
|
+
: def.schema.mainLen;
|
|
27
|
+
if (def.include.main.len === len) {
|
|
22
28
|
// GET ALL MAIN FIELDS
|
|
23
29
|
let m = 0;
|
|
24
30
|
for (const key in def.include.main.include) {
|
|
@@ -32,16 +38,19 @@ export const includeToBuffer = (db, def) => {
|
|
|
32
38
|
else {
|
|
33
39
|
// GET SOME MAIN FIELDS
|
|
34
40
|
const size = Object.keys(def.include.main.include).length;
|
|
35
|
-
mainBuffer =
|
|
36
|
-
mainBuffer
|
|
41
|
+
mainBuffer = new Uint8Array(size * 4 + 2);
|
|
42
|
+
mainBuffer[0] = def.include.main.len;
|
|
43
|
+
mainBuffer[1] = def.include.main.len >>> 8;
|
|
37
44
|
let i = 2;
|
|
38
45
|
let m = 0;
|
|
39
46
|
for (const key in def.include.main.include) {
|
|
40
47
|
const v = def.include.main.include[key];
|
|
41
|
-
mainBuffer
|
|
48
|
+
mainBuffer[i] = v[1].start;
|
|
49
|
+
mainBuffer[i + 1] = v[1].start >>> 8;
|
|
42
50
|
const len = v[1].len;
|
|
43
51
|
v[0] = m;
|
|
44
|
-
mainBuffer
|
|
52
|
+
mainBuffer[i + 2] = len;
|
|
53
|
+
mainBuffer[i + 3] = len >>> 8;
|
|
45
54
|
i += 4;
|
|
46
55
|
m += len;
|
|
47
56
|
}
|
|
@@ -51,7 +60,7 @@ export const includeToBuffer = (db, def) => {
|
|
|
51
60
|
for (const [prop, { codes, def: propDef },] of def.include.langTextFields.entries()) {
|
|
52
61
|
def.include.propsRead[prop] = 0;
|
|
53
62
|
if (codes.has(0)) {
|
|
54
|
-
const b =
|
|
63
|
+
const b = new Uint8Array(3);
|
|
55
64
|
b[0] = prop;
|
|
56
65
|
b[1] = propDef.typeIndex;
|
|
57
66
|
b[2] = 0;
|
|
@@ -59,7 +68,7 @@ export const includeToBuffer = (db, def) => {
|
|
|
59
68
|
}
|
|
60
69
|
else {
|
|
61
70
|
for (const code of codes) {
|
|
62
|
-
const b =
|
|
71
|
+
const b = new Uint8Array(3);
|
|
63
72
|
b[0] = prop;
|
|
64
73
|
b[1] = propDef.typeIndex;
|
|
65
74
|
b[2] = code;
|
|
@@ -71,11 +80,12 @@ export const includeToBuffer = (db, def) => {
|
|
|
71
80
|
const propSize = def.include.props.size ?? 0;
|
|
72
81
|
if (mainBuffer) {
|
|
73
82
|
len = mainBuffer.byteLength + 3 + propSize * 2;
|
|
74
|
-
includeBuffer =
|
|
83
|
+
includeBuffer = new Uint8Array(len);
|
|
75
84
|
includeBuffer[0] = 0;
|
|
76
|
-
includeBuffer
|
|
85
|
+
includeBuffer[1] = mainBuffer.byteLength;
|
|
86
|
+
includeBuffer[2] = mainBuffer.byteLength >>> 8;
|
|
77
87
|
const offset = 3 + mainBuffer.byteLength;
|
|
78
|
-
|
|
88
|
+
includeBuffer.set(mainBuffer, 3);
|
|
79
89
|
if (propSize) {
|
|
80
90
|
let i = 0;
|
|
81
91
|
for (const [prop, propDef] of def.include.props.entries()) {
|
|
@@ -86,7 +96,7 @@ export const includeToBuffer = (db, def) => {
|
|
|
86
96
|
}
|
|
87
97
|
}
|
|
88
98
|
else if (propSize) {
|
|
89
|
-
const buf =
|
|
99
|
+
const buf = new Uint8Array(propSize * 2);
|
|
90
100
|
let i = 0;
|
|
91
101
|
for (const [prop, propDef] of def.include.props.entries()) {
|
|
92
102
|
buf[i] = prop;
|
|
@@ -21,6 +21,7 @@ const createEmptySharedDef = (skipValidation) => {
|
|
|
21
21
|
},
|
|
22
22
|
sort: null,
|
|
23
23
|
references: new Map(),
|
|
24
|
+
aggregation: null,
|
|
24
25
|
};
|
|
25
26
|
return q;
|
|
26
27
|
};
|
|
@@ -30,7 +31,7 @@ export const createQueryDef = (db, type, target, skipValidation) => {
|
|
|
30
31
|
const t = target;
|
|
31
32
|
const q = queryDef;
|
|
32
33
|
q.props = t.ref.edges;
|
|
33
|
-
q.reverseProps = t.ref.reverseEdges
|
|
34
|
+
// q.reverseProps = t.ref.reverseEdges
|
|
34
35
|
q.type = type;
|
|
35
36
|
q.target = t;
|
|
36
37
|
return q;
|
|
@@ -4,5 +4,6 @@ export type Item = {
|
|
|
4
4
|
} & {
|
|
5
5
|
[key: string]: any;
|
|
6
6
|
};
|
|
7
|
-
export
|
|
7
|
+
export type AggItem = Partial<Item>;
|
|
8
|
+
export declare const readAllFields: (q: QueryDef, result: Uint8Array, offset: number, end: number, item: Item | AggItem, id: number) => number;
|
|
8
9
|
export declare const resultToObject: (q: QueryDef, result: Uint8Array, end: number, offset?: number) => any;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { ALIAS, ALIASES, BINARY, BOOLEAN, ENUM, INT16, INT32, INT8, NUMBER, STRING, TEXT, TIMESTAMP, UINT16, UINT32, UINT8, VECTOR, JSON, CARDINALITY, } from '@based/schema/def';
|
|
2
|
+
import { QueryDefType } from '../types.js';
|
|
2
3
|
import { read, readUtf8 } from '../../string.js';
|
|
3
4
|
import { readDoubleLE, readFloatLE, readInt16, readInt32, readUint16, readUint32, } from '../../bitWise.js';
|
|
4
5
|
import { inverseLangMap } from '@based/schema';
|
|
5
|
-
import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES } from '
|
|
6
|
+
import { READ_EDGE, READ_ID, READ_REFERENCE, READ_REFERENCES, READ_AGGREGATION, CREATE_AGGREGATION, } from '../types.js';
|
|
6
7
|
const addField = (p, value, item, defaultOnly = false, lang = 0) => {
|
|
7
8
|
let i = p.__isEdge === true ? 1 : 0;
|
|
8
9
|
// TODO OPTMIZE
|
|
@@ -112,11 +113,14 @@ const readMainValue = (prop, result, index, item) => {
|
|
|
112
113
|
const readMain = (q, result, offset, item) => {
|
|
113
114
|
const mainInclude = q.include.main;
|
|
114
115
|
let i = offset;
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
116
|
+
const isEdge = q.type === QueryDefType.Edge;
|
|
117
|
+
const main = isEdge ? q.target.ref.reverseMainEdges : q.schema.main;
|
|
118
|
+
const len = isEdge ? q.target.ref.edgeMainLen : q.schema.mainLen;
|
|
119
|
+
if (mainInclude.len === len) {
|
|
120
|
+
for (const start in main) {
|
|
121
|
+
readMainValue(main[start], result, Number(start) + i, item);
|
|
118
122
|
}
|
|
119
|
-
i +=
|
|
123
|
+
i += len;
|
|
120
124
|
}
|
|
121
125
|
else {
|
|
122
126
|
for (const k in mainInclude.include) {
|
|
@@ -128,33 +132,36 @@ const readMain = (q, result, offset, item) => {
|
|
|
128
132
|
return i - offset;
|
|
129
133
|
};
|
|
130
134
|
const handleUndefinedProps = (id, q, item) => {
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
135
|
+
if (q.aggregation != -999 /* AggFn.NONE */) {
|
|
136
|
+
for (const k in q.include.propsRead) {
|
|
137
|
+
if (q.include.propsRead[k] !== id) {
|
|
138
|
+
// Only relvant for seperate props
|
|
139
|
+
const prop = q.schema.reverseProps[k];
|
|
140
|
+
if (prop.typeIndex === CARDINALITY) {
|
|
141
|
+
addField(prop, 0, item);
|
|
142
|
+
}
|
|
143
|
+
else if (prop.typeIndex === TEXT && q.lang == 0) {
|
|
144
|
+
const lan = getEmptyField(prop, item);
|
|
145
|
+
const lang = q.include.langTextFields.get(prop.prop).codes;
|
|
146
|
+
if (lang.has(0)) {
|
|
147
|
+
for (const locale in q.schema.locales) {
|
|
148
|
+
if (!lan[locale]) {
|
|
149
|
+
lan[locale] = '';
|
|
150
|
+
}
|
|
144
151
|
}
|
|
145
152
|
}
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
153
|
+
else {
|
|
154
|
+
for (const code of lang) {
|
|
155
|
+
const locale = inverseLangMap.get(code);
|
|
156
|
+
if (!lan[locale]) {
|
|
157
|
+
lan[locale] = '';
|
|
158
|
+
}
|
|
152
159
|
}
|
|
153
160
|
}
|
|
154
161
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
162
|
+
else {
|
|
163
|
+
addField(prop, prop.typeIndex === JSON ? null : '', item);
|
|
164
|
+
}
|
|
158
165
|
}
|
|
159
166
|
}
|
|
160
167
|
}
|
|
@@ -209,41 +216,40 @@ export const readAllFields = (q, result, offset, end, item, id) => {
|
|
|
209
216
|
// ----------------
|
|
210
217
|
}
|
|
211
218
|
else {
|
|
212
|
-
|
|
213
|
-
const
|
|
214
|
-
if (
|
|
215
|
-
i
|
|
216
|
-
|
|
217
|
-
addField(edgeDef, global.JSON.parse(readUtf8(result, i + 6, size + i)), item);
|
|
218
|
-
i += size + 4;
|
|
219
|
-
}
|
|
220
|
-
else if (t === BINARY) {
|
|
221
|
-
i++;
|
|
222
|
-
const size = readUint32(result, i);
|
|
223
|
-
addField(edgeDef, result.subarray(i + 6, size + i), item);
|
|
224
|
-
i += size + 4;
|
|
219
|
+
i++;
|
|
220
|
+
const target = 'ref' in q.edges.target && q.edges.target.ref;
|
|
221
|
+
if (prop === 0) {
|
|
222
|
+
i += readMain(q.edges, result, i, item);
|
|
223
|
+
// i += edgeDef.len
|
|
225
224
|
}
|
|
226
|
-
else
|
|
227
|
-
|
|
228
|
-
const
|
|
229
|
-
if (
|
|
230
|
-
|
|
225
|
+
else {
|
|
226
|
+
const edgeDef = target.reverseSeperateEdges[prop];
|
|
227
|
+
const t = edgeDef.typeIndex;
|
|
228
|
+
if (t === JSON) {
|
|
229
|
+
const size = readUint32(result, i);
|
|
230
|
+
addField(edgeDef, global.JSON.parse(readUtf8(result, i + 6, size + i)), item);
|
|
231
|
+
i += size + 4;
|
|
231
232
|
}
|
|
232
|
-
else {
|
|
233
|
-
|
|
233
|
+
else if (t === BINARY) {
|
|
234
|
+
const size = readUint32(result, i);
|
|
235
|
+
addField(edgeDef, result.subarray(i + 6, size + i), item);
|
|
236
|
+
i += size + 4;
|
|
237
|
+
}
|
|
238
|
+
else if (t === STRING || t === ALIAS || t === ALIASES) {
|
|
239
|
+
const size = readUint32(result, i);
|
|
240
|
+
if (size === 0) {
|
|
241
|
+
addField(edgeDef, '', item);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
addField(edgeDef, read(result, i + 4, size), item);
|
|
245
|
+
}
|
|
246
|
+
i += size + 4;
|
|
247
|
+
}
|
|
248
|
+
else if (t === CARDINALITY) {
|
|
249
|
+
const size = readUint32(result, i);
|
|
250
|
+
addField(edgeDef, readUint32(result, i + 4), item);
|
|
251
|
+
i += size + 4;
|
|
234
252
|
}
|
|
235
|
-
i += size + 4;
|
|
236
|
-
}
|
|
237
|
-
else if (t === CARDINALITY) {
|
|
238
|
-
i++;
|
|
239
|
-
const size = readUint32(result, i);
|
|
240
|
-
addField(edgeDef, readUint32(result, i + 4), item);
|
|
241
|
-
i += size + 4;
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
i++;
|
|
245
|
-
readMainValue(edgeDef, result, i, item);
|
|
246
|
-
i += edgeDef.len;
|
|
247
253
|
}
|
|
248
254
|
}
|
|
249
255
|
}
|
|
@@ -282,6 +288,23 @@ export const readAllFields = (q, result, offset, end, item, id) => {
|
|
|
282
288
|
addField(ref.target.propDef, refs, item);
|
|
283
289
|
i += size + 4;
|
|
284
290
|
}
|
|
291
|
+
else if (index === CREATE_AGGREGATION) {
|
|
292
|
+
i--;
|
|
293
|
+
result[i] = READ_AGGREGATION;
|
|
294
|
+
q.aggregation = -999 /* AggFn.NONE */;
|
|
295
|
+
return i - offset - 4 - (q.search ? 4 : 0);
|
|
296
|
+
}
|
|
297
|
+
else if (index === READ_AGGREGATION) {
|
|
298
|
+
// TODO: To change to a map and also to get the aggregate field name from a query function parameter
|
|
299
|
+
const propAgg = {
|
|
300
|
+
name: 'count',
|
|
301
|
+
path: ['count'],
|
|
302
|
+
typeIndex: UINT32,
|
|
303
|
+
};
|
|
304
|
+
const size = readUint32(result, i);
|
|
305
|
+
addField(propAgg, readUint32(result, i + 4), item);
|
|
306
|
+
i += 4 + size + 4;
|
|
307
|
+
}
|
|
285
308
|
else if (index === 0) {
|
|
286
309
|
i += readMain(q, result, i, item);
|
|
287
310
|
}
|
|
@@ -359,9 +382,13 @@ export const readAllFields = (q, result, offset, end, item, id) => {
|
|
|
359
382
|
handleUndefinedProps(id, q, item);
|
|
360
383
|
return i - offset;
|
|
361
384
|
};
|
|
385
|
+
let cnt = 0;
|
|
362
386
|
export const resultToObject = (q, result, end, offset = 0) => {
|
|
363
387
|
const len = readUint32(result, offset);
|
|
364
388
|
if (len === 0) {
|
|
389
|
+
if ('id' in q.target || 'alias' in q.target) {
|
|
390
|
+
return null;
|
|
391
|
+
}
|
|
365
392
|
return [];
|
|
366
393
|
}
|
|
367
394
|
let items = [];
|
|
@@ -369,9 +396,15 @@ export const resultToObject = (q, result, end, offset = 0) => {
|
|
|
369
396
|
while (i < end) {
|
|
370
397
|
const id = readUint32(result, i);
|
|
371
398
|
i += 4;
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
399
|
+
var item;
|
|
400
|
+
if (q.aggregation == -999 /* AggFn.NONE */) {
|
|
401
|
+
item = {};
|
|
402
|
+
}
|
|
403
|
+
else {
|
|
404
|
+
item = {
|
|
405
|
+
id,
|
|
406
|
+
};
|
|
407
|
+
}
|
|
375
408
|
if (q.search) {
|
|
376
409
|
item.$searchScore = readFloatLE(result, i);
|
|
377
410
|
i += 4;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { BasedDbQuery } from './BasedDbQuery.js';
|
|
2
|
-
export declare const registerQuery: (q: BasedDbQuery) =>
|
|
2
|
+
export declare const registerQuery: (q: BasedDbQuery) => Uint8Array;
|
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import native from '../../native.js';
|
|
2
|
+
import { concatUint8Arr } from '../../utils.js';
|
|
2
3
|
import { defToBuffer } from './toBuffer.js';
|
|
3
4
|
import { handleErrors } from './validation.js';
|
|
4
5
|
export const registerQuery = (q) => {
|
|
5
6
|
if (!q.id) {
|
|
6
7
|
const b = defToBuffer(q.db, q.def);
|
|
7
|
-
const buf =
|
|
8
|
+
const buf = concatUint8Arr(b);
|
|
8
9
|
let id = native.crc32(buf);
|
|
9
10
|
q.id = id;
|
|
10
11
|
q.buffer = buf;
|