@based/db 0.0.17 → 0.0.19

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 (99) hide show
  1. package/dist/lib/darwin_aarch64/include/selva/fields.h +0 -7
  2. package/dist/lib/darwin_aarch64/libnode-v20.11.1.node +0 -0
  3. package/dist/lib/darwin_aarch64/libnode-v20.18.1.node +0 -0
  4. package/dist/lib/darwin_aarch64/libnode-v22.13.0.node +0 -0
  5. package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
  6. package/dist/lib/linux_aarch64/include/selva/fields.h +0 -7
  7. package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
  8. package/dist/lib/linux_aarch64/libnode-v20.11.1.node +0 -0
  9. package/dist/lib/linux_aarch64/libnode-v20.18.1.node +0 -0
  10. package/dist/lib/linux_aarch64/libnode-v22.13.0.node +0 -0
  11. package/dist/lib/linux_aarch64/libselva.so +0 -0
  12. package/dist/lib/linux_x86_64/include/selva/fields.h +0 -7
  13. package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
  14. package/dist/lib/linux_x86_64/libnode-v20.11.1.node +0 -0
  15. package/dist/lib/linux_x86_64/libnode-v20.18.1.node +0 -0
  16. package/dist/lib/linux_x86_64/libnode-v22.13.0.node +0 -0
  17. package/dist/lib/linux_x86_64/libselva.so +0 -0
  18. package/dist/src/client/index.d.ts +1 -1
  19. package/dist/src/client/index.js +8 -3
  20. package/dist/src/client/modify/ModifyRes.d.ts +1 -1
  21. package/dist/src/client/modify/ModifyRes.js +1 -1
  22. package/dist/src/client/modify/alias.d.ts +1 -1
  23. package/dist/src/client/modify/alias.js +1 -1
  24. package/dist/src/client/modify/binary.d.ts +1 -1
  25. package/dist/src/client/modify/cardinality.d.ts +1 -1
  26. package/dist/src/client/modify/cardinality.js +1 -1
  27. package/dist/src/client/modify/create.js +2 -2
  28. package/dist/src/client/modify/delete.js +1 -1
  29. package/dist/src/client/modify/expire.js +1 -1
  30. package/dist/src/client/modify/fixed.d.ts +1 -1
  31. package/dist/src/client/modify/fixed.js +1 -1
  32. package/dist/src/client/modify/json.d.ts +1 -1
  33. package/dist/src/client/modify/modify.d.ts +1 -1
  34. package/dist/src/client/modify/modify.js +1 -1
  35. package/dist/src/client/modify/references/edge.d.ts +1 -1
  36. package/dist/src/client/modify/references/edge.js +1 -1
  37. package/dist/src/client/modify/references/reference.d.ts +1 -1
  38. package/dist/src/client/modify/references/references.d.ts +1 -1
  39. package/dist/src/client/modify/references/references.js +1 -1
  40. package/dist/src/client/modify/setCursor.d.ts +1 -1
  41. package/dist/src/client/modify/string.d.ts +1 -1
  42. package/dist/src/client/modify/text.d.ts +1 -1
  43. package/dist/src/client/modify/text.js +6 -0
  44. package/dist/src/client/modify/update.js +1 -1
  45. package/dist/src/client/modify/upsert.js +1 -1
  46. package/dist/src/client/modify/vector.d.ts +1 -1
  47. package/dist/src/client/operations.d.ts +2 -1
  48. package/dist/src/client/operations.js +1 -0
  49. package/dist/src/client/query/BasedDbQuery.js +1 -1
  50. package/dist/src/client/query/debug.js +1 -1
  51. package/dist/src/client/query/display.d.ts +1 -1
  52. package/dist/src/client/query/display.js +1 -1
  53. package/dist/src/client/query/filter/createFixedFilterBuffer.d.ts +1 -1
  54. package/dist/src/client/query/filter/createFixedFilterBuffer.js +1 -2
  55. package/dist/src/client/query/filter/createReferenceFilter.d.ts +1 -1
  56. package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +1 -1
  57. package/dist/src/client/query/filter/createVariableFilterBuffer.js +1 -1
  58. package/dist/src/client/query/filter/filter.d.ts +1 -1
  59. package/dist/src/client/query/filter/filter.js +1 -1
  60. package/dist/src/client/query/filter/parseFilterValue.d.ts +1 -1
  61. package/dist/src/client/query/filter/parseFilterValue.js +1 -1
  62. package/dist/src/client/query/filter/primitiveFilter.d.ts +1 -1
  63. package/dist/src/client/query/filter/primitiveFilter.js +4 -1
  64. package/dist/src/client/query/include/props.d.ts +1 -1
  65. package/dist/src/client/query/include/props.js +1 -1
  66. package/dist/src/client/query/include/utils.d.ts +1 -1
  67. package/dist/src/client/query/include/utils.js +1 -1
  68. package/dist/src/client/query/include/walk.js +1 -1
  69. package/dist/src/client/query/read/read.js +1 -1
  70. package/dist/src/client/query/search/index.js +1 -1
  71. package/dist/src/client/query/subscription/markers.d.ts +1 -1
  72. package/dist/src/client/query/types.d.ts +1 -1
  73. package/dist/src/client/query/validation.d.ts +1 -1
  74. package/dist/src/client/query/validation.js +9 -8
  75. package/dist/src/index.d.ts +0 -1
  76. package/dist/src/index.js +0 -1
  77. package/dist/src/server/index.d.ts +3 -5
  78. package/dist/src/server/index.js +11 -5
  79. package/dist/src/server/migrate/worker.js +1 -1
  80. package/dist/src/server/start.d.ts +0 -1
  81. package/dist/src/server/start.js +1 -1
  82. package/dist/src/server/tree.d.ts +1 -1
  83. package/package.json +2 -2
  84. package/dist/lib/darwin_aarch64/libxxhash.0.8.2.dylib +0 -0
  85. package/dist/lib/darwin_aarch64/libxxhash.0.dylib +0 -0
  86. package/dist/lib/linux_aarch64/libxxhash.so +0 -0
  87. package/dist/lib/linux_aarch64/libxxhash.so.0.8.2 +0 -0
  88. package/dist/lib/linux_x86_64/libxxhash.so +0 -0
  89. package/dist/lib/linux_x86_64/libxxhash.so.0.8.2 +0 -0
  90. package/dist/src/server/schema/schema.d.ts +0 -3
  91. package/dist/src/server/schema/schema.js +0 -4
  92. package/dist/src/server/schema/selvaBuffer.d.ts +0 -4
  93. package/dist/src/server/schema/selvaBuffer.js +0 -112
  94. package/dist/src/server/schema/typeDef.d.ts +0 -6
  95. package/dist/src/server/schema/typeDef.js +0 -337
  96. package/dist/src/server/schema/types.d.ts +0 -140
  97. package/dist/src/server/schema/types.js +0 -121
  98. package/dist/src/server/schema/utils.d.ts +0 -7
  99. package/dist/src/server/schema/utils.js +0 -34
