Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve transform perf #46

Merged
merged 5 commits into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions src/chunktransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ ChunkTransformer.prototype.validateResetRow = function(chunk) {
* Validates state for new row.
* @private
* @param {chunk} chunk chunk to validate
* @param {newRowKey} newRowKey newRowKey of the new row
*/
ChunkTransformer.prototype.validateNewRow = function(chunk) {
ChunkTransformer.prototype.validateNewRow = function(chunk, newRowKey) {
const row = this.row;
const prevRowKey = this.prevRowKey;
const newRowKey = Mutation.convertFromBytes(chunk.rowKey);
let errorMessage;

if (typeof row.key !== 'undefined') {
Expand Down Expand Up @@ -224,15 +224,30 @@ ChunkTransformer.prototype.validateNewRow = function(chunk) {
*/
ChunkTransformer.prototype.validateRowInProgress = function(chunk) {
const row = this.row;
const newRowKey = Mutation.convertFromBytes(chunk.rowKey);
let errorMessage;
if (chunk.rowKey && newRowKey !== '' && newRowKey !== row.key) {
errorMessage = 'A commit is required between row keys';
} else if (chunk.familyName && !chunk.qualifier) {
errorMessage = 'A qualifier must be specified';
if (chunk.rowKey && chunk.rowKey.length) {
const newRowKey = Mutation.convertFromBytes(chunk.rowKey);
if (
newRowKey &&
chunk.rowKey &&
newRowKey !== '' &&
newRowKey !== row.key
) {
this.destroy(
new TransformError({
message: 'A commit is required between row keys',
chunk,
})
);
return;
}
}
if (errorMessage) {
this.destroy(new TransformError({message: errorMessage, chunk}));
if (chunk.familyName && !chunk.qualifier) {
this.destroy(
new TransformError({
message: 'A qualifier must be specified',
chunk,
})
);
return;
}
this.validateResetRow(chunk);
Expand Down Expand Up @@ -270,13 +285,10 @@ ChunkTransformer.prototype.moveToNextState = function(chunk) {
* Process chunk when in NEW_ROW state.
* @private
* @param {chunks} chunk chunk to process
* @param {options} options options
* @param {callback} callback callback to call with row as and when generates
* @returns {boolean} return false to stop further processing.
*/
ChunkTransformer.prototype.processNewRow = function(chunk) {
const newRowKey = Mutation.convertFromBytes(chunk.rowKey);
this.validateNewRow(chunk);
this.validateNewRow(chunk, newRowKey);
if (chunk.familyName && chunk.qualifier) {
const row = this.row;
row.key = newRowKey;
Expand All @@ -285,7 +297,7 @@ ChunkTransformer.prototype.processNewRow = function(chunk) {
const qualifierName = Mutation.convertFromBytes(chunk.qualifier.value);
this.qualifiers = this.family[qualifierName] = [];
this.qualifier = {
value: Mutation.convertFromBytes(chunk.value, this.options),
value: Mutation.convertFromBytes(chunk.value, this.options, true),
labels: chunk.labels,
timestamp: chunk.timestampMicros,
};
Expand All @@ -297,9 +309,6 @@ ChunkTransformer.prototype.processNewRow = function(chunk) {
* Process chunk when in ROW_IN_PROGRESS state.
* @private
* @param {chunk} chunk chunk to process
* @param {*} options option
* @param {*} callback callback to call with row as and when generates
* @returns {boolean} return false to stop further processing.
*/
ChunkTransformer.prototype.processRowInProgress = function(chunk) {
this.validateRowInProgress(chunk);
Expand Down Expand Up @@ -328,9 +337,6 @@ ChunkTransformer.prototype.processRowInProgress = function(chunk) {
* Process chunk when in CELl_IN_PROGRESS state.
* @private
* @param {chunk} chunk chunk to process
* @param {options} options options
* @param {callback} callback callback to call with row as and when generates
* @returns {boolean} return false to stop further processing.
*/
ChunkTransformer.prototype.processCellInProgress = function(chunk) {
this.validateCellInProgress(chunk);
Expand Down
12 changes: 7 additions & 5 deletions src/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,14 @@ var methods = (Mutation.methods = {
* @param {string} bytes - Base64 encoded string.
* @returns {string|number|buffer}
*/
Mutation.convertFromBytes = function(bytes, options) {
var buf = Buffer.from(bytes, 'base64');
var num = new Int64(buf).toNumber();
Mutation.convertFromBytes = function(bytes, options, isPossibleNumber) {

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

var buf = bytes instanceof Buffer ? bytes : Buffer.from(bytes, 'base64');
if (isPossibleNumber) {
var num = new Int64(buf).toNumber();

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

if (options && options.decode === false) {
Expand Down
2 changes: 1 addition & 1 deletion src/row.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ Row.formatFamilies_ = function(families, options) {
var value = cell.value;

if (options.decode !== false) {
value = Mutation.convertFromBytes(value);
value = Mutation.convertFromBytes(value, null, true);
}

return {
Expand Down
28 changes: 23 additions & 5 deletions test/mutation.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,21 @@ describe('Bigtable/Mutation', function() {
});

describe('convertFromBytes', function() {
it('should convert a base64 encoded number', function() {
var num = 10;
var encoded = new Int64(num).toBuffer().toString('base64');
var decoded = Mutation.convertFromBytes(encoded);
describe('isPossibleNumber', function() {
it('should convert a base64 encoded number when true', function() {
var num = 10;
var encoded = new Int64(num).toBuffer().toString('base64');
var decoded = Mutation.convertFromBytes(encoded, null, true);

assert.strictEqual(num, decoded);
assert.strictEqual(num, decoded);
});
it('should not convert a base64 encoded number when false', function() {
var num = 10;
var encoded = new Int64(num).toBuffer().toString('base64');
var decoded = Mutation.convertFromBytes(encoded);

assert.notEqual(num, decoded);
});
});

it('should convert a base64 encoded string', function() {
Expand All @@ -70,6 +79,15 @@ describe('Bigtable/Mutation', function() {
assert(decoded instanceof Buffer);
assert.strictEqual(decoded.toString(), message);
});

it('should not create a new Buffer needlessly', function() {
var message = 'Hello!';
var encoded = Buffer.from(message);
const stub = sinon.stub(Buffer, 'from');
const decoded = Mutation.convertFromBytes(encoded);
assert.strictEqual(stub.called, false);
assert.strictEqual(decoded.toString(), message);
});
});

describe('convertToBytes', function() {
Expand Down