From cb71873f8d790021858dd0425e04de6ee816a265 Mon Sep 17 00:00:00 2001 From: Dave Gramlich Date: Tue, 6 Sep 2016 09:35:24 -0400 Subject: [PATCH] bigtable: return Buffer instead of base64 when decode is true (#1556) --- packages/bigtable/src/mutation.js | 8 ++++++-- packages/bigtable/src/row.js | 8 +------- packages/bigtable/system-test/bigtable.js | 14 +++++--------- packages/bigtable/test/mutation.js | 11 +++++++++++ packages/bigtable/test/row.js | 15 +++++++++++---- 5 files changed, 34 insertions(+), 22 deletions(-) diff --git a/packages/bigtable/src/mutation.js b/packages/bigtable/src/mutation.js index a829dc4eff6..67a07f51db8 100644 --- a/packages/bigtable/src/mutation.js +++ b/packages/bigtable/src/mutation.js @@ -60,9 +60,9 @@ var methods = Mutation.methods = { * Parses "bytes" returned from proto service. * * @param {string} bytes - Base64 encoded string. - * @return {string} + * @return {string|number|buffer} */ -Mutation.convertFromBytes = function(bytes) { +Mutation.convertFromBytes = function(bytes, options) { var buf = new Buffer(bytes, 'base64'); var num = new Int64(buf).toNumber(); @@ -70,6 +70,10 @@ Mutation.convertFromBytes = function(bytes) { return num; } + if (options && options.decode === false) { + return buf; + } + return buf.toString(); }; diff --git a/packages/bigtable/src/row.js b/packages/bigtable/src/row.js index 64dad9c3ce4..5cca77cfef7 100644 --- a/packages/bigtable/src/row.js +++ b/packages/bigtable/src/row.js @@ -157,14 +157,8 @@ Row.formatChunks_ = function(chunks, options) { } if (qualifier && chunk.value) { - var value = chunk.value; - - if (options.decode !== false) { - value = Mutation.convertFromBytes(value); - } - qualifier.push({ - value: value, + value: Mutation.convertFromBytes(chunk.value, options), labels: chunk.labels, timestamp: chunk.timestampMicros, size: chunk.valueSize diff --git a/packages/bigtable/system-test/bigtable.js b/packages/bigtable/system-test/bigtable.js index 8236ac58224..6e5a9416dce 100644 --- a/packages/bigtable/system-test/bigtable.js +++ b/packages/bigtable/system-test/bigtable.js @@ -573,7 +573,7 @@ describe('Bigtable', function() { }); it('should not decode the values', function(done) { - var row = TABLE.row('alincoln'); + var row = TABLE.row('gwashington'); var options = { decode: false }; @@ -581,15 +581,11 @@ describe('Bigtable', function() { row.get(options, function(err) { assert.ifError(err); - var presidents = Object.keys(row.data.follows); - - assert(presidents.length > 0); + var teeth = row.data.traits.teeth; + var value = teeth[0].value; - presidents.forEach(function(prez) { - var follower = row.data.follows[prez]; - - assert.strictEqual(follower[0].value, 'AAAAAAAAAAE='); - }); + assert(value instanceof Buffer); + assert.strictEqual(value.toString(), 'shiny-wood'); done(); }); diff --git a/packages/bigtable/test/mutation.js b/packages/bigtable/test/mutation.js index eff4369192b..9ab465becff 100644 --- a/packages/bigtable/test/mutation.js +++ b/packages/bigtable/test/mutation.js @@ -59,6 +59,17 @@ describe('Bigtable/Mutation', function() { assert.strictEqual(message, decoded); }); + + it('should return a buffer if decode is set to false', function() { + var message = 'Hello!'; + var encoded = new Buffer(message).toString('base64'); + var decoded = Mutation.convertFromBytes(encoded, { + decode: false + }); + + assert(decoded instanceof Buffer); + assert.strictEqual(decoded.toString(), message); + }); }); describe('convertToBytes', function() { diff --git a/packages/bigtable/test/row.js b/packages/bigtable/test/row.js index 0c4e05c3d80..7fb98e8cebf 100644 --- a/packages/bigtable/test/row.js +++ b/packages/bigtable/test/row.js @@ -299,21 +299,22 @@ describe('Bigtable/Row', function() { commitRow: true }]; - var rows = Row.formatChunks_(chunks, { + var formatOptions = { decode: false - }); + }; + var rows = Row.formatChunks_(chunks, formatOptions); assert.deepEqual(rows, [{ key: 'convertedKey', data: { familyName: { convertedQualifier: [{ - value: 'unconvertedValue', + value: 'convertedValue', labels: ['label'], timestamp: timestamp1, size: 0 }, { - value: 'unconvertedValue2', + value: 'convertedValue2', labels: ['label2'], timestamp: timestamp2, size: 2 @@ -321,6 +322,12 @@ describe('Bigtable/Row', function() { } } }]); + + // 0 === row key + // 1 === qualifier + // 2 === value + var args = FakeMutation.convertFromBytes.getCall(2).args; + assert.strictEqual(args[1], formatOptions); }); it('should discard old data when reset row is found', function() {