@based/schema 5.0.0-alpha.5 → 5.0.0-alpha.7

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.
@@ -0,0 +1,4 @@
1
+ import { SchemaReference } from '../index.js';
2
+ import { PropDef } from './types.js';
3
+ export declare const addEdges: (prop: PropDef, refProp: SchemaReference) => void;
4
+ //# sourceMappingURL=addEdges.d.ts.map
@@ -0,0 +1,58 @@
1
+ import { getPropType } from '../index.js';
2
+ import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, ENUM, } from './types.js';
3
+ import { getPropLen, isSeparate } from './utils.js';
4
+ export const addEdges = (prop, refProp) => {
5
+ for (const key in refProp) {
6
+ if (key[0] === '$') {
7
+ if (!prop.edges) {
8
+ prop.edgeMainLen = 0;
9
+ prop.edges = {};
10
+ prop.reverseSeperateEdges = {};
11
+ prop.reverseMainEdges = {};
12
+ prop.edgesSeperateCnt = 0;
13
+ }
14
+ const edgeProp = refProp[key];
15
+ const edgeType = getPropType(edgeProp);
16
+ const len = getPropLen(edgeProp);
17
+ const separate = isSeparate(edgeProp, len);
18
+ if (separate) {
19
+ prop.edgesSeperateCnt++;
20
+ }
21
+ const edge = {
22
+ __isPropDef: true,
23
+ __isEdge: true,
24
+ prop: separate ? prop.edgesSeperateCnt : 0,
25
+ name: key,
26
+ typeIndex: TYPE_INDEX_MAP[edgeType],
27
+ len,
28
+ separate,
29
+ path: [...prop.path, key],
30
+ start: prop.edgeMainLen,
31
+ };
32
+ prop.edgeMainLen += edge.len;
33
+ if (edge.typeIndex === ENUM) {
34
+ edge.enum = Array.isArray(refProp[key])
35
+ ? refProp[key]
36
+ : refProp[key].enum;
37
+ edge.reverseEnum = {};
38
+ for (let i = 0; i < edge.enum.length; i++) {
39
+ edge.reverseEnum[edge.enum[i]] = i;
40
+ }
41
+ }
42
+ else if (edge.typeIndex === REFERENCES) {
43
+ edge.inverseTypeName = refProp[key].items.ref;
44
+ }
45
+ else if (edge.typeIndex === REFERENCE) {
46
+ edge.inverseTypeName = refProp[key].ref;
47
+ }
48
+ prop.edges[key] = edge;
49
+ if (separate) {
50
+ prop.reverseSeperateEdges[edge.prop] = edge;
51
+ }
52
+ else {
53
+ prop.reverseMainEdges[edge.start] = edge;
54
+ }
55
+ }
56
+ }
57
+ };
58
+ //# sourceMappingURL=addEdges.js.map
@@ -0,0 +1,37 @@
1
+ import { SchemaLocales, SchemaObject, StrictSchemaType } from '../types.js';
2
+ export declare const createEmptyDef: (typeName: string, type: StrictSchemaType | SchemaObject, locales: Partial<SchemaLocales>) => {
3
+ cnt: number;
4
+ blockCapacity: number;
5
+ checksum: number;
6
+ type: string;
7
+ props: {};
8
+ reverseProps: {};
9
+ idUint8: Uint8Array;
10
+ id: number;
11
+ mainLen: number;
12
+ separate: any[];
13
+ tree: {};
14
+ total: number;
15
+ lastId: number;
16
+ locales: {};
17
+ main: {};
18
+ localeSize: number;
19
+ hasSeperateSort: boolean;
20
+ seperateSort: {
21
+ size: number;
22
+ props: any[];
23
+ buffer: Uint8Array;
24
+ bufferTmp: Uint8Array;
25
+ };
26
+ hasSeperateTextSort: boolean;
27
+ seperateTextSort: {
28
+ size: number;
29
+ props: any[];
30
+ buffer: Uint8Array;
31
+ noUndefined: Uint8Array;
32
+ bufferTmp: Uint8Array;
33
+ localeStringToIndex: Map<any, any>;
34
+ localeToIndex: Map<any, any>;
35
+ };
36
+ };
37
+ //# sourceMappingURL=createEmptyDef.d.ts.map
@@ -0,0 +1,39 @@
1
+ import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
2
+ export const createEmptyDef = (typeName, type, locales) => {
3
+ return {
4
+ cnt: 0,
5
+ blockCapacity: 0,
6
+ checksum: hashObjectIgnoreKeyOrder(type),
7
+ type: typeName,
8
+ props: {},
9
+ reverseProps: {},
10
+ idUint8: new Uint8Array([0, 0]),
11
+ id: 0,
12
+ mainLen: 0,
13
+ separate: [],
14
+ tree: {},
15
+ total: 0,
16
+ lastId: 0,
17
+ locales: {},
18
+ main: {},
19
+ localeSize: 0,
20
+ hasSeperateSort: false,
21
+ seperateSort: {
22
+ size: 0,
23
+ props: [],
24
+ buffer: new Uint8Array([]),
25
+ bufferTmp: new Uint8Array([]),
26
+ },
27
+ hasSeperateTextSort: false,
28
+ seperateTextSort: {
29
+ size: 0, // prop len
30
+ props: [],
31
+ buffer: new Uint8Array([]),
32
+ noUndefined: new Uint8Array(new Array(Object.keys(locales).length).fill(0)),
33
+ bufferTmp: new Uint8Array([]),
34
+ localeStringToIndex: new Map(),
35
+ localeToIndex: new Map(),
36
+ },
37
+ };
38
+ };
39
+ //# sourceMappingURL=createEmptyDef.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaProp } from '../types.js';
2
+ export declare function getPropLen(schemaProp: SchemaProp): any;
3
+ //# sourceMappingURL=getPropLen.d.ts.map
@@ -0,0 +1,23 @@
1
+ import { getPropType } from '../parse/utils.js';
2
+ import { isPropType } from '../types.js';
3
+ import { SIZE_MAP } from './types.js';
4
+ export function getPropLen(schemaProp) {
5
+ let len = SIZE_MAP[getPropType(schemaProp)];
6
+ if (isPropType('string', schemaProp) ||
7
+ isPropType('alias', schemaProp) ||
8
+ isPropType('cardinality', schemaProp)) {
9
+ if (typeof schemaProp === 'object') {
10
+ if (schemaProp.maxBytes < 61) {
11
+ len = schemaProp.maxBytes + 1;
12
+ }
13
+ else if ('max' in schemaProp && schemaProp.max < 31) {
14
+ len = schemaProp.max * 2 + 1;
15
+ }
16
+ }
17
+ }
18
+ else if (isPropType('vector', schemaProp)) {
19
+ len = 4 * schemaProp.size;
20
+ }
21
+ return len;
22
+ }
23
+ //# sourceMappingURL=getPropLen.js.map
@@ -3,4 +3,5 @@ export * from './typeDef.js';
3
3
  export * from './utils.js';
