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

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.
@@ -1,7 +1,7 @@
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';
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
2
  const selvaTypeMap = [];
3
3
  selvaTypeMap[NULL] = 0;
4
- selvaTypeMap[TIMESTAMP] = 1;
4
+ selvaTypeMap[TIMESTAMP] = 4;
5
5
  selvaTypeMap[CREATED] = 1;
6
6
  selvaTypeMap[UPDATED] = 1;
7
7
  selvaTypeMap[NUMBER] = 4;
@@ -12,7 +12,6 @@ selvaTypeMap[INT16] = 21;
12
12
  selvaTypeMap[UINT16] = 22;
13
13
  selvaTypeMap[INT32] = 23;
14
14
  selvaTypeMap[UINT32] = 7;
15
- selvaTypeMap[INT64] = 24;
16
15
  selvaTypeMap[BOOLEAN] = 9;
17
16
  selvaTypeMap[ENUM] = 10;
18
17
  selvaTypeMap[STRING] = 11;
@@ -28,6 +27,11 @@ selvaTypeMap[BINARY] = 11;
28
27
  selvaTypeMap[VECTOR] = 17;
29
28
  selvaTypeMap[JSON] = 11;
30
29
  const EDGE_FIELD_CONSTRAINT_FLAG_DEPENDENT = 0x01;
30
+ function blockCapacity(blockCapacity) {
31
+ const buf = Buffer.allocUnsafe(4);
32
+ buf.writeInt32LE(blockCapacity);
33
+ return buf;
34
+ }
31
35
  function sepPropCount(props) {
32
36
  return props.filter((prop) => prop.separate).length;
33
37
  }
@@ -77,11 +81,6 @@ const propDefBuffer = (schema, prop, isEdge) => {
77
81
  return [selvaType];
78
82
  }
79
83
  };
80
- function makeBlockCapacityBuffer(blockCapacity) {
81
- const buf = Buffer.allocUnsafe(4);
82
- buf.writeInt32LE(blockCapacity);
83
- return buf;
84
- }
85
84
  // todo rewrite
