├── .gitignore ├── .travis.yml ├── MIT-LICENSE.txt ├── README.md ├── package.json ├── rdb-parser.js └── tests ├── parseFile.js └── test.rdb /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | *~ 3 | \#*\# 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 0.6 4 | - 0.8 5 | -------------------------------------------------------------------------------- /MIT-LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Carlos Guerreiro, http://perceptiveconstructs.com 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | rdb-parser 2 | ---------- 3 | 4 | [node.js](http://nodejs.org/) asynchronous streaming parser for [redis](http://redis.io) RDB database dumps, written in 100% Javascript 5 | 6 | Installation 7 | ------------ 8 | 9 | `npm install rdb-parser` 10 | 11 | Usage 12 | ----- 13 | 14 | ```javascript 15 | var rdb = require('rdb-parser'); 16 | 17 | console.log(rdb.types); 18 | 19 | var parser = new rdb.Parser(); 20 | 21 | parser.on('entity', function(e) { 22 | console.log(e); 23 | }); 24 | 25 | parser.on('error', function(err) { 26 | throw err; 27 | }); 28 | 29 | parser.on('end', function() { 30 | console.log('done'); 31 | }); 32 | 33 | process.stdin.pipe(parser); 34 | process.stdin.resume(); 35 | ``` 36 | 37 | Status 38 | ------ 39 | 40 | `rdb-parser` aims for complete coverage of rdb dumps. 41 | 42 | It is close to complete at this point supporting all entity types, but may be missing one or two of the variety of encoding variations that redis can use. And it has had limited testing. 43 | 44 | Tests 45 | ----- 46 | 47 | `npm test` parses a (rather small) .rdb file designed to exercise a variety of types and encodings. 48 | 49 | How you can help 50 | ---------------- 51 | 52 | If you have a dump that `rdb-parser` cannot handle please report an issue and/or contribute the dump. 53 | 54 | License 55 | ------- 56 | 57 | (The MIT License) 58 | 59 | Copyright (c) 2011-2012 Carlos Guerreiro, [perceptiveconstructs.com](http://perceptiveconstructs.com) 60 | 61 | Copyright (c) 2012 Igalia S.L. 62 | 63 | Permission is hereby granted, free of charge, to any person obtaining 64 | a copy of this software and associated documentation files (the 65 | "Software"), to deal in the Software without restriction, including 66 | without limitation the rights to use, copy, modify, merge, publish, 67 | distribute, sublicense, and/or sell copies of the Software, and to 68 | permit persons to whom the Software is furnished to do so, subject to 69 | the following conditions: 70 | 71 | The above copyright notice and this permission notice shall be 72 | included in all copies or substantial portions of the Software. 73 | 74 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 75 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 76 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 77 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 78 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 79 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 80 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 81 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rdb-parser", 3 | "version": "0.1.5", 4 | "keywords": ["redis", "rdb", "db", "parser", "streaming"], 5 | "homepage": "http://perceptiveconstructs.com", 6 | "description": "async streaming parser for redis RDB dumps", 7 | "main": "rdb-parser.js", 8 | "repository": { 9 | "type": "git", 10 | "url": "https://github.com/pconstr/rdb-parser" 11 | }, 12 | "engines": { 13 | "node": ">=0.6.0 <0.9.0" 14 | }, 15 | "author": "Carlos Guerreiro buf.length) { 629 | end = buf.length; 630 | } else { 631 | completed = true; 632 | } 633 | fixedBytesBuffers.push(buf.slice(fixedBytesStart, end)); 634 | fixedBytesRem = fixedBytesRem - (end - fixedBytesStart); 635 | i = end; 636 | if(completed) { 637 | fixedBytesCB(null, fixedBytesBuffers); 638 | } else { 639 | fixedBytesStart = 0; 640 | } 641 | break; 642 | case 'encodedLen': 643 | c = buf[i++]; 644 | encodedLenType = c >> 6; 645 | extra = encodedLenTypeExtra[encodedLenType]; 646 | if(extra > 0) { 647 | // TODO: optimize for common case where all extra is available in buffer 648 | encodedLenRem = extra; 649 | encodedLen = extra === 4 ? 0 : c & 63; 650 | state = 'encodedLenExtra'; 651 | } else { 652 | encodedLenCB(null, c & 63, encodedLenType === REDIS_RDB_ENCVAL); 653 | } 654 | break; 655 | case 'encodedLenExtra': 656 | c = buf[i++]; 657 | encodedLen = (encodedLen << 8) + c; 658 | --encodedLenRem; 659 | if (encodedLenRem === 0) { 660 | encodedLenCB(null, encodedLen, false); 661 | } 662 | break; 663 | default: 664 | return encodedLenCB('unknown state: '+ state); 665 | } 666 | } 667 | 668 | this.writable = true; 669 | 670 | this.write = function(data) { 671 | buf = data; i = 0; 672 | while(state !== 'error' && i < buf.length) { 673 | parse(); 674 | } 675 | }; 676 | 677 | this.end = function() { 678 | if(state !== 'eof') { 679 | error('unexpected end'); 680 | } else { 681 | that.emit('end'); 682 | } 683 | }; 684 | } 685 | 686 | util.inherits(Parser, EventEmitter); 687 | 688 | exports.Parser = Parser; 689 | exports.types = { 690 | REDIS_STRING : REDIS_STRING, 691 | REDIS_LIST : REDIS_LIST, 692 | REDIS_SET : REDIS_SET, 693 | REDIS_ZSET : REDIS_ZSET, 694 | REDIS_HASH : REDIS_HASH 695 | }; 696 | -------------------------------------------------------------------------------- /tests/parseFile.js: -------------------------------------------------------------------------------- 1 | #!/usr/local/bin/node 2 | 3 | /* Copyright 2011-2012 Carlos Guerreiro 4 | * http://perceptiveconstructs.com 5 | * Licensed under the MIT license */ 6 | 7 | var fs = require('fs'); 8 | var buffer = require('buffer'); 9 | var assert = require('assert'); 10 | var rdb = require('../rdb-parser.js'); 11 | 12 | var expected = [ 13 | [0, 'k1', 'ssssssss'], 14 | [0, 'k3', 'wwwwwwww'], 15 | [0, 's1', '.ahaa bit longer and with spaceslonger than 256 characters and trivially compressible --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'], 16 | [0, 's2', 'now_exists'], 17 | [0, 'n5b', '1000'], 18 | [1, 'l10', ['100001', '100002', '100003', '100004']], 19 | [1, 'l11', ['9999999999', '9999999998', '9999999997']], 20 | [1, 'l12', ['9999999997', '9999999998', '9999999999']], 21 | [0, 'b1', new buffer.Buffer([255])], 22 | [0, 'b2', new buffer.Buffer([0, 255])], 23 | [0, 'b3', new buffer.Buffer([0, 0, 255])], 24 | [0, 'b4', new buffer.Buffer([0, 0, 0, 255])], 25 | [0, 'b5', new buffer.Buffer([0, 0, 0, 0, 255])], 26 | [4, 'h1', ['c', 'now this is quite a bit longer, but sort of boring....................................................................................................................................................................................................................................................................................................................................................................','a','aha','b','a bit longer, but not very much']], 27 | [4, 'h2', ['a', '101010']], 28 | [4, 'h3', ['b', 'b2', 'c', 'c2', 'd', 'd']], 29 | [1, 'l1', ['yup', 'aha']], 30 | [2, 'set1', ['c','d','a','b']], 31 | [1, 'l2', ['something','now a bit longer and perhaps more interesting']], 32 | [2, 'set2', ['d','a']], 33 | [0, 'n1', '-6'], 34 | [1, 'l3', ['this one is going to be longer -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------', 'a bit more']], 35 | [2, 'set3', ['b']], 36 | [2, 'set4', ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']], 37 | [0, 'n2', '501'], 38 | [1, 'l4', ['b', 'c', 'd']], 39 | [2, 'set5', ['100000', '100001', '100002', '100003']], 40 | [0, 'n3', '500001'], 41 | [1, 'l5', ['c', 'a']], 42 | [2, 'set6', ['9999999997', '9999999998', '9999999999']], 43 | [0, 'n4', '1'], 44 | [1, 'l6', ['b']], 45 | [0, 'n5', '1000'], 46 | [1, 'l7', ['a', 'b']], 47 | [0, 'n6', '1000000'], 48 | [0, 'n4b', '1'], 49 | [1, 'l8', ['c', '1', '2', '3', '4']], 50 | [1, 'l9', ['10001', '10002', '10003', '10004']], 51 | [0, 'n6b', '1000000'], 52 | [3, 'z1', ['a', '1', 'c', '13']], 53 | [3, 'z2', ['1', '1', '2', '2', '3', '3']], 54 | [3, 'z3', ['10002', '10001', '10003', '10003']], 55 | [3, 'z4', ['10000000001', '10000000001', '10000000002', '10000000002', '10000000003', '10000000003']], 56 | ]; 57 | 58 | var parser = new rdb.Parser(); 59 | 60 | function check(got, expected) { 61 | if(typeof got !== 'object') { 62 | assert.strictEqual(typeof got, typeof expected); 63 | assert.strictEqual(got, expected); 64 | return; 65 | } 66 | 67 | var i; 68 | 69 | if(got.constructor === Array) { 70 | assert.strictEqual(expected.constructor, Array); 71 | assert.strictEqual(got.length, expected.length); 72 | for(i = 0; i < got.length; ++i) { 73 | check(got[i], expected[i]); 74 | } 75 | return; 76 | } 77 | 78 | if(typeof expected === 'string') { 79 | assert.strictEqual(got.toString(), expected); 80 | return; 81 | } 82 | 83 | if(typeof expected === 'object' && expected.constructor === buffer.Buffer) { 84 | assert.strictEqual(typeof got, 'object'); 85 | assert.strictEqual(got.constructor, buffer.Buffer); 86 | assert.strictEqual(got.length, expected.length); 87 | for(i = 0; i < got.length; ++i) 88 | assert.strictEqual(got[i], expected[i]); 89 | return; 90 | } 91 | 92 | assert.strictEqual(got, expected); 93 | } 94 | 95 | var expectedIndex = 0; 96 | parser.on('entity', function(o) { 97 | assert(expectedIndex < expected.length, 'more entities than expected: '+ o); 98 | check(o, expected[expectedIndex++]); 99 | }); 100 | 101 | parser.on('error', function(err) { 102 | console.error('error', err); 103 | throw err; 104 | }); 105 | 106 | parser.on('end', function() { 107 | assert.strictEqual(expectedIndex, expected.length, 'less entities than expected: '+ expected[expectedIndex]); 108 | }); 109 | 110 | var s = fs.createReadStream('./tests/test.rdb'); 111 | s.pipe(parser); 112 | -------------------------------------------------------------------------------- /tests/test.rdb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/pconstr/rdb-parser/251b63d8ba4e73459bd1bc11979bc1d73e138d9d/tests/test.rdb --------------------------------------------------------------------------------