Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add the possibility to parse negative integers for the types int, big…

…int and varint to CQL3
  • Loading branch information...
commit 1f71765748a46c7463d4f5dc9617cebb64b57483 1 parent 581aa17
@pieterbos pieterbos authored
Showing with 58 additions and 7 deletions.
  1. +57 −6 lib/marshal/deserializers.js
  2. +1 −1  lib/marshal/index.js
View
63 lib/marshal/deserializers.js
@@ -7,13 +7,19 @@ var UUID = require('../uuid').UUID,
*/
var Deserializers = {};
-Deserializers.decodeInteger = function(bits, val){
+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 +37,69 @@ 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');
+ 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);
+ }
};
/**
- * 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;
+ }
+ console.log("parsing 32 bits: " + new Buffer(val, 'binary').toString('hex'));
+ 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);
+ case 4:
+ return buf.readInt32BE(0);
+ 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);
+ }
+
+ }
+}
+
/**
* 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 },
Please sign in to comment.
Something went wrong with that request. Please try again.