Skip to content

Commit

Permalink
bigtable: add support for families option when creating tables
Browse files Browse the repository at this point in the history
  • Loading branch information
callmehiphop authored and stephenplusplus committed Aug 4, 2016
1 parent a0a237c commit 1a42246
Show file tree
Hide file tree
Showing 3 changed files with 150 additions and 2 deletions.
66 changes: 64 additions & 2 deletions packages/bigtable/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,23 @@ var extend = require('extend');
var PKG = require('../package.json');

/**
* @type {module:bigtable/table}
* @type {module:bigtable/family}
* @private
*/
var Table = require('./table.js');
var Family = require('./family.js');

/**
* @type {module:common/grpcService}
* @private
*/
var GrpcService = common.GrpcService;

/**
* @type {module:bigtable/table}
* @private
*/
var Table = require('./table.js');

/**
* @type {module:common/util}
* @private
Expand Down Expand Up @@ -344,6 +350,8 @@ Bigtable.formatTableName_ = function(name) {
*
* @param {string} name - The name of the table.
* @param {object=} options - Table creation options.
* @param {object|string[]} options.families - Column families to be created
* within the table.
* @param {string} options.operation - Operation used for table that has already
* been queued to be created.
* @param {string[]} options.splits - Initial
Expand All @@ -361,6 +369,38 @@ Bigtable.formatTableName_ = function(name) {
* bigtable.createTable('prezzy', callback);
*
* //-
* // Optionally specify column families to be created within the table.
* //-
* var options = {
* families: ['follows']
* };
*
* bigtable.createTable('prezzy', options, callback);
*
* //-
* // You can also specify garbage collection rules for your column families.
* // See {module:bigtable/table#createFamily} for more information about
* // column families and garbage collection rules.
* //-
* var options = {
* families: [
* {
* name: 'follows',
* rule: {
* age: {
* seconds: 0,
* nanos: 5000
* },
* versions: 3,
* union: true
* }
* }
* ]
* };
*
* bigtable.createTable('prezzy', options, callback);
*
* //-
* // Pre-split the table based on the row key to spread the load across
* // multiple Cloud Bigtable nodes.
* //-
Expand Down Expand Up @@ -403,6 +443,28 @@ Bigtable.prototype.createTable = function(name, options, callback) {
reqOpts.initialSplitKeys = options.splits;
}

if (options.families) {
var columnFamilies = options.families.reduce(function(families, family) {
if (is.string(family)) {
family = {
name: family
};
}

var columnFamily = families[family.name] = {};

if (is.string(family.rule)) {
columnFamily.gcExpression = family.rule;
} else if (is.object(family.rule)) {
columnFamily.gcRule = Family.formatRule_(family.rule);
}

return families;
}, {});

reqOpts.table.columnFamilies = columnFamilies;
}

this.request(protoOpts, reqOpts, function(err, resp) {
if (err) {
callback(err, null, resp);
Expand Down
13 changes: 13 additions & 0 deletions packages/bigtable/system-test/bigtable.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ function generateTableName() {
});
});

it('should create a table with column family data', function(done) {
var name = generateTableName();
var options = {
families: ['test']
};

bigtable.createTable(name, options, function(err, table) {
assert.ifError(err);
assert(table.metadata.columnFamilies.test);
done();
});
});

});

describe('column families', function() {
Expand Down
73 changes: 73 additions & 0 deletions packages/bigtable/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ function FakeTable() {
Table.apply(this, arguments);
}

function FakeFamily() {}

describe('Bigtable', function() {
var PROJECT_ID = 'test-project';
var ZONE = 'test-zone';
Expand All @@ -63,6 +65,7 @@ describe('Bigtable', function() {
GrpcService: FakeGrpcService,
util: fakeUtil
},
'./family.js': FakeFamily,
'./table.js': FakeTable
});
});
Expand Down Expand Up @@ -213,6 +216,76 @@ describe('Bigtable', function() {
bigtable.createTable(TABLE_ID, options, assert.ifError);
});

describe('creating column families', function() {
it('should accept a family name', function(done) {
var options = {
families: ['a', 'b']
};

bigtable.request = function(protoOpts, reqOpts) {
assert.deepEqual(reqOpts.table.columnFamilies, {
a: {},
b: {}
});

done();
};

bigtable.createTable(TABLE_ID, options, assert.ifError);
});

it('should accept a garbage collection expression', function(done) {
var options = {
families: [
{
name: 'c',
rule: 'd'
}
]
};

bigtable.request = function(protoOpts, reqOpts) {
assert.deepEqual(reqOpts.table.columnFamilies, {
c: {
gcExpression: 'd'
}
});
done();
};

bigtable.createTable(TABLE_ID, options, assert.ifError);
});

it('should accept a garbage collection object', function(done) {
var options = {
families: [
{
name: 'e',
rule: {}
}
]
};

var fakeRule = { a: 'b' };

FakeFamily.formatRule_ = function(rule) {
assert.strictEqual(rule, options.families[0].rule);
return fakeRule;
};

bigtable.request = function(protoOpts, reqOpts) {
assert.deepEqual(reqOpts.table.columnFamilies, {
e: {
gcRule: fakeRule
}
});
done();
};

bigtable.createTable(TABLE_ID, options, assert.ifError);
});
});

it('should return an error to the callback', function(done) {
var err = new Error('err');
var response = {};
Expand Down

0 comments on commit 1a42246

Please sign in to comment.