Skip to content

Commit

Permalink
Add option to skip storing reads
Browse files Browse the repository at this point in the history
  • Loading branch information
canmingir committed Apr 29, 2023
1 parent fdc5932 commit 6e5b9cd
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 35 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"url": "https://github.com/NucleoidJS/Nucleoid.git"
},
"dependencies": {
"@nucleoidjs/datastore": "^1.1.1",
"@nucleoidjs/datastore": "^1.1.4",
"axios": "^1.2.0",
"cors": "^2.8.5",
"express": "^4.17.1",
Expand Down
5 changes: 5 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ let defaultConfig = {
port: {
terminal: 8448,
cluster: 4000,
openapi: 3000,
},
declarative: false,
details: false,
cacheOnly: false,
data: {
encryption: true,
skipRead: false,
},
};

let _config = { ...defaultConfig };
Expand Down
14 changes: 8 additions & 6 deletions src/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ const datastore = require("@nucleoidjs/datastore");
const runtime = require("./runtime");
const state = require("./state");
const config = require("./config");
const openapi = require("./lib/openapi");
const context = require("./lib/context");

function init() {
const { id } = config();
datastore.init({ id });
const _config = config();
datastore.init(_config);

setImmediate(() => {
datastore.read().forEach((details) => {
Expand All @@ -14,12 +16,12 @@ function init() {
cacheOnly: true,
};

if (!details.e) {
if (details.w && !details.e) {
runtime.process(details.s, options);
}

if (details.x) {
details.x.map((exec) => state.run(null, exec));
if (details.j) {
details.j.map((adjust) => state.run(null, adjust));
}
}
});
});
Expand Down
38 changes: 19 additions & 19 deletions src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ const config = require("./config");

module.exports.process = function (statement, options = {}) {
options = { ...config(), ...options };
const { declarative, details, cacheOnly } = options;
const { declarative, details, cacheOnly, skipRead } = options;

let before = Date.now();
let statements, result, error, execs;
let statements, result, error, adjusts, write;

let s = Macro.apply(statement, options);

try {
statements = Statement.compile(s, options);
transaction.start();
result = stack.process(statements, null, options);
execs = transaction
.end()
.filter((t) => t.exec)
.map((t) => t.exec);
const list = transaction.end();
adjusts = list.filter((t) => t.adjust).map((t) => t.adjust);
write = !!list.length;
} catch (e) {
transaction.rollback();
result = e;
Expand All @@ -36,25 +35,27 @@ module.exports.process = function (statement, options = {}) {
Message.clear();
Event.clear();

let hash;
let date = Date.now();
let time = date - before;

if (!cacheOnly) {
if (result instanceof Error)
result = `${result.constructor.name}: ${result.message}`;

hash = datastore.write({
s,
c: declarative ? true : undefined,
t: time,
r: result,
d: date,
e: error,
m: messages,
v: events,
x: execs && execs.length ? execs : undefined,
});
if (write || !skipRead) {
datastore.write({
s,
c: declarative ? true : undefined,
t: time,
r: result,
d: date,
e: error,
m: messages,
v: events,
j: adjusts && adjusts.length ? adjusts : undefined,
w: write ? write : undefined,
});
}
}

if (details) {
Expand All @@ -68,7 +69,6 @@ module.exports.process = function (statement, options = {}) {
error,
messages,
events,
hash,
};
} else {
if (error) {
Expand Down
5 changes: 3 additions & 2 deletions src/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@ function register(p1, p2, p3, adjust) {
value = eval(`state.${variable}=expression`);
}

if (adjust)
transaction.exec = `state.${variable}=${JSON.stringify(value)}`;
if (adjust) {
transaction.adjust = `state.${variable}=${JSON.stringify(value)}`;
}

state[variable] = value;
list.push(transaction);
Expand Down

0 comments on commit 6e5b9cd

Please sign in to comment.