Skip to content

Commit

Permalink
bigtable: return Buffer instead of base64 when decode is true (#1556)
Browse files Browse the repository at this point in the history
  • Loading branch information
callmehiphop authored and stephenplusplus committed Sep 6, 2016
1 parent 2eb558d commit cb71873
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 22 deletions.
8 changes: 6 additions & 2 deletions packages/bigtable/src/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,20 @@ 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();

if (!isNaN(num) && isFinite(num)) {
return num;
}

if (options && options.decode === false) {
return buf;
}

return buf.toString();
};

Expand Down
8 changes: 1 addition & 7 deletions packages/bigtable/src/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 5 additions & 9 deletions packages/bigtable/system-test/bigtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -573,23 +573,19 @@ 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
};

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();
});
Expand Down
11 changes: 11 additions & 0 deletions packages/bigtable/test/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
15 changes: 11 additions & 4 deletions packages/bigtable/test/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,28 +299,35 @@ 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
}]
}
}
}]);

// 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() {
Expand Down

0 comments on commit cb71873

Please sign in to comment.