@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.
Files changed (120) hide show
  1. package/dist/lib/darwin_aarch64/include/cdefs.h +4 -0
  2. package/dist/lib/darwin_aarch64/include/selva/db.h +25 -5
  3. package/dist/lib/darwin_aarch64/include/selva/fields.h +34 -72
  4. package/dist/lib/darwin_aarch64/include/selva/selva_lang.h +7 -0
  5. package/dist/lib/darwin_aarch64/include/selva/types.h +9 -13
  6. package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
  7. package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
  8. package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
  9. package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
  10. package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
  11. package/dist/lib/darwin_aarch64/libnode-v25.node +0 -0
  12. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  13. package/dist/lib/linux_aarch64/include/cdefs.h +4 -0
  14. package/dist/lib/linux_aarch64/include/selva/db.h +25 -5
  15. package/dist/lib/linux_aarch64/include/selva/fields.h +34 -72
  16. package/dist/lib/linux_aarch64/include/selva/selva_lang.h +7 -0
  17. package/dist/lib/linux_aarch64/include/selva/types.h +9 -13
  18. package/dist/lib/linux_aarch64/libdeflate.so +0 -0
  19. package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
  20. package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
  21. package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
  22. package/dist/lib/linux_aarch64/libnode-v25.node +0 -0
  23. package/dist/lib/linux_aarch64/libselva.so +0 -0
  24. package/dist/lib/linux_x86_64/include/cdefs.h +4 -0
  25. package/dist/lib/linux_x86_64/include/selva/db.h +25 -5
  26. package/dist/lib/linux_x86_64/include/selva/fields.h +34 -72
  27. package/dist/lib/linux_x86_64/include/selva/selva_lang.h +7 -0
  28. package/dist/lib/linux_x86_64/include/selva/types.h +9 -13
  29. package/dist/lib/linux_x86_64/libdeflate.so +0 -0
  30. package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
  31. package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
  32. package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
  33. package/dist/lib/linux_x86_64/libnode-v25.node +0 -0
  34. package/dist/lib/linux_x86_64/libselva.so +0 -0
  35. package/dist/src/client/modify/Tmp.d.ts +0 -1
  36. package/dist/src/client/modify/Tmp.js +14 -9
  37. package/dist/src/client/modify/create/index.js +6 -5
  38. package/dist/src/client/modify/drain.js +6 -2
  39. package/dist/src/client/modify/edges/binary.js +3 -2
  40. package/dist/src/client/modify/edges/reference.js +2 -3
  41. package/dist/src/client/modify/edges/references.js +2 -3
  42. package/dist/src/client/modify/edges/separate.js +4 -1
  43. package/dist/src/client/modify/edges/string.js +2 -3
  44. package/dist/src/client/modify/error.d.ts +29 -0
  45. package/dist/src/client/modify/error.js +10 -0
  46. package/dist/src/client/modify/props/alias.js +1 -1
  47. package/dist/src/client/modify/props/binary.js +1 -1
  48. package/dist/src/client/modify/props/cardinality.js +1 -1
  49. package/dist/src/client/modify/props/fixed.js +13 -14
  50. package/dist/src/client/modify/props/reference.js +2 -2
  51. package/dist/src/client/modify/props/references.js +7 -7
  52. package/dist/src/client/modify/props/string.js +1 -1
  53. package/dist/src/client/modify/props/vector.js +1 -11
  54. package/dist/src/client/modify/types.d.ts +1 -0
  55. package/dist/src/client/modify/types.js +1 -0
  56. package/dist/src/client/modify/validate.d.ts +1 -1
  57. package/dist/src/client/modify/validate.js +4 -3
  58. package/dist/src/client/query/BasedDbQuery.d.ts +4 -4
  59. package/dist/src/client/query/BasedDbQuery.js +23 -12
  60. package/dist/src/client/query/BasedQueryResponse.d.ts +0 -1
  61. package/dist/src/client/query/BasedQueryResponse.js +0 -3
  62. package/dist/src/client/query/aggregates/aggregation.d.ts +1 -1
  63. package/dist/src/client/query/aggregates/aggregation.js +123 -51
  64. package/dist/src/client/query/display.js +14 -8
  65. package/dist/src/client/query/filter/createFixedFilterBuffer.js +59 -22
  66. package/dist/src/client/query/filter/createReferenceFilter.js +13 -13
  67. package/dist/src/client/query/filter/createVariableFilterBuffer.js +6 -3
  68. package/dist/src/client/query/filter/filter.js +8 -0
  69. package/dist/src/client/query/filter/primitiveFilter.js +4 -1
  70. package/dist/src/client/query/filter/{toBuffer.d.ts → toByteCode.d.ts} +3 -2
  71. package/dist/src/client/query/filter/{toBuffer.js → toByteCode.js} +46 -12
  72. package/dist/src/client/query/filter/types.d.ts +1 -0
  73. package/dist/src/client/query/filter/types.js +33 -5
  74. package/dist/src/client/query/include/toByteCode.d.ts +2 -2
  75. package/dist/src/client/query/include/toByteCode.js +1 -1
  76. package/dist/src/client/query/query.d.ts +1 -1
  77. package/dist/src/client/query/query.js +1 -1
  78. package/dist/src/client/query/queryDef.js +6 -2
  79. package/dist/src/client/query/registerQuery.js +5 -9
  80. package/dist/src/client/query/subscription/index.js +5 -0
  81. package/dist/src/client/query/subscription/toByteCode.d.ts +6 -0
  82. package/dist/src/client/query/subscription/toByteCode.js +139 -0
  83. package/dist/src/client/query/subscription/types.d.ts +6 -0
  84. package/dist/src/client/query/subscription/types.js +7 -9
  85. package/dist/src/client/query/toByteCode/aggregates.d.ts +2 -0
  86. package/dist/src/client/query/toByteCode/aggregates.js +60 -0
  87. package/dist/src/client/query/toByteCode/alias.d.ts +2 -0
  88. package/dist/src/client/query/toByteCode/alias.js +24 -0
  89. package/dist/src/client/query/toByteCode/default.d.ts +2 -2
  90. package/dist/src/client/query/toByteCode/default.js +21 -24
  91. package/dist/src/client/query/toByteCode/id.d.ts +2 -0
  92. package/dist/src/client/query/toByteCode/id.js +17 -0
  93. package/dist/src/client/query/toByteCode/ids.d.ts +2 -0
  94. package/dist/src/client/query/toByteCode/ids.js +52 -0
  95. package/dist/src/client/query/toByteCode/offsets.d.ts +35 -0
  96. package/dist/src/client/query/toByteCode/offsets.js +36 -0
  97. package/dist/src/client/query/toByteCode/reference.d.ts +2 -0
  98. package/dist/src/client/query/toByteCode/reference.js +12 -0
  99. package/dist/src/client/query/toByteCode/references.d.ts +2 -0
  100. package/dist/src/client/query/toByteCode/references.js +35 -0
  101. package/dist/src/client/query/toByteCode/toByteCode.d.ts +4 -2
  102. package/dist/src/client/query/toByteCode/toByteCode.js +62 -189
  103. package/dist/src/client/query/types.d.ts +24 -5
  104. package/dist/src/client/query/validation.d.ts +5 -1
  105. package/dist/src/client/query/validation.js +15 -3
  106. package/dist/src/client/string.js +1 -1
  107. package/dist/src/hooks.js +2 -29
  108. package/dist/src/index.d.ts +1 -0
  109. package/dist/src/index.js +1 -0
  110. package/dist/src/native.d.ts +8 -1
  111. package/dist/src/native.js +23 -2
  112. package/dist/src/server/index.d.ts +2 -0
  113. package/dist/src/server/index.js +11 -2
  114. package/dist/src/server/schema.js +3 -2
  115. package/dist/src/server/schemaSelvaBuffer.d.ts +4 -0
  116. package/dist/src/server/schemaSelvaBuffer.js +182 -0
  117. package/dist/src/server/subscription.d.ts +32 -0
  118. package/dist/src/server/subscription.js +275 -0
  119. package/dist/src/utils.js +2 -3
  120. package/package.json +5 -6
