/
memory.js
239 lines (223 loc) · 5.62 KB
/
memory.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
var sys = require('sys');
var fs = require('fs');
var defer = process.nextTick;
function make_row(id, data) {
var row = {};
process.mixin(row, data);
row._id = id;
return row;
}
function Store(conn, name, columns) {
this.columns = columns;
this.changed = conn.changed;
// Hook up with the main data store
if (!conn.data[name]) {
conn.data[name] = [];
conn.changed();
}
this.data = conn.data[name];
}
Store.prototype = {
get: function (id, callback) {
var table = this.data;
defer(function () {
var data = table[id - 1];
if (data) {
callback(make_row(id, data));
} else {
callback(data);
}
});
},
find: function (conditions, row_callback, callback) {
// row_callback is optional
if (typeof callback === 'undefined') {
callback = row_callback;
row_callback = false;
}
var table = this.data;
// Shortcut if there are no conditions.
if (conditions === undefined || conditions.length === 0) {
return this.all(callback);
}
defer(function () {
var results;
if (conditions.constructor.name !== 'Array') {
conditions = [conditions];
}
results = table.map(function (data, index) {
return make_row(index + 1, data);
}).filter(function (row) {
return conditions.some(function (group) {
return Object.keys(group).every(function (condition) {
var value = group[condition],
operator,
p = condition.indexOf(' ');
if (p === -1) {
return row[condition] == value;
}
operator = condition.substr(p + 1);
condition = condition.substr(0, p);
switch (operator) {
case '<':
return row[condition] < value;
case '>':
return row[condition] > value;
case '=':
return row[condition] === value;
case '<=':
return row[condition] <= value;
case '>=':
return row[condition] >= value;
case '!=':
case '<>':
return row[condition] !== value;
case '~':
return row[condition].match(new RegExp(value));
}
sys.debug(operator);
throw "Invalid operator " + operator;
});
});
});
if (row_callback) {
results.forEach(function (row) {
row_callback(row);
});
callback();
} else {
callback(results);
}
});
},
each: function (row_callback, callback) {
this.data.forEach(function (data, i) {
defer(function () {
row_callback(make_row(i + 1, data));
});
});
defer(function () {
callback();
});
},
all: function (callback) {
var table = this.data;
defer(function () {
callback(table.map(function (data, index) {
return make_row(index + 1, data);
}));
});
},
save: function (row, callback) {
var table = this.data;
var self = this;
defer(function () {
var data = {},
id = table.length + 1;
process.mixin(data, row);
if (data._id) {
id = data._id;
delete data._id;
table[id - 1] = data;
self.changed();
callback();
} else {
table.push(data);
row._id = table.length;
self.changed();
callback(row._id);
}
});
},
remove: function (row, callback) {
var table = this.data;
var self = this;
defer(function () {
var index;
if (row instanceof Object) {
index = row._id - 1;
delete row._id;
} else {
index = row - 1;
}
table.splice(index, 1);
self.changed();
callback();
});
},
nuke: function (callback) {
var self = this;
defer(function () {
self.data = [];
self.changed();
callback();
});
}
};
function Connection(file) {
var data = this.data = {};
var waiting = false;
var one_more = false;
var self = this;
// Save at most often evert 5 seconds.
var timer;
function changed() {
if (waiting) {
one_more = true;
} else {
save();
waiting = true;
timer = setTimeout(function () {
waiting = false;
if (one_more) {
one_more = false;
changed();
}
}, 30000); // 30 second rest between writes
}
}
function save() {
if (timer) { clearTimeout(timer); }
var json = JSON.stringify(data)
fs.writeFile(file, json, function (err) {
if (err) {
self.emit('error', err);
// sys.error(err);
} else {
// sys.puts("Saved " + json.length + " bytes to " + file);
}
});
}
function load() {
fs.readFile(file, function (err, json) {
if (err) {
// sys.error("Warning: cannot open " + file);
save(); // Trigger File creation
} else {
// sys.puts("Loaded " + json.length + " bytes from " + file);
self.data = JSON.parse(json);
}
})
}
if (file) {
load();
this.changed = changed;
this.close = save;
}
}
Connection.prototype = new process.EventEmitter();
Connection.prototype.get_store = function (name, columns) {
return new Store(this, name, columns);
};
Connection.prototype.close = function () {};
Connection.prototype.changed = function () {};
exports.get_store = function (name, columns) {
return (new Connection).get_store(name, columns);
};
exports.new_connection = function (file) {
var db = new Connection(file);
defer(function () {
db.emit('connection');
});
return db;
};