-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
227 lines (216 loc) · 6.4 KB
/
index.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
module.exports = TreeDB;
var path = require('path');
var sublevel = require('level-sublevel/bytewise');
var bytewise = require('bytewise');
var readonly = require('read-only-stream');
var defined = require('defined');
var async = require('async');
var through2 = require('through2');
var TreeDBIndexer = require(path.join(__dirname, 'indexer'));
var TreeDBMeta = require(path.join(__dirname, 'meta'));
var keys = require(path.join(__dirname, 'keys'));
function TreeDB(db, opts) {
if (!(this instanceof TreeDB)) return new TreeDB(db, opts);
if (!opts) opts = {};
this.db = sublevel(db, {keyEncoding: bytewise, valueEncoding: 'json'});
this.branches = this.db.sublevel('branches');
this.roots = this.db.sublevel('roots');
this.indexes = this.db.sublevel('indexes');
this.indexed = this.db.sublevel('indexed');
this.meta = this.db.sublevel('meta');
this.indexer = new TreeDBIndexer(this);
if (opts.meta) {
this.metaTreedb = new TreeDBMeta(this, opts.meta);
}
};
// options: object, type, parentKeys, [callback]
TreeDB.prototype.store = function(options) {
var self = this;
var id = options.id;
var update = options.update;
var object = options.object;
var type = options.type;
var parentKeys = options.parentKeys || false;
var callback = options.callback || noop;
var rows = [], cRels = [], pRels = [];
var key = [type];
if (id) {
// allow supply of id
key.push(id);
}
else {
key.push(keys.hash());
}
rows.push({type: 'put', key: key, value: object});
var storeRequests = [];
if (parentKeys) {
parentKeys.forEach(function(parentKey) {
// assumes parent already has been saved in the database
cRels.push({type: 'put', key: parentKey.concat(key), value: 0});
pRels.push({type: 'put', key: key.concat(parentKey), value: 0});
});
storeRequests.push(function(cb) { self.branches.batch(cRels, cb); });
storeRequests.push(function(cb) { self.roots.batch(pRels, cb); });
}
storeRequests.push(function(cb) {
self.db.batch(rows, cb);
});
commit();
function commit() {
async.parallel(storeRequests, function(err) {
self.indexer.store({key: key, value: object}, function(err, results) {
if (err) {
callback({err: err});
}
else {
self.metaTreedb.store({key: key, value: object, update: update}, function(err) {
callback({err: err, key: key, value: object});
});
}
});
});
};
};
TreeDB.prototype.get = function(key, cb) {
if (!cb) cb = noop;
this.db.get(key, function(err, value) {
return cb(err, {key: key, value: value});
});
};
// options: type, limit, (indexedField, (indexedValue))
TreeDB.prototype.nodes = function(options) {
var self = this;
var type = options.type;
var query;
if (options && options.indexedField) {
var queryPrefix = ['pri', type, options.indexedField];
if (options.agnostic) {
queryPrefix = ['agn', type, options.indexedField];
}
if (options.indexedValue) {
queryPrefix = queryPrefix.concat(options.indexedValue);
}
query = {
limit: options.limit || undefined,
gt: queryPrefix.concat(null),
lt: queryPrefix.concat(undefined)
};
return readonly(self.indexed.createReadStream(query)
.pipe(through2.obj(function(ch, enc, cb) {
var self2 = this;
var dbKey = [ch.key[1], ch.key[ch.key.length - 1]];
self.db.get(dbKey, function(err, val) {
if (err) throw err;
self2.push({key: dbKey, value: val});
cb();
});
})));
}
else {
query = {
gt: [type, null],
lt: [type, undefined]
};
return readonly(self.db.createReadStream(query));
}
};
// options: parentKey, type, indexedField, limit, reverse
TreeDB.prototype.children = function(options) {
var parentKey = options.parentKey;
var type = options.type;
var indexedField = options.indexedField;
var self = this;
var parentType = parentKey[0];
var parentId = parentKey[1];
var query;
if (indexedField) {
var queryKey = ['sec', type, parentKey[0], parentKey[1], indexedField];
query = {
limit: options.limit || undefined,
reverse: options.reverse || false,
gt: queryKey.concat(null),
lt: queryKey.concat(undefined)
};
return readonly(self.indexed.createReadStream(query)
.pipe(through2.obj(function(ch, enc, cb) {
var self2 = this;
// ch is index key/value
var dbKey = [ch.key[1], ch.key[ch.key.length - 1]];
self.db.get(dbKey, function(err, val) {
if (err) throw err;
self2.push({key: dbKey, value: val});
cb();
});
})));
}
else {
query = {
limit: options.limit || undefined,
reverse: options.reverse || false,
gt: parentKey.concat([type, null]),
lt: parentKey.concat([type, undefined])
};
return readonly(self.branches.createReadStream(query)
.pipe(through2.obj(function(ch, enc, cb) {
var self2 = this;
var dbKey = [ch.key[2], ch.key[3]];
self.db.get(dbKey, function(err, val) {
if (err) throw err;
self2.push({key: dbKey, value: val});
cb();
});
})));
}
};
// options: key, parentType, limit
TreeDB.prototype.parents = function(options) {
var self = this;
var queryPrefix = [options.key];
if (options.parentType) {
queryPrefix = queryPrefix.concat(options.parentType);
}
var query = {
limit: options.limit || undefined,
gt: queryPrefix.concat(null),
lt: queryPrefix.concat(undefined)
};
return readonly(self.roots.createReadStream(query)
.pipe(through2.obj(function(ch, enc, cb) {
var self2 = this;
var dbKey = [ch.key[2], ch.key[3]];
self.db.get(dbKey, function(err, val) {
if (err) throw err;
self2.push({key: dbKey, value: val});
cb();
});
})));
};
// options: {type, parentType, field, callback}
TreeDB.prototype.addIndex = function(options) {
this.indexer.addIndex(options);
};
// options: {indexes, callback}
TreeDB.prototype.addIndexes = function(options, cb) {
var self = this;
if (self.indexer) {
async.each(options.indexes, function(index, callback) {
index.callback = callback;
self.indexer.addIndex(index);
}, cb);
}
else {
cb();
}
};
// options: key, field, callback
TreeDB.prototype.metadata = function(options) {
if (this.metaTreedb) {
this.metaTreedb.get(options);
}
else {
if (options.callback) {
options.callback(null);
}
}
};
function noop(){};