-
Notifications
You must be signed in to change notification settings - Fork 10
/
rdb-parser.js
executable file
·695 lines (628 loc) · 15.4 KB
/
rdb-parser.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*jslint white: true, plusplus: true, vars: true, bitwise: true*/
/* Copyright 2011-2012 Carlos Guerreiro
* http://perceptiveconstructs.com
* Copyright 2012 Igalia S.L.
* Licensed under the MIT license */
"use strict";
var EventEmitter = require('events').EventEmitter;
require('bufferjs');
var lzf = require('lzf');
var util = require('util');
var Int64 = require('node-int64');
var REDIS_RDB_6BITLEN = 0;
var REDIS_RDB_14BITLEN = 1;
var REDIS_RDB_32BITLEN = 2;
var REDIS_RDB_ENCVAL = 3;
var encodedLenTypeExtra = [];
encodedLenTypeExtra[REDIS_RDB_6BITLEN] = 0;
encodedLenTypeExtra[REDIS_RDB_14BITLEN] = 1;
encodedLenTypeExtra[REDIS_RDB_32BITLEN] = 4;
encodedLenTypeExtra[REDIS_RDB_ENCVAL] = 0;
var REDIS_RDB_ENC_INT8 = 0;
var REDIS_RDB_ENC_INT16 = 1;
var REDIS_RDB_ENC_INT32 = 2;
var REDIS_RDB_ENC_LZF = 3;
var REDIS_STRING = 0;
var REDIS_LIST = 1;
var REDIS_SET = 2;
var REDIS_ZSET = 3;
var REDIS_HASH = 4;
var REDIS_HASH_ZIPMAP = 9;
var REDIS_LIST_ZIPLIST = 10;
var REDIS_SET_INTSET = 11;
var REDIS_ZSET_ZIPLIST = 12;
var REDIS_SELECTDB = 254;
var REDIS_EOF = 255;
var INTSET_ENC_INT16 = 2;
var INTSET_ENC_INT32 = 4;
var INTSET_ENC_INT64 = 8;
var expectedStart = new Buffer('REDIS0002', 'ascii');
function readInt64(b, i) {
var l, h, i64;
l = b.readUInt32LE(i, i + 4);
i += 4;
h = b.readUInt32LE(i, i + 4);
i += 4;
i64 = new Int64(h, l);
return new Buffer(i64.toString());
}
function buffArrayLength(bA) {
var i, totalLength;
totalLength = 0;
for (i = 0; i < bA.length; i++) {
totalLength += bA[i].length;
}
return totalLength;
}
function buffEquals(a, b) {
if(a.length !== b.length) {
return false;
}
var i;
for(i=0; i<a.length; i++) {
if(a[i] !== b[i]) {
return false;
}
}
return true;
}
function buffArrayEquals(bA, b) {
var bI, i, subB, l;
l = buffArrayLength(bA);
if (l !== b.length) {
return false;
}
bI = 0;
for(i = 0; i < bA.length; i++) {
subB = b.slice(bI, bI + bA[i].length);
if(!buffEquals(bA[i], subB)) {
return false;
}
bI = bI + bA[i].length;
}
return true;
}
function Parser() {
var that, state, buf, i;
var fixedBytesRem, fixedBytesStart, fixedBytesCB, fixedBytesBuffers;
var encodedLenCB;
var encodedLenType;
var encodedLenRem;
var encodedLen;
var type;
var key;
that = this;
state = 'start';
function getFixedBytes(len, cb) {
fixedBytesRem = len;
fixedBytesStart = (i === buf.length ? 0: i);
fixedBytesBuffers = [];
fixedBytesCB = cb;
state = 'fixedBytes';
}
function getEncodedLen(cb) {
encodedLenCB = cb;
state = 'encodedLen';
}
// TODO: write a variant that can be pipelined
// this will require changing lzf decompression to work incrementally
function getString(cb) {
getEncodedLen(function(err, encLen, isEncoded) {
if(err) {
return cb(err);
}
if (isEncoded) {
switch(encLen) {
case REDIS_RDB_ENC_INT8:
getFixedBytes(1, function(err, buffers) {
if(err) {
cb(err);
} else {
cb(null, new Buffer(String(Buffer.concat(buffers).readInt8(0))));
}
});
break;
case REDIS_RDB_ENC_INT16:
getFixedBytes(2, function(err, buffers) {
if(err) {
cb(err);
} else {
cb(null, new Buffer(String(Buffer.concat(buffers).readInt16LE(0))));
}
});
break;
case REDIS_RDB_ENC_INT32:
getFixedBytes(4, function(err, buffers) {
if(err) {
cb(err);
} else {
cb(null, new Buffer(String(Buffer.concat(buffers).readInt32LE(0))));
}
});
break;
case REDIS_RDB_ENC_LZF:
getEncodedLen(function(err, encLen) {
if(err) {
return cb(err);
}
getEncodedLen(function(err, len) {
if(err) {
return cb(err);
}
getFixedBytes(encLen, function(err, buffers) {
if(err) {
return cb(err);
}
var compressed = Buffer.concat(buffers);
var decompressed = lzf.decompress(compressed);
cb(null, decompressed);
});
});
});
break;
default:
cb('unknown encoding');
}
} else {
// FIXME can getFixedBytes be called inside a callback?
getFixedBytes(encLen, function(err, buffers) {
if(err) {
cb(err);
} else {
cb(null, Buffer.concat(buffers));
}
});
}
});
}
function getDouble(cb) {
getFixedBytes(1, function(err, buffers) {
var l = Buffer.concat(buffers)[0];
if(l === 253) {
return cb(null, 'NaN');
}
if(l === 254) {
return cb(null, '+inf');
}
if(l === 255) {
return cb(null, '-inf');
}
getFixedBytes(l, function(err, buffers) {
if(err) {
return cb(err);
}
var v = Buffer.concat(buffers).toString();
return cb(null, v);
});
});
}
function getZipMap(cb) {
// TODO: pipeline getString into an incremental ziplist parser
getString(function(err, s) {
if(err) {
return cb(err);
}
var map = [];
var i = 0;
var cnt = 0;
var free;
i ++; // skip zmlen
while(true) {
var len = s[i++];
if(len === 255) {
break;
}
if(len === 254) {
len = null; // free space only
} else if(len === 253) {
len = s.readUInt32LE(i);
i = i + 4;
}
if((cnt & 1) === 1) {
free = s[i++];
} else {
free = 0;
}
if(len !== null) {
var v = s.slice(i, i+ len);
map.push(v);
i += (len + free);
}
cnt++;
}
cb(null, map);
});
}
function getIntSet(cb) {
getString(function(err, s) {
if(err) {
return cb(err);
}
var is = [];
function outNumber(n) {
is.push(new Buffer(String(n)));
}
var i = 0;
var encoding = s.readUInt32LE(i);
i += 4;
var length = s.readUInt32LE(i);
i += 4;
var k;
for(k = 0; k < length; ++k) {
switch(encoding) {
case INTSET_ENC_INT16:
outNumber(s.readInt16LE(i));
i += 2;
break;
case INTSET_ENC_INT32:
outNumber(s.readInt32LE(i));
i += 4;
break;
case INTSET_ENC_INT64:
is.push(readInt64(s, i));
i += 8;
break;
default:
return cb('unsupported intset encoding');
}
}
cb(null, is);
});
}
function getZSet(cb) {
// dict size
// entry key (string)
// entry score (double)
getEncodedLen(function(err, zLen, isEncoded) {
if(err) {
return cb(err);
}
var set = [];
var rem = zLen;
function getItem() {
if(rem === 0) {
return cb(null, set);
}
getString(function(err, key) {
if(err) {
return cb(err);
}
getDouble(function(err, score) {
if(err) {
return cb(err);
}
set.push(key);
set.push(score);
--rem;
getItem();
});
});
}
getItem();
});
}
function getZipList(cb) {
// TODO: pipeline getString into an incremental ziplist parser
getString(function(err, s) {
if(err) {
return cb(err);
}
var list = [];
var i = 0;
function stringEntry(len) {
var se = s.slice(i, i+ len);
list.push(se);
i += len;
}
function numberEntry(n) {
list.push(new Buffer(String(n)));
}
var zlbytes = s.readUInt32LE(i);
i += 4; // skip zlbytes
i += 4; // skip zltail
i += 2; // skip zllen
var b, len;
while(true) {
if (s[i] === 255) {
break;
}
i += s[i] === 254 ? 4 : 1; // skip prev len
b = s[i];
switch(b & 192) {
case 0: // string 1 byte len
i += 1;
stringEntry(b & 63);
break;
case 64: // string 2 bytes
i += 1;
len = ((b & 63) << 8) + s[i];
i += 1;
stringEntry(len);
break;
case 128: // string 5 bytes
i += 1;
len = s.readInt32LE(i, i + 4);
i += 4;
stringEntry(len);
break;
case 192: // integer
++i;
switch(b & 48) {
case 0: // int16
numberEntry(s.readInt16LE(i, i + 2));
i += 2;
break;
case 16: // int32
numberEntry(s.readInt32LE(i, i + 4));
i += 4;
break;
case 32: // int64
list.push(readInt64(s, i));
i += 8;
break;
case 48: // unsupported
return cb('undefined integer encoding');
default:
return cb('unknown encoding');
}
break;
}
}
cb(null, list);
});
}
function getHash(cb) {
getEncodedLen(function(err, encLen, isEncoded) {
if(err) {
return cb(err);
}
var remCount = encLen * 2;
var hash = [];
function getRemaining() {
if (remCount === 0) {
return cb(null, hash);
}
getString(function(err, s) {
if(err) {
return cb(err);
}
hash.push(s);
--remCount;
getRemaining();
});
}
getRemaining();
});
}
function getList(cb) {
getEncodedLen(function(err, encLen, isEncoded) {
if(err) {
return cb(err);
}
var remCount = encLen;
var list = [];
// TODO: too much recursion?
function getRemaining() {
if (remCount === 0) {
return cb(null, list);
}
getString(function(err, s) {
if(err) {
return cb(err);
}
list.push(s);
--remCount;
getRemaining();
});
}
getRemaining();
});
}
function error(err) {
that.emit('error', err);
state = 'error';
}
function parse() {
var c, end, completed, extra;
switch(state) {
case 'error':
break;
case 'eof':
return error('data past eof');
case 'start':
getFixedBytes(9, function(err, buffers) {
if(err) {
return error(err);
}
if (!buffArrayEquals(buffers, expectedStart)) {
return error('unsupported rdb format');
}
state = 'type';
});
break;
case 'type':
type = buf[i++];
if(type === REDIS_SELECTDB) {
state = 'dbId';
} else if(type === REDIS_EOF) {
state = 'eof';
} else {
state = 'key';
}
break;
case 'dbId':
getEncodedLen(function(err, dbId) {
if(err) {
return error(err);
}
state = 'type';
});
break;
case 'key':
getString(function(err, s) {
if(err) {
return error(err);
}
key = s;
state = 'value';
});
break;
case 'value':
switch(type) {
case REDIS_STRING:
getString(function(err, s) {
if(err) {
return error(err);
}
that.emit('entity', [type, key, s]);
state = 'type';
});
break;
case REDIS_LIST_ZIPLIST:
// TODO: emit incrementally
getZipList(function(err, l) {
if(err) {
return error(err);
}
that.emit('entity', [REDIS_LIST, key, l]);
state = 'type';
});
break;
case REDIS_LIST:
// TODO: emit incrementally
getList(function(err, l) {
if(err) {
return error(err);
}
that.emit('entity', [REDIS_LIST, key, l]);
state = 'type';
});
break;
case REDIS_SET:
// TODO: emit incrementally
getList(function(err, s) { // encoded identially to a list
if(err) {
return error(err);
}
that.emit('entity', [REDIS_SET, key, s]);
state = 'type';
});
break;
case REDIS_SET_INTSET:
getIntSet(function(err, s) {
if(err) {
return error(err);
}
that.emit('entity', [REDIS_SET, key, s]);
state = 'type';
});
break;
case REDIS_ZSET_ZIPLIST:
// TODO: emit incrementally
getZipList(function(err, zs) {
if(err) {
return error(err);
}
that.emit('entity', [REDIS_ZSET, key, zs]);
state = 'type';
});
break;
case REDIS_ZSET:
getZSet(function(err, zs) {
if(err) {
return error(err);
}
that.emit('entity', [REDIS_ZSET, key, zs]);
state = 'type';
});
break;
case REDIS_HASH:
getHash(function(err, h) {
if(err) {
return error(err);
}
that.emit('entity', [REDIS_HASH, key, h]);
state = 'type';
});
break;
case REDIS_HASH_ZIPMAP:
// TODO: emit incrementally
getZipMap(function(err, h) {
if(err) {
return error(err);
}
that.emit('entity', [REDIS_HASH, key, h]);
state = 'type';
});
break;
default:
console.error(type, key);
return error('unknown type');
}
break;
case 'fixedBytes':
end = fixedBytesStart + fixedBytesRem;
completed = false;
if(end > buf.length) {
end = buf.length;
} else {
completed = true;
}
fixedBytesBuffers.push(buf.slice(fixedBytesStart, end));
fixedBytesRem = fixedBytesRem - (end - fixedBytesStart);
i = end;
if(completed) {
fixedBytesCB(null, fixedBytesBuffers);
} else {
fixedBytesStart = 0;
}
break;
case 'encodedLen':
c = buf[i++];
encodedLenType = c >> 6;
extra = encodedLenTypeExtra[encodedLenType];
if(extra > 0) {
// TODO: optimize for common case where all extra is available in buffer
encodedLenRem = extra;
encodedLen = extra === 4 ? 0 : c & 63;
state = 'encodedLenExtra';
} else {
encodedLenCB(null, c & 63, encodedLenType === REDIS_RDB_ENCVAL);
}
break;
case 'encodedLenExtra':
c = buf[i++];
encodedLen = (encodedLen << 8) + c;
--encodedLenRem;
if (encodedLenRem === 0) {
encodedLenCB(null, encodedLen, false);
}
break;
default:
return encodedLenCB('unknown state: '+ state);
}
}
this.writable = true;
this.write = function(data) {
buf = data; i = 0;
while(state !== 'error' && i < buf.length) {
parse();
}
};
this.end = function() {
if(state !== 'eof') {
error('unexpected end');
} else {
that.emit('end');
}
};
}
util.inherits(Parser, EventEmitter);
exports.Parser = Parser;
exports.types = {
REDIS_STRING : REDIS_STRING,
REDIS_LIST : REDIS_LIST,
REDIS_SET : REDIS_SET,
REDIS_ZSET : REDIS_ZSET,
REDIS_HASH : REDIS_HASH
};