@based/db 0.1.4 → 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 (121) 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/migrate/worker.js +87 -84
  115. package/dist/src/server/schema.js +3 -2
  116. package/dist/src/server/schemaSelvaBuffer.d.ts +4 -0
  117. package/dist/src/server/schemaSelvaBuffer.js +182 -0
  118. package/dist/src/server/subscription.d.ts +32 -0
  119. package/dist/src/server/subscription.js +275 -0
  120. package/dist/src/utils.js +2 -3
  121. package/package.json +5 -6
@@ -0,0 +1,275 @@
1
+ import { readInt64, readUint16, readUint32, writeInt64 } from '@based/utils';
2
+ import { SubscriptionType, } from '../client/query/subscription/types.js';
3
+ import native from '../native.js';
4
+ import { MAX_ID } from '@based/schema';
5
+ import { styleText } from 'util';
6
+ export const startUpdateHandler = (server) => {
7
+ // skip next if queries are sitll in progress can add a number for each staged sub
8
+ // combine this with handled modify
9
+ const scheduleUpdate = () => {
10
+ if (server.stopped) {
11
+ return;
12
+ }
13
+ server.subscriptions.updateId++;
14
+ if (server.subscriptions.updateId > MAX_ID) {
15
+ server.subscriptions.updateId = 1;
16
+ }
17
+ // can do seperate timing for id / type
18
+ // scince multi queries are much heavier ofc
19
+ const markedIdSubs = native.getMarkedIdSubscriptions(server.dbCtxExternal);
20
+ if (markedIdSubs) {
21
+ const buffer = new Uint8Array(markedIdSubs);
22
+ for (let i = 0; i < buffer.byteLength; i += 8) {
23
+ const subId = readUint32(buffer, i);
24
+ const id = readUint32(buffer, i + 4);
25
+ const subContainer = server.subscriptions.ids.get(subId);
26
+ const ids = subContainer.ids.get(id);
27
+ if (ids) {
28
+ for (const fn of ids) {
29
+ fn();
30
+ }
31
+ }
32
+ }
33
+ }
34
+ const markedMultiSubs = native.getMarkedMultiSubscriptions(server.dbCtxExternal);
35
+ if (markedMultiSubs) {
36
+ const buffer = new Uint8Array(markedMultiSubs);
37
+ for (let i = 0; i < buffer.byteLength; i += 2) {
38
+ const typeId = readUint16(buffer, i);
39
+ const subs = server.subscriptions.fullType.get(typeId);
40
+ if (subs) {
41
+ for (const fn of subs.listeners) {
42
+ fn();
43
+ }
44
+ }
45
+ }
46
+ }
47
+ if (server.subscriptions.updateId - server.subscriptions.now.lastUpdated >
48
+ Math.max(1000 / server.subscriptions.subInterval, 1) // 1 time per second
49
+ ) {
50
+ server.subscriptions.now.lastUpdated = server.subscriptions.updateId;
51
+ for (const fn of server.subscriptions.now.listeners) {
52
+ fn();
53
+ }
54
+ }
55
+ server.subscriptions.updateHandler = setTimeout(scheduleUpdate, server.subscriptions.subInterval);
56
+ };
57
+ server.subscriptions.updateHandler = setTimeout(scheduleUpdate, server.subscriptions.subInterval);
58
+ };
59
+ const addToMultiSub = (server, typeId, runQuery) => {
60
+ let fullType;
61
+ let listeners;
62
+ if (!server.subscriptions.fullType.has(typeId)) {
63
+ listeners = new Set();
64
+ fullType = {
65
+ listeners,
66
+ };
67
+ server.subscriptions.fullType.set(typeId, fullType);
68
+ native.addMultiSubscription(server.dbCtxExternal, typeId);
69
+ }
70
+ else {
71
+ fullType = server.subscriptions.fullType.get(typeId);
72
+ listeners = fullType.listeners;
73
+ }
74
+ listeners.add(runQuery);
75
+ };
76
+ const removeFromMultiSub = (server, typeId, runQuery) => {
77
+ const typeSub = server.subscriptions.fullType.get(typeId);
78
+ if (!typeSub) {
79
+ return;
80
+ }
81
+ typeSub.listeners.delete(runQuery);
82
+ if (typeSub.listeners.size === 0) {
83
+ native.removeMultiSubscription(server.dbCtxExternal, typeId);
84
+ server.subscriptions.fullType.delete(typeId);
85
+ }
86
+ };
87
+ const replaceNowValues = (query, now) => {
88
+ const dateNow = Date.now();
89
+ for (let i = 0; i < now.byteLength; i += 16) {
90
+ const offset = readInt64(now, i + 4);
91
+ const byteIndex = readUint32(now, i + 12);
92
+ writeInt64(query, dateNow + offset, byteIndex);
93
+ }
94
+ };
95
+ export const registerSubscription = (server, query, sub, onData, onError, subInterval) => {
96
+ // this can change dynamicly
97
+ if (subInterval) {
98
+ server.subscriptions.subInterval = subInterval;
99
+ }
100
+ let killed = false;
101
+ // now maybe just once per second? (for now)
102
+ if (server.subscriptions.active === 0) {
103
+ startUpdateHandler(server);
104
+ }
105
+ let lastUpdated = 0;
106
+ let now;
107
+ const runQuery = () => {
108
+ if (lastUpdated !== server.subscriptions.updateId) {
109
+ lastUpdated = server.subscriptions.updateId;
110
+ if (now) {
111
+ replaceNowValues(query, now);
112
+ }
113
+ server.getQueryBuf(query).then((res) => {
114
+ if (killed) {
115
+ return;
116
+ }
117
+ if (res.byteLength >= 4) {
118
+ onData(res);
119
+ }
120
+ else if (res.byteLength === 1 && res[0] === 0) {
121
+ server.emit('info', `Subscribe schema mismatch - should resolve after update`);
122
+ return;
123
+ }
124
+ else {
125
+ let name = styleText('red', `QueryError[]\n`);
126
+ name += ` Incorrect buffer received in subscription (maybe server not started ${res.byteLength}) bytes\n`;
127
+ onError(new Error(name));
128
+ }
129
+ });
130
+ }
131
+ else {
132
+ console.log('Allready fired block');
133
+ }
134
+ };
135
+ server.subscriptions.active++;
136
+ if (sub[0] === SubscriptionType.singleId) {
137
+ const subId = readUint32(sub, 1);
138
+ const id = readUint32(sub, 7);
139
+ const headerLen = 16;
140
+ let subContainer;
141
+ let listeners;
142
+ if (!server.subscriptions.ids.get(subId)) {
143
+ subContainer = { ids: new Map() };
144
+ server.subscriptions.ids.set(subId, subContainer);
145
+ const typesLen = readUint16(sub, 12);
146
+ const nowLen = readUint16(sub, 14);
147
+ const fLen = sub[11];
148
+ if (typesLen != 0) {
149
+ // double check if this is alignment correct with the byteOffset else copy
150
+ const byteOffset = sub.byteOffset + headerLen + fLen;
151
+ if (byteOffset % 2 === 0) {
152
+ subContainer.types = new Uint16Array(sub.buffer, byteOffset, typesLen);
153
+ }
154
+ else {
155
+ subContainer.types = new Uint16Array(sub.slice(headerLen + fLen, headerLen + fLen + typesLen * 2));
156
+ }
157
+ subContainer.typesListener = () => {
158
+ for (const set of subContainer.ids.values()) {
159
+ for (const fn of set) {
160
+ fn();
161
+ }
162
+ }
163
+ };
164
+ for (const typeId of subContainer.types) {
165
+ addToMultiSub(server, typeId, subContainer.typesListener);
166
+ }
167
+ }
168
+ if (nowLen != 0) {
169
+ // when this is the case do a completely different strategy
170
+ // keep track of last update on sub id container
171
+ // and get the date allways (as a seperate query)
172
+ // when getting the date mark next in line
173
+ // have to make a copy (subArray is weak)
174
+ now = sub.slice(headerLen + fLen + typesLen * 2);
175
+ subContainer.nowListener = () => {
176
+ // per id want to have a last eval and needs next eval
177
+ for (const set of subContainer.ids.values()) {
178
+ for (const fn of set) {
179
+ fn();
180
+ }
181
+ }
182
+ };
183
+ server.subscriptions.now.listeners.add(subContainer.nowListener);
184
+ }
185
+ }
186
+ else {
187
+ subContainer = server.subscriptions.ids.get(subId);
188
+ }
189
+ if (!subContainer.ids.has(id)) {
190
+ listeners = new Set();
191
+ subContainer.ids.set(id, listeners);
192
+ native.addIdSubscription(server.dbCtxExternal, sub);
193
+ }
194
+ else {
195
+ listeners = subContainer.ids.get(id);
196
+ }
197
+ listeners.add(runQuery);
198
+ process.nextTick(() => {
199
+ runQuery();
200
+ });
201
+ return () => {
202
+ killed = true;
203
+ listeners.delete(runQuery);
204
+ if (listeners.size === 0) {
205
+ native.removeIdSubscription(server.dbCtxExternal, sub);
206
+ subContainer.ids.delete(id);
207
+ }
208
+ if (subContainer.ids.size === 0) {
209
+ if (subContainer.types) {
210
+ for (const typeId of subContainer.types) {
211
+ removeFromMultiSub(server, typeId, subContainer.typesListener);
212
+ }
213
+ }
214
+ if (now) {
215
+ server.subscriptions.now.listeners.delete(subContainer.nowListener);
216
+ }
217
+ server.subscriptions.ids.delete(subId);
218
+ }
219
+ server.subscriptions.active--;
220
+ if (server.subscriptions.active === 0) {
221
+ clearTimeout(server.subscriptions.updateHandler);
222
+ server.subscriptions.updateHandler = null;
223
+ }
224
+ };
225
+ }
226
+ else if (sub[0] === SubscriptionType.fullType) {
227
+ const headerLen = 8;
228
+ const typeId = readUint16(sub, 1);
229
+ addToMultiSub(server, typeId, runQuery);
230
+ const typesLen = readUint16(sub, 3);
231
+ let types;
232
+ if (typesLen != 0) {
233
+ // double check if this is alignment correct with the byteOffset else copy
234
+ const byteOffset = sub.byteOffset + headerLen;
235
+ if (byteOffset % 2 === 0) {
236
+ types = new Uint16Array(sub.buffer, byteOffset, typesLen);
237
+ }
238
+ else {
239
+ types = new Uint16Array(sub.slice(headerLen, headerLen + typesLen * 2));
240
+ }
241
+ for (const typeId of types) {
242
+ addToMultiSub(server, typeId, runQuery);
243
+ }
244
+ }
245
+ if (readUint16(sub, 5) != 0) {
246
+ // have to make a copy (subArray is weak)
247
+ now = sub.slice(headerLen + typesLen * 2);
248
+ server.subscriptions.now.listeners.add(runQuery);
249
+ }
250
+ process.nextTick(() => {
251
+ runQuery();
252
+ });
253
+ return () => {
254
+ killed = true;
255
+ if (now) {
256
+ server.subscriptions.now.listeners.delete(runQuery);
257
+ }
258
+ removeFromMultiSub(server, typeId, runQuery);
259
+ if (types) {
260
+ for (const typeId of types) {
261
+ removeFromMultiSub(server, typeId, runQuery);
262
+ }
263
+ }
264
+ server.subscriptions.active--;
265
+ if (server.subscriptions.active === 0) {
266
+ clearTimeout(server.subscriptions.updateHandler);
267
+ server.subscriptions.updateHandler = null;
268
+ }
269
+ };
270
+ }
271
+ else {
272
+ throw new Error('Unhandled subscription!');
273
+ }
274
+ };
275
+ //# sourceMappingURL=subscription.js.map
package/dist/src/utils.js CHANGED
@@ -1,5 +1,4 @@
1
- import { inspect } from 'node:util';
2
- import picocolors from 'picocolors';
1
+ import { inspect, styleText } from 'node:util';
3
2
  export const debugMode = (target, getInfo = null) => {
4
3
  const opts = { showHidden: false, depth: null, colors: true };
5
4
  const info = (v) => (typeof v === 'object' ? inspect(v, opts) : v);
@@ -29,7 +28,7 @@ export const debugMode = (target, getInfo = null) => {
29
28
  let cnt = 0;
30
29
  const color = colors[colorKey++ % colors.length];
31
30
  target[key] = function () {
32
- const arr = [picocolors[color](`[${key}:${++cnt}]`)];
31
+ const arr = [styleText(color, `[${key}:${++cnt}]`)];
33
32
  const add = getInfo?.(key);
34
33
  if (add)
35
34
  arr.push(add);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.1.4",
3
+ "version": "0.2.0",
4
4
  "type": "module",
5
5
  "main": "./dist/src/index.js",
6
6
  "scripts": {
@@ -42,11 +42,10 @@
42
42
  ],
43
43
  "dependencies": {
44
44
  "@based/hash": "1.1.0",
45
- "@based/schema": "5.0.4",
46
- "@based/utils": "1.1.1",
47
- "@based/protocol": "0.0.5",
48
- "exit-hook": "^4.0.0",
49
- "picocolors": "^1.1.0"
45
+ "@based/schema": "5.1.0",
46
+ "@based/utils": "1.2.0",
47
+ "@based/protocol": "0.1.0",
48
+ "exit-hook": "^4.0.0"
50
49
  },
51
50
  "optionalDependencies": {
52
51
  "@based/locale-x86-64-gnu": "*"