@based/db 0.0.64 → 0.0.67
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/README.md +2 -2
- package/dist/lib/darwin_aarch64/include/selva/colvec.h +71 -0
- package/dist/lib/darwin_aarch64/include/selva/db.h +33 -4
- package/dist/lib/darwin_aarch64/include/selva/fields.h +37 -25
- package/dist/lib/darwin_aarch64/include/selva/hll.h +5 -3
- package/dist/lib/darwin_aarch64/include/selva/membar.h +23 -0
- package/dist/lib/darwin_aarch64/include/selva/types.h +8 -1
- package/dist/lib/darwin_aarch64/include/selva/worker_ctx.h +19 -3
- package/dist/lib/darwin_aarch64/libdeflate.dylib +0 -0
- package/dist/lib/darwin_aarch64/libjemalloc_selva.2.dylib +0 -0
- package/dist/lib/darwin_aarch64/libnode-v20.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v21.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v22.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v23.node +0 -0
- package/dist/lib/darwin_aarch64/libnode-v24.node +0 -0
- package/dist/lib/darwin_aarch64/libselva.dylib +0 -0
- package/dist/lib/darwin_aarch64/libxxhash.dylib +0 -0
- package/dist/lib/linux_aarch64/include/selva/colvec.h +71 -0
- package/dist/lib/linux_aarch64/include/selva/db.h +33 -4
- package/dist/lib/linux_aarch64/include/selva/fields.h +37 -25
- package/dist/lib/linux_aarch64/include/selva/hll.h +5 -3
- package/dist/lib/linux_aarch64/include/selva/membar.h +23 -0
- package/dist/lib/linux_aarch64/include/selva/types.h +8 -1
- package/dist/lib/linux_aarch64/include/selva/worker_ctx.h +19 -3
- package/dist/lib/linux_aarch64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_aarch64/libnode-v20.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v21.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v22.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v23.node +0 -0
- package/dist/lib/linux_aarch64/libnode-v24.node +0 -0
- package/dist/lib/linux_aarch64/libselva.so +0 -0
- package/dist/lib/linux_x86_64/include/selva/colvec.h +71 -0
- package/dist/lib/linux_x86_64/include/selva/db.h +33 -4
- package/dist/lib/linux_x86_64/include/selva/fields.h +37 -25
- package/dist/lib/linux_x86_64/include/selva/hll.h +5 -3
- package/dist/lib/linux_x86_64/include/selva/membar.h +23 -0
- package/dist/lib/linux_x86_64/include/selva/types.h +8 -1
- package/dist/lib/linux_x86_64/include/selva/worker_ctx.h +19 -3
- package/dist/lib/linux_x86_64/libjemalloc_selva.so.2 +0 -0
- package/dist/lib/linux_x86_64/libnode-v20.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v21.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v22.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v23.node +0 -0
- package/dist/lib/linux_x86_64/libnode-v24.node +0 -0
- package/dist/lib/linux_x86_64/libselva.so +0 -0
- package/dist/src/client/flushModify.d.ts +2 -1
- package/dist/src/client/flushModify.js +12 -4
- package/dist/src/client/modify/create.js +11 -0
- package/dist/src/client/modify/delete.js +3 -0
- package/dist/src/client/modify/fixed.js +1 -1
- package/dist/src/client/modify/modify.js +2 -2
- package/dist/src/client/modify/setCursor.d.ts +2 -1
- package/dist/src/client/query/BasedDbQuery.d.ts +10 -4
- package/dist/src/client/query/BasedDbQuery.js +114 -6
- package/dist/src/client/query/aggregates/aggregation.js +24 -11
- package/dist/src/client/query/aggregates/types.d.ts +22 -2
- package/dist/src/client/query/aggregates/types.js +34 -1
- package/dist/src/client/query/display.js +8 -2
- package/dist/src/client/query/filter/createVariableFilterBuffer.d.ts +2 -3
- package/dist/src/client/query/filter/createVariableFilterBuffer.js +20 -7
- package/dist/src/client/query/filter/filter.js +13 -3
- package/dist/src/client/query/filter/primitiveFilter.d.ts +1 -2
- package/dist/src/client/query/include/props.js +18 -2
- package/dist/src/client/query/include/toBuffer.js +11 -3
- package/dist/src/client/query/include/walk.js +5 -1
- package/dist/src/client/query/queryDef.js +4 -1
- package/dist/src/client/query/read/read.js +52 -22
- package/dist/src/client/query/registerQuery.js +1 -0
- package/dist/src/client/query/search/index.d.ts +1 -1
- package/dist/src/client/query/search/index.js +21 -7
- package/dist/src/client/query/sort.d.ts +1 -1
- package/dist/src/client/query/toByteCode/default.d.ts +1 -1
- package/dist/src/client/query/toByteCode/default.js +0 -2
- package/dist/src/client/query/toByteCode/toBuffer.js +0 -7
- package/dist/src/client/query/types.d.ts +16 -5
- package/dist/src/client/query/validation.d.ts +3 -0
- package/dist/src/client/query/validation.js +34 -2
- package/dist/src/client/xxHash64.d.ts +1 -1
- package/dist/src/index.d.ts +1 -2
- package/dist/src/index.js +0 -1
- package/dist/src/native.d.ts +7 -4
- package/dist/src/native.js +23 -13
- package/dist/src/server/IoWorker.d.ts +8 -0
- package/dist/src/server/IoWorker.js +39 -0
- package/dist/src/server/QueryWorker.d.ts +8 -0
- package/dist/src/server/QueryWorker.js +26 -0
- package/dist/src/server/blocks.d.ts +24 -0
- package/dist/src/server/blocks.js +112 -0
- package/dist/src/server/dbHash.d.ts +1 -1
- package/dist/src/server/index.d.ts +10 -16
- package/dist/src/server/index.js +39 -15
- package/dist/src/server/migrate/index.d.ts +5 -0
- package/dist/src/server/migrate/index.js +11 -7
- package/dist/src/server/migrate/worker.js +3 -0
- package/dist/src/server/save.d.ts +8 -6
- package/dist/src/server/save.js +34 -78
- package/dist/src/server/schema.js +6 -5
- package/dist/src/server/start.js +57 -60
- package/dist/src/server/tree.d.ts +24 -13
- package/dist/src/server/tree.js +95 -66
- package/dist/src/server/workers/DbWorker.d.ts +17 -0
- package/dist/src/server/{DbWorker.js → workers/DbWorker.js} +15 -17
- package/dist/src/server/workers/io_worker.js +39 -0
- package/dist/src/server/workers/io_worker_types.d.ts +12 -0
- package/dist/src/server/workers/io_worker_types.js +2 -0
- package/dist/src/server/workers/query_worker.d.ts +1 -0
- package/dist/src/server/workers/query_worker.js +4 -0
- package/dist/src/server/workers/worker.d.ts +1 -0
- package/dist/src/server/workers/worker.js +41 -0
- package/dist/src/shared/Emitter.d.ts +1 -0
- package/dist/src/types.d.ts +1 -1
- package/dist/src/types.js +1 -1
- package/package.json +3 -3
- package/dist/lib/darwin_aarch64/include/selva/find.h +0 -47
- package/dist/lib/darwin_aarch64/include/selva/history.h +0 -64
- package/dist/lib/darwin_aarch64/include/selva/queue_r.h +0 -190
- package/dist/lib/darwin_aarch64/include/selva/traverse.h +0 -65
- package/dist/lib/linux_aarch64/include/selva/find.h +0 -47
- package/dist/lib/linux_aarch64/include/selva/history.h +0 -64
- package/dist/lib/linux_aarch64/include/selva/queue_r.h +0 -190
- package/dist/lib/linux_aarch64/include/selva/traverse.h +0 -65
- package/dist/lib/linux_x86_64/include/selva/find.h +0 -47
- package/dist/lib/linux_x86_64/include/selva/history.h +0 -64
- package/dist/lib/linux_x86_64/include/selva/queue_r.h +0 -190
- package/dist/lib/linux_x86_64/include/selva/traverse.h +0 -65
- package/dist/src/client/query/serialize.d.ts +0 -4
- package/dist/src/client/query/serialize.js +0 -26
- package/dist/src/server/DbWorker.d.ts +0 -13
- package/dist/src/server/csmt/draw-dot.d.ts +0 -4
- package/dist/src/server/csmt/draw-dot.js +0 -38
- package/dist/src/server/csmt/index.d.ts +0 -4
- package/dist/src/server/csmt/index.js +0 -5
- package/dist/src/server/csmt/match.d.ts +0 -7
- package/dist/src/server/csmt/match.js +0 -10
- package/dist/src/server/csmt/memebership-proof.d.ts +0 -7
- package/dist/src/server/csmt/memebership-proof.js +0 -122
- package/dist/src/server/csmt/tree-utils.d.ts +0 -6
- package/dist/src/server/csmt/tree-utils.js +0 -33
- package/dist/src/server/csmt/tree.d.ts +0 -3
- package/dist/src/server/csmt/tree.js +0 -270
- package/dist/src/server/csmt/types.d.ts +0 -46
- package/dist/src/server/csmt/types.js +0 -2
- package/dist/src/server/worker.js +0 -33
- /package/dist/src/server/{worker.d.ts → workers/io_worker.d.ts} +0 -0
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { MessageChannel, Worker } from 'node:worker_threads';
|
|
2
2
|
import { fileURLToPath } from 'node:url';
|
|
3
3
|
import { dirname, join } from 'node:path';
|
|
4
|
-
import { readUint64 } from '@saulx/utils';
|
|
5
4
|
const __filename = fileURLToPath(import.meta.url);
|
|
6
5
|
const __dirname = dirname(__filename);
|
|
7
|
-
const workerPath = join(__dirname, 'worker.js');
|
|
8
6
|
export class DbWorker {
|
|
9
|
-
constructor(address, db,
|
|
7
|
+
constructor(address, db, onExit, workerName) {
|
|
10
8
|
const { port1, port2 } = new MessageChannel();
|
|
11
9
|
this.db = db;
|
|
12
10
|
this.channel = port1;
|
|
13
|
-
this.worker = new Worker(
|
|
11
|
+
this.worker = new Worker(join(__dirname, workerName), {
|
|
14
12
|
workerData: {
|
|
15
13
|
isDbWorker: true,
|
|
16
14
|
channel: port2,
|
|
@@ -41,12 +39,12 @@ export class DbWorker {
|
|
|
41
39
|
resolve(err);
|
|
42
40
|
}
|
|
43
41
|
this.resolvers = [];
|
|
44
|
-
|
|
42
|
+
onExit(code);
|
|
45
43
|
}
|
|
46
44
|
});
|
|
47
|
-
|
|
45
|
+
this.channel.on('message', (buf) => {
|
|
48
46
|
this.resolvers.shift()(new Uint8Array(buf));
|
|
49
|
-
this.
|
|
47
|
+
this.handleMsg(buf);
|
|
50
48
|
});
|
|
51
49
|
}
|
|
52
50
|
db;
|
|
@@ -54,21 +52,21 @@ export class DbWorker {
|
|
|
54
52
|
worker;
|
|
55
53
|
resolvers = [];
|
|
56
54
|
readyPromise;
|
|
55
|
+
async terminate() {
|
|
56
|
+
return this.worker.terminate();
|
|
57
|
+
}
|
|
57
58
|
callback = (resolve) => {
|
|
58
|
-
this.db.processingQueries++;
|
|
59
59
|
this.resolvers.push(resolve);
|
|
60
60
|
};
|
|
61
|
-
|
|
62
|
-
|
|
61
|
+
/**
|
|
62
|
+
* Send msg to the worker thread and return a promise to the response.
|
|
63
|
+
*/
|
|
64
|
+
call(msg) {
|
|
65
|
+
this.channel.postMessage(msg);
|
|
63
66
|
return new Promise(this.callback);
|
|
64
67
|
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (schemaChecksum !== this.db.schema?.hash) {
|
|
68
|
-
return Promise.resolve(new Uint8Array(1));
|
|
69
|
-
}
|
|
70
|
-
this.channel.postMessage(buf);
|
|
71
|
-
return new Promise(this.callback);
|
|
68
|
+
updateCtx(address) {
|
|
69
|
+
return this.call(address);
|
|
72
70
|
}
|
|
73
71
|
}
|
|
74
72
|
//# sourceMappingURL=DbWorker.js.map
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { registerMsgHandler } from './worker.js';
|
|
2
|
+
import { ENCODER, writeInt32 } from '@saulx/utils';
|
|
3
|
+
import native from '../../native.js';
|
|
4
|
+
function loadBlock(dbCtx, filepath) {
|
|
5
|
+
try {
|
|
6
|
+
native.loadBlock(filepath, dbCtx);
|
|
7
|
+
}
|
|
8
|
+
catch (e) {
|
|
9
|
+
// need to get rid of the shared buffer
|
|
10
|
+
return new Uint8Array(ENCODER.encode(e.toString())).buffer;
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
14
|
+
function unloadBlock(dbCtx, filepath, typeId, start) {
|
|
15
|
+
const buf = new ArrayBuffer(20); // [[4 bytes err], [16 bytes hash]]
|
|
16
|
+
const hash = new Uint8Array(buf, 4);
|
|
17
|
+
const err = native.saveBlock(filepath, typeId, start, dbCtx, hash);
|
|
18
|
+
if (err) {
|
|
19
|
+
const errCodeBuf = new Uint8Array(buf, 0, 4);
|
|
20
|
+
writeInt32(errCodeBuf, err, 0);
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
native.delBlock(dbCtx, typeId, start);
|
|
24
|
+
}
|
|
25
|
+
return buf;
|
|
26
|
+
}
|
|
27
|
+
registerMsgHandler((dbCtx, msg) => {
|
|
28
|
+
if (typeof msg?.type === 'string') {
|
|
29
|
+
const job = msg;
|
|
30
|
+
if (job.type === 'load') {
|
|
31
|
+
return loadBlock(dbCtx, job.filepath);
|
|
32
|
+
}
|
|
33
|
+
else if (job.type === 'unload') {
|
|
34
|
+
return unloadBlock(dbCtx, job.filepath, job.typeId, job.start);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return null;
|
|
38
|
+
});
|
|
39
|
+
//# sourceMappingURL=io_worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function registerMsgHandler(onMsg: (dbCtx: any, msg: any) => Uint8Array<ArrayBufferLike> | ArrayBuffer | null): void;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { isMainThread, parentPort, workerData } from 'node:worker_threads';
|
|
2
|
+
import native from '../../native.js';
|
|
3
|
+
let dbCtx;
|
|
4
|
+
let wCtx; // This must be held until the worker exits otherwise the ctx will be autofreed instantly
|
|
5
|
+
if (isMainThread) {
|
|
6
|
+
console.warn(`running a worker in the mainthread - incorrect`);
|
|
7
|
+
}
|
|
8
|
+
else if (workerData?.isDbWorker) {
|
|
9
|
+
let { address } = workerData;
|
|
10
|
+
dbCtx = native.externalFromInt(address);
|
|
11
|
+
wCtx = native.workerCtxInit();
|
|
12
|
+
}
|
|
13
|
+
else {
|
|
14
|
+
console.info('incorrect worker db query');
|
|
15
|
+
}
|
|
16
|
+
export function registerMsgHandler(onMsg) {
|
|
17
|
+
if (!workerData?.isDbWorker) {
|
|
18
|
+
throw new Error('Not a DbWorker');
|
|
19
|
+
}
|
|
20
|
+
let { channel } = workerData;
|
|
21
|
+
const handleMsg = (msg) => {
|
|
22
|
+
try {
|
|
23
|
+
if (typeof msg === 'bigint') {
|
|
24
|
+
// it's a ctx address
|
|
25
|
+
dbCtx = native.externalFromInt(msg);
|
|
26
|
+
channel.postMessage(null);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
// a message to the worker handler
|
|
30
|
+
const arrayBuf = onMsg(dbCtx, msg);
|
|
31
|
+
channel.postMessage(arrayBuf, [arrayBuf]);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
catch (e) {
|
|
35
|
+
channel.postMessage(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
channel.on('message', handleMsg);
|
|
39
|
+
parentPort.postMessage('READY');
|
|
40
|
+
}
|
|
41
|
+
//# sourceMappingURL=worker.js.map
|
package/dist/src/types.d.ts
CHANGED
package/dist/src/types.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@based/db",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.67",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/src/index.js",
|
|
@@ -38,9 +38,9 @@
|
|
|
38
38
|
"basedDbNative.cjs"
|
|
39
39
|
],
|
|
40
40
|
"dependencies": {
|
|
41
|
-
"@based/schema": "5.0.0-alpha.
|
|
41
|
+
"@based/schema": "5.0.0-alpha.23",
|
|
42
42
|
"@saulx/hash": "^3.0.0",
|
|
43
|
-
"@saulx/utils": "^6.7.
|
|
43
|
+
"@saulx/utils": "^6.7.2",
|
|
44
44
|
"exit-hook": "^4.0.0",
|
|
45
45
|
"picocolors": "^1.1.0",
|
|
46
46
|
"@based/crc32c": "^1.0.0"
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024 SAULX
|
|
3
|
-
* SPDX-License-Identifier: MIT
|
|
4
|
-
*/
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include "selva/_export.h"
|
|
8
|
-
#include "selva/types.h"
|
|
9
|
-
#include "selva/traverse.h"
|
|
10
|
-
|
|
11
|
-
struct SelvaFindParam {
|
|
12
|
-
SelvaTraversalNodeCallback node_cb;
|
|
13
|
-
void *node_arg;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Traverse field selector.
|
|
17
|
-
*/
|
|
18
|
-
const struct FindFields {
|
|
19
|
-
uint8_t len;
|
|
20
|
-
struct {
|
|
21
|
-
node_type_t type;
|
|
22
|
-
field_t field;
|
|
23
|
-
} __packed data[];
|
|
24
|
-
} __packed *fields;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Skip the first n nodes in the traversal.
|
|
28
|
-
* This happens before filtering.
|
|
29
|
-
*/
|
|
30
|
-
ssize_t skip;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Skip the first n - 1 results.
|
|
34
|
-
* This skipping is executed on the final result set.
|
|
35
|
-
* 0 for no offset.
|
|
36
|
-
*/
|
|
37
|
-
ssize_t offset;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Limit the number of results.
|
|
41
|
-
* -1 for no limit (inf).
|
|
42
|
-
*/
|
|
43
|
-
ssize_t limit;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
SELVA_EXPORT
|
|
47
|
-
int selva_find(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFindParam *param);
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2025 SAULX
|
|
3
|
-
* SPDX-License-Identifier: MIT
|
|
4
|
-
*/
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include <stddef.h>
|
|
8
|
-
#include <stdint.h>
|
|
9
|
-
#include "selva/types.h"
|
|
10
|
-
#include "selva/_export.h"
|
|
11
|
-
|
|
12
|
-
struct selva_history;
|
|
13
|
-
|
|
14
|
-
struct selva_history_event {
|
|
15
|
-
int64_t ts;
|
|
16
|
-
node_id_t node_id;
|
|
17
|
-
uint32_t crc;
|
|
18
|
-
} __packed __attribute__((aligned(4)));
|
|
19
|
-
|
|
20
|
-
static_assert(alignof(struct selva_history_event) == alignof(uint32_t));
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Create a new history object.
|
|
24
|
-
*/
|
|
25
|
-
SELVA_EXPORT
|
|
26
|
-
int selva_history_create(const char *pathname, size_t bsize, struct selva_history **hist_out);
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* Destroy a history object.
|
|
30
|
-
*/
|
|
31
|
-
SELVA_EXPORT
|
|
32
|
-
void selva_history_destroy(struct selva_history *hist);
|
|
33
|
-
|
|
34
|
-
/**
|
|
35
|
-
* Append a block of size `bsize` to a history object.
|
|
36
|
-
*/
|
|
37
|
-
SELVA_EXPORT
|
|
38
|
-
void selva_history_append(struct selva_history *hist, int64_t ts, node_id_t node_id, void *buf);
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Ensure that the history file is fully written to the disk.
|
|
42
|
-
*/
|
|
43
|
-
SELVA_EXPORT
|
|
44
|
-
void selva_history_fsync(struct selva_history *hist);
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Find a range.
|
|
48
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
49
|
-
*/
|
|
50
|
-
SELVA_EXPORT
|
|
51
|
-
uint32_t *selva_history_find_range(struct selva_history *hist, int64_t from, int64_t to, size_t *size_out);
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Find a range.
|
|
55
|
-
* The returned buffer must be freed with selva_history_free_range().
|
|
56
|
-
*/
|
|
57
|
-
SELVA_EXPORT
|
|
58
|
-
uint32_t *selva_history_find_range_node(struct selva_history *hist, int64_t from, int64_t to, node_id_t node_id, size_t *size_out);
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Free a range returned by selva_history_find_range() or selva_history_find_range_node().
|
|
62
|
-
*/
|
|
63
|
-
SELVA_EXPORT
|
|
64
|
-
void selva_history_free_range(uint32_t *range);
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
*******************************************************************************
|
|
3
|
-
* @file queue_r.h
|
|
4
|
-
* @author Olli Vanhoja
|
|
5
|
-
* @brief Thread-safe queue
|
|
6
|
-
* @section LICENSE
|
|
7
|
-
* Copyright (c) 2020-2021, 2023, 2025 SAULX
|
|
8
|
-
* Copyright (c) 2013, 2015, 2016 Olli Vanhoja <olli.vanhoja@cs.helsinki.fi>
|
|
9
|
-
* Copyright (c) 2012, 2013 Ninjaware Oy,
|
|
10
|
-
* Olli Vanhoja <olli.vanhoja@ninjaware.fi>
|
|
11
|
-
* All rights reserved.
|
|
12
|
-
*
|
|
13
|
-
* Redistribution and use in source and binary forms, with or without
|
|
14
|
-
* modification, are permitted provided that the following conditions are met:
|
|
15
|
-
*
|
|
16
|
-
* 1. Redistributions of source code must retain the above copyright notice,
|
|
17
|
-
* this list of conditions and the following disclaimer.
|
|
18
|
-
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
19
|
-
* this list of conditions and the following disclaimer in the documentation
|
|
20
|
-
* and/or other materials provided with the distribution.
|
|
21
|
-
*
|
|
22
|
-
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
23
|
-
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
24
|
-
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
25
|
-
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
26
|
-
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
27
|
-
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
28
|
-
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
29
|
-
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
30
|
-
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
31
|
-
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
32
|
-
* POSSIBILITY OF SUCH DAMAGE.
|
|
33
|
-
*
|
|
34
|
-
* SPDX-License-Identifier: BSD-2-Clause
|
|
35
|
-
*******************************************************************************
|
|
36
|
-
*/
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* @addtogroup queue_r
|
|
40
|
-
* @{
|
|
41
|
-
*/
|
|
42
|
-
|
|
43
|
-
#pragma once
|
|
44
|
-
|
|
45
|
-
#include <stddef.h>
|
|
46
|
-
#include "selva/_export.h"
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Queue control block.
|
|
50
|
-
*/
|
|
51
|
-
typedef struct queue_cb {
|
|
52
|
-
void * data; /*!< Pointer to the data array used for queue. */
|
|
53
|
-
size_t b_size; /*!< Block size in bytes. */
|
|
54
|
-
size_t a_len; /*!< Array length. */
|
|
55
|
-
size_t m_write; /*!< Write index. */
|
|
56
|
-
size_t m_read; /*!< Read index. */
|
|
57
|
-
} queue_cb_t;
|
|
58
|
-
|
|
59
|
-
#define QUEUE_INITIALIZER(data_array, block_size, array_size) \
|
|
60
|
-
(struct queue_cb){ \
|
|
61
|
-
.data = (data_array), \
|
|
62
|
-
.b_size = (block_size), \
|
|
63
|
-
.a_len = (array_size / block_size), \
|
|
64
|
-
.m_write = 0, \
|
|
65
|
-
.m_read = 0, \
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Create a new queue control block.
|
|
70
|
-
* Initializes a new queue control block and returns it as a value.
|
|
71
|
-
* @param data_array an array where the actual queue data is stored.
|
|
72
|
-
* @param block_size the size of single data block/struct/data type in
|
|
73
|
-
* data_array in bytes.
|
|
74
|
-
* @param array_size the size of the data_array in bytes.
|
|
75
|
-
* @return a new queue_cb_t queue control block structure.
|
|
76
|
-
*/
|
|
77
|
-
SELVA_EXPORT
|
|
78
|
-
queue_cb_t queue_create(void * data_array, size_t block_size,
|
|
79
|
-
size_t array_size)
|
|
80
|
-
__attribute__((pure, access(read_only, 1, 3)));
|
|
81
|
-
|
|
82
|
-
/**
|
|
83
|
-
* Push an element to the queue.
|
|
84
|
-
* @param cb is a pointer to the queue control block.
|
|
85
|
-
* @param element the new element to be copied.
|
|
86
|
-
* @return 0 if queue is already full; otherwise operation was succeed.
|
|
87
|
-
* @note element is always copied to the queue, so it is safe to remove the
|
|
88
|
-
* original data after a push.
|
|
89
|
-
*/
|
|
90
|
-
SELVA_EXPORT
|
|
91
|
-
int queue_push(queue_cb_t * cb, const void * element)
|
|
92
|
-
__attribute__((access(read_write, 1)));
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Allocate an element from the queue.
|
|
96
|
-
* @param cb is a pointer to the queue control block.
|
|
97
|
-
* @return A pointer to the element in the queue.
|
|
98
|
-
*/
|
|
99
|
-
SELVA_EXPORT
|
|
100
|
-
void * queue_alloc_get(queue_cb_t * cb)
|
|
101
|
-
__attribute__((access(read_write, 1)));
|
|
102
|
-
|
|
103
|
-
/**
|
|
104
|
-
* Commit previous allocation from the queue.
|
|
105
|
-
* @note queue_push() should not be called between calls to queue_alloc_get()
|
|
106
|
-
* and queue_alloc_commit().
|
|
107
|
-
*/
|
|
108
|
-
SELVA_EXPORT
|
|
109
|
-
void queue_alloc_commit(queue_cb_t * cb)
|
|
110
|
-
__attribute__((access(read_write, 1)));
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* Pop an element from the queue.
|
|
114
|
-
* @param cb is a pointer to the queue control block.
|
|
115
|
-
* @param element is the location where element is copied to from the queue.
|
|
116
|
-
* @return 0 if queue is empty; otherwise operation was succeed.
|
|
117
|
-
*/
|
|
118
|
-
SELVA_EXPORT
|
|
119
|
-
int queue_pop(queue_cb_t * cb, void * element)
|
|
120
|
-
__attribute__((access(read_write, 1)));
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Peek an element from the queue.
|
|
124
|
-
* @param cb is a pointer to the queue control block.
|
|
125
|
-
* @param element is the location where element is copied to from the queue.
|
|
126
|
-
* @return 0 if queue is empty; otherwise operation was succeed.
|
|
127
|
-
*/
|
|
128
|
-
SELVA_EXPORT
|
|
129
|
-
int queue_peek(queue_cb_t * cb, void ** element)
|
|
130
|
-
__attribute__((access(read_only, 1), access(write_only, 2)));
|
|
131
|
-
|
|
132
|
-
/**
|
|
133
|
-
* Skip n number of elements in the queue.
|
|
134
|
-
* @param cb is a pointer to the queue control block.
|
|
135
|
-
* @return Returns the number of elements skipped.
|
|
136
|
-
*/
|
|
137
|
-
SELVA_EXPORT
|
|
138
|
-
int queue_skip(queue_cb_t * cb, size_t n)
|
|
139
|
-
__attribute__((access(read_write, 1)));
|
|
140
|
-
|
|
141
|
-
/**
|
|
142
|
-
* Clear the queue.
|
|
143
|
-
* This operation is considered safe when committed from the push end thread.
|
|
144
|
-
* @param cb is a pointer to the queue control block.
|
|
145
|
-
*/
|
|
146
|
-
SELVA_EXPORT
|
|
147
|
-
void queue_clear_from_push_end(queue_cb_t * cb)
|
|
148
|
-
__attribute__((access(read_write, 1)));
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* Clear the queue.
|
|
152
|
-
* This operation is considered safe when committed from the pop end thread.
|
|
153
|
-
* @param cb is a pointer to the queue control block.
|
|
154
|
-
*/
|
|
155
|
-
SELVA_EXPORT
|
|
156
|
-
void queue_clear_from_pop_end(queue_cb_t * cb)
|
|
157
|
-
__attribute__((access(read_write, 1)));
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* Check if the queue is empty.
|
|
161
|
-
* @param cb is a pointer to the queue control block.
|
|
162
|
-
* @return 0 if the queue is not empty.
|
|
163
|
-
*/
|
|
164
|
-
SELVA_EXPORT
|
|
165
|
-
int queue_isempty(const queue_cb_t * cb)
|
|
166
|
-
__attribute__((access(read_only, 1)));
|
|
167
|
-
|
|
168
|
-
/**
|
|
169
|
-
* Check if the queue is full.
|
|
170
|
-
* @param cb is a pointer to the queue control block.
|
|
171
|
-
* @return 0 if the queue is not full.
|
|
172
|
-
*/
|
|
173
|
-
SELVA_EXPORT
|
|
174
|
-
int queue_isfull(const queue_cb_t * cb)
|
|
175
|
-
__attribute__((access(read_only, 1)));
|
|
176
|
-
|
|
177
|
-
/**
|
|
178
|
-
* Seek queue.
|
|
179
|
-
* @param cb is a pointer to the queue control block.
|
|
180
|
-
* @param i index.
|
|
181
|
-
* @param[in] element returned element.
|
|
182
|
-
* @return 0 if failed; otherwise succeed.
|
|
183
|
-
*/
|
|
184
|
-
SELVA_EXPORT
|
|
185
|
-
int seek(const queue_cb_t * cb, size_t i, void * element)
|
|
186
|
-
__attribute__((access(read_only, 1), access(write_only, 3)));
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* @}
|
|
190
|
-
*/
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024 SAULX
|
|
3
|
-
* SPDX-License-Identifier: MIT
|
|
4
|
-
*/
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include "selva/_export.h"
|
|
8
|
-
|
|
9
|
-
struct SelvaNode;
|
|
10
|
-
struct SelvaDb;
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Traversal metadata for child/adjacent nodes.
|
|
14
|
-
* Note that SelvaTraversalOrder expects this to be copyable.
|
|
15
|
-
*/
|
|
16
|
-
struct SelvaTraversalMetadata {
|
|
17
|
-
const struct SelvaFields *edge_data; /*!< Edge metadata. */
|
|
18
|
-
long long depth;
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
#define SELVA_TRAVERSAL_ABORT (-2)
|
|
22
|
-
#define SELVA_TRAVERSAL_STOP (-1)
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Called for each node found during a traversal.
|
|
26
|
-
* @param node a pointer to the node.
|
|
27
|
-
* @param arg a pointer to node_arg give in SelvaTraversalCallback structure.
|
|
28
|
-
*/
|
|
29
|
-
typedef int (*SelvaTraversalNodeCallback)(
|
|
30
|
-
struct SelvaDb *db,
|
|
31
|
-
const struct SelvaTraversalMetadata *metadata,
|
|
32
|
-
struct SelvaNode *node,
|
|
33
|
-
void *arg);
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Callback descriptor used for traversals.
|
|
37
|
-
*/
|
|
38
|
-
struct SelvaTraversalParam {
|
|
39
|
-
/**
|
|
40
|
-
* Called for each orphan head in the hierarchy.
|
|
41
|
-
*/
|
|
42
|
-
SelvaTraversalNodeCallback head_cb;
|
|
43
|
-
void *head_arg;
|
|
44
|
-
|
|
45
|
-
/**
|
|
46
|
-
* Called for each node in the hierarchy.
|
|
47
|
-
*/
|
|
48
|
-
SelvaTraversalNodeCallback node_cb;
|
|
49
|
-
void *node_arg;
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* Called for each child of current node.
|
|
53
|
-
*/
|
|
54
|
-
SelvaTraversalNodeCallback child_cb;
|
|
55
|
-
void *child_arg;
|
|
56
|
-
};
|
|
57
|
-
|
|
58
|
-
SELVA_EXPORT
|
|
59
|
-
int selva_traverse_field_bfs(
|
|
60
|
-
struct SelvaDb *db,
|
|
61
|
-
struct SelvaNode *head,
|
|
62
|
-
const struct SelvaTraversalParam *cb);
|
|
63
|
-
|
|
64
|
-
SELVA_EXPORT
|
|
65
|
-
int selva_traverse_type(struct SelvaDb *db, struct SelvaTypeEntry *te, SelvaTraversalNodeCallback node_cb, void *node_arg);
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
* Copyright (c) 2024 SAULX
|
|
3
|
-
* SPDX-License-Identifier: MIT
|
|
4
|
-
*/
|
|
5
|
-
#pragma once
|
|
6
|
-
|
|
7
|
-
#include "selva/_export.h"
|
|
8
|
-
#include "selva/types.h"
|
|
9
|
-
#include "selva/traverse.h"
|
|
10
|
-
|
|
11
|
-
struct SelvaFindParam {
|
|
12
|
-
SelvaTraversalNodeCallback node_cb;
|
|
13
|
-
void *node_arg;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Traverse field selector.
|
|
17
|
-
*/
|
|
18
|
-
const struct FindFields {
|
|
19
|
-
uint8_t len;
|
|
20
|
-
struct {
|
|
21
|
-
node_type_t type;
|
|
22
|
-
field_t field;
|
|
23
|
-
} __packed data[];
|
|
24
|
-
} __packed *fields;
|
|
25
|
-
|
|
26
|
-
/**
|
|
27
|
-
* Skip the first n nodes in the traversal.
|
|
28
|
-
* This happens before filtering.
|
|
29
|
-
*/
|
|
30
|
-
ssize_t skip;
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Skip the first n - 1 results.
|
|
34
|
-
* This skipping is executed on the final result set.
|
|
35
|
-
* 0 for no offset.
|
|
36
|
-
*/
|
|
37
|
-
ssize_t offset;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Limit the number of results.
|
|
41
|
-
* -1 for no limit (inf).
|
|
42
|
-
*/
|
|
43
|
-
ssize_t limit;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
SELVA_EXPORT
|
|
47
|
-
int selva_find(struct SelvaDb *db, struct SelvaNode *node, const struct SelvaFindParam *param);
|