@based/db 0.0.14 → 0.0.15

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.
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@based/db",
3
- "version": "0.0.14",
3
+ "version": "0.0.15",
4
4
  "license": "MIT",
5
5
  "type": "module",
6
6
  "main": "./dist/src/index.js",
@@ -1,5 +0,0 @@
1
- import { ModifyRes } from './ModifyRes.js';
2
- import { ModifyOpts } from './types.js';
3
- import { DbClient } from '../index.js';
4
- export type CreateObj = Record<string, any>;
5
- export declare function create(db: DbClient, type: string, obj: CreateObj, opts?: ModifyOpts): ModifyRes;
@@ -1,112 +0,0 @@
1
- import { MICRO_BUFFER } from '../../server/schema/schema.js';
2
- import { startDrain, flushBuffer } from '../operations.js';
3
- import { setCursor } from './setCursor.js';
4
- import { modify } from './modify.js';
5
- import { ModifyState } from './ModifyRes.js';
6
- import { CREATE, RANGE_ERR } from './types.js';
7
- import { writeFixedValue } from './fixed.js';
8
- import { getSubscriptionMarkers } from '../query/subscription/index.js';
9
- const appendCreate = (ctx, def, obj, res, unsafe) => {
10
- const len = ctx.len;
11
- let err = modify(ctx, res, obj, def, CREATE, def.tree, true, unsafe);
12
- if (err) {
13
- return err;
14
- }
15
- if (ctx.len === len || def.mainLen === 0) {
16
- if (ctx.len + 10 > ctx.max) {
17
- return RANGE_ERR;
18
- }
19
- setCursor(ctx, def, 0, MICRO_BUFFER, res.tmpId, CREATE);
20
- }
21
- if (def.createTs) {
22
- const createTs = Date.now();
23
- for (const prop of def.createTs) {
24
- if (ctx.lastMain === -1) {
25
- let mainLenU32 = def.mainLen;
26
- setCursor(ctx, def, prop.prop, MICRO_BUFFER, res.tmpId, CREATE);
27
- ctx.buf[ctx.len++] = CREATE;
28
- ctx.buf[ctx.len++] = mainLenU32;
29
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
30
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
31
- ctx.buf[ctx.len++] = mainLenU32 >>>= 8;
32
- ctx.lastMain = ctx.len;
33
- ctx.buf.fill(0, ctx.len, (ctx.len += def.mainLen));
34
- }
35
- err = writeFixedValue(ctx, createTs, prop, ctx.lastMain + prop.start);
36
- if (err) {
37
- return err;
38
- }
39
- }
40
- }
41
- // if touched lets see perf impact here
42
- if (def.hasStringProp) {
43
- if (ctx.hasStringField !== def.stringPropsSize - 1) {
44
- if (ctx.len + 3 > ctx.max) {
45
- return RANGE_ERR;
46
- }
47
- ctx.buf[ctx.len++] = 7;
48
- let sizepos = ctx.len;
49
- ctx.len += 2;
50
- for (const { prop } of def.stringPropsLoop) {
51
- if (def.stringPropsCurrent[prop] === 1) {
52
- if (ctx.len + 1 > ctx.max) {
53
- return RANGE_ERR;
54
- }
55
- ctx.buf[ctx.len++] = prop;
56
- }
57
- }
58
- let size = ctx.len - sizepos - 2;
59
- ctx.buf[sizepos++] = size;
60
- ctx.buf[sizepos] = size >>>= 8;
61
- }
62
- if (ctx.hasStringField !== -1) {
63
- def.stringProps.copy(def.stringPropsCurrent);
64
- }
65
- }
66
- };
67
- export function create(db, type, obj, opts) {
68
- const def = db.schemaTypesParsed[type];
69
- if (!def) {
70
- throw new Error(`Unknown type: ${type}. Did you mean on of: ${Object.keys(db.schemaTypesParsed).join(', ')}`);
71
- }
72
- let id;
73
- if ('id' in obj) {
74
- if (opts?.unsafe) {
75
- id = obj.id;
76
- }
77
- else {
78
- throw Error('create with "id" is not allowed');
79
- }
80
- }
81
- else {
82
- id = def.lastId + 1;
83
- }
84
- const ctx = db.modifyCtx;
85
- const res = new ModifyState(def.id, id, db, getSubscriptionMarkers(db, def.id, id, true), opts);
86
- const pos = ctx.len;
87
- const err = appendCreate(ctx, def, obj, res, opts?.unsafe);
88
- if (err) {
89
- ctx.prefix0 = -1; // force a new cursor
90
- ctx.len = pos;
91
- if (err === RANGE_ERR) {
92
- if (pos === 0) {
93
- throw new Error('out of range');
94
- }
95
- flushBuffer(db);
96
- return db.create(type, obj, opts);
97
- }
98
- res.error = err;
99
- // @ts-ignore
100
- return res;
101
- }
102
- if (!db.isDraining) {
103
- startDrain(db);
104
- }
105
- if (id > def.lastId) {
106
- def.lastId = id;
107
- def.total++;
108
- }
109
- // @ts-ignore
110
- return res;
111
- }
112
- //# sourceMappingURL=create%20copy.js.map
@@ -1,4 +0,0 @@
1
- import { ModifyCtx } from '../../index.js';
2
- import { SchemaTypeDef, PropDef } from '../../server/schema/types.js';
3
- import { ModifyOp, ModifyErr } from './types.js';
4
- export declare function writeHll(value: string | null | Buffer, ctx: ModifyCtx, def: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,58 +0,0 @@
1
- import { UPDATE, RANGE_ERR, DELETE } from './types.js';
2
- import { ModifyError } from './ModifyRes.js';
3
- import { setCursor } from './setCursor.js';
4
- import { crc32 } from '../crc32.js';
5
- export function writeHll(value, ctx, def, t, parentId, modifyOp) {
6
- if (typeof value !== 'object') {
7
- return new ModifyError(t, value);
8
- }
9
- if (value === null) {
10
- if (modifyOp === UPDATE) {
11
- if (ctx.len + 11 > ctx.max) {
12
- return RANGE_ERR;
13
- }
14
- setCursor(ctx, def, t.prop, parentId, modifyOp);
15
- ctx.buf[ctx.len++] = DELETE;
16
- }
17
- }
18
- else if (Array.isArray(value)) {
19
- return addHll(value, ctx, def, t, parentId, modifyOp, 0);
20
- }
21
- else {
22
- for (const key in value) {
23
- if (key === 'add') {
24
- const err = addHll(value, ctx, def, t, parentId, modifyOp, 1);
25
- if (err) {
26
- return err;
27
- }
28
- }
29
- else {
30
- return new ModifyError(t, value);
31
- }
32
- }
33
- }
34
- }
35
- function addHll(value, ctx, def, t, parentId, modifyOp, addOrPut) {
36
- let size = value.length * 4 + 1;
37
- if (ctx.len + size + 11 > ctx.max) {
38
- return RANGE_ERR;
39
- }
40
- setCursor(ctx, def, t.prop, parentId, modifyOp);
41
- ctx.buf[ctx.len++] = modifyOp;
42
- ctx.buf[ctx.len++] = size;
43
- ctx.buf[ctx.len++] = size >>>= 8;
44
- ctx.buf[ctx.len++] = size >>>= 8;
45
- ctx.buf[ctx.len++] = size >>>= 8;
46
- ctx.buf[ctx.len++] = addOrPut;
47
- for (const str of value) {
48
- if (typeof str !== 'string') {
49
- return new ModifyError(t, value);
50
- }
51
- let hash = crc32(Buffer.from(str));
52
- ctx.buf[ctx.len++] = hash;
53
- ctx.buf[ctx.len++] = hash >>>= 8;
54
- ctx.buf[ctx.len++] = hash >>>= 8;
55
- ctx.buf[ctx.len++] = hash >>>= 8;
56
- }
57
- }
58
- //# sourceMappingURL=hll.js.map
@@ -1 +0,0 @@
1
- export {};
@@ -1,39 +0,0 @@
1
- import { flushBuffer, startDrain } from '../operations.js';
2
- import { setCursor } from './setCursor.js';
3
- import { UPDATE } from './types.js';
4
- import { MICRO_BUFFER } from '../../server/schema/schema.js';
5
- export const ;
6
- delete ;
7
- (db, type, id) => {
8
- const ctx = db.modifyCtx;
9
- const schema = db.schemaTypesParsed[type];
10
- const separate = schema.separate;
11
- if (separate) {
12
- const size = 12 + separate.length * 12;
13
- if (ctx.len + size > ctx.max) {
14
- flushBuffer(db);
15
- return delete (db, type, id);
16
- }
17
- setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
18
- ctx.buf[ctx.len++] = 4;
19
- for (const s of separate) {
20
- setCursor(ctx, schema, s.prop, s.typeIndex, id, UPDATE);
21
- ctx.buf[ctx.len++] = 4;
22
- }
23
- ctx.buf[ctx.len++] = 10;
24
- }
25
- else {
26
- if (ctx.len + 12 > ctx.max) {
27
- flushBuffer(db);
28
- return delete (db, type, id);
29
- }
30
- setCursor(ctx, schema, 0, MICRO_BUFFER, id, UPDATE);
31
- ctx.buf[ctx.len++] = 4;
32
- ctx.buf[ctx.len++] = 10;
33
- }
34
- if (!db.isDraining) {
35
- startDrain(db);
36
- }
37
- return true;
38
- };
39
- //# sourceMappingURL=remove.js.map
@@ -1,4 +0,0 @@
1
- import { ModifyCtx } from '../../index.js';
2
- import { PropDef, SchemaTypeDef } from '../../server/schema/types.js';
3
- import { ModifyOp, ModifyErr } from './types.js';
4
- export declare function writeVector(value: any, ctx: ModifyCtx, schema: SchemaTypeDef, t: PropDef, parentId: number, modifyOp: ModifyOp): ModifyErr;
@@ -1,46 +0,0 @@
1
- import { UPDATE, RANGE_ERR, DELETE } from './types.js';
2
- import { ModifyError } from './ModifyRes.js';
3
- import { setCursor } from './setCursor.js';
4
- function write(value, ctx, fieldSize) {
5
- const size = Math.min(value.byteLength, fieldSize);
6
- let tmp = size;
7
- // 16-bits would be enough but the zig code now expects 32-bits
8
- ctx.buf[ctx.len++] = tmp;
9
- ctx.buf[ctx.len++] = tmp >>>= 8;
10
- ctx.buf[ctx.len++] = tmp >>>= 8;
11
- ctx.buf[ctx.len++] = tmp >>>= 8;
12
- Buffer.from(value.buffer).copy(ctx.buf, ctx.len, 0, size);
13
- ctx.len += size;
14
- }
15
- export function writeVector(value, ctx, schema, t, parentId, modifyOp) {
16
- let size;
17
- if (value === null) {
18
- size = 0;
19
- }
20
- else {
21
- if (!value) {
22
- return new ModifyError(t, value);
23
- }
24
- size = value.byteLength + 4;
25
- }
26
- if (size === 0) {
27
- if (modifyOp === UPDATE) {
28
- if (ctx.len + 11 > ctx.max) {
29
- // TODO ???
30
- return RANGE_ERR;
31
- }
32
- setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
33
- ctx.buf[ctx.len++] = DELETE;
34
- }
35
- }
36
- else {
37
- if (ctx.len + 15 + size > ctx.max) {
38
- // TODO ???
39
- return RANGE_ERR;
40
- }
41
- setCursor(ctx, schema, t.prop, t.typeIndex, parentId, modifyOp);
42
- ctx.buf[ctx.len++] = modifyOp;
43
- write(value, ctx, t.len);
44
- }
45
- }
46
- //# sourceMappingURL=vector%20copy.js.map
@@ -1,6 +0,0 @@
1
- export declare const validOperators: readonly ["=", "has", "!has", "<", ">", "!=", "like", ">=", "<=", "..", "!..", "hasLoose"];
2
- export type Operator = '=' | 'has' | '!has' | '<' | '>' | '!=' | 'like' | '>=' | '<=' | '..' | '!..' | 'like' | 'hasLoose';
3
- export declare const operationToByte: (op: Operator) => number;
4
- export declare const isNumerical: (op: number) => boolean;
5
- export declare const stripNegation: (op: number) => number;
6
- export declare const negateType: (op: number) => number;
@@ -1,98 +0,0 @@
1
- export const validOperators = [
2
- '=',
3
- 'has',
4
- '!has',
5
- '<',
6
- '>',
7
- '!=',
8
- 'like',
9
- '>=',
10
- '<=',
11
- '..',
12
- '!..',
13
- 'hasLoose',
14
- ];
15
- // -------------------------------------------
16
- // operations shared
17
- // 1 = equality
18
- // 2 = has (simd)
19
- // 3 = not equal
20
- // 4 = ends with
21
- // 5 = starts with
22
- // -------------------------------------------
23
- // operations numbers
24
- // 6 = larger then
25
- // 7 = smaller then
26
- // 8 = larger then inclusive
27
- // 9 = smaller then inclusive
28
- // 10 = range
29
- // 11 = exclude range
30
- // -------------------------------------------
31
- // operations strings
32
- // 12 = equality to lower case
33
- // 13 = has to lower case (simd)
34
- // 14 = starts with to lower case
35
- // 15 = ends with to lower case
36
- // 18 = search
37
- // -------------------------------------------
38
- export const operationToByte = (op) => {
39
- if (op === '=') {
40
- return 1;
41
- }
42
- if (op === 'has') {
43
- return 2;
44
- }
45
- if (op === '!=') {
46
- return 3;
47
- }
48
- if (op === '!has') {
49
- return 16;
50
- }
51
- if (op === '>') {
52
- return 6;
53
- }
54
- if (op === '<') {
55
- return 7;
56
- }
57
- if (op === '>=') {
58
- return 8;
59
- }
60
- if (op === '<=') {
61
- return 9;
62
- }
63
- if (op === '..') {
64
- return 10;
65
- }
66
- if (op === '!..') {
67
- return 11;
68
- }
69
- if (op === 'like') {
70
- return 18;
71
- }
72
- if (op === 'hasLoose') {
73
- return 19;
74
- }
75
- return 0;
76
- };
77
- export const isNumerical = (op) => {
78
- if (op === 6 || op === 7 || op === 8 || op === 9 || op === 10 || op === 11) {
79
- return true;
80
- }
81
- return false;
82
- };
83
- export const stripNegation = (op) => {
84
- if (op === 16) {
85
- return 2;
86
- }
87
- if (op === 3) {
88
- return 1;
89
- }
90
- return op;
91
- };
92
- export const negateType = (op) => {
93
- if (op === 3) {
94
- return 1;
95
- }
96
- return 2;
97
- };
98
- //# sourceMappingURL=operators.js.map