Permalink
Browse files

Add decoding for varInts with odd sizes

Support for sizes other than 1, 2, 4 or 8 bytes. 
Includes test with 3 byte variant.
  • Loading branch information...
1 parent 1f71765 commit cc6c5e769e7a96a0fbb02def56c26aba9b2ea0e4 @pieterbos pieterbos committed Oct 16, 2012
Showing with 26 additions and 17 deletions.
  1. +15 −17 lib/marshal/deserializers.js
  2. +9 −0 test/cql3.js
  3. +2 −0 test/helpers/cql3.json
View
32 lib/marshal/deserializers.js
@@ -7,6 +7,16 @@ var UUID = require('../uuid').UUID,
*/
var Deserializers = {};
+var decodeLongFromBuffer = function(buf) {
+ var negative = buf.readInt8(0) < 0;
+ if(negative) {
+ //TODO: replace this with a better negative reading function that does up to 53 bytes
+ return buf.readInt32BE(4);
+ } else {
+ return parseInt(buf.toString('hex'), 16);
+ }
+}
+
Deserializers.decodeInteger = function(bits, val, signed){
if(signed === null || signed === undefined){
signed = false;
@@ -48,14 +58,7 @@ Deserializers.decodeLong = function(val){
}
var buf = new Buffer(val, 'binary');
- console.log("parsing 64 bits: " + buf.toString('hex'));
- var negative = buf.readInt8(0) < 0;
- if(negative) {
- //TODO: replace this with a better negative reading function that does up to 53 bytes
- return buf.readInt32BE(4);
- } else {
- return parseInt(buf.toString('hex'), 16);
- }
+ return decodeLongFromBuffer(buf);
};
/**
@@ -67,7 +70,6 @@ Deserializers.decodeInt32 = function(val){
if(val === null || val === undefined){
return null;
}
- console.log("parsing 32 bits: " + new Buffer(val, 'binary').toString('hex'));
return new Buffer(val, 'binary').readInt32BE(0, true);
};
@@ -86,17 +88,13 @@ Deserializers.decodeVarInt = function(val){
return buf.readInt8(0);
case 2:
return buf.readInt16BE(0);
+ //3 is handled by default
case 4:
return buf.readInt32BE(0);
+ case 8:
+ return decodeLongFromBuffer(buf);
default:
- var negative = buf.readInt8(0) < 0;
- if(negative) {
- //TODO: replace this with a better negative reading function that does up to 53 bytes
- return buf.readInt32BE(4);
- } else {
- return parseInt(buf.toString('hex'), 16);
- }
-
+ return Deserializers.decodeInteger(buf.length*8, val, true);
}
}
View
9 test/cql3.js
@@ -225,6 +225,7 @@ module.exports = {
'test cql integers CF create column family':testResultless(config['integers_create_cf#cql']),
'test cql integers CF update 1':testResultless(config['integers_update#cql'], config['integers_update#vals1']),
'test cql integers CF update 2':testResultless(config['integers_update#cql'], config['integers_update#vals2']),
+ 'test cql integers CF update 3':testResultless(config['integers_update#cql'], config['integers_update#vals3']),
'test cql integers CF select positive numbers':testCql(config['integers_select1#cql'], function(test, assert, err, res){
assert.strictEqual(res.length, 1);
assert.ok(res[0] instanceof Helenus.Row);
@@ -241,6 +242,14 @@ module.exports = {
assert.strictEqual(res[0].get('longnumber').value, -25);
assert.strictEqual(res[0].get('varnumber').value, -36);
}),
+ 'test cql integers CF select negative numbers with 3 byte varint':testCql(config['integers_select3#cql'], function(test, assert, err, res){
+ assert.strictEqual(res.length, 1);
+ assert.ok(res[0] instanceof Helenus.Row);
+ assert.strictEqual(res[0].length, 3);
+ assert.strictEqual(res[0].get('number').value, -2);
+ assert.strictEqual(res[0].get('longnumber').value, -25);
+ assert.strictEqual(res[0].get('varnumber').value, -8388607);//test a 3 byte-long variable integer
+ }),
'test cql drop keyspace':testResultless(config['drop_ks#cql']),
View
2 test/helpers/cql3.json
@@ -45,8 +45,10 @@
"integers_update#cql" : "UPDATE integers set longnumber = ?, varnumber = ? where number = ?",
"integers_update#vals1" : [25, 36, 1],
"integers_update#vals2" : [-25, -36, -1],
+ "integers_update#vals3" : [-25, -8388607, -2],
"integers_select1#cql" : "SELECT * FROM integers where number = 1",
"integers_select2#cql" : "SELECT * FROM integers where number = -1",
+ "integers_select3#cql" : "SELECT * FROM integers where number = -2",
"prepare#cql" : "SELECT * FROM ? WHERE KEY = ?",

0 comments on commit cc6c5e7

Please sign in to comment.