Revision: 8e2e7d4f5823
Author:   gdusbabek <[email protected]>
Date:     Mon Jan 30 10:16:45 2012
Log:      Issue 11: allow inserting blobs directly.
(http://code.google.com/a/apache-extras.org/p/cassandra-node/issues/detail?id=28)
Patch by Christoph Tavan <[email protected]>

* buffers get converted to hex strings on the fly.

http://code.google.com/a/apache-extras.org/p/cassandra-node/source/detail?r=8e2e7d4f5823

Modified:
 /lib/decoder.js
 /lib/driver.js
 /test/test_decoder.js
 /test/test_driver.js

=======================================
--- /lib/decoder.js     Tue Jan 17 09:03:33 2012
+++ /lib/decoder.js     Mon Jan 30 10:16:45 2012
@@ -65,24 +65,6 @@
   return bytesToBigInt(bytes);
 };

-// just what you think.
-function byteToHex(n) {
-  if (n < 16) {
-    return '0' + n.toString(16);
-  } else {
-    return n.toString(16);
-  }
-}
-
-/** convert a buffer to a hex string suitable for use in cql statements. */
-var bufferToString = module.exports.bufferToString = function(buf) {
-  var chars = [];
-  for (var i = 0; i < buf.length; i++) {
-    chars[i] = byteToHex(buf[i]);
-  }
-  return chars.join('');
-};
-
 // Cassandra datatypes according to
 // http://www.datastax.com/docs/1.0/ddl/column_family
 // Those commented out are not correctly dealt with yet and will appear as
=======================================
--- /lib/driver.js      Mon Jan 23 11:22:55 2012
+++ /lib/driver.js      Mon Jan 30 10:16:45 2012
@@ -23,7 +23,6 @@

 var util = require('util');
 var constants = require('constants');
-var Buffer = require('buffer').Buffer;
 var EventEmitter = require('events').EventEmitter;

 var thrift = require('thrift');
@@ -59,11 +58,14 @@

 /** converts object to a string using toString() method if it exists. */
 function stringify(x) {
+  // node buffers should be hex encoded
+  if (x instanceof Buffer) {
+    return x.toString('hex');
+  }
   if (x.toString) {
     return x.toString();
-  } else {
-    return x;
-  }
+  }
+  return x;
 }

 /** wraps in quotes */
=======================================
--- /test/test_decoder.js       Wed Jan 11 08:05:25 2012
+++ /test/test_decoder.js       Mon Jan 30 10:16:45 2012
@@ -252,6 +252,6 @@
   buf[3] = 0x99;
   buf[4] = 0xcc;
   buf[5] = 0xff;
-  assert.strictEqual('00336699ccff', bufferToString(buf));
+  assert.strictEqual('00336699ccff', buf.toString('hex'));
   test.finish();
 };
=======================================
--- /test/test_driver.js        Mon Jan 16 08:31:48 2012
+++ /test/test_driver.js        Mon Jan 30 10:16:45 2012
@@ -438,14 +438,13 @@
     assert.ifError(err);
     var key = 'binarytest';
var binaryParams = [util.randomBuffer(), util.randomBuffer(), util.randomBuffer()];
-    var stringParams = binaryParams.map(decoder.bufferToString);
- con.execute('update CfBytes set ?=? where key=?', stringParams, function(updErr) { + con.execute('update CfBytes set ?=? where key=?', binaryParams, function(updErr) {
       if (updErr) {
         con.close();
         assert.ok(false);
         test.finish();
       } else {
- con.execute('select ? from CfBytes where key=?', [stringParams[0], stringParams[2]], function(selErr, rows) { + con.execute('select ? from CfBytes where key=?', [binaryParams[0], binaryParams[2]], function(selErr, rows) {
           con.close();
           assert.strictEqual(rows.rowCount(), 1);
           var row = rows[0];
@@ -457,7 +456,7 @@
         });
       }
     });
-  });
+  });
 };

 exports.testLong = function(test, assert) {

Reply via email to