@@ -1,16 +1,26 @@
1
1
  import { createSortBuffer } from '../sort.js';
2
- import { QueryDefType, QueryType } from '../types.js';
2
+ import { QueryDefType, } from '../types.js';
3
3
  import { includeToBuffer } from '../include/toByteCode.js';
4
- import { filterToBuffer } from '../query.js';
5
4
  import { searchToBuffer } from '../search/index.js';
6
- import { ENCODER, writeUint64 } from '@based/utils';
7
- import { aggregateToBuffer, isRootCountOnly, } from '../aggregates/aggregation.js';
5
+ import { writeUint64 } from '@based/utils';
8
6
  import { defaultQuery } from './default.js';
7
+ import { idQuery } from './id.js';
8
+ import { aliasQuery } from './alias.js';
9
+ import { idsQuery } from './ids.js';
10
+ import { referencesQuery } from './references.js';
11
+ import { referenceQuery } from './reference.js';
12
+ import { aggregatesQuery } from './aggregates.js';
13
+ import { resolveMetaIndexes } from '../query.js';
9
14
  const byteSize = (arr) => {
10
15
  return arr.reduce((a, b) => {
11
- return a + b.byteLength;
16
+ return a + b.buffer.byteLength;
12
17
  }, 0);
13
18
  };
