@based/db 0.0.7 → 0.0.9

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 (133) hide show
  1. package/README.md +425 -8
  2. package/dist/lib/darwin_aarch64/include/selva/db.h +12 -3
  3. package/dist/lib/darwin_aarch64/include/selva/fields.h +22 -11
  4. package/dist/lib/darwin_aarch64/include/selva/history.h +49 -0
  5. package/dist/lib/darwin_aarch64/include/selva/hll.h +21 -0
  6. package/dist/lib/darwin_aarch64/include/selva/sort.h +14 -0
  7. package/dist/lib/darwin_aarch64/include/selva/types.h +9 -2
  8. package/dist/lib/darwin_aarch64/include/selva/vector.h +22 -1
  9. package/dist/lib/darwin_aarch64/include/selva/xxhash64.h +23 -0
  10. package/dist/lib/darwin_aarch64/libnode-v20.11.1.node +0 -0
  11. package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
  12. package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
  13. package/dist/lib/darwin_aarch64/libnode-v22.8.0.node +0 -0
  14. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  15. package/dist/lib/linux_aarch64/include/selva/db.h +12 -3
  16. package/dist/lib/linux_aarch64/include/selva/fields.h +22 -11
  17. package/dist/lib/linux_aarch64/include/selva/history.h +49 -0
  18. package/dist/lib/linux_aarch64/include/selva/hll.h +21 -0
  19. package/dist/lib/linux_aarch64/include/selva/sort.h +14 -0
  20. package/dist/lib/linux_aarch64/include/selva/types.h +9 -2
  21. package/dist/lib/linux_aarch64/include/selva/vector.h +22 -1
  22. package/dist/lib/linux_aarch64/include/selva/xxhash64.h +23 -0
  23. package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
  24. package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
  25. package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
  26. package/dist/lib/linux_aarch64/libselva.so +0 -0
  27. package/dist/lib/linux_x86_64/include/selva/db.h +12 -3
  28. package/dist/lib/linux_x86_64/include/selva/fields.h +22 -11
  29. package/dist/lib/linux_x86_64/include/selva/history.h +49 -0
  30. package/dist/lib/linux_x86_64/include/selva/hll.h +21 -0
  31. package/dist/lib/linux_x86_64/include/selva/sort.h +14 -0
  32. package/dist/lib/linux_x86_64/include/selva/types.h +9 -2
  33. package/dist/lib/linux_x86_64/include/selva/vector.h +22 -1
  34. package/dist/lib/linux_x86_64/include/selva/xxhash64.h +23 -0
  35. package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
  36. package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
  37. package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
  38. package/dist/lib/linux_x86_64/libselva.so +0 -0
  39. package/dist/src/client/bitWise.js +21 -1
  40. package/dist/src/client/index.d.ts +11 -5
  41. package/dist/src/client/index.js +57 -11
  42. package/dist/src/client/modify/ModifyRes.d.ts +4 -1
  43. package/dist/src/client/modify/ModifyRes.js +8 -1
  44. package/dist/src/client/modify/alias.js +3 -3
  45. package/dist/src/client/modify/binary.js +5 -2
  46. package/dist/src/client/modify/cardinality.d.ts +4 -0
  47. package/dist/src/client/modify/cardinality.js +50 -0
  48. package/dist/src/client/modify/create copy.d.ts +5 -0
  49. package/dist/src/client/modify/create copy.js +112 -0
  50. package/dist/src/client/modify/create.d.ts +2 -1
  51. package/dist/src/client/modify/create.js +11 -7
  52. package/dist/src/client/modify/delete.d.ts +2 -0
  53. package/dist/src/client/modify/delete.js +37 -0
  54. package/dist/src/client/modify/expire.d.ts +3 -0
  55. package/dist/src/client/modify/expire.js +25 -0
  56. package/dist/src/client/modify/fixed.js +11 -1
  57. package/dist/src/client/modify/index.d.ts +1 -1
  58. package/dist/src/client/modify/index.js +1 -1
  59. package/dist/src/client/modify/json.d.ts +4 -0
  60. package/dist/src/client/modify/json.js +5 -0
  61. package/dist/src/client/modify/modify.js +11 -7
  62. package/dist/src/client/modify/references/edge.js +21 -6
  63. package/dist/src/client/modify/references/reference.js +2 -2
  64. package/dist/src/client/modify/references/references.d.ts +0 -1
  65. package/dist/src/client/modify/references/references.js +4 -4
  66. package/dist/src/client/modify/remove.d.ts +1 -2
  67. package/dist/src/client/modify/remove.js +9 -6
  68. package/dist/src/client/modify/setCursor.d.ts +1 -1
  69. package/dist/src/client/modify/setCursor.js +4 -1
  70. package/dist/src/client/modify/string.js +2 -2
  71. package/dist/src/client/modify/text.d.ts +2 -1
  72. package/dist/src/client/modify/text.js +13 -7
  73. package/dist/src/client/modify/types.d.ts +8 -1
  74. package/dist/src/client/modify/types.js +1 -0
  75. package/dist/src/client/modify/update.d.ts +2 -1
  76. package/dist/src/client/modify/update.js +9 -5
  77. package/dist/src/client/modify/upsert.d.ts +2 -1
  78. package/dist/src/client/modify/upsert.js +3 -3
  79. package/dist/src/client/modify/vector copy.d.ts +4 -0
  80. package/dist/src/client/modify/vector copy.js +46 -0
  81. package/dist/src/client/modify/vector.js +6 -4
  82. package/dist/src/client/query/BasedDbQuery.d.ts +4 -3
  83. package/dist/src/client/query/BasedDbQuery.js +39 -16
  84. package/dist/src/client/query/BasedIterable.js +3 -3
  85. package/dist/src/client/query/filter/FilterBranch.d.ts +2 -2
  86. package/dist/src/client/query/filter/FilterBranch.js +2 -2
  87. package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +3 -2
  88. package/dist/src/client/query/filter/createFixedFilterBuffer.js +14 -11
  89. package/dist/src/client/query/filter/createReferenceFilter.d.ts +2 -1
  90. package/dist/src/client/query/filter/createReferenceFilter.js +6 -5
  91. package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -1
  92. package/dist/src/client/query/filter/createVariableFilterBuffer.js +61 -30
  93. package/dist/src/client/query/filter/filter.d.ts +2 -2
  94. package/dist/src/client/query/filter/filter.js +27 -22
  95. package/dist/src/client/query/filter/parseFilterValue.js +9 -64
  96. package/dist/src/client/query/filter/primitiveFilter.js +7 -11
  97. package/dist/src/client/query/filter/toBuffer.js +5 -7
  98. package/dist/src/client/query/filter/types.d.ts +51 -2
  99. package/dist/src/client/query/filter/types.js +114 -0
  100. package/dist/src/client/query/include/props.d.ts +2 -0
  101. package/dist/src/client/query/include/props.js +25 -6
  102. package/dist/src/client/query/include/toBuffer.js +21 -1
  103. package/dist/src/client/query/include/walk.js +17 -2
  104. package/dist/src/client/query/queryDef.js +1 -0
  105. package/dist/src/client/query/read/read.js +85 -21
  106. package/dist/src/client/query/search/index.d.ts +2 -0
  107. package/dist/src/client/query/search/index.js +79 -23
  108. package/dist/src/client/query/subscription/index.js +2 -2
  109. package/dist/src/client/query/subscription/markers.d.ts +1 -1
  110. package/dist/src/client/query/subscription/markers.js +2 -2
  111. package/dist/src/client/query/toBuffer.js +0 -4
  112. package/dist/src/client/query/types.d.ts +10 -0
  113. package/dist/src/client/query/validation.d.ts +3 -2
  114. package/dist/src/client/query/validation.js +17 -2
  115. package/dist/src/client/timestamp.d.ts +1 -0
  116. package/dist/src/client/timestamp.js +68 -0
  117. package/dist/src/client/xxHash64.d.ts +1 -0
  118. package/dist/src/client/xxHash64.js +5 -0
  119. package/dist/src/index.d.ts +4 -1
  120. package/dist/src/index.js +13 -3
  121. package/dist/src/native.d.ts +1 -0
  122. package/dist/src/native.js +4 -1
  123. package/dist/src/server/csmt/tree.js +12 -2
  124. package/dist/src/server/index.d.ts +12 -4
  125. package/dist/src/server/index.js +63 -17
  126. package/dist/src/server/migrate/index.js +6 -2
  127. package/dist/src/server/migrate/worker.js +3 -3
  128. package/dist/src/server/schema/selvaBuffer.js +20 -11
  129. package/dist/src/server/schema/typeDef.d.ts +2 -2
  130. package/dist/src/server/schema/typeDef.js +14 -5
  131. package/dist/src/server/schema/types.d.ts +7 -2
  132. package/dist/src/server/schema/types.js +6 -3
  133. package/package.json +1 -1
