-
Notifications
You must be signed in to change notification settings - Fork 17
/
redis.js
384 lines (348 loc) · 11.8 KB
/
redis.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Redis client for Node.js
// Author: Brian Hammond <brian at fictorial dot com>
// Copyright (C) 2009 Fictorial LLC
// License: MIT
var sys = require("sys"),
tcp = require("tcp");
var crlf = "\r\n",
crlf_len = 2;
var inline_commands = {
auth:1, bgsave:1, dbsize:1, decr:1, decrby:1, del:1,
exists:1, expire:1, flushall:1, flushdb:1, get:1, incr:1, incrby:1, info:1,
keys:1, lastsave:1, lindex:1, llen:1, lpop:1, lrange:1, ltrim:1, mget:1,
move:1, randomkey:1, rename:1, renamenx:1, rpop:1, save:1, scard:1, sdiff:1,
sdiffstore:1, select:1, shutdown:1, sinter:1, sinterstore:1, smembers:1,
spop:1, srandmember:1, sunion:1, sunionstore:1, ttl:1, type:1,
zrange:1, zrevrange:1, zcard:1, zrangebyscore:1
};
var bulk_commands = {
getset:1, lpush:1, lrem:1, lset:1, rpush:1, sadd:1, set:1,
setnx:1, sismember:1, smove:1, srem:1, zadd:1, zrem:1, zscore:1
};
var Client = exports.Client = function (port, host) {
this.host = host || '127.0.0.1';
this.port = port || 6379;
this.callbacks = [];
this.conn = null;
};
// Callback a function after we've ensured we're connected to Redis.
Client.prototype.connect = function (callback_on_connect) {
var self = this;
if (this.conn && this.conn.readyState === "open") {
if (typeof(callback_on_connect) === "function")
callback_on_connect();
} else {
this.conn = new process.tcp.Connection();
this.conn.addListener("connect", function () {
this.setEncoding("binary");
this.setTimeout(0); // try to stay connected.
this.setNoDelay();
if (typeof(callback_on_connect) === "function")
callback_on_connect();
});
this.conn.addListener("receive", function (data) {
if (!self.buffer)
self.buffer = "";
self.buffer += data;
self.handle_replies();
});
this.conn.addListener("close", function (encountered_error) {
if (encountered_error)
throw new Error("redis server up?");
});
this.conn.connect(this.port, this.host);
}
};
Client.prototype.close = function () {
if (this.conn && this.conn.readyState === "open") {
this.conn.close();
this.conn = null;
}
};
// Reply handlers read replies from the current reply buffer. At the time of
// the call the buffer will start with at least the prefix associated with the
// relevant reply type which is at this time always of length 1.
//
// Note the buffer may not contain a full reply in which case these reply
// handlers return null. In this case the buffer is left intact for future
// "receive" events to append onto, and the read-replies process repeats.
// Repeat ad infinitum.
//
// Each handler returns [ value, next_command_index ] on success, null on
// underflow.
var prefix_len = 1;
// Bulk replies resemble:
// $6\r\nFOOBAR\r\n
Client.prototype.handle_bulk_reply = function (start_at, buf) {
var buffer = buf || this.buffer;
start_at = (start_at || 0) + prefix_len;
var crlf_at = buffer.indexOf(crlf, start_at);
if (crlf_at === -1)
return null;
var value_len_str = buffer.substring(start_at, crlf_at);
var value_len = parseInt(value_len_str, 10);
if (value_len === NaN)
throw new Error("invalid bulk value len: " + value_len_str);
if (value_len === -1) // value doesn't exist
return [ null, crlf_at + crlf_len ];
var value_at = crlf_at + crlf_len;
var next_reply_at = value_at + value_len + crlf_len;
if (next_reply_at > buffer.length)
return null;
var value = buffer.substr(value_at, value_len);
return [ value, next_reply_at ];
}
// Mult-bulk replies resemble:
// *4\r\n$3\r\nFOO\r\n$3\r\nBAR\r\n$5\r\nHELLO\r\n$5\r\nWORLD\r\n
// *4 is the number of bulk replies to follow.
Client.prototype.handle_multi_bulk_reply = function (buf) {
var buffer = buf || this.buffer;
var crlf_at = buffer.indexOf(crlf, prefix_len);
if (crlf_at === -1)
return null;
var count_str = buffer.substring(prefix_len, crlf_at);
var count = parseInt(count_str, 10);
if (count === NaN)
throw new Error("invalid multi-bulk count: " + count_str);
var next_reply_at = crlf_at + crlf_len;
if (count === -1) // value doesn't exist
return [ null, next_reply_at ];
if (next_reply_at >= buffer.length)
return null;
var results = [];
for (var i = 0; i < count; ++i) {
var bulk_reply = this.handle_bulk_reply(next_reply_at, buffer);
if (bulk_reply === null) // no full multi-bulk cmd
return null;
var bulk_reply_value = bulk_reply[0];
results.push(bulk_reply_value);
next_reply_at = bulk_reply[1];
}
return [ results, next_reply_at ];
};
// Single line replies resemble:
// +OK\r\n
Client.prototype.handle_single_line_reply = function (buf) {
var buffer = buf || this.buffer;
var crlf_at = buffer.indexOf(crlf, prefix_len);
if (crlf_at === -1)
return null;
var value = buffer.substring(prefix_len, crlf_at);
if (value === 'OK')
value = true;
var next_reply_at = crlf_at + crlf_len;
return [ value, next_reply_at ];
};
// Integer replies resemble:
// :1000\r\n
Client.prototype.handle_integer_reply = function (buf) {
var buffer = buf || this.buffer;
var crlf_at = buffer.indexOf(crlf, prefix_len);
if (crlf_at === -1)
return null;
var value_str = buffer.substring(prefix_len, crlf_at);
var value = parseInt(value_str, 10);
if (value === NaN)
throw new Error("invalid integer reply: " + value_str);
var next_reply_at = crlf_at + crlf_len;
return [ value, next_reply_at ];
};
// Error replies resemble:
// -ERR you suck at tennis\r\n
Client.prototype.handle_error_reply = function (buf) {
var buffer = buf || this.buffer;
var crlf_at = buffer.indexOf(crlf, prefix_len);
if (crlf_at === -1)
return null;
var value = buffer.substring(prefix_len, crlf_at);
var next_reply_at = crlf_at + crlf_len;
if (value.indexOf("ERR ") === 0)
value = value.substr("ERR ".length);
return [ value, next_reply_at ];
}
// Try to read as many replies from the current buffer as we can. Leave
// partial replies in the buffer, else eat 'em. Dispatch any promises waiting
// for these replies. Error replies emit error on the promise, else success is
// emitted.
Client.prototype.handle_replies = function () {
while (this.buffer.length > 0) {
if (GLOBAL.DEBUG) {
write_debug('---');
write_debug('buffer: ' + this.buffer);
}
var prefix = this.buffer.charAt(0);
var result, is_error = false;
switch (prefix) {
case '$': result = this.handle_bulk_reply(); break;
case '*': result = this.handle_multi_bulk_reply(); break;
case '+': result = this.handle_single_line_reply(); break;
case ':': result = this.handle_integer_reply(); break;
case '-': result = this.handle_error_reply(); is_error = true; break;
}
// The handlers return null when there's not enough data
// in the buffer to read a full reply. Leave the buffer alone until
// we receive more data.
if (result === null)
break;
if (GLOBAL.DEBUG) {
write_debug('prefix: ' + prefix);
write_debug('result: ' + JSON.stringify(result));
}
var next_reply_at = result[1];
this.buffer = this.buffer.substring(next_reply_at);
var callback = this.callbacks.shift();
if (callback.promise) {
var result_value = result[0];
if (is_error)
callback.promise.emitError(result_value);
else {
result_value = post_process_results(callback.command, result_value);
callback.promise.emitSuccess(result_value);
}
}
}
};
function write_debug(data) {
if (!GLOBAL.DEBUG || !data) return;
sys.puts(data.replace(/\r\n/g, '<CRLF>'));
}
function try_convert_to_number(str) {
var value = parseInt(str, 10);
if (value === NaN)
value = parseFloat(str);
if (value === NaN)
return str;
return value;
}
function format_inline(name, args) {
var command = name;
for (var arg in args)
command += ' ' + args[arg].toString();
return command + crlf;
}
function format_bulk_command(name, args) {
var output = name;
for (var i = 0; i < args.length - 1; ++i)
output += ' ' + args[i].toString();
var last_arg = args[args.length - 1].toString();
return output + ' ' + last_arg.length + crlf + last_arg + crlf;
}
function make_command_sender(name) {
Client.prototype[name] = function () {
if (GLOBAL.DEBUG) {
var description = "client." + name + "( ";
for (var a in arguments)
description += "'" + arguments[a] + "',";
description = description.substr(0, description.length - 1) + " )";
}
var args = arguments;
var self = this;
var promise = new process.Promise();
this.connect(function () {
var command;
if (inline_commands[name])
command = format_inline(name, args);
else if (bulk_commands[name])
command = format_bulk_command(name, args);
else
throw new Error('unknown command type for "' + name + '"');
if (GLOBAL.DEBUG) {
write_debug("---");
write_debug("call: " + description);
write_debug("command:" + command);
}
self.callbacks.push({ promise:promise, command:name.toLowerCase() });
self.conn.send(command);
});
return promise;
};
}
for (var name in inline_commands)
make_command_sender(name);
for (var name in bulk_commands)
make_command_sender(name);
function post_process_results(command, result) {
var new_result = result;
switch (command) {
case 'info':
var info = {};
result.split(/\r\n/g).forEach(function (line) {
var parts = line.split(':');
if (parts.length === 2)
info[parts[0]] = try_convert_to_number(parts[1]);
});
new_result = info;
break;
case 'keys':
new_result = result.split(' ');
break;
case 'lastsave':
new_result = try_convert_to_number(result);
break;
default:
break;
}
return new_result;
}
// Read this: http://code.google.com/p/redis/wiki/SortCommand
// 'key' is what to sort, 'options' is how to sort.
// 'options' is an object with optional properties:
// 'by_pattern': 'pattern'
// 'limit': [start, end]
// 'get_patterns': [ 'pattern', 'pattern', ... ]
// 'ascending': true|false
// 'lexicographically': true|false
// 'store_key': 'a_key_name'
Client.prototype.sort = function (key, options) {
var promise = new process.Promise();
var self = this;
this.connect(function () {
var opts = [];
if (typeof(options) == 'object') {
if (options.by_pattern)
opts.push('by ' + options.by_pattern);
if (options.get_patterns) {
options.get_patterns.forEach(function (pat) {
opts.push('get ' + pat);
});
}
if (!options.ascending)
opts.push('desc');
if (options.lexicographically)
opts.push('alpha');
if (options.store_key)
opts.push('store ' + options.store_key);
if (options.limit)
opts.push('limit ' + options.limit[0] + ' ' + options.limit[1]);
}
var command = 'sort ' + key + ' ' + opts.join(' ') + crlf;
write_debug("call: client.sort(...)\ncommand: " + command);
self.callbacks.push({ promise:promise, command:'sort' });
self.conn.send(command);
});
return promise;
}
Client.prototype.quit = function () {
if (this.conn.readyState != "open") {
this.conn.close();
} else {
this.conn.send('quit' + crlf);
this.conn.close();
}
};
Client.prototype.make_master = function () {
var self = this;
this.connect(function () {
self.callbacks.push({ promise:null, command:'slaveof' });
self.conn.send('slaveof no one');
});
};
Client.prototype.make_slave_of = function (host, port) {
var self = this;
this.connect(function () {
port = port || 6379;
var command = 'slaveof ' + host + ' ' + port;
self.callbacks.push({ promise:null, command:'slaveof' });
self.conn.send(command);
});
};