-
Notifications
You must be signed in to change notification settings - Fork 14
/
iterator.js
235 lines (216 loc) · 6.1 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
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
'use strict';
var inherits = require('inherits');
var AbstractIterator = require('abstract-leveldown').AbstractIterator;
var Buffer = require('safe-buffer').Buffer;
inherits(Iterator, AbstractIterator);
module.exports = Iterator;
var scriptsLoader = require('./scriptsLoader');
var concatKey = function (prefix, key, force) {
if (typeof key === 'string' && (force || key.length)) {
return prefix + key;
}
if (Buffer.isBuffer(key) && (force || key.length)) {
return Buffer.concat([Buffer.from(prefix), key]);
}
return key;
};
function goodOptions(opts, name) {
if (!(name in opts)) {
return;
}
var thing = opts[name];
if (thing === null) {
delete opts[name];
return;
}
if (Buffer.isBuffer(thing) || typeof thing === 'string') {
if (!thing.length) {
delete opts[name];
}
}
}
function Iterator(db, options) {
AbstractIterator.call(this, db);
options = options || {};
for (var i = 0; i < options.length; i++) {
goodOptions(options, i);
}
this._count = 0;
this._limit = isNaN(options.limit) ? -1 : options.limit;
this._keyAsBuffer = 'keyAsBuffer' in options ? !!options.keyAsBuffer : false;
this._valueAsBuffer = 'valueAsBuffer' in options ? !!options.valueAsBuffer : false;
this._keys = 'keys' in options ? !!options.keys : true;
this._values = 'values' in options ? !!options.values : true;
this._iterations = 0;
this._offset = 0;
this._highWaterMark = options.highWaterMark || db.highWaterMark || 256;
this._pointer = 0;
this._buffered = [];
_processArithmOptions(options);
this.prepareQuery(options);
}
function _processArithmOptions(options) {
var reverse = !!options.reverse;
if (options.gt !== undefined) {
if (!reverse) {
options._exclusiveStart = true;
options.start = options.gt;
} else {
options._exclusiveEnd = true;
options.end = options.gt;
}
} else if (options.gte !== undefined) {
if (!reverse) {
options.start = options.gte;
} else {
options.end = options.gte;
}
}
if (options.lt !== undefined) {
if (!reverse) {
options._exclusiveEnd = true;
options.end = options.lt;
} else {
options._exclusiveStart = true;
options.start = options.lt;
}
} else if (options.lte !== undefined) {
if (!reverse) {
options.end = options.lte;
} else {
options.start = options.lte;
}
}
}
Iterator.prototype.prepareQuery = function(options) {
this._reverse = !!options.reverse;
if (!this._values) {
// key streams: no need for lua
this.cmdName = this._reverse ? 'zrevrangebylex' : 'zrangebylex';
} else {
var scriptName;
if (!this._keys) {
if (this._reverse) {
scriptName = 'zhrevvalues-lk';
} else {
scriptName = 'zhvalues-lk';
}
} else {
if (this._reverse) {
scriptName = 'zhrevpairs';
} else {
scriptName = 'zhpairs';
}
}
this.sha = scriptsLoader.getSha(scriptName);
}
var reverse = this._reverse;
if (options.start || options.end) {
var start = options.start !== undefined ? (options.start) : '';
var end = options.end !== undefined ? (options.end) : '';
if (start !== '' || end !== '') {
this._start = start === '' ? (reverse ? '+' : '-') : (concatKey((options._exclusiveStart ? '(' : '['), start));
this._done = end === '' ? (reverse ? '-' : '+') : (concatKey((options._exclusiveEnd ? '(' : '['), end));
return;
}
}
this._start = reverse ? '+' : '-';
this._done = reverse ? '-' : '+';
};
Iterator.prototype.makeRangeArgs = function() {
if (this.sha) {
return [ this.sha, 1, this.db.location, this._start, this._done, 'LIMIT', 0 ];
} else {
return [ this.db.location+':z', this._start, this._done, 'LIMIT', 0 ];
}
};
Iterator.prototype._next = function (callback) {
if (this._limit > -1 && this._count >= this._limit) {
return setImmediate(callback);
}
if (this._pointer && this._pointer < this._buffered.length) {
this._shift(callback);
} else {
this._fetch(callback);
}
};
/**
* Gets a batch of key or values or pairs
*/
Iterator.prototype._fetch = function(callback) {
if (this.db.closed) { return callback(); }
var size;
if (this._limit > -1) {
var remain = this._limit - this._offset;
if (remain <= 0) {
return callback();
}
size = remain <= this._highWaterMark ? remain : this._highWaterMark;
} else {
size = this._highWaterMark;
}
var rangeArgs = this.makeRangeArgs();
rangeArgs.push(size);
this._offset += size;
this._iterations++;
var self = this;
function complete(err, reply) {
if (err) {
return callback(err);
}
if (!reply || reply.length === 0) {
return setImmediate(callback);
}
self._pointer = 0;
self._buffered = reply;
if (self._buffered.length) {
// update _start for the next page fetch
if (!self._keys) {
self._start = concatKey('(', self._buffered.pop());
} else if (!self._values) {
self._start = concatKey('(', self._buffered[self._buffered.length-1]);
} else {
self._start = concatKey('(', self._buffered[self._buffered.length-2]);
}
}
self._shift(callback);
}
if (this.cmdName) {
return this.db.db.send_command(this.cmdName, rangeArgs, complete);
} else {
this.db.db.evalsha(rangeArgs, complete);
}
};
/**
* Gets the next key/value from the buffered keys and values.
*/
Iterator.prototype._shift = function(callback) {
// todo: tell redis to return buffers and we have less things to do?
var key, value;
if (this._keys) {
var vkey = this._buffered[this._pointer];
this._pointer++;
if (vkey !== undefined) {
if (this._keyAsBuffer) {
key = Buffer.from(vkey);
} else {
key = String(vkey);
}
}
}
if (this._values) {
var vvalue = this._buffered[this._pointer];
this._pointer++;
if (vvalue !== undefined) {
if (this._valueAsBuffer) {
value = Buffer.from(vvalue);
} else {
value = String(vvalue);
}
}
}
if (key !== undefined || value !== undefined) {
this._count++;
}
callback(null, key, value);
};