-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindexer.js
143 lines (131 loc) · 3.98 KB
/
indexer.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
module.exports = TreeDBIndexer;
var async = require('async');
function TreeDBIndexer(tree) {
if (!(this instanceof TreeDBIndexer)) return new TreeDBIndexer(tree);
var self = this;
self.tree = tree;
};
TreeDBIndexer.prototype.store = function(ch, cb) {
var self = this;
var key = ch.key;
var q = {gt: key.concat(null), lt: key.concat(undefined)};
var parentKeys = [];
self.tree.roots.createReadStream(q).on('data', function(ch) {
parentKeys.push([ch.key[2], ch.key[3]]);
}).on('end', function() {
if (parentKeys.length === 0) self.putIndexes(ch, null, cb);
else {
async.map(
parentKeys,
function(parentKey, asyncCb) {
self.putIndexes(ch, parentKey, asyncCb);
},
function(err, results) {
cb(err, results);
}
);
}
});
};
// options: {type, parentType, field, callback}
TreeDBIndexer.prototype.addIndex = function(options) {
var type = options.type;
var parentType = options.parentType || false;
var agnostic = options.agnostic || false;
var field = options.field;
var callback = options.callback || noop;
var self = this;
var rows = [];
var key = ['pri', type, field];
if (parentType) {
key = ['sec', type, parentType, field];
}
else if (agnostic) {
key = ['agn', type, field];
}
rows.push({type: 'put', key: key, value: 0});
var storeRequests = [];
storeRequests.push(function(cb) { self.tree.indexes.batch(rows, cb); });
commit();
function commit() {
async.parallel(storeRequests, function(err) { callback(err, key); });
};
};
TreeDBIndexer.prototype.delIndexes = function(key) {
// console.log('del indexes');
};
TreeDBIndexer.prototype.putIndexes = function(ch, parentKey, cb) {
if (!cb) cb = noop;
var self = this;
var rows = [];
var storeRequests = [];
var key = ch.key;
var type = ch.key[0];
var val = ch.value;
var id = key[1];
self.indexesOf(key, function(err, indexes) {
indexes.forEach(function(index) {
// gets all primary/secondary indexes
var indexedField = index.key[index.key.length - 1].split('.');
var indexedValue = getIndexedValue(indexedField, val);
if (indexedValue) {
var indexedKey = index.key.concat([indexedValue, id]);
if (index.key[0] === 'sec') {
indexedKey.splice(3, 0, parentKey[1]);
}
// ['pri', 'board', 'created_at', 1381891311050, '-y_Jrwa1B']
// ['sec', 'thread', 'board', 'Wk-hvQmvHr', 'updated_at', 1415323275770,
// 'ZJc6RZ48Hr']
var row = {type: 'put', key: indexedKey, value: 0};
rows.push(row);
}
});
storeRequests.push(function(cb) { self.tree.indexed.batch(rows, cb); });
commit();
});
function commit() {
async.parallel(storeRequests, function(err) { cb(err, key); });
};
};
TreeDBIndexer.prototype.indexQuery = function(q, cb) {
var indexStream = this.tree.indexes.createReadStream(q);
var indexes = [];
indexStream.on('data', function(index) {
indexes.push(index);
})
.on('end', function() {
cb(null, indexes);
});
};
TreeDBIndexer.prototype.indexesOf = function(key, cb) {
var self = this;
var type = key[0];
var priQuery = {gt: ['pri', type, null], lt: ['pri', type, undefined]};
var secQuery = {gt: ['sec', type, null], lt: ['sec', type, undefined]};
var agnQuery = {gt: ['agn', type, null], lt: ['agn', type, undefined]};
// example: [ 'pri', 'board', 'created_at' ]
async.parallel([
function(cb) { self.indexQuery(priQuery, cb); },
function(cb) { self.indexQuery(secQuery, cb); },
function(cb) { self.indexQuery(agnQuery, cb); }
], function(err, results) {
var indexes = results[0].concat(results[1], results[2]);
return cb(err, indexes);
});
};
function noop(){};
function getIndexedValue(arr, obj) {
var result = obj;
var successful = arr.every(function(field, index, array){
if (result[field]) {
result = result[field];
return true;
}
else {
return false;
}
});
if (successful) {
return result;
}
};