Skip to content

Commit c29a317

Browse files
author
zaygo
committed
Added new features, fixed more bugs
1 parent ef0fe84 commit c29a317

File tree

6 files changed

+224
-52
lines changed

6 files changed

+224
-52
lines changed

common-functions.js

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ let config = ejf('./.config.json');
33
const uniqid = require('uniqid');
44
const log = require('simple-node-logger').createSimpleFileLogger(config.get("logs.lazlo_log"));
55

6-
//operator comparison for where clause
6+
//operator comparison for simple where clause
77
function whereComp(prop, operator, val) {
88
let output = [];
99

@@ -82,7 +82,7 @@ function docDeletionLog(docname, db) {
8282

8383
module.exports.docDeletionLog = docDeletionLog;
8484

85-
function dbLog(dbname,op) {
85+
function dbLog(dbname, op) {
8686
if (op === true) {
8787
log.info(`DBC : ${dbname} created in ${process.env.LAZLO_SOURCE} on `, new Date().toISOString().replace('T', '@').substr(0, 16));
8888
} else {
@@ -96,4 +96,52 @@ function docUpdateLog(id, docname) {
9696
log.info(`DU : Entry(id : ${id}) updated in ${docname} on `, new Date().toISOString().replace('T', '@').substr(0, 16));
9797
}
9898

99-
module.exports.docUpdateLog = docUpdateLog;
99+
module.exports.docUpdateLog = docUpdateLog;
100+
101+
// operator comparison for propWhere
102+
function propComp(prop1, operator, prop2) {
103+
let output = [];
104+
105+
if (operator === '=') {
106+
cache.forEach((obj) => {
107+
if (obj[prop1] === obj[prop2]) {
108+
output.push(obj);
109+
}
110+
});
111+
} else if (operator === '!=') {
112+
cache.forEach((obj) => {
113+
if (obj[prop1] !== obj[prop2]) {
114+
output.push(obj);
115+
}
116+
});
117+
} else if (operator === '>') {
118+
cache.forEach((obj) => {
119+
if (obj[prop1] > obj[prop2]) {
120+
output.push(obj);
121+
}
122+
});
123+
} else if (operator === '<') {
124+
cache.forEach((obj) => {
125+
if (obj[prop1] < obj[prop2]) {
126+
output.push(obj);
127+
}
128+
});
129+
} else if (operator === '>=') {
130+
cache.forEach((obj) => {
131+
if (obj[prop1] > obj[prop2] || obj[prop1] === obj[prop2]) {
132+
output.push(obj);
133+
}
134+
});
135+
} else if (operator === '<=') {
136+
cache.forEach((obj) => {
137+
if (obj[prop1] < obj[prop2] || obj[prop1] === obj[prop2]) {
138+
output.push(obj);
139+
}
140+
});
141+
} else {
142+
output = null;
143+
}
144+
return output;
145+
}
146+
147+
module.exports.propComp = propComp;

doc-functions.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,15 @@ function recordsByDate(date,docname,callback) {
389389
callback((chalk.red.bold(msg)));
390390
}
391391
}
392-
module.exports.recordsByDate = recordsByDate;
392+
module.exports.recordsByDate = recordsByDate;
393+
394+
//check if a property exists in atleast one record in a doc
395+
function propExists(prop) {
396+
let exists = false;
397+
cache.forEach((obj) => {
398+
if(obj.hasOwnProperty(prop))
399+
return exists = true;
400+
});
401+
return exists;
402+
}
403+
module.exports.propExists = propExists;

functions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const os = require('os');
12
const ejf = require('edit-json-file');
23
let config = ejf('./.config.json');
34
const mkdir = require('mkdirp');
@@ -11,7 +12,7 @@ const cfn = require('./common-functions');
1112
function setsrc(src,callback) {
1213
let msg;
1314
if(fs.existsSync(src) && src!==process.env.LAZLO_SOURCE) {
14-
config.set("db_src",src);
15+
config.set(`db_src.${os.platform()}`,src);
1516
config.save();
1617
process.env.LAZLO_SOURCE = src;
1718
fs.writeFileSync('./db_tracker.txt','');

main.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ else {
2727
global.db = null; //Stores db name being currently used
2828
global.db_tracker = null;
2929
global.cache = [];
30-
global.current_doc = null; //Stores doc name being currently used
30+
global.current_doc = null; //Stores doc name being currently stored in cache
3131

3232
//Reading db_tracker log
3333
let text = fs.readFileSync('./db_tracker.txt').toString();
@@ -331,6 +331,26 @@ lazlo
331331
cb();
332332
});
333333

334+
lazlo
335+
.command('identify from <docname> <where> <prop1> <operator> <prop2>', 'Compare two properties')
336+
.action(function(args,cb) {
337+
let docname = args.docname;
338+
let where = args.where;
339+
let prop1 = args.prop1;
340+
let operator = args.operator;
341+
let prop2 = args.prop2;
342+
if(where === 'where') {
343+
sad.propWhere(docname,prop1,operator,prop2,(msg) => {
344+
lazlo.log(msg);
345+
})
346+
} else {
347+
let msg = 'Synatactical error detected !';
348+
lazlo.log(chalk.red.bold(msg));
349+
}
350+
cb();
351+
});
352+
353+
334354
lazlo
335355
.delimiter(chalk.magenta.bold('lazlo =>'))
336356
.log(chalk.red.bold(`ATTENTION : CURRENT DATA SOURCE IS ${process.env.LAZLO_SOURCE}`))

screenshots/4.png

24 KB
Loading

slice&dice.js

Lines changed: 138 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const chalk = require('chalk');
66
const lodash = require('lodash');
77

88
const cfn = require('./common-functions');
9+
const docfn = require('./doc-functions');
910

1011
//simple where clause (Compare single property with single value)
1112
function whereClause(docname, prop, operator, val, callback) {
@@ -15,15 +16,22 @@ function whereClause(docname, prop, operator, val, callback) {
1516
if (db !== null && fs.existsSync(p)) {
1617
if (cache.length !== 0 && current_doc === docname) {
1718
//fast retrieval using cache
18-
output = cfn.whereComp(prop, operator, val);
19-
if (output !== null) {
20-
if (callback)
21-
callback(output)
22-
} else {
23-
msg = 'Operator not recognized !';
19+
if(!docfn.propExists(prop)) {
20+
msg = 'Property does not exist in any record !';
2421
if (callback)
2522
callback((chalk.red.bold(msg)));
2623
}
24+
else {
25+
output = cfn.whereComp(prop, operator, val);
26+
if (output !== null) {
27+
if (callback)
28+
callback(output)
29+
} else {
30+
msg = 'Operator not recognized !';
31+
if (callback)
32+
callback((chalk.red.bold(msg)));
33+
}
34+
}
2735
} else {
2836
//normal retrieval
2937
if (fs.statSync(p).size !== 0) {
@@ -35,15 +43,22 @@ function whereClause(docname, prop, operator, val, callback) {
3543
} else {
3644
cache = msgpack.decode(data);
3745
current_doc = docname;
38-
output = cfn.whereComp(prop, operator, val);
39-
if (output !== null) {
40-
if (callback)
41-
callback(output)
42-
} else {
43-
msg = 'Operator not recognized !';
46+
if(!docfn.propExists(prop)) {
47+
msg = 'Property does not exist in any record !';
4448
if (callback)
4549
callback((chalk.red.bold(msg)));
4650
}
51+
else {
52+
output = cfn.whereComp(prop, operator, val);
53+
if (output !== null) {
54+
if (callback)
55+
callback(output)
56+
} else {
57+
msg = 'Operator not recognized !';
58+
if (callback)
59+
callback((chalk.red.bold(msg)));
60+
}
61+
}
4762
}
4863
});
4964
} else {
@@ -69,27 +84,34 @@ function whereClause2(docname, prop1, operator1, val1, conjunction, prop2, opera
6984
if (db !== null && fs.existsSync(p)) {
7085
if (cache.length !== 0 && current_doc === docname) {
7186
//fast retrieval using cache
72-
output1 = cfn.whereComp(prop1, operator1, val1);
73-
output2 = cfn.whereComp(prop2, operator2, val2);
74-
if(output1 === null || output2 === null) {
75-
msg = 'Operator not recognized !';
87+
if(!docfn.propExists(prop1) || !docfn.propExists(prop2)) {
88+
msg = 'One or more properties do not exist in any record !';
7689
if (callback)
7790
callback((chalk.red.bold(msg)));
78-
} else {
79-
if(conjunction === 'and') {
80-
let result = lodash.intersection(output1,output2);
81-
if(callback)
82-
callback(result)
83-
}
84-
else if(conjunction === 'or') {
85-
let result = lodash.union(output1,output2);
86-
if(callback)
87-
callback(result)
88-
}
89-
else {
90-
msg = 'Conjunction not recognized !';
91+
}
92+
else {
93+
output1 = cfn.whereComp(prop1, operator1, val1);
94+
output2 = cfn.whereComp(prop2, operator2, val2);
95+
if(output1 === null || output2 === null) {
96+
msg = 'Operator not recognized !';
9197
if (callback)
9298
callback((chalk.red.bold(msg)));
99+
} else {
100+
if(conjunction === 'and') {
101+
let result = lodash.intersection(output1,output2);
102+
if(callback)
103+
callback(result)
104+
}
105+
else if(conjunction === 'or') {
106+
let result = lodash.union(output1,output2);
107+
if(callback)
108+
callback(result)
109+
}
110+
else {
111+
msg = 'Conjunction not recognized !';
112+
if (callback)
113+
callback((chalk.red.bold(msg)));
114+
}
93115
}
94116
}
95117
} else {
@@ -103,30 +125,100 @@ function whereClause2(docname, prop1, operator1, val1, conjunction, prop2, opera
103125
} else {
104126
cache = msgpack.decode(data);
105127
current_doc = docname;
106-
output1 = cfn.whereComp(prop1, operator1, val1);
107-
output2 = cfn.whereComp(prop2, operator2, val2);
108-
if(output1 === null || output2 === null) {
109-
msg = 'Operator not recognized !';
128+
if (!docfn.propExists(prop1) || !docfn.propExists(prop2)) {
129+
msg = 'One or more properties do not exist in any record !';
110130
if (callback)
111131
callback((chalk.red.bold(msg)));
112132
} else {
113-
if(conjunction === 'and') {
114-
let result = lodash.intersection(output1,output2);
115-
if(callback)
116-
callback(result)
117-
}
118-
else if(conjunction === 'or') {
119-
let result = lodash.union(output1,output2);
120-
if(callback)
121-
callback(result)
133+
output1 = cfn.whereComp(prop1, operator1, val1);
134+
output2 = cfn.whereComp(prop2, operator2, val2);
135+
if (output1 === null || output2 === null) {
136+
msg = 'Operator not recognized !';
137+
if (callback)
138+
callback((chalk.red.bold(msg)));
139+
} else {
140+
if (conjunction === 'and') {
141+
let result = lodash.intersection(output1, output2);
142+
if (callback)
143+
callback(result)
144+
} else if (conjunction === 'or') {
145+
let result = lodash.union(output1, output2);
146+
if (callback)
147+
callback(result)
148+
} else {
149+
msg = 'Conjunction not recognized !';
150+
if (callback)
151+
callback((chalk.red.bold(msg)));
152+
}
122153
}
123-
else {
124-
msg = 'Conjunction not recognized !';
154+
}
155+
}
156+
});
157+
} else {
158+
msg = 'Document is empty !';
159+
if (callback)
160+
callback((chalk.red.bold(msg)));
161+
}
162+
}
163+
} else {
164+
msg = 'No database selected or the document does not exist !';
165+
if (callback)
166+
callback((chalk.red.bold(msg)));
167+
}
168+
}
169+
module.exports.whereClause2 = whereClause2;
170+
171+
// compare properties in a record
172+
function propWhere(docname, prop1, operator, prop2, callback) {
173+
let msg;
174+
let output;
175+
let p = `${process.env.LAZLO_SOURCE}/${db}/${docname}.laz`;
176+
if (db !== null && fs.existsSync(p)) {
177+
if (cache.length !== 0 && current_doc === docname) {
178+
//fast retrieval using cache
179+
if(!docfn.propExists(prop1) || !docfn.propExists(prop2)) {
180+
msg = 'One or more properties do not exist in any record !';
181+
if (callback)
182+
callback((chalk.red.bold(msg)));
183+
}
184+
else {
185+
output = cfn.propComp(prop1, operator, prop2);
186+
if (output !== null) {
187+
if (callback)
188+
callback(output)
189+
} else {
190+
msg = 'Operator not recognized !';
191+
if (callback)
192+
callback((chalk.red.bold(msg)));
193+
}
194+
}
195+
} else {
196+
//normal retrieval
197+
if (fs.statSync(p).size !== 0) {
198+
fs.readFile(p, (err, data) => {
199+
if (err) {
200+
msg = 'Data seems to be corrupted !';
201+
if (callback)
202+
callback((chalk.red.bold(msg)));
203+
} else {
204+
cache = msgpack.decode(data);
205+
current_doc = docname;
206+
if (!docfn.propExists(prop1) || !docfn.propExists(prop2)) {
207+
msg = 'One or more properties do not exist in any record !';
208+
if (callback)
209+
callback((chalk.red.bold(msg)));
210+
} else {
211+
output = cfn.propComp(prop1, operator, prop2);
212+
if (output !== null) {
213+
if (callback)
214+
callback(output)
215+
} else {
216+
msg = 'Operator not recognized !';
125217
if (callback)
126218
callback((chalk.red.bold(msg)));
127219
}
128220
}
129-
}
221+
}
130222
});
131223
} else {
132224
msg = 'Document is empty !';
@@ -140,4 +232,4 @@ function whereClause2(docname, prop1, operator1, val1, conjunction, prop2, opera
140232
callback((chalk.red.bold(msg)));
141233
}
142234
}
143-
module.exports.whereClause2 = whereClause2;
235+
module.exports.propWhere = propWhere;

0 commit comments

Comments
 (0)