@@ -1,6 +1,5 @@
1
1
  import picocolors from 'picocolors';
2
- import { ALIAS, BINARY, BOOLEAN, REFERENCE, REFERENCES, REVERSE_TYPE_INDEX_MAP, STRING, TEXT, TIMESTAMP, VECTOR, } from '../../server/schema/types.js';
3
- import { propIsNumerical } from '../../server/schema/utils.js';
2
+ import { ALIAS, BINARY, BOOLEAN, REFERENCE, REFERENCES, REVERSE_TYPE_INDEX_MAP, STRING, TEXT, TIMESTAMP, VECTOR, propIsNumerical, } from '@based/schema/def';
4
3
  import { EQUAL, HAS, isNumerical, LIKE, operatorReverseMap, VECTOR_FNS, } from './filter/types.js';
5
4
  import { MAX_ID, MAX_ID_VALUE, MAX_IDS_PER_QUERY, MIN_ID_VALUE, } from './thresholds.js';
6
5
  import { displayTarget, safeStringify } from './display.js';
@@ -439,8 +438,9 @@ export const EMPTY_SCHEMA_DEF = {
439
438
  lastId: 0,
440
439
  blockCapacity: 0,
441
440
  mainLen: 0,
442
- buf: Buffer.from([]),
443
- propNames: Buffer.from([]),
441
+ buf: new Uint8Array([]),
442
+ propNames: new Uint8Array([]),
443
+ packed: new Uint8Array([]),
444
444
  props: {},
445
445
  locales: {},
446
446
  reverseProps: {},
@@ -452,16 +452,17 @@ export const EMPTY_SCHEMA_DEF = {
452
452
  hasSeperateSort: false,
453
453
  seperateSort: {
454
454
  size: 0,
455
- buffer: Buffer.from([]),
456
- bufferTmp: Buffer.from([]),
455
+ buffer: new Uint8Array([]),
456
+ bufferTmp: new Uint8Array([]),
457
457
  props: [],
458
458
  },
459
459
  hasSeperateTextSort: false,
460
460
  seperateTextSort: {
461
461
  size: 0,
462
- buffer: Buffer.from([]),
463
- bufferTmp: Buffer.from([]),
462
+ buffer: new Uint8Array([]),
463
+ bufferTmp: new Uint8Array([]),
464
464
  props: [],
465
465
  },
466
+ localeSize: 0,
466
467
  };
467
468
  //# sourceMappingURL=validation.js.map
@@ -2,7 +2,6 @@ import { compress, decompress } from './client/string.js';
2
2
  import { ModifyCtx } from './client/operations.js';
3
3
  import { DbServer } from './server/index.js';
4
4
  import { DbClient } from './client/index.js';
5
- export * from './server/schema/typeDef.js';
6
5
  export * from './client/modify/modify.js';
7
6
  export { compress, decompress };
8
7
  export { ModifyCtx };
package/dist/src/index.js CHANGED
@@ -3,7 +3,6 @@ import { ModifyCtx } from './client/operations.js';
3
3
  import { DbServer } from './server/index.js';
4
4
  import { DbClient } from './client/index.js';
5
5
  import picocolors from 'picocolors';
6
- export * from './server/schema/typeDef.js';
7
6
  export * from './client/modify/modify.js';
8
7
  export { compress, decompress };
9
8
  export { ModifyCtx }; // TODO move this somewhere
@@ -1,5 +1,5 @@
1
1
  import { LangName, StrictSchema } from '@based/schema';
2
- import { SchemaTypeDef } from './schema/types.js';
2
+ import { SchemaTypesParsed, SchemaTypesParsedById } from '@based/schema/def';
3
3
  import { createTree } from './csmt/index.js';
4
4
  import { Worker, MessagePort } from 'node:worker_threads';
5
5
  import { TransformFns } from './migrate/index.js';
@@ -29,10 +29,8 @@ export declare class DbServer {
29
29
  lastId: number;
30
30
  };
31
31
  migrating: number;
32
- schemaTypesParsed: {
33
- [key: string]: SchemaTypeDef;
34
- };
35
- schemaTypesParsedById: Record<number, SchemaTypeDef>;
32
+ schemaTypesParsed: SchemaTypesParsed;
33
+ schemaTypesParsedById: SchemaTypesParsedById;
36
34
  fileSystemPath: string;
37
35
  maxModifySize: number;
38
36
  merkleTree: ReturnType<typeof createTree>;
@@ -2,9 +2,9 @@ import native from '../native.js';
2
2
  import { rm, writeFile } from 'node:fs/promises';
3
3
  import { dirname, join } from 'node:path';
4
4
  import { getPropType, langCodesMap, } from '@based/schema';
5
- import { genRootId } from './schema/utils.js';
6
- import { updateTypeDefs } from './schema/typeDef.js';
7
- import { schemaToSelvaBuffer } from './schema/selvaBuffer.js';
5
+ import {
6
+ // genRootId,
7
+ updateTypeDefs, schemaToSelvaBuffer, } from '@based/schema/def';
8
8
  import { start } from './start.js';
9
9
  import { foreachDirtyBlock, makeCsmtKey, makeCsmtKeyFromNodeId, } from './tree.js';
10
10
  import { save } from './save.js';
@@ -310,12 +310,18 @@ export class DbServer {
310
310
  }
311
311
  // @ts-ignore This creates an internal type to use for root props
312
312
  this.schema.types._root = {
313
- id: genRootId(),
313
+ id: 1,
314
314
  props,
315
315
  };
316
316
  delete this.schema.props;
317
317
  }