19
+ const schemaChecksum = (def) => {
20
+ const checksum = new Uint8Array(8);
21
+ writeUint64(checksum, def.schemaChecksum ?? 0, 0);
22
+ return { buffer: checksum, def };
23
+ };
14
24
  export function defToBuffer(db, def) {
15
25
  const result = [];
16
26
  const include = includeToBuffer(db, def);
@@ -34,211 +44,52 @@ export function defToBuffer(db, def) {
34
44
  }
35
45
  const size = (edges ? edgesSize + 3 : 0) + byteSize(include);
36
46
  if (def.aggregate) {
37
- const aggregateSize = def.aggregate.size || 0;
38
- if (aggregateSize === 0) {
39
- throw new Error('Wrong aggregate size (0)');
40
- }
41
- const filterSize = def.filter.size || 0;
42
- if (def.type === QueryDefType.References) {
43
- const buf = new Uint8Array(13 + filterSize + aggregateSize);
44
- const sz = 10 + filterSize + aggregateSize;
45
- buf[0] = 2 /* includeOp.REFERENCES_AGGREGATION */;
46
- buf[1] = sz;
47
- buf[2] = sz >>> 8;
48
- buf[3] = filterSize;
49
- buf[4] = filterSize >>> 8;
50
- buf[5] = def.range.offset;
51
- buf[6] = def.range.offset >>> 8;
52
- buf[7] = def.range.offset >>> 16;
53
- buf[8] = def.range.offset >>> 24;
54
- if (filterSize) {
55
- buf.set(filterToBuffer(def.filter), 9);
56
- }
57
- // required to get typeEntry and fieldSchema
58
- buf[9 + filterSize] = def.schema.idUint8[0]; // typeId
59
- buf[9 + 1 + filterSize] = def.schema.idUint8[1]; // typeId
60
- buf[9 + 2 + filterSize] = def.target.propDef.prop; // refField
61
- const aggregateBuffer = aggregateToBuffer(def.aggregate);
62
- buf.set(aggregateBuffer, 9 + 3 + filterSize);
63
- result.push(buf);
64
- }
65
- else {
66
- const buf = new Uint8Array(16 + filterSize + aggregateSize);
67
- buf[0] = isRootCountOnly(def, filterSize)
68
- ? QueryType.aggregatesCountType
69
- : QueryType.aggregates;
70
- buf[1] = def.schema.idUint8[0];
71
- buf[2] = def.schema.idUint8[1];
72
- buf[3] = def.range.offset;
73
- buf[4] = def.range.offset >>> 8;
74
- buf[5] = def.range.offset >>> 16;
75
- buf[6] = def.range.offset >>> 24;
76
- buf[7] = def.range.limit;
77
- buf[8] = def.range.limit >>> 8;
78
- buf[9] = def.range.limit >>> 16;
79
- buf[10] = def.range.limit >>> 24;
80
- buf[11] = filterSize;
81
- buf[12] = filterSize >>> 8;
82
- if (filterSize) {
83
- buf.set(filterToBuffer(def.filter), 13);
84
- }
85
- const aggregateBuffer = aggregateToBuffer(def.aggregate);
86
- buf[14 + filterSize] = aggregateSize;
87
- buf[15 + filterSize] = aggregateSize >>> 8;
88
- buf.set(aggregateBuffer, 16 + filterSize);
89
- result.push(buf);
90
- }
47
+ result.push(aggregatesQuery(def));
91
48
  if (def.type === QueryDefType.Root) {
92
- const checksum = new Uint8Array(8);
93
- writeUint64(checksum, def.schemaChecksum ?? 0, 0);
94
- result.push(checksum);
49
+ result.push(schemaChecksum(def));
95
50
  }
96
51
  return result;
97
52
  }
98
53
  if (def.type === QueryDefType.Root) {
99
- let search;
100
- let searchSize = 0;
101
- const filterSize = def.filter.size || 0;
102
- if (def.search) {
103
- search = searchToBuffer(def.search);
104
- searchSize = def.search.size;
105
- }
106
54
  if (def.target.resolvedAlias) {
107
- const alias = def.target.resolvedAlias;
108
- const aliasStr = ENCODER.encode(alias.value);
109
- const aliasLen = aliasStr.byteLength;
110
- const buf = new Uint8Array(8 + filterSize + aliasLen);
111
- buf[0] = QueryType.alias;
112
- buf[1] = def.schema.idUint8[0];
113
- buf[2] = def.schema.idUint8[1];
114
- buf[3] = alias.def.prop;
115
- buf[4] = aliasLen;
116
- buf[5] = aliasLen >>> 8;
117
- buf.set(aliasStr, 6);
118
- buf[6 + aliasLen] = filterSize;
119
- buf[7 + aliasLen] = filterSize >>> 8;
120
- if (filterSize) {
121
- buf.set(filterToBuffer(def.filter), 8 + aliasLen);
122
- }
123
- result.push(buf);
55
+ result.push(aliasQuery(def));
124
56
  }
125
57
  else if (typeof def.target.id === 'number') {
126
- const buf = new Uint8Array(9 + filterSize);
127
- buf[0] = QueryType.id;
128
- buf[1] = def.schema.idUint8[0];
129
- buf[2] = def.schema.idUint8[1];
130
- buf[3] = def.target.id;
131
- buf[4] = def.target.id >>> 8;
132
- buf[5] = def.target.id >>> 16;
133
- buf[6] = def.target.id >>> 24;
134
- buf[7] = filterSize;
135
- buf[8] = filterSize >>> 8;
136
- if (filterSize) {
137
- buf.set(filterToBuffer(def.filter), 9);
138
- }
139
- result.push(buf);
58
+ result.push(idQuery(def));
140
59
  }
141
60
  else {
142
- let sort;
143
- let sortSize = 0;
144
- if (def.sort) {
145
- sort = createSortBuffer(def.sort);
146
- sortSize = sort.byteLength;
147
- }
148
61
  if (def.target.ids) {
62
+ const sortSize = def.sort ? createSortBuffer(def.sort).byteLength : 0;
149
63
  if (!sortSize &&
150
64
  (def.range.offset || def.range.limit < def.target.ids.length)) {
65
+ ;
151
66
  def.target.ids = def.target.ids.slice(def.range.offset, def.range.offset + def.range.limit);
152
67
  }
153
- const idsSize = def.target.ids.length * 4;
154
- const buf = new Uint8Array(21 + idsSize + filterSize + sortSize + searchSize);
155
- buf[0] = QueryType.ids;
156
- buf[1] = def.schema.idUint8[0];
157
- buf[2] = def.schema.idUint8[1];
158
- buf[3] = idsSize;
159
- buf[4] = idsSize >>> 8;
160
- buf[5] = idsSize >>> 16;
161
- buf[6] = idsSize >>> 24;
162
- buf.set(new Uint8Array(def.target.ids.buffer), 7);
163
- buf[idsSize + 7] = def.range.offset;
164
- buf[idsSize + 8] = def.range.offset >>> 8;
165
- buf[idsSize + 9] = def.range.offset >>> 16;
166
- buf[idsSize + 10] = def.range.offset >>> 24;
167
- buf[idsSize + 11] = def.range.limit;
168
- buf[idsSize + 12] = def.range.limit >>> 8;
169
- buf[idsSize + 13] = def.range.limit >>> 16;
170
- buf[idsSize + 14] = def.range.limit >>> 24;
171
- // if (filterSize && isSimpleMainFilter(def.filter)) {
172
- buf[idsSize + 15] = filterSize;
173
- buf[idsSize + 16] = filterSize >>> 8;
174
- if (filterSize) {
175
- // just write it here dont do set
176
- buf.set(filterToBuffer(def.filter), idsSize + 17);
177
- }
178
- buf[17 + filterSize + idsSize] = sortSize;
179
- buf[18 + filterSize + idsSize] = sortSize >>> 8;
180
- if (sortSize) {
181
- buf.set(sort, 19 + filterSize + idsSize);
182
- }
183
- buf[19 + filterSize + idsSize + sortSize] = searchSize;
184
- buf[20 + filterSize + idsSize + sortSize] = searchSize >>> 8;
185
- if (searchSize) {
186
- buf.set(search, 21 + filterSize + idsSize + sortSize);
187
- }
188
- // ----------
189
- result.push(buf);
68
+ result.push(idsQuery(def));
190
69
  }
191
70
  else {
71
+ let search;
72
+ let searchSize = 0;
73
+ if (def.search) {
74
+ search = searchToBuffer(def.search);
75
+ searchSize = def.search.size;
76
+ }
77
+ let sort;
78
+ let sortSize = 0;
79
+ if (def.sort) {
80
+ sort = createSortBuffer(def.sort);
81
+ sortSize = sort.byteLength;
82
+ }
83
+ const filterSize = def.filter.size || 0;
192
84
  result.push(defaultQuery(def, filterSize, sortSize, searchSize, sort, search));
193
85
  }
194
86
  }
195
87
  }
196
88
  else if (def.type === QueryDefType.References) {
197
- const filterSize = def.filter.size || 0;
198
- let sort;
199
- if (def.sort) {
200
- sort = createSortBuffer(def.sort);
201
- }
202
- const sortSize = sort?.byteLength ?? 0;
203
- const modsSize = filterSize + sortSize;
204
- const meta = new Uint8Array(modsSize + 10 + 8);
205
- const sz = size + 7 + modsSize + 8;
206
- meta[0] = 4 /* includeOp.REFERENCES */;
207
- meta[1] = sz;
208
- meta[2] = sz >>> 8;
209
- meta[3] = filterSize;
210
- meta[4] = filterSize >>> 8;
211
- meta[5] = sortSize;
212
- meta[6] = sortSize >>> 8;
213
- meta[7] = def.range.offset;
214
- meta[8] = def.range.offset >>> 8;
215
- meta[9] = def.range.offset >>> 16;
216
- meta[10] = def.range.offset >>> 24;
217
- meta[11] = def.range.limit;
218
- meta[12] = def.range.limit >>> 8;
219
- meta[13] = def.range.limit >>> 16;
220
- meta[14] = def.range.limit >>> 24;
221
- if (filterSize) {
222
- meta.set(filterToBuffer(def.filter), 15);
223
- }
224
- if (sort) {
225
- meta.set(sort, 15 + filterSize);
226
- }
227
- meta[15 + modsSize] = def.schema.idUint8[0];
228
- meta[15 + 1 + modsSize] = def.schema.idUint8[1];
229
- meta[15 + 2 + modsSize] = def.target.propDef.prop;
230
- result.push(meta);
89
+ result.push(referencesQuery(def, size));
231
90
  }
232
91
  else if (def.type === QueryDefType.Reference) {
233
- const meta = new Uint8Array(6);
234
- const sz = size + 3;
235
- meta[0] = 5 /* includeOp.REFERENCE */;
236
- meta[1] = sz;
237
- meta[2] = sz >>> 8;
238
- meta[3] = def.schema.idUint8[0];
239
- meta[4] = def.schema.idUint8[1];
240
- meta[5] = def.target.propDef.prop;
241
- result.push(meta);
92
+ result.push(referenceQuery(def, size));
242
93
  }
243
94
  result.push(...include);
244
95
  if (edges) {
@@ -249,10 +100,32 @@ export function defToBuffer(db, def) {
249
100
  result.push(metaEdgeBuffer, ...edges);
250
101
  }
251
102
  if (def.type === QueryDefType.Root) {
252
- const checksum = new Uint8Array(8);
253
- writeUint64(checksum, def.schemaChecksum ?? 0, 0);
254
- result.push(checksum);
103
+ result.push(schemaChecksum(def));
255
104
  }
256
105
  return result;
257
106
  }
107
+ export const queryToBuffer = (query) => {
108
+ const bufs = defToBuffer(query.db, query.def);
109
+ // allow both uint8 and def
110
+ let totalByteLength = bufs.reduce((acc, cur) => acc + cur.buffer.byteLength, 0);
111
+ const res = new Uint8Array(totalByteLength);
112
+ let offset = 0;
113
+ for (let i = 0; i < bufs.length; i++) {
114
+ const intermediateResult = bufs[i];
115
+ if (intermediateResult instanceof Uint8Array) {
116
+ res.set(intermediateResult, offset);
117
+ offset += intermediateResult.byteLength;
118
+ }
119
+ else {
120
+ if (intermediateResult.needsMetaResolve) {
121
+ if (intermediateResult.def.filter.hasSubMeta) {
122
+ resolveMetaIndexes(intermediateResult.def.filter, offset);
123
+ }
124
+ }
125
+ res.set(intermediateResult.buffer, offset);
126
+ offset += intermediateResult.buffer.byteLength;
127
+ }
128
+ }
129
+ return res;
130
+ };
258
131
  //# sourceMappingURL=toByteCode.js.map
@@ -1,6 +1,6 @@
1
1
  import { LangCode, LangName } from '@based/schema';
2
2
  import { PropDef, PropDefEdge, SchemaTypeDef } from '@based/schema/def';
3
- import { FilterOpts } from './filter/types.js';
3
+ import { FilterCtx, FilterOpts } from './filter/types.js';
4
4
  import { QueryError } from './validation.js';
5
5
  import { Interval, aggFnOptions } from './aggregates/types.js';
6
6
  import { AggregateType, ReaderSchema } from '@based/protocol/db-read';
@@ -52,7 +52,19 @@ export type Target = {
52
52
  };
53
53
  };
