-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorg.favo.db.js
265 lines (218 loc) · 5.39 KB
/
org.favo.db.js
1
2
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* Copyright (c) 2014 Mario Micklisch */
/*
small database module
Example:
```
// open database
var DB = require('org.favo.db').connect("test.db");
// add dummy entries
DB.add({"test": 1});
DB.add({"test": 2});
DB.add({"test": 3});
// get entries
DB.get(1); // returns {"test": 1}
DB.get(2); // returns {"test": 2}
DB.get(3); // returns {"test": 3}
// entry count
DB.len(); // returns 3
// remove entries
DB.rem(1); // removes the first entry
// reset database by deleting all entries
DB.truncate();
// open a second database
var DB2 = require('org.favo.db').connect("test2.db");
DB2.add({});
```
*/
var BUFFER_READ = 32;
exports.connect = function (DB) {
var DB_LEN = -1;
/**
* delete all entries of a database file
* @return {DB}
*/
function truncate () {
var f = E.openFile(DB, 'w');
f.write('');
f.close();
DB_LEN = 0;
return this;
}
/**
* add a new entry to the database
* @param {Object} row
* @return {DB}
*/
function add(row) {
var f = E.openFile(DB, 'a');
f.write(JSON.stringify(row) + "\n");
f.close();
if ( DB_LEN >= 0 ) {
DB_LEN++;
}
return this;
}
/**
* remove an entry from the database
* @param {Number} idx index
* @return {DB}
*/
function rem(idx) {
var f = E.openFile(DB, 'r');
var f2 = E.openFile(DB + '.tmp', 'w');
var curIdx = 0;
var buffer = '';
var data = '';
var removed = false;
var saveRows, ignore, newCurIdx, rows, i;
// read the file in n-byte-steps
while ( !!(data = f.read(BUFFER_READ)) ) {
// if the targed entry was already removed, stream the rest of the data into the tmp file
if ( removed ) {
f2.write(data);
continue;
}
buffer += data;
// is there a line break, indicating different entries?
if ( buffer.indexOf("\n") != -1 ) {
// split entries
rows = buffer.split("\n");
// was there a partial entry read at the end?
if ( buffer[buffer.length-1] != "\n" ) {
buffer = rows.pop();
}
// otherwise reset the buffer
else {
buffer = '';
}
// calculate index where we are at
newCurIdx = curIdx + rows.length;
// are we at the requested index?
if ( idx >= curIdx && idx <= newCurIdx ) {
ignore = idx - curIdx - 1;
saveRows = [];
for ( i in rows ) {
if ( i != ignore ) {
saveRows.push(rows[i]);
if ( DB_LEN >= 0 ) {
DB_LEN--;
}
}
}
f2.write(saveRows.join("\n"));
saveRows = null;
removed = true;
}
else {
f2.write(rows.join("\n"));
}
curIdx += rows.length;
rows = null;
}
}
f.close();
f2.close();
// copy f2 back to f
var f3 = E.openFile(DB, 'w');
var f4 = E.openFile(DB + '.tmp', 'r');
while ( (data = f4.read(BUFFER_READ)) ) {
f3.write(data);
}
f3.close();
f4.close();
// remove tmp file
var f6 = E.openFile(DB + '.tmp', 'w');
f6.write('');
f6.close();
return this;
}
/**
* get the number of entries in the database
* @param {Bool} forceUpdate optional: ignore cache and enforce reading from source file
* @return {Number}
*/
function len(forceUpdate) {
// use cached DB_LEN if available
if ( !forceUpdate && DB_LEN >= 0 ) {
return DB_LEN;
}
var f = E.openFile(DB, 'r');
var cnt = -1;
var buffer = '';
var data = '';
var rows;
while ( !!(data = f.read(BUFFER_READ)) ) {
buffer += data;
// is there a line break, indicating different entries?
if ( buffer.indexOf("\n") != -1 ) {
// split entries
rows = buffer.split("\n");
// was there a partial entry read at the end?
if ( buffer[buffer.length-1] != "\n" ) {
buffer = rows.pop();
}
// otherwise reset the buffer
else {
buffer = '';
}
cnt += rows.length;
}
}
f.close();
DB_LEN = cnt;
return DB_LEN;
}
/**
* get a single entry from the database, identified by its index
* @param {Number} idx index
* @return {Object} row
*/
function get(idx) {
var f = E.openFile(DB, 'r');
var curIdx = 0;
var match = null;
var buffer = '';
var data = '';
var rows;
// read the file in n-byte-steps
while ( !!(data = f.read(BUFFER_READ)) ) {
buffer += data;
// is there a line break, indicating different entries?
if ( buffer.indexOf("\n") != -1 ) {
// split entries
rows = buffer.split("\n");
// was there a partial entry read at the end?
if ( buffer[buffer.length-1] != "\n" ) {
buffer = rows.pop();
}
// otherwise reset the buffer
else {
buffer = '';
}
// calculate index where we are at
var newCurIdx = curIdx + rows.length;
// are we at the requested index?
if ( idx >= curIdx && idx <= newCurIdx ) {
match = rows[idx - curIdx - 1];
break;
}
curIdx += rows.length;
}
}
f.close();
try {
match = JSON.parse(match);
}
catch (e) {
match = false;
}
return match;
}
return {
add: add,
rem: rem,
len: len,
get: get,
truncate: truncate
};
}