86
85
  export function schemaToSelvaBuffer(schema) {
87
86
  if (typeof Buffer === 'undefined') {
@@ -102,7 +101,7 @@ export function schemaToSelvaBuffer(schema) {
102
101
  }
103
102
  rest.sort((a, b) => a.prop - b.prop);
104
103
  return Buffer.from([
105
- ...makeBlockCapacityBuffer(t.blockCapacity).values(),
104
+ ...blockCapacity(t.blockCapacity).values(),
106
105
  1 + sepPropCount(props),
107
106
  1 + refFields,
108
107
  ...propDefBuffer(schema, {
@@ -1,9 +1,31 @@
1
1
  import { isPropType, getPropType, } from '../index.js';
2
2
  import { setByPath } from '@saulx/utils';
3
3
  import { hashObjectIgnoreKeyOrder } from '@saulx/hash';
4
- import { SIZE_MAP, TYPE_INDEX_MAP, STRING, ALIAS, CARDINALITY, REFERENCES, REFERENCE, TEXT, } from './types.js';
4
+ import { SIZE_MAP, TYPE_INDEX_MAP, STRING, ALIAS, CARDINALITY, REFERENCES, REFERENCE, TEXT, ENUM, } from './types.js';
5
5
  // TMP
6
6
  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
+ }
7
29
  const addEdges = (prop, refProp) => {
8
30
  let edgesCnt = 0;
9
31
  for (const key in refProp) {
@@ -32,7 +54,7 @@ const addEdges = (prop, refProp) => {
32
54
  // [field] [size] [data]
33
55
  prop.edgesTotalLen += 1 + 2 + edge.len; // field len
34
56
  }
35
- if (edge.typeIndex === 10) {
57
+ if (edge.typeIndex === ENUM) {
36
58
  edge.enum = Array.isArray(refProp[key])
37
59
  ? refProp[key]
38
60
  : refProp[key].enum;
@@ -41,10 +63,10 @@ const addEdges = (prop, refProp) => {
41
63
  edge.reverseEnum[edge.enum[i]] = i;
42
64
  }
43
65
  }
44
- else if (edge.typeIndex === 14) {
66
+ else if (edge.typeIndex === REFERENCES) {
45
67
  edge.inverseTypeName = refProp[key].items.ref;
46
68
  }
47
- else if (edge.typeIndex === 13) {
69
+ else if (edge.typeIndex === REFERENCE) {
48
70
  edge.inverseTypeName = refProp[key].ref;
49
71
  }
50
72
  prop.edges[key] = edge;
@@ -52,6 +74,101 @@ const addEdges = (prop, refProp) => {
52
74
  }
53
75
  }
54
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
+ }
55
172
  export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById) => {
56
173
  for (const field in schemaTypesParsed) {
57
174
  if (field in schema.types) {
@@ -123,7 +240,6 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
123
240
  result.localeSize = Object.keys(locales).length;
124
241
  result.idUint8[0] = result.id & 255;
125
242
  result.idUint8[1] = result.id >> 8;
126
- const encoder = new TextEncoder();
127
243
  const target = type.props;
128
244
  let separateSortProps = 0;
129
245
  let separateSortText = 0;
@@ -135,18 +251,12 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
135
251
  createSchemaTypeDef(typeName, schemaProp, parsed, locales, result, propPath, false);
136
252
  }
137
253
  else {
138
- let len = SIZE_MAP[propType];
254
+ const len = getPropLen(schemaProp);
139
255
  if (isPropType('string', schemaProp) ||
140
256
  isPropType('alias', schemaProp) ||
141
257
  isPropType('cardinality', schemaProp)) {
142
258
  if (typeof schemaProp === 'object') {
143
- if (schemaProp.maxBytes < 61) {
144
- len = schemaProp.maxBytes + 1;
145
- }
146
- else if ('max' in schemaProp && schemaProp.max < 31) {
147
- len = schemaProp.max * 2 + 1;
148
- }
149
- else {
259
+ if (!(schemaProp.maxBytes < 61) || !('max' in schemaProp && schemaProp.max < 31)) {
150
260
  separateSortProps++;
151
261
  }
152
262
  }
@@ -154,16 +264,10 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
154
264
  separateSortProps++;
155
265
  }
156
266
  }
157
- else if (isPropType('vector', schemaProp)) {
158
- len = 4 * schemaProp.size;
159
- }
160
267
  else if (isPropType('text', schemaProp)) {
161
268
  separateSortText++;
162
269
  }
163
- const isseparate = len === 0 || isPropType('vector', schemaProp);
164
- if (isseparate) {
165
- result.cnt++;
166
- }
270
+ const isseparate = isSeparate(schemaProp, len);
167
271
  const prop = {
168
272
  typeIndex: TYPE_INDEX_MAP[propType],
169
273
  __isPropDef: true,
@@ -171,7 +275,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
171
275
  path: propPath,
172
276
  start: 0,
173
277
  len,
174
- prop: isseparate ? result.cnt : 0,
278
+ prop: isseparate ? ++result.cnt : 0,
175
279
  };
176
280
  if (isPropType('enum', schemaProp)) {
177
281
  prop.enum = Array.isArray(schemaProp) ? schemaProp : schemaProp.enum;
@@ -231,8 +335,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
231
335
  let lastProp = 0;
232
336
  for (const p of vals) {
233
337
  if (p.separate) {
234
- lastProp++;
235
- p.prop = lastProp;
338
+ p.prop = ++lastProp;
236
339
  }
237
340
  }
238
341
  let len = 2;
@@ -251,109 +354,12 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
251
354
  setByPath(result.tree, f.path, f);
252
355
  }
253
356
  }
254
- const mainFields = [];
255
- const restFields = [];
256
- for (const f of vals) {
257
- if (f.separate) {
258
- restFields.push(f);
259
- }
260
- else {
261
- mainFields.push(f);
262
- }
263
- }
264
- // make packed version
265
- result.buf = new Uint8Array(len);
266
- result.buf[0] = result.idUint8[0];
267
- result.buf[1] = result.idUint8[1];
268
- const fieldNames = [];
269
- const tNameBuf = encoder.encode(typeName);
270
- fieldNames.push(tNameBuf);
271
- let fieldNameLen = tNameBuf.byteLength + 1;
272
- let i = 2;
273
- if (result.mainLen) {
274
- result.buf[i] = 0;
275
- for (const f of vals) {
276
- if (!f.separate) {
277
- i++;
278
- result.buf[i] = f.typeIndex;
279
- const name = encoder.encode(f.path.join('.'));
280
- fieldNames.push(name);
281
- fieldNameLen += name.byteLength + 1;
282
- }
283
- }
284
- i++;
285
- result.buf[i] = 0;
286
- }
287
- for (const f of vals) {
288
- if (f.separate) {
289
- i++;
290
- result.buf[i] = f.prop;
291
- i++;
292
- result.buf[i] = f.typeIndex;
293
- const name = encoder.encode(f.path.join('.'));
294
- fieldNames.push(name);
295
- fieldNameLen += name.byteLength + 1;
296
- }
297
- }
298
- result.propNames = new Uint8Array(fieldNameLen);
299
- let lastWritten = 0;
300
- for (const f of fieldNames) {
301
- result.propNames[lastWritten] = f.byteLength;
302
- result.propNames.set(f, lastWritten + 1);
303
- lastWritten += f.byteLength + 1;
304
- }
305
- let bufLen = result.buf.length;
306
- result.packed = new Uint8Array(2 + bufLen + result.propNames.length);
307
- result.packed[0] = bufLen;
308
- result.packed[1] = bufLen >>>= 8;
309
- result.packed.set(result.buf, 2);
310
- result.packed.set(result.propNames, result.buf.length + 2);
311
- // done making packed bversion
357
+ makePacked(result, typeName, vals, len);
312
358
  if (separateSortText > 0) {
313
- result.hasSeperateTextSort = true;
314
- let max = 0;
315
- for (const f of result.separate) {
316
- if (f.typeIndex === TEXT) {
317
- if (f.prop > max) {
318
- max = f.prop;
319
- }
320
- }
321
- }
322
- result.seperateTextSort.buffer = new Uint8Array(max * result.localeSize + 1);
323
- for (const f of result.separate) {
324
- if (f.typeIndex === TEXT) {
325
- result.seperateTextSort.buffer[f.prop] = 1;
326
- result.seperateTextSort.props.push(f);
327
- result.seperateTextSort.size += result.localeSize;
328
- }
329
- }
330
- result.seperateTextSort.bufferTmp = new Uint8Array(max * result.localeSize + 1);
331
- result.seperateTextSort.buffer.set(result.seperateTextSort.bufferTmp);
359
+ makeSeparateTextSort(result);
332
360
  }
333
361
  if (separateSortProps > 0) {
334
- result.hasSeperateSort = true;
335
- let max = 0;
336
- for (const f of result.separate) {
337
- if (f.typeIndex === STRING ||
338
- f.typeIndex === ALIAS ||
339
- f.typeIndex === CARDINALITY) {
340
- if (f.prop > max) {
341
- max = f.prop;
342
- }
343
- }
344
- }
345
- result.seperateSort.buffer = new Uint8Array(max + 1);
346
- for (const f of result.separate) {
347
- if (f.typeIndex === STRING ||
348
- f.typeIndex === ALIAS ||
349
- f.typeIndex === CARDINALITY) {
350
- result.seperateSort.buffer[f.prop] = 1;
351
- result.seperateSort.props.push(f);
352
- result.seperateSort.size++;
353
- }
354
- }
355
- result.seperateSort.bufferTmp = new Uint8Array(max + 1);
356
- result.seperateSort.buffer.set(result.seperateSort.bufferTmp);
362
+ makeSeparateSort(result);
357
363
  }
358
364
  for (const p in result.props) {
359
365
  const x = result.props[p];
@@ -11,7 +11,6 @@ export declare const INT16 = 21;
11
11
  export declare const UINT16 = 22;
12
12
  export declare const INT32 = 23;
13
13
  export declare const UINT32 = 7;
14
- export declare const INT64 = 24;
15
14
  export declare const BOOLEAN = 9;
16
15
  export declare const ENUM = 10;
17
16
  export declare const STRING = 11;
package/dist/def/types.js CHANGED
@@ -11,7 +11,6 @@ export const INT16 = 21;
11
11
  export const UINT16 = 22;
12
12
  export const INT32 = 23;
13
13
  export const UINT32 = 7;
14
- export const INT64 = 24;
15
14
  export const BOOLEAN = 9;
16
15
  export const ENUM = 10;
17
16
  export const STRING = 11;
package/dist/def/utils.js CHANGED
@@ -1,7 +1,7 @@
1
- import { INT16, INT32, INT64, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, } from './types.js';
1
+ import { INT16, INT32, INT8, UINT16, UINT32, UINT8, NUMBER, TIMESTAMP, } from './types.js';
2
2
  export const propIsSigned = (prop) => {
3
3
  const t = prop.typeIndex;
4
- if (t === INT16 || t === INT32 || t === INT64 || t === INT8) {
4
+ if (t === INT16 || t === INT32 || t === INT8) {
5
5
  return true;
6
6
  }
7
7
  return false;
@@ -10,7 +10,6 @@ export const propIsNumerical = (prop) => {
10
10
  const t = prop.typeIndex;
11
11
  if (t === INT16 ||
12
12
  t === INT32 ||
13
- t === INT64 ||
14
13
  t === INT8 ||
15
14
  t === UINT8 ||
16
15
  t === UINT16 ||
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/schema",
3
- "version": "5.0.0-alpha.4",
3
+ "version": "5.0.0-alpha.5",
4
4
  "license": "MIT",
5
5
  "files": [
6
6
  "dist",