@based/schema 5.0.0-alpha.4 → 5.0.0-alpha.6
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.
- package/dist/def/addEdges.d.ts +4 -0
- package/dist/def/addEdges.js +51 -0
- package/dist/def/createEmptyDef.d.ts +37 -0
- package/dist/def/createEmptyDef.js +39 -0
- package/dist/def/getPropLen.d.ts +3 -0
- package/dist/def/getPropLen.js +23 -0
- package/dist/def/index.d.ts +1 -0
- package/dist/def/index.js +1 -0
- package/dist/def/makePacked.d.ts +3 -0
- package/dist/def/makePacked.js +50 -0
- package/dist/def/makeSeparateSort.d.ts +3 -0
- package/dist/def/makeSeparateSort.js +27 -0
- package/dist/def/makeSeparateTextSort.d.ts +3 -0
- package/dist/def/makeSeparateTextSort.js +38 -0
- package/dist/def/readFromPacked.js +9 -2
- package/dist/def/selvaBuffer.d.ts +1 -1
- package/dist/def/selvaBuffer.js +23 -25
- package/dist/def/typeDef.js +18 -198
- package/dist/def/types.d.ts +6 -3
- package/dist/def/types.js +0 -1
- package/dist/def/utils.d.ts +3 -0
- package/dist/def/utils.js +26 -3
- package/dist/lang.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { getPropType } from '../index.js';
|
|
2
|
+
import { SIZE_MAP, TYPE_INDEX_MAP, REFERENCES, REFERENCE, ENUM, } from './types.js';
|
|
3
|
+
export const addEdges = (prop, refProp) => {
|
|
4
|
+
let edgesCnt = 0;
|
|
5
|
+
for (const key in refProp) {
|
|
6
|
+
if (key[0] === '$') {
|
|
7
|
+
if (!prop.edges) {
|
|
8
|
+
prop.edges = {};
|
|
9
|
+
prop.reverseEdges = {};
|
|
10
|
+
prop.edgesTotalLen = 0;
|
|
11
|
+
}
|
|
12
|
+
edgesCnt++;
|
|
13
|
+
const edgeType = getPropType(refProp[key]);
|
|
14
|
+
const edge = {
|
|
15
|
+
__isPropDef: true,
|
|
16
|
+
__isEdge: true,
|
|
17
|
+
prop: edgesCnt,
|
|
18
|
+
name: key,
|
|
19
|
+
typeIndex: TYPE_INDEX_MAP[edgeType],
|
|
20
|
+
len: SIZE_MAP[edgeType],
|
|
21
|
+
separate: true,
|
|
22
|
+
path: [...prop.path, key],
|
|
23
|
+
};
|
|
24
|
+
if (edge.len == 0) {
|
|
25
|
+
prop.edgesTotalLen = 0;
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
// [field] [size] [data]
|
|
29
|
+
prop.edgesTotalLen += 1 + 2 + edge.len; // field len
|
|
30
|
+
}
|
|
31
|
+
if (edge.typeIndex === ENUM) {
|
|
32
|
+
edge.enum = Array.isArray(refProp[key])
|
|
33
|
+
? refProp[key]
|
|
34
|
+
: refProp[key].enum;
|
|
35
|
+
edge.reverseEnum = {};
|
|
36
|
+
for (let i = 0; i < edge.enum.length; i++) {
|
|
37
|
+
edge.reverseEnum[edge.enum[i]] = i;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else if (edge.typeIndex === REFERENCES) {
|
|
41
|
+
edge.inverseTypeName = refProp[key].items.ref;
|
|
42
|
+
}
|
|
43
|
+
else if (edge.typeIndex === REFERENCE) {
|
|
44
|
+
edge.inverseTypeName = refProp[key].ref;
|
|
45
|
+
}
|
|
46
|
+
prop.edges[key] = edge;
|
|
47
|
+
prop.reverseEdges[edge.prop] = edge;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
//# 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,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
|
package/dist/def/index.d.ts
CHANGED
package/dist/def/index.js
CHANGED
|
@@ -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,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,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
|
-
|
|
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
|
-
|
|
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 {
|
package/dist/def/selvaBuffer.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { ALIAS, ALIASES, BINARY, BOOLEAN, CREATED, EMPTY_MICRO_BUFFER, ENUM, CARDINALITY, INT16, INT32,
|
|
2
|
-
const selvaTypeMap =
|
|
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 = new Uint8Array(32); // 1.2x faster than JS array
|
|
3
3
|
selvaTypeMap[NULL] = 0;
|
|
4
|
-
selvaTypeMap[TIMESTAMP] =
|
|
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,12 @@ 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 = new Uint8Array(Uint32Array.BYTES_PER_ELEMENT);
|
|
32
|
+
const view = new DataView(buf.buffer);
|
|
33
|
+
view.setUint32(0, blockCapacity, true);
|
|
34
|
+
return buf;
|
|
35
|
+
}
|
|
31
36
|
function sepPropCount(props) {
|
|
32
37
|
return props.filter((prop) => prop.separate).length;
|
|
33
38
|
}
|
|
@@ -38,20 +43,22 @@ const propDefBuffer = (schema, prop, isEdge) => {
|
|
|
38
43
|
const type = prop.typeIndex;
|
|
39
44
|
const selvaType = selvaTypeMap[type];
|
|
40
45
|
if (prop.len && (type === MICRO_BUFFER || type === VECTOR)) {
|
|
41
|
-
const buf =
|
|
46
|
+
const buf = new Uint8Array(3);
|
|
47
|
+
const view = new DataView(buf.buffer);
|
|
42
48
|
buf[0] = selvaType;
|
|
43
|
-
|
|
44
|
-
return [...buf
|
|
49
|
+
view.setUint16(1, prop.len, true);
|
|
50
|
+
return [...buf];
|
|
45
51
|
}
|
|
46
52
|
else if (type === REFERENCE || type === REFERENCES) {
|
|
47
|
-
const buf =
|
|
53
|
+
const buf = new Uint8Array(9);
|
|
54
|
+
const view = new DataView(buf.buffer);
|
|
48
55
|
const dstType = schema[prop.inverseTypeName];
|
|
49
56
|
let eschema = [];
|
|
50
57
|
// @ts-ignore
|
|
51
58
|
buf[0] = selvaType + 2 * !!isEdge; // field type
|
|
52
59
|
buf[1] = makeEdgeConstraintFlags(prop); // flags
|
|
53
|
-
|
|
54
|
-
|
|
60
|
+
view.setUint16(2, dstType.id, true); // dst_node_type
|
|
61
|
+
view.setUint32(5, 0, true); // schema_len
|
|
55
62
|
if (!isEdge) {
|
|
56
63
|
prop.inverseTypeId = dstType.id;
|
|
57
64
|
prop.inversePropNumber = dstType.props[prop.inversePropName].prop;
|
|
@@ -62,10 +69,10 @@ const propDefBuffer = (schema, prop, isEdge) => {
|
|
|
62
69
|
.map((prop) => propDefBuffer(schema, prop, true))
|
|
63
70
|
.flat(1);
|
|
64
71
|
eschema.unshift(0, 0, 0, 0, sepPropCount(props), 0);
|
|
65
|
-
|
|
72
|
+
view.setUint32(5, eschema.length, true);
|
|
66
73
|
}
|
|
67
74
|
}
|
|
68
|
-
return [...buf
|
|
75
|
+
return [...buf, ...eschema];
|
|
69
76
|
}
|
|
70
77
|
else if (type === STRING ||
|
|
71
78
|
type === BINARY ||
|
|
@@ -77,17 +84,8 @@ const propDefBuffer = (schema, prop, isEdge) => {
|
|
|
77
84
|
return [selvaType];
|
|
78
85
|
}
|
|
79
86
|
};
|
|
80
|
-
|
|
81
|
-
const buf = Buffer.allocUnsafe(4);
|
|
82
|
-
buf.writeInt32LE(blockCapacity);
|
|
83
|
-
return buf;
|
|
84
|
-
}
|
|
85
|
-
// todo rewrite
|
|
87
|
+
// TODO rewrite
|
|
86
88
|
export function schemaToSelvaBuffer(schema) {
|
|
87
|
-
if (typeof Buffer === 'undefined') {
|
|
88
|
-
// TMP
|
|
89
|
-
return [];
|
|
90
|
-
}
|
|
91
89
|
return Object.values(schema).map((t, i) => {
|
|
92
90
|
const props = Object.values(t.props);
|
|
93
91
|
const rest = [];
|
|
@@ -101,8 +99,8 @@ export function schemaToSelvaBuffer(schema) {
|
|
|
101
99
|
}
|
|
102
100
|
}
|
|
103
101
|
rest.sort((a, b) => a.prop - b.prop);
|
|
104
|
-
return
|
|
105
|
-
...
|
|
102
|
+
return Uint8Array.from([
|
|
103
|
+
...blockCapacity(t.blockCapacity),
|
|
106
104
|
1 + sepPropCount(props),
|
|
107
105
|
1 + refFields,
|
|
108
106
|
...propDefBuffer(schema, {
|
|
@@ -110,7 +108,7 @@ export function schemaToSelvaBuffer(schema) {
|
|
|
110
108
|
len: t.mainLen === 0 ? 1 : t.mainLen,
|
|
111
109
|
}),
|
|
112
110
|
...rest.map((f) => propDefBuffer(schema, f)).flat(1),
|
|
113
|
-
]);
|
|
111
|
+
]).buffer;
|
|
114
112
|
});
|
|
115
113
|
}
|
|
116
114
|
//# sourceMappingURL=selvaBuffer.js.map
|
package/dist/def/typeDef.js
CHANGED
|
@@ -1,57 +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, } from './types.js';
|
|
5
|
-
// TMP
|
|
6
12
|
export const DEFAULT_BLOCK_CAPACITY = 100_000;
|
|
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
13
|
export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById) => {
|
|
56
14
|
for (const field in schemaTypesParsed) {
|
|
57
15
|
if (field in schema.types) {
|
|
@@ -82,35 +40,7 @@ export const updateTypeDefs = (schema, schemaTypesParsed, schemaTypesParsedById)
|
|
|
82
40
|
}
|
|
83
41
|
}
|
|
84
42
|
};
|
|
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: new Uint8Array([]),
|
|
104
|
-
bufferTmp: new Uint8Array([]),
|
|
105
|
-
},
|
|
106
|
-
hasSeperateTextSort: false,
|
|
107
|
-
seperateTextSort: {
|
|
108
|
-
size: 0, // prop len
|
|
109
|
-
props: [],
|
|
110
|
-
buffer: new Uint8Array([]),
|
|
111
|
-
bufferTmp: new Uint8Array([]),
|
|
112
|
-
},
|
|
113
|
-
}, path = [], top = true) => {
|
|
43
|
+
export const createSchemaTypeDef = (typeName, type, parsed, locales, result = createEmptyDef(typeName, type, locales), path = [], top = true) => {
|
|
114
44
|
if (result.id == 0 && top) {
|
|
115
45
|
if ('id' in type) {
|
|
116
46
|
result.id = type.id;
|
|
@@ -123,7 +53,6 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
|
|
|
123
53
|
result.localeSize = Object.keys(locales).length;
|
|
124
54
|
result.idUint8[0] = result.id & 255;
|
|
125
55
|
result.idUint8[1] = result.id >> 8;
|
|
126
|
-
const encoder = new TextEncoder();
|
|
127
56
|
const target = type.props;
|
|
128
57
|
let separateSortProps = 0;
|
|
129
58
|
let separateSortText = 0;
|
|
@@ -135,18 +64,13 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
|
|
|
135
64
|
createSchemaTypeDef(typeName, schemaProp, parsed, locales, result, propPath, false);
|
|
136
65
|
}
|
|
137
66
|
else {
|
|
138
|
-
|
|
67
|
+
const len = getPropLen(schemaProp);
|
|
139
68
|
if (isPropType('string', schemaProp) ||
|
|
140
69
|
isPropType('alias', schemaProp) ||
|
|
141
70
|
isPropType('cardinality', schemaProp)) {
|
|
142
71
|
if (typeof schemaProp === 'object') {
|
|
143
|
-
if (schemaProp.maxBytes < 61)
|
|
144
|
-
|
|
145
|
-
}
|
|
146
|
-
else if ('max' in schemaProp && schemaProp.max < 31) {
|
|
147
|
-
len = schemaProp.max * 2 + 1;
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
72
|
+
if (!(schemaProp.maxBytes < 61) ||
|
|
73
|
+
!('max' in schemaProp && schemaProp.max < 31)) {
|
|
150
74
|
separateSortProps++;
|
|
151
75
|
}
|
|
152
76
|
}
|
|
@@ -154,16 +78,10 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
|
|
|
154
78
|
separateSortProps++;
|
|
155
79
|
}
|
|
156
80
|
}
|
|
157
|
-
else if (isPropType('vector', schemaProp)) {
|
|
158
|
-
len = 4 * schemaProp.size;
|
|
159
|
-
}
|
|
160
81
|
else if (isPropType('text', schemaProp)) {
|
|
161
82
|
separateSortText++;
|
|
162
83
|
}
|
|
163
|
-
const isseparate =
|
|
164
|
-
if (isseparate) {
|
|
165
|
-
result.cnt++;
|
|
166
|
-
}
|
|
84
|
+
const isseparate = isSeparate(schemaProp, len);
|
|
167
85
|
const prop = {
|
|
168
86
|
typeIndex: TYPE_INDEX_MAP[propType],
|
|
169
87
|
__isPropDef: true,
|
|
@@ -171,7 +89,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
|
|
|
171
89
|
path: propPath,
|
|
172
90
|
start: 0,
|
|
173
91
|
len,
|
|
174
|
-
prop: isseparate ? result.cnt : 0,
|
|
92
|
+
prop: isseparate ? ++result.cnt : 0,
|
|
175
93
|
};
|
|
176
94
|
if (isPropType('enum', schemaProp)) {
|
|
177
95
|
prop.enum = Array.isArray(schemaProp) ? schemaProp : schemaProp.enum;
|
|
@@ -231,8 +149,7 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
|
|
|
231
149
|
let lastProp = 0;
|
|
232
150
|
for (const p of vals) {
|
|
233
151
|
if (p.separate) {
|
|
234
|
-
lastProp
|
|
235
|
-
p.prop = lastProp;
|
|
152
|
+
p.prop = ++lastProp;
|
|
236
153
|
}
|
|
237
154
|
}
|
|
238
155
|
let len = 2;
|
|
@@ -251,109 +168,12 @@ export const createSchemaTypeDef = (typeName, type, parsed, locales, result = {
|
|
|
251
168
|
setByPath(result.tree, f.path, f);
|
|
252
169
|
}
|
|
253
170
|
}
|
|
254
|
-
|
|
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
|
|
171
|
+
makePacked(result, typeName, vals, len);
|
|
312
172
|
if (separateSortText > 0) {
|
|
313
|
-
result
|
|
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);
|
|
173
|
+
makeSeparateTextSort(result);
|
|
332
174
|
}
|
|
333
175
|
if (separateSortProps > 0) {
|
|
334
|
-
result
|
|
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);
|
|
176
|
+
makeSeparateSort(result);
|
|
357
177
|
}
|
|
358
178
|
for (const p in result.props) {
|
|
359
179
|
const x = result.props[p];
|
package/dist/def/types.d.ts
CHANGED
|
@@ -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;
|
|
@@ -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;
|
|
@@ -127,7 +126,11 @@ export type SchemaTypeDef = {
|
|
|
127
126
|
hasSeperateSort: boolean;
|
|
128
127
|
seperateSort: SchemaSortUndefinedHandler;
|
|
129
128
|
hasSeperateTextSort: boolean;
|
|
130
|
-
seperateTextSort: SchemaSortUndefinedHandler
|
|
129
|
+
seperateTextSort: SchemaSortUndefinedHandler & {
|
|
130
|
+
noUndefined: Uint8Array;
|
|
131
|
+
localeStringToIndex: Map<string, Uint8Array>;
|
|
132
|
+
localeToIndex: Map<LangCode, number>;
|
|
133
|
+
};
|
|
131
134
|
createTs?: PropDef[];
|
|
132
135
|
updateTs?: PropDef[];
|
|
133
136
|
locales: Partial<SchemaLocales>;
|
package/dist/def/types.js
CHANGED
package/dist/def/utils.d.ts
CHANGED
|
@@ -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,7 +1,12 @@
|
|
|
1
|
-
import { INT16, INT32,
|
|
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
|
-
if (t === INT16 || t === INT32 || t ===
|
|
9
|
+
if (t === INT16 || t === INT32 || t === INT8) {
|
|
5
10
|
return true;
|
|
6
11
|
}
|
|
7
12
|
return false;
|
|
@@ -10,7 +15,6 @@ export const propIsNumerical = (prop) => {
|
|
|
10
15
|
const t = prop.typeIndex;
|
|
11
16
|
if (t === INT16 ||
|
|
12
17
|
t === INT32 ||
|
|
13
|
-
t === INT64 ||
|
|
14
18
|
t === INT8 ||
|
|
15
19
|
t === UINT8 ||
|
|
16
20
|
t === UINT16 ||
|
|
@@ -21,4 +25,23 @@ export const propIsNumerical = (prop) => {
|
|
|
21
25
|
}
|
|
22
26
|
return false;
|
|
23
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
|
+
}
|
|
24
47
|
//# sourceMappingURL=utils.js.map
|
package/dist/lang.d.ts
CHANGED
|
@@ -145,7 +145,7 @@ declare const langCodes: {
|
|
|
145
145
|
readonly yo: 143;
|
|
146
146
|
readonly zu: 144;
|
|
147
147
|
};
|
|
148
|
-
export declare const langCodesMap: Map<string, 0 |
|
|
148
|
+
export declare const langCodesMap: Map<string, 0 | 2 | 1 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144>;
|
|
149
149
|
export declare const inverseLangMap: Map<any, any>;
|
|
150
150
|
export type LangName = keyof typeof langCodes;
|
|
151
151
|
export type LangCode = (typeof langCodes)[LangName];
|