forked from nlf/riakdown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiterator.js
155 lines (124 loc) · 4.51 KB
/
iterator.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
var util = require('util');
var utils = require('./utils');
var async = require('async');
var Transform = require('stream').Transform;
var AbstractIterator = require('abstract-leveldown').AbstractIterator;
function RiakIterator(db, options) {
AbstractIterator.call(this, db);
options = JSON.parse(JSON.stringify(options));
this._bucket = options.bucket || db._bucket;
this._reverse = !!options.reverse;
this._keyAsBuffer = !!options.keyAsBuffer;
this._valueAsBuffer = !!options.valueAsBuffer;
var low, high;
if (options.hasOwnProperty('lt') && options.lt !== null && options.lt !== '') {
high = utils.decrement(options.lt);
} else if (options.hasOwnProperty('lte') && options.lte !== null && options.lte !== '') {
high = utils.roundDown(options.lte);
}
if (typeof high === 'undefined' || high === null || high === '') {
high = utils.roundDown(this._reverse ? options.start : options.end);
}
if (typeof high === 'undefined' || high === null || high === '') {
high = '~';
}
if (options.hasOwnProperty('gt') && options.gt !== null && options.gt !== '') {
low = utils.increment(options.gt);
} else if (options.hasOwnProperty('gte') && options.gte !== null && options.gte !== '') {
low = utils.roundUp(options.gte);
}
if (typeof low === 'undefined' || low === null || low === '') {
low = utils.roundUp(this._reverse ? options.end : options.start);
}
if (typeof low === 'undefined' || low === null || low === '') {
low = '!';
}
var query = {
bucket: this._bucket,
qtype: 1,
pagination_sort: true
};
if (options.index) {
query.index = this._reverse ? '_reverse_' + options.index : options.index;
} else {
query.index = this._reverse ? '_reverse_key_bin' : '$key';
}
if (options.limit > 0) {
query.max_results = options.limit;
if (options.continuation) {
query.continuation = options.continuation;
}
}
var keyIsString = (/_bin$/.test(query.index) || query.index === '$key') ? true : false;
if (this._reverse) {
if (keyIsString) {
while (low.length < high.length) {
low += '!';
}
}
query.range_min = keyIsString ? utils.reverseString(high) : -1 * high;
query.range_max = keyIsString ? utils.reverseString(low) : -1 * low;
} else {
if (keyIsString) {
while (high.length < low.length) {
high += '~';
}
}
query.range_max = high;
query.range_min = low;
}
var continuation;
var keyTransform = new Transform({ objectMode: true });
keyTransform._transform = function (chunk, encoding, next) {
if (chunk.continuation) {
continuation = chunk.continuation;
}
if (!chunk.keys) {
return next();
}
for (var i = 0, l = chunk.keys.length; i < l; i++) {
this.push(chunk.keys[i]);
}
next();
};
var self = this;
this._results = new Transform({ objectMode: true });
this._results._transform = function (chunk, encoding, next) {
db._client.get({ bucket: self._bucket, key: chunk }, function (err, res) {
if (err || !res.content) {
return next();
}
this.push({ key: chunk, value: res.content[0].value, extra: { vclock: res.vclock, continuation: continuation } });
next();
}.bind(this));
};
this._results.once('end', function () {
this._endEmitted = true;
}.bind(this));
db._client.getIndex(query).pipe(keyTransform).pipe(this._results);
}
util.inherits(RiakIterator, AbstractIterator);
RiakIterator.prototype._next = function (callback) {
var self = this;
var onEnd = function () {
self._results.removeListener('readable', onReadable);
callback();
};
var onReadable = function () {
self._results.removeListener('end', onEnd);
self._next(callback);
};
var obj = this._results.read();
if (self._endEmitted) {
callback();
} else if (obj === null) {
this._results.once('readable', onReadable);
this._results.once('end', onEnd);
} else {
callback(null, this._keyAsBuffer ? new Buffer(obj.key) : obj.key, this._valueAsBuffer ? obj.value : obj.value.toString(), obj.extra);
}
};
RiakIterator.prototype._end = function (callback) {
callback();
};
module.exports = RiakIterator;