@@ -4,11 +4,32 @@
4
4
  */
5
5
  #pragma once
6
6
 
7
+ #include "selva/_export.h"
8
+
7
9
  typedef float v2f __attribute__((vector_size(2 * sizeof(float))));
8
10
  typedef float v4f __attribute__((vector_size(4 * sizeof(float))));
9
11
  typedef float v8f __attribute__((vector_size(8 * sizeof(float))));
10
12
 
13
+ /**
14
+ * Calculate the dot product of two vectors of length len.
15
+ */
16
+ SELVA_EXPORT
17
+ float vector_dot(const float *a, const float *b, size_t len);
18
+
19
+ /**
20
+ * Calculate the Manhattan distance of two vectors of length len.
21
+ */
22
+ SELVA_EXPORT
23
+ float vector_l1(const float *a, const float *b, size_t len);
24
+
25
+ /**
26
+ * Calculate the squared Euclidean distance of two vectors of length len.
27
+ */
28
+ SELVA_EXPORT
29
+ float vector_l2s(const float *a, const float *b, size_t len);
30
+
11
31
  /**
12
32
  * Calculate the cosine similarity of two vectors of length len.
13
33
  */
14
- float vector_sc(float *a, float *b, size_t len);
34
+ SELVA_EXPORT
35
+ float vector_sc(const float *a, const float *b, size_t len);
@@ -0,0 +1,23 @@
1
+ #pragma once
2
+ #ifndef XXHASH64_H
3
+ #define XXHASH64_H
4
+
5
+ #include <stdlib.h>
6
+ #include <stdint.h>
7
+ #include "selva/_export.h"
8
+ #include "cdefs.h"
9
+
10
+
11
+ /**
12
+ * @brief Wrapper function that computes the 64-bit hash of a given input string using the xxHash algorithm.
13
+ *
14
+ * Takes a string as input and returns a 64-bit hash value.
15
+ *
16
+ * @param s The input string to be hashed.
17
+ * @return A 64-bit hash value of the input string.
18
+ */
19
+
20
+ SELVA_EXPORT
21
+ uint64_t xxHash64(const char *s, size_t len);
22
+
23
+ #endif
Binary file
@@ -28,7 +28,27 @@ export const readDoubleLE = (val, offset) => {
28
28
  return sign * fraction * 2 ** (exponent - 1075);
29
29
  };