4
4
  export * from './selvaBuffer.js';
5
5
  export * from './readFromPacked.js';
6
+ export * from './createEmptyDef.js';
6
7
  //# sourceMappingURL=index.d.ts.map
package/dist/def/index.js CHANGED
@@ -4,4 +4,5 @@ export * from './typeDef.js';
4
4
  export * from './utils.js';
5
5
  export * from './selvaBuffer.js';
6
6
  export * from './readFromPacked.js';
7
+ export * from './createEmptyDef.js';
7
8
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaTypeDef, PropDef } from './types.js';
2
+ export declare function makePacked(result: Partial<SchemaTypeDef>, typeName: string, vals: PropDef[], len: number): void;
3
+ //# sourceMappingURL=makePacked.d.ts.map
@@ -0,0 +1,50 @@
1
+ export function makePacked(result, typeName, vals, len) {
2
+ const encoder = new TextEncoder();
3
+ result.buf = new Uint8Array(len);
4
+ result.buf[0] = result.idUint8[0];
5
+ result.buf[1] = result.idUint8[1];
6
+ const fieldNames = [];
7
+ const tNameBuf = encoder.encode(typeName);
8
+ fieldNames.push(tNameBuf);
9
+ let fieldNameLen = tNameBuf.byteLength + 1;
10
+ let i = 2;
11
+ if (result.mainLen) {
12
+ result.buf[i] = 0;
13
+ for (const f of vals) {
14
+ if (!f.separate) {
15
+ i++;
16
+ result.buf[i] = f.typeIndex;
17
+ const name = encoder.encode(f.path.join('.'));
18
+ fieldNames.push(name);
19
+ fieldNameLen += name.byteLength + 1;
20
+ }
21
+ }
22
+ i++;
23
+ result.buf[i] = 0;
24
+ }
25
+ for (const f of vals) {
26
+ if (f.separate) {
27
+ i++;
28
+ result.buf[i] = f.prop;
29
+ i++;
30
+ result.buf[i] = f.typeIndex;
31
+ const name = encoder.encode(f.path.join('.'));
32
+ fieldNames.push(name);
33
+ fieldNameLen += name.byteLength + 1;
34
+ }
35
+ }
36
+ result.propNames = new Uint8Array(fieldNameLen);
37
+ let lastWritten = 0;
38
+ for (const f of fieldNames) {
39
+ result.propNames[lastWritten] = f.byteLength;
40
+ result.propNames.set(f, lastWritten + 1);
41
+ lastWritten += f.byteLength + 1;
42
+ }
43
+ let bufLen = result.buf.length;
44
+ result.packed = new Uint8Array(2 + bufLen + result.propNames.length);
45
+ result.packed[0] = bufLen;
46
+ result.packed[1] = bufLen >>>= 8;
47
+ result.packed.set(result.buf, 2);
48
+ result.packed.set(result.propNames, result.buf.length + 2);
49
+ }
50
+ //# sourceMappingURL=makePacked.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaTypeDef } from './types.js';
2
+ export declare function makeSeparateSort(result: Partial<SchemaTypeDef>): void;
3
+ //# sourceMappingURL=makeSeparateSort.d.ts.map
@@ -0,0 +1,27 @@
1
+ import { STRING, ALIAS, CARDINALITY } from './types.js';
2
+ export function makeSeparateSort(result) {
3
+ result.hasSeperateSort = true;
4
+ let max = 0;
5
+ for (const f of result.separate) {
6
+ if (f.typeIndex === STRING ||
7
+ f.typeIndex === ALIAS ||
8
+ f.typeIndex === CARDINALITY) {
9
+ if (f.prop > max) {
10
+ max = f.prop;
11
+ }
12
+ }
13
+ }
14
+ result.seperateSort.buffer = new Uint8Array(max + 1);
15
+ for (const f of result.separate) {
16
+ if (f.typeIndex === STRING ||
17
+ f.typeIndex === ALIAS ||
18
+ f.typeIndex === CARDINALITY) {
19
+ result.seperateSort.buffer[f.prop] = 1;
20
+ result.seperateSort.props.push(f);
21
+ result.seperateSort.size++;
22
+ }
23
+ }
24
+ result.seperateSort.bufferTmp = new Uint8Array(max + 1);
25
+ result.seperateSort.buffer.set(result.seperateSort.bufferTmp);
26
+ }
27
+ //# sourceMappingURL=makeSeparateSort.js.map
@@ -0,0 +1,3 @@
1
+ import { SchemaTypeDef } from './types.js';
2
+ export declare function makeSeparateTextSort(result: Partial<SchemaTypeDef>): void;
3
+ //# sourceMappingURL=makeSeparateTextSort.d.ts.map
@@ -0,0 +1,38 @@
1
+ import { langCodesMap } from '../lang.js';
2
+ import { TEXT } from './types.js';
3
+ export function makeSeparateTextSort(result) {
4
+ result.hasSeperateTextSort = true;
5
+ let max = 0;
6
+ for (const f of result.separate) {
7
+ if (f.typeIndex === TEXT) {
8
+ if (f.prop > max) {
9
+ max = f.prop;
10
+ }
11
+ }
12
+ }
13
+ const bufLen = (max + 1) * (result.localeSize + 1);
14
+ result.seperateTextSort.buffer = new Uint8Array(bufLen);
15
+ let index = 0;
16
+ for (const code in result.locales) {
17
+ const codeLang = langCodesMap.get(code);
18
+ result.seperateTextSort.localeStringToIndex.set(code, new Uint8Array([index + 1, codeLang]));
19
+ result.seperateTextSort.localeToIndex.set(codeLang, index + 1);
20
+ index++;
21
+ }
22
+ for (const f of result.separate) {
23
+ if (f.typeIndex === TEXT) {
24
+ const index = f.prop * (result.localeSize + 1);
25
+ result.seperateTextSort.buffer[index] = result.localeSize;
26
+ for (const [, locales] of result.seperateTextSort.localeStringToIndex) {
27
+ result.seperateTextSort.buffer[locales[0] + index] = locales[1];
28
+ }
29
+ result.seperateTextSort.props.push(f);
30
+ result.seperateTextSort.size += result.localeSize;
31
+ }
32
+ }
33
+ result.seperateTextSort.props.sort((a, b) => (a.prop > b.prop ? 1 : -1));
34
+ result.seperateTextSort.bufferTmp = new Uint8Array(bufLen);
35
+ result.seperateTextSort.bufferTmp.fill(0);
36
+ result.seperateTextSort.bufferTmp.set(result.seperateTextSort.buffer);
37
+ }
38
+ //# sourceMappingURL=makeSeparateTextSort.js.map
@@ -78,6 +78,9 @@ export const readFromPacked = (packed) => {
78
78
  },