318
- updateTypeDefs(this);
318
+ for (const field in this.schema.types) {
319
+ if (!('id' in this.schema.types[field])) {
320
+ this.schema.lastId++;
321
+ this.schema.types[field].id = this.schema.lastId;
322
+ }
323
+ }
324
+ updateTypeDefs(this.schema, this.schemaTypesParsed, this.schemaTypesParsedById);
319
325
  if (!fromStart) {
320
326
  writeFile(join(this.fileSystemPath, SCHEMA_FILE), JSON.stringify(this.schema)).catch((err) => console.error(SCHEMA_FILE, err));
321
327
  let types = Object.keys(this.schemaTypesParsed);
@@ -1,7 +1,7 @@
1
1
  import { isMainThread, receiveMessageOnPort, workerData, } from 'node:worker_threads';
2
2
  import native from '../../native.js';
3
3
  import { BasedDb } from '../../index.js';
4
- import { REFERENCE, REFERENCES } from '../schema/types.js';
4
+ import { REFERENCE, REFERENCES } from '@based/schema/def';
5
5
  if (isMainThread) {
6
6
  console.warn('running worker.ts in mainthread');
7
7
  }
@@ -1,6 +1,5 @@
1
1
  import { DbServer } from './index.js';
2
2
  import './worker.js';
3
- export declare const DEFAULT_BLOCK_CAPACITY = 100000;
4
3
  export declare function start(db: DbServer, opts: {
5
4
  clean?: boolean;
6
5
  }): Promise<void>;
@@ -9,9 +9,9 @@ import { availableParallelism } from 'node:os';
9
9
  import exitHook from 'exit-hook';
10
10
  import './worker.js';
11
11
  import { save } from './save.js';
12
+ import { DEFAULT_BLOCK_CAPACITY } from '@based/schema/def';
12
13
  const SCHEMA_FILE = 'schema.json';
13
14
  const WRITELOG_FILE = 'writelog.json';
14
- export const DEFAULT_BLOCK_CAPACITY = 100_000;
15
15
  const makeCsmtKey = (typeId, start) => typeId * 4294967296 + start;
16
16
  export async function start(db, opts) {
17
17
  const path = db.fileSystemPath;
@@ -1,5 +1,5 @@
1
1
  import { DbServer } from './index.js';
2
- import { SchemaTypeDef } from './schema/types.js';
2
+ import { SchemaTypeDef } from '@based/schema/def';
3
3
  export type CsmtNodeRange = {
4
4
  file: string;
5
5
  typeId: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.17",
3
+ "version": "0.0.19",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -31,7 +31,7 @@
31
31
  "basedDbNative.cjs"
32
32
  ],
33
33
  "dependencies": {
34
- "@based/schema": "5.0.0-alpha.3",
34
+ "@based/schema": "5.0.0-alpha.4",
35
35
  "@saulx/hash": "^3.0.0",
36
36
  "@saulx/utils": "^4.3.2",
37
37
  "exit-hook": "^4.0.0",
Binary file
Binary file
@@ -1,3 +0,0 @@
1
- export * from './types.js';
2
- export * from './typeDef.js';
3
- export * from './selvaBuffer.js';
@@ -1,4 +0,0 @@
1
- export * from './types.js';
2
- export * from './typeDef.js';
3
- export * from './selvaBuffer.js';
4
- //# sourceMappingURL=schema.js.map
@@ -1,4 +0,0 @@
1
- import { SchemaTypeDef } from './types.js';
2
- export declare function schemaToSelvaBuffer(schema: {
3
- [key: string]: SchemaTypeDef;
4
- }): Buffer[];
@@ -1,112 +0,0 @@
1
- import { ALIAS, ALIASES, BINARY, BOOLEAN, CREATED, EMPTY_MICRO_BUFFER, ENUM, CARDINALITY, INT16, INT32, INT64, INT8, MICRO_BUFFER, NULL, NUMBER, REFERENCE, REFERENCES, STRING, TEXT, TIMESTAMP, UINT16, UINT32, UINT8, UPDATED, VECTOR, WEAK_REFERENCE, WEAK_REFERENCES, JSON, } from './types.js';
2
- const selvaTypeMap = [];
3
- selvaTypeMap[NULL] = 0;
4
- selvaTypeMap[TIMESTAMP] = 1;
5
- selvaTypeMap[CREATED] = 1;
6
- selvaTypeMap[UPDATED] = 1;
7
- selvaTypeMap[NUMBER] = 4;
8
- selvaTypeMap[CARDINALITY] = 11;
9
- selvaTypeMap[INT8] = 20;
10
- selvaTypeMap[UINT8] = 6;
11
- selvaTypeMap[INT16] = 21;
12
- selvaTypeMap[UINT16] = 22;
13
- selvaTypeMap[INT32] = 23;
14
- selvaTypeMap[UINT32] = 7;
15
- selvaTypeMap[INT64] = 24;
16
- selvaTypeMap[BOOLEAN] = 9;
17
- selvaTypeMap[ENUM] = 10;
18
- selvaTypeMap[STRING] = 11;
19
- selvaTypeMap[TEXT] = 12;
20
- selvaTypeMap[REFERENCE] = 13;
21
- selvaTypeMap[REFERENCES] = 14;
22
- selvaTypeMap[WEAK_REFERENCE] = 15;
23
- selvaTypeMap[WEAK_REFERENCES] = 16;
24
- selvaTypeMap[MICRO_BUFFER] = 17;
25
- selvaTypeMap[ALIAS] = 18;
26
- selvaTypeMap[ALIASES] = 19;
27
- selvaTypeMap[BINARY] = 11;
28
- selvaTypeMap[VECTOR] = 17;
29
- selvaTypeMap[JSON] = 11;
30
- const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
31
- function sepPropCount(props) {
32
- return props.filter((prop) => prop.separate).length;
33
- }
34
- function makeEdgeConstraintFlags(prop) {
35
- return prop.dependent ? EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT : 0x00;
36
- }
37
- const propDefBuffer = (schema, prop, isEdge) => {
38
- const type = prop.typeIndex;
39
- const selvaType = selvaTypeMap[type];
40
- if (prop.len && (type === MICRO_BUFFER || type === VECTOR)) {
41
- const buf = Buffer.allocUnsafe(3);
42
- buf[0] = selvaType;
43
- buf.writeUint16LE(prop.len, 1);
44
- return [...buf.values()];
45
- }
46
- else if (type === REFERENCE || type === REFERENCES) {
47
- const buf = Buffer.allocUnsafe(9);
48
- const dstType = schema[prop.inverseTypeName];
49
- let eschema = [];
50
- // @ts-ignore
51
- buf[0] = selvaType + 2 * !!isEdge; // field type
52
- buf[1] = makeEdgeConstraintFlags(prop); // flags
53
- buf.writeUInt16LE(dstType.id, 2); // dst_node_type
54
- buf.writeUint32LE(0, 5); // schema_len
55
- if (!isEdge) {
56
- prop.inverseTypeId = dstType.id;
57
- prop.inversePropNumber = dstType.props[prop.inversePropName].prop;
58
- buf[4] = prop.inversePropNumber;
59
- if (prop.edges) {
60
- const props = Object.values(prop.edges);
61
- eschema = props
62
- .map((prop) => propDefBuffer(schema, prop, true))
63
- .flat(1);
64
- eschema.unshift(0, 0, 0, 0, sepPropCount(props), 0);
65
- buf.writeUint32LE(eschema.length, 5);
66
- }
67
- }
68
- return [...buf.values(), ...eschema];
69
- }
70
- else if (type === STRING ||
71
- type === BINARY ||
72
- type === CARDINALITY ||
73
- type === JSON) {
74
- return [selvaType, prop.len < 50 ? prop.len : 0];
75
- }
76
- {
77
- return [selvaType];
78
- }
79
- };
80
- function makeBlockCapacityBuffer(blockCapacity) {
81
- const buf = Buffer.allocUnsafe(4);
82
- buf.writeInt32LE(blockCapacity);
83
- return buf;
84
- }
85
- // todo rewrite
86
- export function schemaToSelvaBuffer(schema) {
87
- return Object.values(schema).map((t, i) => {
88
- const props = Object.values(t.props);
89
- const rest = [];
90
- let refFields = 0;
91
- for (const f of props) {
92
- if (f.separate) {
93
- if (f.typeIndex === REFERENCE || f.typeIndex === REFERENCES) {
94
- refFields++;
95
- }
96
- rest.push(f);
97
- }
98
- }
99
- rest.sort((a, b) => a.prop - b.prop);
100
- return Buffer.from([
101
- ...makeBlockCapacityBuffer(t.blockCapacity).values(),
102
- 1 + sepPropCount(props),
103
- 1 + refFields,
104
- ...propDefBuffer(schema, {
105
- ...EMPTY_MICRO_BUFFER,
106
- len: t.mainLen === 0 ? 1 : t.mainLen,
107
- }),
108
- ...rest.map((f) => propDefBuffer(schema, f)).flat(1),
109
- ]);
110
- });
111
- }
112
- //# sourceMappingURL=selvaBuffer.js.map
@@ -1,6 +0,0 @@
1
- import { SchemaObject, StrictSchemaType, SchemaLocales } from '@based/schema';
2
- import { SchemaTypeDef } from './types.js';
3
- import { DbClient } from '../../client/index.js';
4
- import { DbServer } from '../index.js';
5
- export declare const updateTypeDefs: (db: DbClient | DbServer) => void;
6
- export declare const createSchemaTypeDef: (typeName: string, type: StrictSchemaType | SchemaObject, parsed: DbClient["schemaTypesParsed"], locales: Partial<SchemaLocales>, result?: Partial<SchemaTypeDef>, path?: string[], top?: boolean) => SchemaTypeDef;
@@ -1,337 +0,0 @@
1
- import { isPropType, getPropType, } from '@based/schema';
2
- import { setByPath } from '@saulx/utils';
3
- import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
4
- import { SIZE_MAP, TYPE_INDEX_MAP, STRING, ALIAS, CARDINALITY, } from './types.js';
5
- import { genId } from './utils.js';
6
- import { DEFAULT_BLOCK_CAPACITY } from '../start.js';
7
- const addEdges = (prop, refProp) => {
8
- let edgesCnt = 0;
9
- for (const key in refProp) {
10
- if (key[0] === '$') {
11
- if (!prop.edges) {
12
- prop.edges = {};
13
- prop.reverseEdges = {};
14
- prop.edgesTotalLen = 0;
15
- }
16
- edgesCnt++;
17
- const edgeType = getPropType(refProp[key]);
18
- const edge = {
19
- __isPropDef: true,
20
- __isEdge: true,
21
- prop: edgesCnt,
22
- name: key,
23
- typeIndex: TYPE_INDEX_MAP[edgeType],
24
- len: SIZE_MAP[edgeType],
25
- separate: true,
26
- path: [...prop.path, key],
27
- };
28
- if (edge.len == 0) {
29
- prop.edgesTotalLen = 0;
30
- }
31
- else {
32
- // [field] [size] [data]
33
- prop.edgesTotalLen += 1 + 2 + edge.len; // field len
34
- }
35
- if (edge.typeIndex === 10) {
36
- edge.enum = Array.isArray(refProp[key])
37
- ? refProp[key]
38
- : refProp[key].enum;
39
- edge.reverseEnum = {};
40
- for (let i = 0; i < edge.enum.length; i++) {
41
- edge.reverseEnum[edge.enum[i]] = i;
42
- }
43
- }
44
- else if (edge.typeIndex === 14) {
45
- edge.inverseTypeName = refProp[key].items.ref;
46
- }
47
- else if (edge.typeIndex === 13) {
48
- edge.inverseTypeName = refProp[key].ref;
49
- }
50
- prop.edges[key] = edge;
51
- prop.reverseEdges[edge.prop] = edge;
52
- }
53
- }
54
- };
55
- export const updateTypeDefs = (db) => {
56
- for (const field in db.schemaTypesParsed) {
57
- if (field in db.schema.types) {
58
- continue;
59
- }
60
- const id = db.schemaTypesParsed[field].id;
61
- delete db.schemaTypesParsed[field];
62
- delete db.schemaTypesParsedById[id];
63
- }
64
- for (const field in db.schema.types) {
65
- const type = db.schema.types[field];
66
- if (db.schemaTypesParsed[field] &&
67
- db.schemaTypesParsed[field].checksum === hashObjectIgnoreKeyOrder(type) // bit weird..
68
- ) {
69
- continue;
70
- }
71
- else {
72
- if (!type.id) {
73
- type.id = genId(db);
74
- }
75
- const def = createSchemaTypeDef(field, type, db.schemaTypesParsed, db.schema.locales ?? {
76
- en: {},
77
- });
78
- def.blockCapacity =
79
- field === '_root' ? 2147483647 : DEFAULT_BLOCK_CAPACITY; // TODO this should come from somewhere else
80
- db.schemaTypesParsed[field] = def;
81
- db.schemaTypesParsedById[type.id] = def;
82
- }
83
- }
84
- };
85
- export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
86
- cnt: 0,
87
- checksum: hashObjectIgnoreKeyOrder(type),
88
- type: typeName,
89
- props: {},
90
- reverseProps: {},
91
- idUint8: new Uint8Array([0, 0]),
92
- id: 0,
93
- mainLen: 0,
94
- separate: [],
95
- tree: {},
96
- total: 0,
97
- lastId: 0,
98
- main: {},
99
- hasSeperateSort: false,
100
- seperateSort: {
101
- size: 0,
102
- props: [],
103
- buffer: Buffer.allocUnsafe(0),
104
- bufferTmp: Buffer.allocUnsafe(0),
105
- },
106
- hasSeperateTextSort: false,
107
- seperateTextSort: {
108
- size: 0,
109
- props: [],
110
- buffer: Buffer.allocUnsafe(0),
111
- bufferTmp: Buffer.allocUnsafe(0),
112
- },
113
- }, path = [], top = true) => {
114
- if (result.id == 0 && top) {
115
- if ('id' in type) {
116
- result.id = type.id;
117
- }
118
- else {
119
- throw new Error(`Invalid schema type id ${result.type}`);
120
- }
121
- }
122
- result.locales = locales;
123
- result.idUint8[0] = result.id & 255;
124
- result.idUint8[1] = result.id >> 8;
125
- const encoder = new TextEncoder();
126
- const target = type.props;
127
- let separateSortProps = 0;
128
- let separateSortText = 0;
129
- for (const key in target) {
130
- const schemaProp = target[key];
131
- const propPath = [...path, key];
132
- const propType = getPropType(schemaProp);
133
- if (propType === 'object') {
134
- createSchemaTypeDef(typeName, schemaProp, parsed, locales, result, propPath, false);
135
- }
136
- else {
137
- let len = SIZE_MAP[propType];
138
- if (isPropType('string', schemaProp) ||
139
- isPropType('alias', schemaProp) ||
140
- isPropType('cardinality', schemaProp)) {
141
- if (typeof schemaProp === 'object') {
142
- if (schemaProp.maxBytes < 61) {
143
- len = schemaProp.maxBytes + 1;
144
- }
145
- else if ('max' in schemaProp && schemaProp.max < 31) {
146
- len = schemaProp.max * 2 + 1;
147
- }
148
- else {
149
- separateSortProps++;
150
- }
151
- }
152
- else {
153
- separateSortProps++;
154
- }
155
- }
156
- else if (isPropType('vector', schemaProp)) {
157
- len = 4 * schemaProp.size;
158
- }
159
- const isseparate = len === 0 || isPropType('vector', schemaProp);
160
- if (isseparate) {
161
- result.cnt++;
162
- }
163
- const prop = {
164
- typeIndex: TYPE_INDEX_MAP[propType],
165
- __isPropDef: true,
166
- separate: isseparate,
167
- path: propPath,
168
- start: 0,
169
- len,
170
- prop: isseparate ? result.cnt : 0,
171
- };
172
- if (isPropType('enum', schemaProp)) {
173
- prop.enum = Array.isArray(schemaProp) ? schemaProp : schemaProp.enum;
174
- prop.reverseEnum = {};
175
- for (let i = 0; i < prop.enum.length; i++) {
176
- prop.reverseEnum[prop.enum[i]] = i;
177
- }
178
- }
179
- else if (isPropType('references', schemaProp)) {
180
- prop.inversePropName = schemaProp.items.prop;
181
- prop.inverseTypeName = schemaProp.items.ref;
182
- prop.dependent = schemaProp.items.dependent;
183
- addEdges(prop, schemaProp.items);
184
- }
185
- else if (isPropType('reference', schemaProp)) {
186
- prop.inversePropName = schemaProp.prop;
187
- prop.inverseTypeName = schemaProp.ref;
188
- prop.dependent = schemaProp.dependent;
189
- addEdges(prop, schemaProp);
190
- }
191
- else if (typeof schemaProp === 'object') {
192
- if (isPropType('string', schemaProp) ||
193
- isPropType('text', schemaProp)) {
194
- prop.compression =
195
- 'compression' in schemaProp && schemaProp.compression === 'none'
196
- ? 0
197
- : 1;
198
- }
199
- else if (isPropType('timestamp', schemaProp) && 'on' in schemaProp) {
200
- if (schemaProp.on[0] === 'c') {
201
- result.createTs ??= [];
202
- result.createTs.push(prop);
203
- }
204
- else if (schemaProp.on[0] === 'u') {
205
- result.createTs ??= [];
206
- result.createTs.push(prop);
207
- result.updateTs ??= [];
208
- result.updateTs.push(prop);
209
- }
210
- }
211
- }
212
- result.props[propPath.join('.')] = prop;
213
- if (isseparate) {
214
- result.separate.push(prop);
215
- }
216
- }
217
- }
218
- if (top) {
219
- const vals = Object.values(result.props);
220
- vals.sort((a, b) => {
221
- if (b.separate && (a.typeIndex === 14 || a.typeIndex === 13)) {
222
- return -1;
223
- }
224
- return a.prop - b.prop;
225
- });
226
- let lastProp = 0;
227
- for (const p of vals) {
228
- if (p.separate) {
229
- lastProp++;
230
- p.prop = lastProp;
231
- }
232
- }
233
- let len = 2;
234
- for (const f of vals) {
235
- if (f.separate) {
236
- len += 2;
237
- setByPath(result.tree, f.path, f);
238
- }
239
- else {
240
- if (!result.mainLen) {
241
- len += 2;
242
- }
243
- len += 1;
244
- f.start = result.mainLen;
245
- result.mainLen += f.len;
246
- setByPath(result.tree, f.path, f);
247
- }
248
- }
249
- const mainFields = [];
250
- const restFields = [];
251
- for (const f of vals) {
252
- if (f.separate) {
253
- restFields.push(f);
254
- }
255
- else {
256
- mainFields.push(f);
257
- }
258
- }
259
- result.buf = Buffer.allocUnsafe(len);
260
- result.buf[0] = result.idUint8[0];
261
- result.buf[1] = result.idUint8[1];
262
- const fieldNames = [];
263
- const tNameBuf = encoder.encode(typeName);
264
- fieldNames.push(tNameBuf);
265
- let fieldNameLen = tNameBuf.byteLength + 1;
266
- let i = 2;
267
- if (result.mainLen) {
268
- result.buf[i] = 0;
269
- for (const f of vals) {
270
- if (!f.separate) {
271
- i++;
272
- result.buf[i] = f.typeIndex;
273
- const name = encoder.encode(f.path.join('.'));
274
- fieldNames.push(name);
275
- fieldNameLen += name.byteLength + 1;
276
- }
277
- }
278
- i++;
279
- result.buf[i] = 0;
280
- }
281
- for (const f of vals) {
282
- if (f.separate) {
283
- i++;
284
- result.buf[i] = f.prop;
285
- i++;
286
- result.buf[i] = f.typeIndex;
287
- const name = encoder.encode(f.path.join('.'));
288
- fieldNames.push(name);
289
- fieldNameLen += name.byteLength + 1;
290
- }
291
- }
292
- result.propNames = Buffer.allocUnsafe(fieldNameLen);
293
- let lastWritten = 0;
294
- for (const f of fieldNames) {
295
- result.propNames[lastWritten] = f.byteLength;
296
- result.propNames.set(f, lastWritten + 1);
297
- lastWritten += f.byteLength + 1;
298
- }
299
- if (separateSortProps > 0) {
300
- result.hasSeperateSort = true;
301
- let max = 0;
302
- for (const f of result.separate) {
303
- // Does not only need to be on string can also be HLL
304
- if (f.typeIndex === STRING ||
305
- f.typeIndex === ALIAS ||
306
- f.typeIndex === CARDINALITY) {
307
- if (f.prop > max) {
308
- max = f.prop;
309
- }
310
- }
311
- }
312
- result.seperateSort.buffer = Buffer.allocUnsafe(max + 1);
313
- for (const f of result.separate) {
314
- if (f.typeIndex === STRING ||
315
- f.typeIndex === ALIAS ||
316
- f.typeIndex === CARDINALITY) {
317
- result.seperateSort.buffer[f.prop] = 1;
318
- result.seperateSort.props.push(f);
319
- result.seperateSort.size++;
320
- }
321
- }
322
- result.seperateSort.bufferTmp = Buffer.allocUnsafe(max + 1);
323
- result.seperateSort.buffer.copy(result.seperateSort.bufferTmp);
324
- }
325
- for (const p in result.props) {
326
- const x = result.props[p];
327
- if (!x.separate) {
328
- result.main[x.start] = x;
329
- }
330
- else {
331
- result.reverseProps[x.prop] = x;
332
- }
333
- }
334
- }
335
- return result;
336
- };
337
- //# sourceMappingURL=typeDef.js.map