54
54
  export declare const isRefDef: (def: QueryDef) => def is QueryDefRest;
55
- export type FilterCondition = Uint8Array;
55
+ export type FilterMetaNow = {
56
+ byteIndex: number;
57
+ resolvedByteIndex: number;
58
+ offset: number;
59
+ ctx: FilterCtx;
60
+ prop: PropDef | PropDefEdge;
61
+ };
62
+ export type FilterCondition = {
63
+ buffer: Uint8Array;
64
+ subscriptionMeta?: {
65
+ now?: FilterMetaNow[];
66
+ };
67
+ };
56
68
  export type QueryDefFilter = {
57
69
  size: number;
58
70
  conditions: Map<number, FilterCondition[]>;
@@ -66,6 +78,7 @@ export type QueryDefFilter = {
66
78
  edges?: Map<number, FilterCondition[]>;
67
79
  or?: QueryDefFilter;
68
80
  and?: QueryDefFilter;
81
+ hasSubMeta: boolean;
69
82
  };
70
83
  export type QueryDefSearch = {
71
84
  size: number;
@@ -95,9 +108,10 @@ export type QueryDefSort = {
95
108
  };
96
109
  export type Aggregation = {
97
110
  type: AggregateType;
98
- propDef: PropDef;
111
+ propDef: PropDef | PropDefEdge;
99
112
  resultPos: number;
100
113
  accumulatorPos: number;
114
+ isEdge: boolean;
101
115
  };
102
116
  export type QueryDefAggregation = {
103
117
  size: number;
@@ -115,7 +129,7 @@ export interface aggPropDef extends PropDef {
115
129
  }
116
130
  export type LangFallback = LangName | false;
117
131
  export type QueryDefShared = {
118
- queryId?: number;
132
+ queryType: QueryType;
119
133
  schemaChecksum?: number;
120
134
  errors: QueryError[];
121
135
  lang: {
@@ -167,10 +181,15 @@ export type QueryByAliasObj = {
167
181
  export declare const isAlias: (id: Promise<number> | QueryByAliasObj | number | Uint32Array | (QueryByAliasObj | number | Promise<number>)[]) => id is QueryByAliasObj;
168
182
  export declare const enum includeOp {
169
183
  DEFAULT = 1,
170
- REFERENCES_AGGREGATION = 2,
184
+ REFS_AGGREGATION = 2,
171
185
  EDGE = 3,
172
186
  REFERENCES = 4,
173
187
  REFERENCE = 5,
174
188
  META = 6,// this can be a small buffer as well
175
189
  PARTIAL = 7
176
190
  }
191
+ export type IntermediateByteCode = {
192
+ buffer: Uint8Array;
193
+ def: QueryDef;
194
+ needsMetaResolve?: boolean;
195
+ } | Uint8Array;
@@ -2,6 +2,7 @@ import { PropDef, PropDefEdge, SchemaTypeDef } from '@based/schema/def';
2
2
  import { DbClient } from '../index.js';
3
3
  import { Filter } from './query.js';
4
4
  import { QueryByAliasObj, QueryDef } from './types.js';
5
+ import { Validation } from '@based/schema';
5
6
  import { StepInput } from './aggregates/types.js';
6
7
  export type QueryError = {
7
8
  code: number;
@@ -35,6 +36,7 @@ export declare const ERR_AGG_ENOENT = 25;
35
36
  export declare const ERR_AGG_TYPE = 26;
36
37
  export declare const ERR_AGG_INVALID_STEP_TYPE = 27;
37
38
  export declare const ERR_AGG_INVALID_STEP_RANGE = 28;
39
+ export declare const ERR_AGG_NOT_IMPLEMENTED = 29;
38
40
  declare const messages: {
39
41
  1: (p: any) => string;
40
42
  2: (p: any) => string;
@@ -64,13 +66,14 @@ declare const messages: {
64
66
  26: (p: any) => string;
65
67
  27: (p: any) => string;
66
68
  28: (p: any) => string;
69
+ 29: (p: any) => string;
67
70
  };
68
71
  export type ErrorCode = keyof typeof messages;
69
72
  export declare const searchIncorrecQueryValue: (def: QueryDef, payload: any) => void;
70
73
  export declare const searchIncorrectType: (def: QueryDef, payload: PropDef | PropDefEdge) => void;
71
74
  export declare const searchDoesNotExist: (def: QueryDef, field: string, isVector: boolean) => PropDef;
72
75
  export declare const validateRange: (def: QueryDef, offset: number, limit: number) => boolean;
73
- export declare const validateVal: (def: QueryDef, f: Filter, validate: (v: any, p: PropDef | PropDefEdge) => boolean) => boolean;
76
+ export declare const validateVal: (def: QueryDef, f: Filter, validate: Validation) => boolean;
74
77
  export declare const validateFilter: (def: QueryDef, prop: PropDef | PropDefEdge, f: Filter) => boolean;
75
78
  export declare const validateType: (db: DbClient, def: QueryDef, type: string) => SchemaTypeDef;
76
79
  export declare const filterOperatorDoesNotExist: (def: QueryDef, field: string) => void;
@@ -94,4 +97,5 @@ export declare const EMPTY_SCHEMA_DEF: SchemaTypeDef;
94
97
  export declare const aggregationFieldDoesNotExist: (def: QueryDef, field: string) => void;
95
98
  export declare const aggregationFieldNotNumber: (def: QueryDef, field: string) => void;
96
99
  export declare const validateStepRange: (def: QueryDef, step: StepInput) => void;
100
+ export declare const edgeNotImplemented: (def: QueryDef, field: string) => void;
97
101
  export {};
@@ -32,6 +32,7 @@ export const ERR_AGG_ENOENT = 25;
32
32
  export const ERR_AGG_TYPE = 26;
33
33
  export const ERR_AGG_INVALID_STEP_TYPE = 27;
34
34
  export const ERR_AGG_INVALID_STEP_RANGE = 28;
35
+ export const ERR_AGG_NOT_IMPLEMENTED = 29;
35
36
  const messages = {
36
37
  [ERR_TARGET_INVAL_TYPE]: (p) => `Type "${p}" does not exist`,
37
38
  [ERR_TARGET_INVAL_ALIAS]: (p) => {
@@ -53,7 +54,7 @@ const messages = {
53
54
  return `Filter: Invalid value ${p[0]} ${operatorReverseMap[p[1].operation]} "${safeStringify(p[2])}"`;
54
55
  },
55
56
  [ERR_SORT_ENOENT]: (p) => `Sort: field does not exist "${p}"`,
56
- [ERR_SORT_WRONG_TARGET]: (p) => `Sort: incorrect qeury target "${displayTarget(p)}"`,
57
+ [ERR_SORT_WRONG_TARGET]: (p) => `Sort: incorrect query target "${displayTarget(p)}"`,
57
58
  [ERR_SORT_ORDER]: (p) => `Sort: incorrect order option "${safeStringify(p.order)}" passed to sort "${p.field}"`,
58
59
  [ERR_SORT_TYPE]: (p) => `Sort: cannot sort on type "${REVERSE_TYPE_INDEX_MAP[p.typeIndex]}" on field "${p.path.join('.')}"`,
59
60
  [ERR_RANGE_INVALID_OFFSET]: (p) => `Range: incorrect start "${safeStringify(p)}"`,
@@ -67,6 +68,7 @@ const messages = {
67
68
  [ERR_AGG_TYPE]: (p) => `Aggregate: incorrect type "${p.path.join('.')}"`,
68
69
  [ERR_AGG_INVALID_STEP_TYPE]: (p) => `Aggregate: Incorrect step type "${p}"`,
69
70
  [ERR_AGG_INVALID_STEP_RANGE]: (p) => `Aggregate: Incorrect step range "${p}". Step ranges are limited to uint32 max value in seconds => group by ~136 years.`,
71
+ [ERR_AGG_NOT_IMPLEMENTED]: (p) => `Aggregate: Can't aggregate, feature not implemented yet. Prop: "${p}".`,
70
72
  };
71
73
  export const searchIncorrecQueryValue = (def, payload) => {
72
74
  def.errors.push({ code: ERR_SEARCH_INCORRECT_VALUE, payload });
@@ -112,13 +114,13 @@ export const validateVal = (def, f, validate) => {
112
114
  const value = f[2];
113
115
  if (Array.isArray(value)) {
114
116
  for (const v of value) {
115
- if (!validate(v, f[2])) {
117
+ if (validate(v, f[2].schema) !== true) {
116
118
  def.errors.push({ code: ERR_FILTER_INVALID_VAL, payload: f });
117
119
  return true;
118
120
  }
119
121
  }
120
122
  }
121
- else if (!validate(value, f[2])) {
123
+ else if (validate(value, f[2].schema) !== true) {
122
124
  def.errors.push({
123
125
  code: ERR_FILTER_INVALID_VAL,
124
126
  payload: f,
@@ -447,6 +449,7 @@ export const handleErrors = (def) => {
447
449
  }
448
450
  };
449
451
  export const EMPTY_ALIAS_PROP_DEF = {
452
+ schema: null,
450
453
  prop: 1,
451
454
  typeIndex: ALIAS,
452
455
  __isPropDef: true,
@@ -458,6 +461,7 @@ export const EMPTY_ALIAS_PROP_DEF = {
458
461
  path: ['ERROR_ALIAS'],
459
462
  };
460
463
  export const ERROR_STRING = {
464
+ schema: null,
461
465
  prop: 1,
462
466
  typeIndex: STRING,
463
467
  __isPropDef: true,
@@ -469,6 +473,7 @@ export const ERROR_STRING = {
469
473
  path: ['ERROR_STRING'],
470
474
  };
471
475
  export const ERROR_VECTOR = {
476
+ schema: null,
472
477
  prop: 1,
473
478
  typeIndex: VECTOR,
474
479
  __isPropDef: true,
@@ -510,4 +515,11 @@ export const validateStepRange = (def, step) => {
510
515
  handleErrors(def);
511
516
  }
512
517
  };
518
+ export const edgeNotImplemented = (def, field) => {
519
+ def.errors.push({
520
+ code: ERR_AGG_NOT_IMPLEMENTED,
521
+ payload: field,
522
+ });
523
+ handleErrors(def);
524
+ };
513
525
  //# sourceMappingURL=validation.js.map
@@ -36,7 +36,7 @@ export const write = (ctx, value, offset, noCompression, lang) => {
36
36
  }
37
37
  else {
38
38
  buf[offset + 1] = NOT_COMPRESSED;
39
- writeUint32(buf, crc, offset + l + 2);
39
+ writeUint32(buf, crc, offset + 2 + l);
40
40
  return l + 6;
41
41
  }
42
42
  };
package/dist/src/hooks.js CHANGED
@@ -1,35 +1,8 @@
1
- import picocolors from 'picocolors';
2
- import { displayTarget } from './client/query/display.js';
1
+ import { registerSubscription } from './server/subscription.js';
3
2
  export const getDefaultHooks = (server, subInterval = 200) => {
4
3
  return {
5
4
  subscribe(q, onData, onError) {
6
- let timer;
7
- let killed = false;
8
- const poll = async () => {
9
- const res = await server.getQueryBuf(q.buffer);
10
- if (killed) {
11
- return;
12
- }
13
- if (res.byteLength >= 4) {
14
- onData(res);
15
- }
16
- else if (res.byteLength === 1 && res[0] === 0) {
17
- server.emit('info', `[${displayTarget(q.def)}] Subscribe schema mismatch - should resolve after update`);
18
- return;
19
- }
20
- else {
21
- const def = q.def;
22
- let name = picocolors.red(`QueryError[${displayTarget(def)}]\n`);
23
- name += ` Incorrect buffer received in subscription (maybe server not started ${res.byteLength}) bytes\n`;
24
- onError(new Error(name));
25
- }
26
- timer = setTimeout(poll, subInterval);
27
- };
28
- void poll();
29
- return () => {
30
- clearTimeout(timer);
31
- killed = true;
32
- };
5
+ return registerSubscription(server, q.buffer, q.subscriptionBuffer, onData, onError, subInterval);
33
6
  },
34
7
  setSchema(schema, transformFns) {
35
8
  return server.setSchema(schema, transformFns);
@@ -15,6 +15,7 @@ export * from './client/query/BasedDbQuery.js';
15
15
  export * from './client/query/BasedQueryResponse.js';
16
16
  export * from './server/save.js';
17
17
  export * from './hooks.js';
18
+ export * from './server/subscription.js';
18
19
  export { getDefaultHooks, BasedDbOpts };
19
20
  export declare class BasedDb extends Emitter {
20
21
  client: DbClient;
package/dist/src/index.js CHANGED
@@ -16,6 +16,7 @@ export * from './client/query/BasedDbQuery.js';
16
16
  export * from './client/query/BasedQueryResponse.js';
17
17
  export * from './server/save.js';
18
18
  export * from './hooks.js';
19
+ export * from './server/subscription.js';
19
20
  export { getDefaultHooks };
20
21
  export class BasedDb extends Emitter {
21
22
  client;
@@ -1,4 +1,10 @@
1
1
  declare const native: {
2
+ addMultiSubscription: (dbCtx: any, typeId: number) => void;
3
+ removeMultiSubscription: (dbCtx: any, typeId: number) => void;
4
+ removeIdSubscription: (dbCtx: any, value: Uint8Array) => void;
5
+ addIdSubscription: (dbCtx: any, value: Uint8Array) => void;
6
+ getMarkedIdSubscriptions: (dbCtx: any) => ArrayBuffer | null;
7
+ getMarkedMultiSubscriptions: (dbCtx: any) => ArrayBuffer | null;
2
8
  getThreadId: () => BigInt;
3
9
  createThreadCtx: (dbCtx: any, threadId: BigInt) => void;
4
10
  destroyThreadCtx: (dbCtx: any, threadId: BigInt) => void;
@@ -13,7 +19,7 @@ declare const native: {
13
19
  loadCommon: (path: string, dbCtx: any) => void;
14
20
  loadBlock: (path: string, dbCtx: any) => void;
15
21
  delBlock: (dbCtx: any, typeId: number, block: number) => void;
16
- setSchemaType: (prefix: number, buf: Uint8Array, dbCtx: any) => any;
22
+ setSchemaType: (dbCtx: any, prefix: number, buf: Uint8Array) => any;
17
23
  setSchemaIds: (ids: Uint32Array, dbCtx: any) => any;
18
24
  getSchemaIds: (dbCtx: any) => Uint32Array;
19
25
  getNodeRangeHash: (typeId: number, start: number, end: number, bufOut: Uint8Array, dbCtx: any) => any;
@@ -29,6 +35,7 @@ declare const native: {
29
35
  membarSyncRead: () => void;
30
36
  membarSyncWrite: () => void;
31
37
  selvaStrerror: (err: number) => any;
38
+ selvaLangAll: () => string;
32
39
  colvecTest: (dbCtx: any, typeId: number, field: number, nodeId: number, len: number) => any;
33
40
  };
34
41
  export default native;
@@ -10,6 +10,24 @@ function SelvaIoErrlogToString(buf) {
10
10
  return DECODER.decode(selvaIoErrlog.slice(0, len));
11
11
  }
12
12
  const native = {
13
+ addMultiSubscription: (dbCtx, typeId) => {
14
+ return db.addMultiSubscription(dbCtx, typeId);
15
+ },
16
+ removeMultiSubscription: (dbCtx, typeId) => {
17
+ return db.removeMultiSubscription(dbCtx, typeId);
18
+ },
19
+ removeIdSubscription: (dbCtx, value) => {
20
+ return db.removeIdSubscription(dbCtx, value);
21
+ },
22
+ addIdSubscription: (dbCtx, value) => {
23
+ return db.addIdSubscription(dbCtx, value);
24
+ },
25
+ getMarkedIdSubscriptions: (dbCtx) => {
26
+ return db.getMarkedIdSubscriptions(dbCtx);
27
+ },
28
+ getMarkedMultiSubscriptions: (dbCtx) => {
29
+ return db.getMarkedMultiSubscriptions(dbCtx);
30
+ },
13
31
  getThreadId: () => {
14
32
  return db.getThreadId();
15
33
  },
@@ -63,8 +81,8 @@ const native = {
63
81
  delBlock: (dbCtx, typeId, block) => {
64
82
  db.delBlock(dbCtx, typeId, block);
65
83
  },
66
- setSchemaType: (prefix, buf, dbCtx) => {
67
- return db.setSchemaType(prefix, buf, dbCtx);
84
+ setSchemaType: (dbCtx, prefix, buf) => {
85
+ return db.setSchemaType(dbCtx, prefix, buf);
68
86
  },
69
87
  setSchemaIds: (ids, dbCtx) => {
70
88
  return db.setSchemaIds(ids, dbCtx);
@@ -111,6 +129,9 @@ const native = {
111
129
  selvaStrerror: (err) => {
112
130
  return db.selvaStrerror(err);
113
131
  },
132
+ selvaLangAll: () => {
133
+ return db.selvaLangAll();
134
+ },
114
135
  colvecTest: (dbCtx, typeId, field, nodeId, len) => {
115
136
  return db.colvecTest(dbCtx, typeId, field, nodeId, len);
116
137
  },
@@ -5,6 +5,7 @@ import exitHook from 'exit-hook';
5
5
  import { IoWorker } from './IoWorker.js';
6
6
  import { QueryWorker } from './QueryWorker.js';
7
7
  import { DbShared } from '../shared/DbBase.js';
8
+ import { Subscriptions } from './subscription.js';
8
9
  declare class SortIndex {
9
10
  constructor(buf: Uint8Array, dbCtxExternal: any);
10
11
  buf: Uint8Array;
@@ -15,6 +16,7 @@ export declare class DbServer extends DbShared {
15
16
  #private;
16
17
  modifyDirtyRanges: Float64Array;
17
18
  dbCtxExternal: any;
19
+ subscriptions: Subscriptions;
18
20
  migrating: number;
19
21
  saveInProgress: boolean;
20
22
  fileSystemPath: string;