Permalink
Browse files

Merge remote-tracking branch 'nedap/master'

  • Loading branch information...
2 parents 0609ec4 + cc6c5e7 commit 35a8303173f98fe8c58ea39c7044d3f980a93e79 @devdazed devdazed committed Oct 26, 2012
Showing with 95 additions and 7 deletions.
  1. +55 −6 lib/marshal/deserializers.js
  2. +1 −1 lib/marshal/index.js
  3. +29 −0 test/cql3.js
  4. +10 −0 test/helpers/cql3.json
View
61 lib/marshal/deserializers.js
@@ -7,13 +7,29 @@ var UUID = require('../uuid').UUID,
*/
var Deserializers = {};
-Deserializers.decodeInteger = function(bits, val){
+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;
+ }
if(val === null || val === undefined){
return null;
}
+ //this approach does not work above 32 bits (or perhaps 53)
var hex = new Buffer(val, 'binary').toString('hex');
- return parseInt(hex, 16);
+ var x = parseInt(hex, 16);
+ var max = Math.pow( 2, bits );
+ return signed && x >= max / 2 ? x - max : x;
};
/**
@@ -31,24 +47,57 @@ Deserializers.decodeBinary = function(val){
};
/**
- * Decodes a Long (UInt64)
+ * Decodes a Long (Int64)
* @static
* @param {String} val The binary string to decode
* @returns {Number} The number value decoded from the binary string
*/
Deserializers.decodeLong = function(val){
- return Deserializers.decodeInteger(64, val);
+ if(val === null || val === undefined){
+ return null;
+ }
+
+ var buf = new Buffer(val, 'binary');
+ return decodeLongFromBuffer(buf);
};
/**
- * Decodes a 32bit Unsinged Integer
+ * Decodes a 32bit signed Integer
* @param {String} val The binary string to decode
* @returns {Number} The number value decoded from the binary string
*/
Deserializers.decodeInt32 = function(val){
- return Deserializers.decodeInteger(32, val);
+ if(val === null || val === undefined){
+ return null;
+ }
+ return new Buffer(val, 'binary').readInt32BE(0, true);
};
+/**
+ * Decodes a variable length signed integer
+ * @param {String} val The binary string to decode
+ * @returns {Number} The number value decoded from the binary string
+ */
+Deserializers.decodeVarInt = function(val){
+ if(val === null || val === undefined){
+ return null;
+ }
+ var buf = new Buffer(val, 'binary')
+ switch(buf.length) {
+ case 1:
+ 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:
+ return Deserializers.decodeInteger(buf.length*8, val, true);
+ }
+}
+
/**
* Decodes for UTF8
* @param {String} val The binary string to decode
View
2 lib/marshal/index.js
@@ -24,7 +24,7 @@ var IDENTITY = function (val) { return val; };
var TYPES = {
'BytesType': { ser:Serializers.encodeBinary, de:Deserializers.decodeBinary },
'LongType': { ser:Serializers.encodeLong, de:Deserializers.decodeLong },
- 'IntegerType': { ser:Serializers.encodeInt32, de:Deserializers.decodeInt32 },
+ 'IntegerType': { ser:Serializers.encodeInt32, de:Deserializers.decodeVarInt },
'Int32Type': { ser:Serializers.encodeInt32, de:Deserializers.decodeInt32 },
'UTF8Type': { ser:Serializers.encodeUTF8, de:Deserializers.decodeUTF8 },
'AsciiType': { ser:Serializers.encodeAscii, de:Deserializers.decodeAscii },
View
29 test/cql3.js
@@ -221,6 +221,35 @@ module.exports = {
assert.strictEqual(res[0].get('body').value, 'body text 1');
assert.strictEqual(res[0].get('posted_by').value, 'author1');
}),
+
+ '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);
+ assert.strictEqual(res[0].length, 3);
+ assert.strictEqual(res[0].get('number').value, 1);
+ assert.strictEqual(res[0].get('longnumber').value, 25);
+ assert.strictEqual(res[0].get('varnumber').value, 36);
+ }),
+ 'test cql integers CF select negative numbers':testCql(config['integers_select2#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, -1);
+ 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
10 test/helpers/cql3.json
@@ -40,6 +40,16 @@
"sparse_update#vals3" : ["body text 3", "author3", 10, "2012-03-02 00:00:00+0000"],
"sparse_select1#cql" : "SELECT posted_at, body, posted_by FROM timeline WHERE userid = 10 AND posted_at > '2012-03-01 00:00:00+0000'",
"sparse_select2#cql" : "SELECT body, posted_by FROM timeline WHERE userid = 10 AND posted_at = '2012-03-01 00:00:00+0000'",
+
+ "integers_create_cf#cql" : "CREATE TABLE integers (number int, longnumber bigint, varnumber varint, PRIMARY KEY (number))",
+ "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 = ?",
"error#cql" : "SOME INVALID CQL"

0 comments on commit 35a8303

Please sign in to comment.