79
79
  hasSeperateTextSort: false,
80
80
  seperateTextSort: {
81
+ localeToIndex: new Map(),
82
+ localeStringToIndex: new Map(),
83
+ noUndefined: new Uint8Array([]),
81
84
  size: 0,
82
85
  buffer: new Uint8Array([]),
83
86
  bufferTmp: new Uint8Array([]),
@@ -90,7 +93,7 @@ export const readFromPacked = (packed) => {
90
93
  let s = 0;
91
94
  for (const p of mainProps) {
92
95
  const len = REVERSE_SIZE_MAP[p.typeIndex];
93
- result.props[p.path] = {
96
+ const prop = {
94
97
  prop: p.prop,
95
98
  separate: false,
96
99
  __isPropDef: true,
@@ -99,10 +102,12 @@ export const readFromPacked = (packed) => {
99
102
  path: p.path.split('.'),
100
103
  len,
101
104
  };
105
+ result.props[p.path] = prop;
106
+ result.main[prop.start] = prop;
102
107
  s += len;
103
108
  }
104
109
  for (const p of props) {
105
- result.props[p.path] = {
110
+ const prop = {
106
111
  prop: p.prop,
107
112
  separate: true,
108
113
  __isPropDef: true,
@@ -112,6 +117,8 @@ export const readFromPacked = (packed) => {
112
117
  len: 0,
113
118
  compression: 1,
114
119
  };
120
+ result.props[p.path] = prop;
121
+ result.reverseProps[prop.prop] = prop;
115
122
  }
116
123
  // make this into a typeDef
117
124
  // return {
@@ -1,5 +1,5 @@
1
1
  import { SchemaTypeDef } from './types.js';
2
2
  export declare function schemaToSelvaBuffer(schema: {
3
3
  [key: string]: SchemaTypeDef;
4
- }): Buffer[];
4
+ }): ArrayBuffer[];
5
5
  //# sourceMappingURL=selvaBuffer.d.ts.map
@@ -1,35 +1,23 @@
1
- import { ALIAS, ALIASES, BINARY, BOOLEAN, CREATED, EMPTY_MICRO_BUFFER, ENUM, CARDINALITY, INT16, INT32, 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] = 4;
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[BOOLEAN] = 9;
16
- selvaTypeMap[ENUM] = 10;
17
- selvaTypeMap[STRING] = 11;
18
- selvaTypeMap[TEXT] = 12;
19
- selvaTypeMap[REFERENCE] = 13;
20
- selvaTypeMap[REFERENCES] = 14;
21
- selvaTypeMap[WEAK_REFERENCE] = 15;
22
- selvaTypeMap[WEAK_REFERENCES] = 16;
23
- selvaTypeMap[MICRO_BUFFER] = 17;
24
- selvaTypeMap[ALIAS] = 18;
25
- selvaTypeMap[ALIASES] = 19;
26
- selvaTypeMap[BINARY] = 11;
27
- selvaTypeMap[VECTOR] = 17;
28
- selvaTypeMap[JSON] = 11;
1
+ import { ALIAS, ALIASES, BINARY, EMPTY_MICRO_BUFFER, CARDINALITY, MICRO_BUFFER, REFERENCE, REFERENCES, STRING, TEXT, VECTOR, WEAK_REFERENCE, WEAK_REFERENCES, JSON, } from './types.js';
2
+ const selvaTypeMap = new Uint8Array(32); // 1.2x faster than JS array
3
+ selvaTypeMap[MICRO_BUFFER] = 1;
4
+ selvaTypeMap[VECTOR] = 1;
5
+ selvaTypeMap[BINARY] = 2;
6
+ selvaTypeMap[CARDINALITY] = 2;
7
+ selvaTypeMap[JSON] = 2;
8
+ selvaTypeMap[STRING] = 2;
9
+ selvaTypeMap[TEXT] = 3;
10
+ selvaTypeMap[REFERENCE] = 4;
11
+ selvaTypeMap[REFERENCES] = 5;
12
+ selvaTypeMap[WEAK_REFERENCE] = 6;
13
+ selvaTypeMap[WEAK_REFERENCES] = 7;
14
+ selvaTypeMap[ALIAS] = 8;
15
+ selvaTypeMap[ALIASES] = 9;
29
16
  const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
30
17
  function blockCapacity(blockCapacity) {
31
- const buf = Buffer.allocUnsafe(4);
32
- buf.writeInt32LE(blockCapacity);
18
+ const buf = new Uint8Array(Uint32Array.BYTES_PER_ELEMENT);
19
+ const view = new DataView(buf.buffer);
20
+ view.setUint32(0, blockCapacity, true);
33
21
  return buf;
34
22
  }
35
23
  function sepPropCount(props) {
@@ -42,34 +30,50 @@ const propDefBuffer = (schema, prop, isEdge) => {
42
30
  const type = prop.typeIndex;
43
31
  const selvaType = selvaTypeMap[type];
44
32
  if (prop.len && (type === MICRO_BUFFER || type === VECTOR)) {
45
- const buf = Buffer.allocUnsafe(3);
33
+ const buf = new Uint8Array(3);
34
+ const view = new DataView(buf.buffer);
46
35
  buf[0] = selvaType;
47
- buf.writeUint16LE(prop.len, 1);
48
- return [...buf.values()];
36
+ view.setUint16(1, prop.len, true);
37
+ return [...buf];
49
38
  }
50
39
  else if (type === REFERENCE || type === REFERENCES) {
51
- const buf = Buffer.allocUnsafe(9);
40
+ const buf = new Uint8Array(9);
41
+ const view = new DataView(buf.buffer);
52
42
  const dstType = schema[prop.inverseTypeName];
53
43
  let eschema = [];
54
44
  // @ts-ignore
55
45
  buf[0] = selvaType + 2 * !!isEdge; // field type
56
46
  buf[1] = makeEdgeConstraintFlags(prop); // flags
57
- buf.writeUInt16LE(dstType.id, 2); // dst_node_type
58
- buf.writeUint32LE(0, 5); // schema_len
47
+ view.setUint16(2, dstType.id, true); // dst_node_type
48
+ view.setUint32(5, 0, true); // schema_len
59
49
  if (!isEdge) {
60
50
  prop.inverseTypeId = dstType.id;
61
51
  prop.inversePropNumber = dstType.props[prop.inversePropName].prop;
62
52
  buf[4] = prop.inversePropNumber;
63
53
  if (prop.edges) {
64
- const props = Object.values(prop.edges);
65
- eschema = props
66
- .map((prop) => propDefBuffer(schema, prop, true))
67
- .flat(1);
68
- eschema.unshift(0, 0, 0, 0, sepPropCount(props), 0);
69
- buf.writeUint32LE(eschema.length, 5);
54
+ const edgesS = Object.values(prop.edges);
55
+ if (edgesS.length) {
56
+ const props = edgesS
57
+ .filter((v) => v.separate === true)
58
+ .sort((a, b) => (a.prop > b.prop ? 1 : -1));
59
+ const p = [
60
+ {
61
+ ...EMPTY_MICRO_BUFFER,
62
+ len: prop.edgeMainLen || 1, // allow zero here... else useless padding
63
+ __isEdgeDef: true,
64
+ },
65
+ // or handle this here...
66
+ ...props,
67
+ ];
68
+ eschema = p
69
+ .map((prop) => propDefBuffer(schema, prop, true))
70
+ .flat(1);
71
+ eschema.unshift(0, 0, 0, 0, sepPropCount(p), 0);
72
+ view.setUint32(5, eschema.length, true);
73
+ }
70
74
  }
71
75
  }
72
- return [...buf.values(), ...eschema];
76
+ return [...buf, ...eschema];
73
77
  }
74
78
  else if (type === STRING ||
75
79
  type === BINARY ||
@@ -81,12 +85,8 @@ const propDefBuffer = (schema, prop, isEdge) => {
81
85
  return [selvaType];
82
86
  }
83
87
  };
84
- // todo rewrite
88
+ // TODO rewrite
85
89
  export function schemaToSelvaBuffer(schema) {
86
- if (typeof Buffer === 'undefined') {
87
- // TMP
88
- return [];
89
- }
90
90
  return Object.values(schema).map((t, i) => {
91
91
  const props = Object.values(t.props);
92
92
  const rest = [];
@@ -100,8 +100,8 @@ export function schemaToSelvaBuffer(schema) {
100
100
  }
101
101
  }
102
102
  rest.sort((a, b) => a.prop - b.prop);
103
- return Buffer.from([
104
- ...blockCapacity(t.blockCapacity).values(),
103
+ return Uint8Array.from([
104
+ ...blockCapacity(t.blockCapacity),
105
105
  1 + sepPropCount(props),
106
106
  1 + refFields,
107
107
  ...propDefBuffer(schema, {
@@ -109,7 +109,7 @@ export function schemaToSelvaBuffer(schema) {
109
109
  len: t.mainLen === 0 ? 1 : t.mainLen,
110
110
  }),
111
111
  ...rest.map((f) => propDefBuffer(schema, f)).flat(1),
112
- ]);
112
+ ]).buffer;
113
113
  });
114
114
  }
115
115
  //# sourceMappingURL=selvaBuffer.js.map
@@ -1,174 +1,15 @@
1
1
  import { isPropType, getPropType, } from '../index.js';
2
2
  import { setByPath } from '@saulx/utils';
3
+ import { TYPE_INDEX_MAP, REFERENCES, REFERENCE, } from './types.js';
4
+ import { makePacked } from './makePacked.js';
5
+ import { makeSeparateTextSort } from './makeSeparateTextSort.js';
6
+ import { makeSeparateSort } from './makeSeparateSort.js';
7
+ import { getPropLen } from './getPropLen.js';
8
+ import { isSeparate } from './utils.js';
9
+ import { addEdges } from './addEdges.js';
10
+ import { createEmptyDef } from './createEmptyDef.js';
3
11
  import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
4
- import { SIZE_MAP, TYPE_INDEX_MAP, STRING, ALIAS, CARDINALITY, REFERENCES, REFERENCE, TEXT, ENUM, } from './types.js';
5
- // TMP
6
12
  export const DEFAULT_BLOCK_CAPACITY = 100_000;
7
- function getPropLen(schemaProp) {
8
- let len = SIZE_MAP[getPropType(schemaProp)];
9
- if (isPropType('string', schemaProp) ||
10
- isPropType('alias', schemaProp) ||
11
- isPropType('cardinality', schemaProp)) {
12
- if (typeof schemaProp === 'object') {
13
- if (schemaProp.maxBytes < 61) {
14
- len = schemaProp.maxBytes + 1;
15
- }
16
- else if ('max' in schemaProp && schemaProp.max < 31) {
17
- len = schemaProp.max * 2 + 1;
18
- }
19
- }
20
- }
21
- else if (isPropType('vector', schemaProp)) {
22
- len = 4 * schemaProp.size;
23
- }
24
- return len;
25
- }
26
- function isSeparate(schemaProp, len) {
27
- return len === 0 || isPropType('vector', schemaProp);
28
- }
29
- const addEdges = (prop, refProp) => {
30
- let edgesCnt = 0;
31
- for (const key in refProp) {
32
- if (key[0] === '$') {
33
- if (!prop.edges) {
34
- prop.edges = {};
35
- prop.reverseEdges = {};
36
- prop.edgesTotalLen = 0;
37
- }
38
- edgesCnt++;
39
- const edgeType = getPropType(refProp[key]);
40
- const edge = {
41
- __isPropDef: true,
42
- __isEdge: true,
43
- prop: edgesCnt,
44
- name: key,
45
- typeIndex: TYPE_INDEX_MAP[edgeType],
46
- len: SIZE_MAP[edgeType],
47
- separate: true,
48
- path: [...prop.path, key],
49
- };
50
- if (edge.len == 0) {
51
- prop.edgesTotalLen = 0;
52
- }
53
- else {
54
- // [field] [size] [data]
55
- prop.edgesTotalLen += 1 + 2 + edge.len; // field len
56
- }
57
- if (edge.typeIndex === ENUM) {
58
- edge.enum = Array.isArray(refProp[key])
59
- ? refProp[key]
60
- : refProp[key].enum;
61
- edge.reverseEnum = {};
62
- for (let i = 0; i < edge.enum.length; i++) {
63
- edge.reverseEnum[edge.enum[i]] = i;
64
- }
65
- }
66
- else if (edge.typeIndex === REFERENCES) {
67
- edge.inverseTypeName = refProp[key].items.ref;
68
- }
69
- else if (edge.typeIndex === REFERENCE) {
70
- edge.inverseTypeName = refProp[key].ref;
71
- }
72
- prop.edges[key] = edge;
73
- prop.reverseEdges[edge.prop] = edge;
74
- }
75
- }
76
- };
77
- function makePacked(result, typeName, vals, len) {
78
- const encoder = new TextEncoder();
79
- result.buf = new Uint8Array(len);
80
- result.buf[0] = result.idUint8[0];
81
- result.buf[1] = result.idUint8[1];
82
- const fieldNames = [];
83
- const tNameBuf = encoder.encode(typeName);
84
- fieldNames.push(tNameBuf);
85
- let fieldNameLen = tNameBuf.byteLength + 1;
86
- let i = 2;
87
- if (result.mainLen) {
88
- result.buf[i] = 0;
89
- for (const f of vals) {
90
- if (!f.separate) {
91
- i++;
92
- result.buf[i] = f.typeIndex;
93
- const name = encoder.encode(f.path.join('.'));
94
- fieldNames.push(name);
95
- fieldNameLen += name.byteLength + 1;
96
- }
97
- }
98
- i++;
99
- result.buf[i] = 0;
100
- }
101
- for (const f of vals) {
102
- if (f.separate) {
103
- i++;
104
- result.buf[i] = f.prop;
105
- i++;
106
- result.buf[i] = f.typeIndex;
107
- const name = encoder.encode(f.path.join('.'));
108
- fieldNames.push(name);
109
- fieldNameLen += name.byteLength + 1;
110
- }
111
- }
112
- result.propNames = new Uint8Array(fieldNameLen);
113
- let lastWritten = 0;
114
- for (const f of fieldNames) {
115
- result.propNames[lastWritten] = f.byteLength;
116
- result.propNames.set(f, lastWritten + 1);
117
- lastWritten += f.byteLength + 1;
118
- }
119
- let bufLen = result.buf.length;
120
- result.packed = new Uint8Array(2 + bufLen + result.propNames.length);
121
- result.packed[0] = bufLen;
122
- result.packed[1] = bufLen >>>= 8;
123
- result.packed.set(result.buf, 2);
124
- result.packed.set(result.propNames, result.buf.length + 2);
125
- }
126
- function makeSeparateTextSort(result) {
127
- result.hasSeperateTextSort = true;
128
- let max = 0;
129
- for (const f of result.separate) {
130
- if (f.typeIndex === TEXT) {
131
- if (f.prop > max) {
132
- max = f.prop;
133
- }
134
- }
135
- }
136
- result.seperateTextSort.buffer = new Uint8Array(max * result.localeSize + 1);
137
- for (const f of result.separate) {
138
- if (f.typeIndex === TEXT) {
139
- result.seperateTextSort.buffer[f.prop] = 1;
140
- result.seperateTextSort.props.push(f);
141
- result.seperateTextSort.size += result.localeSize;
142
- }
143
- }
144
- result.seperateTextSort.bufferTmp = new Uint8Array(max * result.localeSize + 1);
145
- result.seperateTextSort.buffer.set(result.seperateTextSort.bufferTmp);
146
- }
147
- function makeSeparateSort(result) {
148
- result.hasSeperateSort = true;
149
- let max = 0;
150
- for (const f of result.separate) {
151
- if (f.typeIndex === STRING ||
152
- f.typeIndex === ALIAS ||
153
- f.typeIndex === CARDINALITY) {
154
- if (f.prop > max) {
155
- max = f.prop;
156
- }
157
- }
158
- }
159
- result.seperateSort.buffer = new Uint8Array(max + 1);
160
- for (const f of result.separate) {
161
- if (f.typeIndex === STRING ||
162
- f.typeIndex === ALIAS ||
163
- f.typeIndex === CARDINALITY) {
164
- result.seperateSort.buffer[f.prop] = 1;
165
- result.seperateSort.props.push(f);
166
- result.seperateSort.size++;
167
- }
168
- }
169
- result.seperateSort.bufferTmp = new Uint8Array(max + 1);
170
- result.seperateSort.buffer.set(result.seperateSort.bufferTmp);
171
- }
172
13
  export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById) => {
173
14
  for (const field in schemaTypesParsed) {
174
15
  if (field in schema.types) {
@@ -199,35 +40,7 @@ export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById)
199
40
  }
200
41
  }
201
42
  };
202
- export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
203
- cnt: 0,
204
- checksum: hashObjectIgnoreKeyOrder(type),
205
- type: typeName,
206
- props: {},
207
- reverseProps: {},
208
- idUint8: new Uint8Array([0, 0]),
209
- id: 0,
210
- mainLen: 0,
211
- separate: [],
212
- tree: {},
213
- total: 0,
214
- lastId: 0,
215
- main: {},
216
- hasSeperateSort: false,
217
- seperateSort: {
218
- size: 0,
219
- props: [],
220
- buffer: new Uint8Array([]),
221
- bufferTmp: new Uint8Array([]),
222
- },
223
- hasSeperateTextSort: false,
224
- seperateTextSort: {
225
- size: 0, // prop len
226
- props: [],
227
- buffer: new Uint8Array([]),
228
- bufferTmp: new Uint8Array([]),
229
- },
230
- }, path = [], top = true) => {
43
+ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = createEmptyDef(typeName, type, locales), path = [], top = true) => {
231
44
  if (result.id == 0 && top) {
232
45
  if ('id' in type) {
233
46
  result.id = type.id;
@@ -256,7 +69,8 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
256
69
  isPropType('alias', schemaProp) ||
257
70
  isPropType('cardinality', schemaProp)) {
258
71
  if (typeof schemaProp === 'object') {
259
- if (!(schemaProp.maxBytes < 61) || !('max' in schemaProp && schemaProp.max < 31)) {
72
+ if (!(schemaProp.maxBytes < 61) ||
73
+ !('max' in schemaProp && schemaProp.max < 31)) {
260
74
  separateSortProps++;
261
75
  }
262
76
  }
@@ -1,4 +1,4 @@
1
- import type { SchemaLocales } from '../index.js';
1
+ import type { LangCode, SchemaLocales } from '../index.js';
2
2
  export declare const NULL = 0;
3
3
  export declare const TIMESTAMP = 1;
4
4
  export declare const CREATED = 2;
@@ -68,18 +68,22 @@ export type PropDef = {
68
68
  inverseTypeId?: number;
69
69
  inversePropNumber?: number;
70
70
  enum?: any[];
71
+ dependent?: boolean;
72
+ edgeMainLen?: 0;
71
73
  reverseEnum?: {
72
74
  [key: string]: number;
73
75
  };
74
- edgesTotalLen?: number;
76
+ edgesSeperateCnt?: number;
75
77
  edges?: {
76
78
  [key: string]: PropDefEdge;
77
79
  };
78
- reverseEdges?: {
80
+ reverseSeperateEdges?: {
79
81
  [prop: string]: PropDefEdge;
80
82
  };
83
+ reverseMainEdges?: {
84
+ [start: string]: PropDefEdge;
85
+ };
81
86
  __isEdge?: boolean;
82
- dependent?: boolean;
83
87
  };
84
88
  export type PropDefEdge = Partial<PropDef> & {
85
89
  __isPropDef: true;
@@ -90,6 +94,13 @@ export type PropDefEdge = Partial<PropDef> & {
90
94
  edgesTotalLen?: number;
91
95
  __isEdge: true;
92
96
  };
97
+ export type PropDefAggregate = Partial<PropDef> & {
98
+ __isPropDef: true;
99
+ typeIndex: TypeIndex;
100
+ len: number;
101
+ prop: number;
102
+ name: string;
103
+ };
93
104
  export type SchemaPropTree = {
94
105
  [key: string]: SchemaPropTree | PropDef;
95
106
  };
@@ -126,7 +137,11 @@ export type SchemaTypeDef = {
126
137
  hasSeperateSort: boolean;
127
138
  seperateSort: SchemaSortUndefinedHandler;
128
139
  hasSeperateTextSort: boolean;
129
- seperateTextSort: SchemaSortUndefinedHandler;
140
+ seperateTextSort: SchemaSortUndefinedHandler & {
141
+ noUndefined: Uint8Array;
142
+ localeStringToIndex: Map<string, Uint8Array>;
143
+ localeToIndex: Map<LangCode, number>;
144
+ };
130
145
  createTs?: PropDef[];
131
146
  updateTs?: PropDef[];
132
147
  locales: Partial<SchemaLocales>;
@@ -1,4 +1,7 @@
1
1
  import { PropDef, PropDefEdge } from './types.js';
2
+ import { SchemaProp } from '../types.js';
3
+ export declare function isSeparate(schemaProp: SchemaProp, len: number): boolean;
2
4
  export declare const propIsSigned: (prop: PropDef | PropDefEdge) => boolean;
3
5
  export declare const propIsNumerical: (prop: PropDef | PropDefEdge) => boolean;
6
+ export declare function getPropLen(schemaProp: SchemaProp): any;
4
7
  //# sourceMappingURL=utils.d.ts.map
package/dist/def/utils.js CHANGED
@@ -1,4 +1,9 @@
1
- import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, } from './types.js';
1
+ import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, SIZE_MAP, } from './types.js';
2
+ import { isPropType } from '../types.js';
3
+ import { getPropType } from '../parse/utils.js';
4
+ export function isSeparate(schemaProp, len) {
5
+ return len === 0 || isPropType('vector', schemaProp);
6
+ }
2
7
  export const propIsSigned = (prop) => {
3
8
  const t = prop.typeIndex;
4
9
  if (t === INT16 || t === INT32 || t === INT8) {
@@ -20,4 +25,23 @@ export const propIsNumerical = (prop) => {
20
25
  }
21
26
  return false;
22
27
  };
28
+ export function getPropLen(schemaProp) {
29
+ let len = SIZE_MAP[getPropType(schemaProp)];
30
+ if (isPropType('string', schemaProp) ||
31
+ isPropType('alias', schemaProp) ||
32
+ isPropType('cardinality', schemaProp)) {
33
+ if (typeof schemaProp === 'object') {
34
+ if (schemaProp.maxBytes < 61) {
35
+ len = schemaProp.maxBytes + 1;
36
+ }
37
+ else if ('max' in schemaProp && schemaProp.max < 31) {
38
+ len = schemaProp.max * 2 + 1;
39
+ }
40
+ }
41
+ }
42
+ else if (isPropType('vector', schemaProp)) {
43
+ len = 4 * schemaProp.size;
44
+ }
45
+ return len;
46
+ }
23
47
  //# sourceMappingURL=utils.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.5",
3
+ "version": "5.0.0-alpha.7",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",