30
30
  export const readFloatLE = (val, offset) => {
31
- return Buffer.from(val.buffer).readFloatLE(offset);
31
+ const bits = val[offset] |
32
+ (val[offset + 1] << 8) |
33
+ (val[offset + 2] << 16) |
34
+ (val[offset + 3] << 24);
35
+ const sign = bits >>> 31 ? -1 : 1;
36
+ let exponent = (bits >>> 23) & 0xff;
37
+ let fraction = bits & 0x7fffff;
38
+ if (exponent === 0xff) {
39
+ if (fraction === 0)
40
+ return sign * Infinity;
41
+ return NaN;
42
+ }
43
+ if (exponent === 0) {
44
+ if (fraction === 0)
45
+ return sign * 0;
46
+ exponent = 1;
47
+ }
48
+ else {
49
+ fraction |= 0x800000;
50
+ }
51
+ return sign * fraction * 2 ** (exponent - 150);
32
52
  };
33
53
  export const readUint32 = (val, offset) => {
34
54
  return ((val[offset] |
@@ -7,6 +7,7 @@ import { BasedDbQuery, QueryByAliasObj } from './query/BasedDbQuery.js';
7
7
  import { ModifyRes } from './modify/ModifyRes.js';
8
8
  import { DbServer } from '../server/index.js';
9
9
  import { TransformFns } from '../server/migrate/index.js';
10
+ import { ModifyOpts } from './modify/types.js';
10
11
  export type DbClientHooks = {
11
12
  putSchema(schema: StrictSchema, fromStart?: boolean, transformFns?: TransformFns): Promise<DbServer['schema']>;
12
13
  flushModify(buf: Buffer): Promise<{
@@ -44,13 +45,18 @@ export declare class DbClient {
44
45
  putLocalSchema(schema: any): StrictSchema & {
45
46
  lastId: number;
46
47
  };
47
- create(type: string, obj: CreateObj, unsafe?: boolean): ModifyRes;
48
+ create(type: string, obj?: CreateObj, opts?: ModifyOpts): ModifyRes;
49
+ copy(type: string, target: number | ModifyRes, objOrTransformFn?: Record<string, any> | ((item: Record<string, any>) => Promise<any>)): Promise<ModifyRes>;
48
50
  query(type: string, id?: number | ModifyRes | (number | ModifyRes)[] | QueryByAliasObj | QueryByAliasObj[]): BasedDbQuery;
49
51
  query(): BasedDbQuery;
50
- update(type: string, id: number | ModifyRes, value: any, overwrite?: boolean): ModifyRes;
51
- update(value: any, overwrite?: boolean): ModifyRes;
52
- upsert(type: string, obj: Record<string, any>): Promise<any>;
53
- remove(type: string, id: number | ModifyRes): boolean;
52
+ update(type: string, id: number | ModifyRes, value: any, opts?: ModifyOpts): ModifyRes;
53
+ update(type: string, value: Record<string, any> & {
54
+ id: number | ModifyRes;
55
+ }, opts?: ModifyOpts): ModifyRes;
56
+ update(value: any, opts?: ModifyOpts): ModifyRes;
57
+ upsert(type: string, obj: Record<string, any>, opts?: ModifyOpts): Promise<any>;
58
+ delete(type: string, id: number | ModifyRes): boolean;
59
+ expire(type: string, id: number | ModifyRes, seconds: number): void;
54
60
  destroy(): void;
55
61
  stop(): void;
56
62
  drain(): Promise<number>;
@@ -5,11 +5,12 @@ import { BasedDbQuery } from './query/BasedDbQuery.js';
5
5
  import { ModifyState } from './modify/ModifyRes.js';
6
6
  import { upsert } from './modify/upsert.js';
7
7
  import { update } from './modify/update.js';
8
- import { remove } from './modify/remove.js';
8
+ import { deleteFn } from './modify/delete.js';
9
9
  import { updateTypeDefs } from '../server/schema/typeDef.js';
10
10
  import { schemaToSelvaBuffer } from '../server/schema/selvaBuffer.js';
11
11
  import { deepEqual } from '@saulx/utils';
12
12
  import { hash } from '@saulx/hash';
13
+ import { expire } from './modify/expire.js';
13
14
  export class DbClient {
14
15
  constructor({ hooks, maxModifySize = 100 * 1e3 * 1e3 }) {
15
16
  this.hooks = hooks;
@@ -57,8 +58,42 @@ export class DbClient {
57
58
  schemaToSelvaBuffer(this.schemaTypesParsed);
58
59
  return this.schema;
59
60
  }
60
- create(type, obj, unsafe) {
61
- return create(this, type, obj, unsafe);
61
+ create(type, obj = {}, opts) {
62
+ return create(this, type, obj, opts);
63
+ }
64
+ async copy(type, target, objOrTransformFn) {
65
+ const item = await this.query(type, target)
66
+ .include('*', '**.id')
67
+ .get()
68
+ .toObject();
69
+ if (typeof objOrTransformFn === 'function') {
70
+ const { id, ...props } = await objOrTransformFn(item);
71
+ return this.create(type, props);
72
+ }
73
+ if (typeof objOrTransformFn === 'object' && objOrTransformFn !== null) {
74
+ const { id, ...props } = item;
75
+ await Promise.all(Object.keys(objOrTransformFn).map(async (key) => {
76
+ const val = objOrTransformFn[key];
77
+ if (val === null) {
78
+ delete props[key];
79
+ }
80
+ else if (typeof val === 'function') {
81
+ const res = await val(item);
82
+ if (Array.isArray(res)) {
83
+ props[key] = await Promise.all(res);
84
+ }
85
+ else {
86
+ props[key] = res;
87
+ }
88
+ }
89
+ else {
90
+ props[key] = val;
91
+ }
92
+ }));
93
+ return this.create(type, props);
94
+ }
95
+ const { id, ...props } = item;
96
+ return this.create(type, props);
62
97
  }
63
98
  query(type, id) {
64
99
  if (type === undefined) {
@@ -89,19 +124,30 @@ export class DbClient {
89
124
  }
90
125
  return new BasedDbQuery(this, type, id);
91
126
  }
92
- update(typeOrValue, idOrOverwrite, value, overwrite) {
127
+ update(typeOrValue, idOverwriteOrValue, value, opts) {
93
128
  if (typeof typeOrValue === 'string') {
94
- const id = typeof idOrOverwrite === 'object' ? idOrOverwrite.tmpId : idOrOverwrite;
95
- return update(this, typeOrValue, id, value, overwrite);
129
+ if (typeof idOverwriteOrValue === 'object') {
130
+ if (idOverwriteOrValue instanceof ModifyState) {
131
+ return update(this, typeOrValue, idOverwriteOrValue.tmpId, value, opts);
132
+ }
133
+ if ('id' in idOverwriteOrValue) {
134
+ const { id, ...props } = idOverwriteOrValue;
135
+ return this.update(typeOrValue, id, props, opts);
136
+ }
137
+ }
138
+ return update(this, typeOrValue, idOverwriteOrValue, value, opts);
96
139
  }
97
140
  // else it is rootProps
98
- return update(this, '_root', 1, typeOrValue, idOrOverwrite);
141
+ return update(this, '_root', 1, typeOrValue, idOverwriteOrValue);
142
+ }
143
+ upsert(type, obj, opts) {
144
+ return upsert(this, type, obj, opts);
99
145
  }
100
- upsert(type, obj) {
101
- return upsert(this, type, obj);
146
+ delete(type, id) {
147
+ return deleteFn(this, type, typeof id === 'number' ? id : id.tmpId);
102
148
  }
103
- remove(type, id) {
104
- return remove(this, type, typeof id === 'number' ? id : id.tmpId);
149
+ expire(type, id, seconds) {
150
+ return expire(this, type, typeof id === 'number' ? id : id.tmpId, seconds);
105
151
  }
106
152
  destroy() {
107
153
  this.modifyCtx.len = 0;
@@ -2,6 +2,8 @@ import { PropDef, PropDefEdge, SchemaPropTree } from '../../server/schema/types.
2
2
  import { inspect } from 'node:util';
3
3
  import { SubscriptionMarkersCheck } from '../query/subscription/index.js';
4
4
  import { DbClient } from '../index.js';
5
+ import { ModifyOpts } from './types.js';
6
+ import { LangCode } from '@based/schema';
5
7
  export type ModifyRes = {
6
8
  tmpId: number;
7
9
  error?: Error;
@@ -14,8 +16,9 @@ export declare class ModifyError {
14
16
  }
15
17
  export declare class ModifyState {
16
18
  #private;
17
- constructor(typeId: number, tmpId: number, db: DbClient, subMarkers: SubscriptionMarkersCheck | false);
19
+ constructor(typeId: number, tmpId: number, db: DbClient, subMarkers: SubscriptionMarkersCheck | false, opts?: ModifyOpts);
18
20
  subMarkers: SubscriptionMarkersCheck | false;
21
+ i18n: LangCode;
19
22
  tmpId: number;
20
23
  error?: ModifyError;
21
24
  promises?: Promise<any>[];
@@ -1,5 +1,6 @@
1
1
  import { isPropDef, REVERSE_TYPE_INDEX_MAP, } from '../../server/schema/types.js';
2
2
  import { inspect } from 'node:util';
3
+ import { langCodesMap } from '@based/schema';
3
4
  const MAGIC_KEY = Math.random().toString(36).substring(2);
4
5
  const MAGIC_REG = RegExp(`("${MAGIC_KEY}|${MAGIC_KEY}")`, 'g');
5
6
  const walk = (val) => {
@@ -44,14 +45,20 @@ export class ModifyError {
44
45
  }
45
46
  }
46
47
  export class ModifyState {
47
- constructor(typeId, tmpId, db, subMarkers) {
48
+ constructor(typeId, tmpId, db, subMarkers, opts) {
48
49
  this.tmpId = tmpId;
49
50
  this.#typeId = typeId;
50
51
  this.#buf = db.modifyCtx;
51
52
  this.#ctx = db.modifyCtx.ctx;
52
53
  this.subMarkers = subMarkers;
54
+ if (opts) {
55
+ if (opts.i18n) {
56
+ this.i18n = langCodesMap.get(opts.i18n);
57
+ }
58
+ }
53
59
  }
54
60
  subMarkers;
61
+ i18n;
55
62
  #buf;
56
63
  #ctx;
57
64
  #typeId;
@@ -8,7 +8,7 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
8
8
  if (ctx.len + 11 > ctx.max) {
9
9
  return RANGE_ERR;
10
10
  }
11
- setCursor(ctx, def, t.prop, parentId, modifyOp);
11
+ setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
12
12
  ctx.buf[ctx.len++] = DELETE;
13
13
  }
14
14
  }
@@ -22,7 +22,7 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
22
22
  def.stringPropsCurrent[t.prop] = 2;
23
23
  ctx.hasStringField++;
24
24
  }
25
- setCursor(ctx, def, t.prop, parentId, modifyOp);
25
+ setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
26
26
  ctx.buf[ctx.len++] = modifyOp;
27
27
  ctx.buf[ctx.len++] = size;
28
28
  ctx.buf[ctx.len++] = size >>>= 8;
@@ -36,7 +36,7 @@ export function writeAlias(value, ctx, def, t, parentId, modifyOp) {
36
36
  if (ctx.len + 11 > ctx.max) {
37
37
  return RANGE_ERR;
38
38
  }
39
- setCursor(ctx, def, t.prop, parentId, modifyOp);
39
+ setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
40
40
  ctx.buf[ctx.len++] = DELETE;
41
41
  }
42
42
  }
@@ -11,6 +11,9 @@ export function getBuffer(value) {
11
11
  return Buffer.from(value.buffer);
12
12
  }
13
13
  }
14
+ else if (typeof value === 'string') {
15
+ return Buffer.from(value);
16
+ }
14
17
  }
15
18
  export function writeBinaryRaw(value, ctx) {
16
19
  let size = value.byteLength + 6;
@@ -45,7 +48,7 @@ export function writeBinary(value, ctx, schema, t, parentId, modifyOp) {
45
48
  if (ctx.len + 11 > ctx.max) {
46
49
  return RANGE_ERR;
47
50
  }
48
- setCursor(ctx, schema, t.prop, parentId, modifyOp);
51
+ setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
49
52
  ctx.buf[ctx.len++] = DELETE;
50
53
  }
51
54
  }
@@ -53,7 +56,7 @@ export function writeBinary(value, ctx, schema, t, parentId, modifyOp) {
53
56
  if (ctx.len + 15 + size > ctx.max) {
54
57
  return RANGE_ERR;
55
58
  }
56
- setCursor(ctx, schema, t.prop, parentId, modifyOp);
59
+ setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
57
60
  ctx.buf[ctx.len++] = modifyOp;
58
61
  writeBinaryRaw(value, ctx);
59
62
  }
@@ -0,0 +1,4 @@
1
+ import { ModifyCtx } from '../../index.js';
2
+ import { SchemaTypeDef, PropDef } from '../../server/schema/types.js';
3
+ import { ModifyOp, ModifyErr } from './types.js';
4
+ export declare function writeHll(value: string | null | Buffer, ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -0,0 +1,50 @@
1
+ import { RANGE_ERR } from './types.js';
2
+ import { ModifyError } from './ModifyRes.js';
3
+ import { setCursor } from './setCursor.js';
4
+ import { xxHash64 } from '../xxHash64.js';
5
+ export function writeHll(value, ctx, def, t, parentId, modifyOp) {
6
+ if (!value) {
7
+ return new ModifyError(t, value);
8
+ }
9
+ if (value === null) {
10
+ // console.log('modify/cardinality.ts trying to reset?')
11
+ // if (modifyOp === UPDATE) {
12
+ // if (ctx.len + 11 > ctx.max) {
13
+ // return RANGE_ERR
14
+ // }
15
+ // setCursor(ctx, def, t.prop, parentId, modifyOp)
16
+ // ctx.buf[ctx.len++] = DELETE
17
+ // }
18
+ }
19
+ else if (Array.isArray(value)) {
20
+ // console.log('modify/cardinality.ts Array.isArray(value)')
21
+ // for (const key in value) {
22
+ // if (key === 'add') {
23
+ // // @ts-ignore
24
+ // const err = addHll(value[key], ctx, def, t, parentId, modifyOp, 1)
25
+ // if (err) {
26
+ // return err
27
+ // }
28
+ // } else {
29
+ // return new ModifyError(t, value)
30
+ // }
31
+ // }
32
+ }
33
+ else {
34
+ return addHll(value, ctx, def, t, parentId, modifyOp);
35
+ }
36
+ }
37
+ function addHll(value, ctx, def, t, parentId, modifyOp) {
38
+ let size = 8;
39
+ if (ctx.len + size + 11 > ctx.max) {
40
+ return RANGE_ERR;
41
+ }
42
+ // console.log(`test JS original value = ${value}`)
43
+ setCursor(ctx, def, t.prop, t.typeIndex, parentId, modifyOp);
44
+ ctx.buf[ctx.len++] = modifyOp;
45
+ let hash = xxHash64(Buffer.from(value)); //1ec6c662633f0026 or 2217677992400715814
46
+ ctx.buf.writeBigUInt64LE(hash, ctx.len);
47
+ // console.log('js hash:', hash)
48
+ ctx.len += 8;
49
+ }
50
+ //# sourceMappingURL=cardinality.js.map
@@ -0,0 +1,5 @@
1
+ import { ModifyRes } from './ModifyRes.js';
2
+ import { ModifyOpts } from './types.js';
3
+ import { DbClient } from '../index.js';
4
+ export type CreateObj = Record<string, any>;
5
+ export declare function create(db: DbClient, type: string, obj: CreateObj, opts?: ModifyOpts): ModifyRes;
@@ -0,0 +1,112 @@
1
+ import { MICRO_BUFFER } from '../../server/schema/schema.js';
2
+ import { startDrain, flushBuffer } from '../operations.js';
3
+ import { setCursor } from './setCursor.js';
4
+ import { modify } from './modify.js';
5
+ import { ModifyState } from './ModifyRes.js';
6
+ import { CREATE, RANGE_ERR } from './types.js';
7
+ import { writeFixedValue } from './fixed.js';
8
+ import { getSubscriptionMarkers } from '../query/subscription/index.js';
9
+ const appendCreate = (ctx, def, obj, res, unsafe) => {
10
+ const len = ctx.len;
11
+ let err = modify(ctx, res, obj, def, CREATE, def.tree, true, unsafe);
12
+ if (err) {
13
+ return err;
14
+ }
15
+ if (ctx.len === len || def.mainLen === 0) {
16
+ if (ctx.len + 10 > ctx.max) {
17
+ return RANGE_ERR;
18
+ }
19
+ setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, CREATE);
20
+ }
21
+ if (def.createTs) {
22
+ const createTs = Date.now();
23
+ for (const prop of def.createTs) {
24
+ if (ctx.lastMain === -1) {
25
+ let mainLenU32 = def.mainLen;
26
+ setCursor(ctx, def, prop.prop, MICRO_BUFFER, res.tmpId, CREATE);
27
+ ctx.buf[ctx.len++] = CREATE;
28
+ ctx.buf[ctx.len++] = mainLenU32;
29
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
30
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
31
+ ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
32
+ ctx.lastMain = ctx.len;
33
+ ctx.buf.fill(0, ctx.len, (ctx.len += def.mainLen));
34
+ }
35
+ err = writeFixedValue(ctx, createTs, prop, ctx.lastMain + prop.start);
36
+ if (err) {
37
+ return err;
38
+ }
39
+ }
40
+ }
41
+ // if touched lets see perf impact here
42
+ if (def.hasStringProp) {
43
+ if (ctx.hasStringField !== def.stringPropsSize - 1) {
44
+ if (ctx.len + 3 > ctx.max) {
45
+ return RANGE_ERR;
46
+ }
47
+ ctx.buf[ctx.len++] = 7;
48
+ let sizepos = ctx.len;
49
+ ctx.len += 2;
50
+ for (const { prop } of def.stringPropsLoop) {
51
+ if (def.stringPropsCurrent[prop] === 1) {
52
+ if (ctx.len + 1 > ctx.max) {
53
+ return RANGE_ERR;
54
+ }
55
+ ctx.buf[ctx.len++] = prop;
56
+ }
57
+ }
58
+ let size = ctx.len - sizepos - 2;
59
+ ctx.buf[sizepos++] = size;
60
+ ctx.buf[sizepos] = size >>>= 8;
61
+ }
62
+ if (ctx.hasStringField !== -1) {
63
+ def.stringProps.copy(def.stringPropsCurrent);
64
+ }
65
+ }
66
+ };
67
+ export function create(db, type, obj, opts) {
68
+ const def = db.schemaTypesParsed[type];
69
+ if (!def) {
70
+ throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
71
+ }
72
+ let id;
73
+ if ('id' in obj) {
74
+ if (opts?.unsafe) {
75
+ id = obj.id;
76
+ }
77
+ else {
78
+ throw Error('create with "id" is not allowed');
79
+ }
80
+ }
81
+ else {
82
+ id = def.lastId + 1;
83
+ }
84
+ const ctx = db.modifyCtx;
85
+ const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, true), opts);
86
+ const pos = ctx.len;
87
+ const err = appendCreate(ctx, def, obj, res, opts?.unsafe);
88
+ if (err) {
89
+ ctx.prefix0 = -1; // force a new cursor
90
+ ctx.len = pos;
91
+ if (err === RANGE_ERR) {
92
+ if (pos === 0) {
93
+ throw new Error('out of range');
94
+ }
95
+ flushBuffer(db);
96
+ return db.create(type, obj, opts);
97
+ }
98
+ res.error = err;
99
+ // @ts-ignore
100
+ return res;
101
+ }
102
+ if (!db.isDraining) {
103
+ startDrain(db);
104
+ }
105
+ if (id > def.lastId) {
106
+ def.lastId = id;
107
+ def.total++;
108
+ }
109
+ // @ts-ignore
110
+ return res;
111
+ }
112
+ //# sourceMappingURL=create%20copy.js.map
@@ -1,4 +1,5 @@
1
1
  import { ModifyRes } from './ModifyRes.js';
2
+ import { ModifyOpts } from './types.js';
2
3
  import { DbClient } from '../index.js';
3
4
  export type CreateObj = Record<string, any>;
4
- export declare function create(db: DbClient, type: string, obj: CreateObj, unsafe?: boolean): ModifyRes;
5
+ export declare function create(db: DbClient, type: string, obj: CreateObj, opts?: ModifyOpts): ModifyRes;
@@ -1,3 +1,4 @@
1
+ import { MICRO_BUFFER } from '../../server/schema/schema.js';
1
2
  import { startDrain, flushBuffer } from '../operations.js';
2
3
  import { setCursor } from './setCursor.js';
3
4
  import { modify } from './modify.js';
@@ -15,14 +16,14 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
15
16
  if (ctx.len + 10 > ctx.max) {
16
17
  return RANGE_ERR;
17
18
  }
18
- setCursor(ctx, def, 0, res.tmpId, CREATE);
19
+ setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, CREATE);
19
20
  }
20
21
  if (def.createTs) {
21
22
  const createTs = Date.now();
22
23
  for (const prop of def.createTs) {
23
24
  if (ctx.lastMain === -1) {
24
25
  let mainLenU32 = def.mainLen;
25
- setCursor(ctx, def, prop.prop, res.tmpId, CREATE);
26
+ setCursor(ctx, def, prop.prop, MICRO_BUFFER, res.tmpId, CREATE);
26
27
  ctx.buf[ctx.len++] = CREATE;
27
28
  ctx.buf[ctx.len++] = mainLenU32;
28
29
  ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
@@ -63,11 +64,14 @@ const appendCreate = (ctx, def, obj, res, unsafe) => {
63
64
  }
64
65
  }
65
66
  };
66
- export function create(db, type, obj, unsafe) {
67
+ export function create(db, type, obj, opts) {
67
68
  const def = db.schemaTypesParsed[type];
69
+ if (!def) {
70
+ throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
71
+ }
68
72
  let id;
69
73
  if ('id' in obj) {
70
- if (unsafe) {
74
+ if (opts?.unsafe) {
71
75
  id = obj.id;
72
76
  }
73
77
  else {
@@ -78,9 +82,9 @@ export function create(db, type, obj, unsafe) {
78
82
  id = def.lastId + 1;
79
83
  }
80
84
  const ctx = db.modifyCtx;
81
- const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, true));
85
+ const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, true), opts);
82
86
  const pos = ctx.len;
83
- const err = appendCreate(ctx, def, obj, res, unsafe);
87
+ const err = appendCreate(ctx, def, obj, res, opts?.unsafe);
84
88
  if (err) {
85
89
  ctx.prefix0 = -1; // force a new cursor
86
90
  ctx.len = pos;
@@ -89,7 +93,7 @@ export function create(db, type, obj, unsafe) {
89
93
  throw new Error('out of range');
90
94
  }
91
95
  flushBuffer(db);
92
- return db.create(type, obj, unsafe);
96
+ return db.create(type, obj, opts);
93
97
  }
94
98
  res.error = err;
95
99
  // @ts-ignore
@@ -0,0 +1,2 @@
1
+ import { DbClient } from '../index.js';
2
+ export declare const deleteFn: (db: DbClient, type: string, id: number) => boolean;
@@ -0,0 +1,37 @@
1
+ import { flushBuffer, startDrain } from '../operations.js';
2
+ import { setCursor } from './setCursor.js';
3
+ import { UPDATE } from './types.js';
4
+ import { MICRO_BUFFER } from '../../server/schema/schema.js';
5
+ export const deleteFn = (db, type, id) => {
6
+ const ctx = db.modifyCtx;
7
+ const schema = db.schemaTypesParsed[type];
8
+ const separate = schema.separate;
9
+ if (separate) {
10
+ const size = 12 + separate.length * 12;
11
+ if (ctx.len + size > ctx.max) {
12
+ flushBuffer(db);
13
+ return deleteFn(db, type, id);
14
+ }
15
+ setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
16
+ ctx.buf[ctx.len++] = 4;
17
+ for (const s of separate) {
18
+ setCursor(ctx, schema, s.prop, s.typeIndex, id, UPDATE);
19
+ ctx.buf[ctx.len++] = 4;
20
+ }
21
+ ctx.buf[ctx.len++] = 10;
22
+ }
23
+ else {
24
+ if (ctx.len + 12 > ctx.max) {
25
+ flushBuffer(db);
26
+ return deleteFn(db, type, id);
27
+ }
28
+ setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
29
+ ctx.buf[ctx.len++] = 4;
30
+ ctx.buf[ctx.len++] = 10;
31
+ }
32
+ if (!db.isDraining) {
33
+ startDrain(db);
34
+ }
35
+ return true;
36
+ };
37
+ //# sourceMappingURL=delete.js.map
@@ -0,0 +1,3 @@
1
+ import { DbClient } from '../index.js';
2
+ export type CreateObj = Record<string, any>;
3
+ export declare function expire(db: DbClient, type: string, id: number, seconds: number): void;
@@ -0,0 +1,25 @@
1
+ import { MICRO_BUFFER } from '../../server/schema/schema.js';
2
+ import { startDrain, flushBuffer } from '../operations.js';
3
+ import { setCursor } from './setCursor.js';
4
+ import { EXPIRE } from './types.js';
5
+ export function expire(db, type, id, seconds) {
6
+ const def = db.schemaTypesParsed[type];
7
+ if (!def) {
8
+ throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
9
+ }
10
+ const ctx = db.modifyCtx;
11
+ if (ctx.len + 8 + 1 + 4 > ctx.max) {
12
+ flushBuffer(db);
13
+ return expire(db, type, id, seconds);
14
+ }
15
+ setCursor(ctx, def, 0, MICRO_BUFFER, id, EXPIRE);
16
+ ctx.buf[ctx.len++] = EXPIRE;
17
+ ctx.buf[ctx.len++] = seconds;
18
+ ctx.buf[ctx.len++] = seconds >>>= 8;
19
+ ctx.buf[ctx.len++] = seconds >>>= 8;
20
+ ctx.buf[ctx.len++] = seconds >>>= 8;
21
+ if (!db.isDraining) {
22
+ startDrain(db);
23
+ }
24
+ }
25
+ //# sourceMappingURL=expire.js.map