Skip to content

Commit

Permalink
datastore: Key partition should only include namespace and should be …
Browse files Browse the repository at this point in the history
…optional.
  • Loading branch information
rakyll committed Aug 6, 2014
1 parent 67e7b8e commit c977820
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 42 deletions.
8 changes: 2 additions & 6 deletions lib/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,7 @@ var SCOPES = [
*/
function Dataset(opts) {
opts = opts || {};
var id = opts.projectId || '';

if (id.indexOf('s~') === -1 && id.indexOf('e~') === -1) {
id = 's~' + id;
}
var id = opts.projectId;

this.connection = new conn.Connection({
keyFilename: opts.keyFilename,
Expand Down Expand Up @@ -109,7 +105,7 @@ Dataset.prototype.allocateIds = function(incompleteKey, n, callback) {
}
var incompleteKeys = [];
for (var i = 0; i < n; i++) {
incompleteKeys.push(entity.keyToKeyProto(this.id, incompleteKey));
incompleteKeys.push(entity.keyToKeyProto(incompleteKey));
}
this.transaction.makeReq(
'allocateIds', { keys: incompleteKeys }, function(err, resp) {
Expand Down
15 changes: 6 additions & 9 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ var keyFromKeyProto = function(proto) {

module.exports.keyFromKeyProto = keyFromKeyProto;

var keyToKeyProto = function(datasetId, key) {
var keyToKeyProto = function(key) {
if (key.length < 2) {
throw new Error('A key should contain at least a kind and an identifier.');
}
Expand Down Expand Up @@ -120,12 +120,9 @@ var keyToKeyProto = function(datasetId, key) {
path: path
};
if (namespace) {
proto.partitionId = {};
proto.partitionId.namespace = namespace;
}
if (datasetId) {
proto.partitionId = proto.partitionId || {};
proto.partitionId.datasetId = datasetId;
proto.partitionId = {
namespace: namespace
};
}
return proto;
};
Expand All @@ -142,7 +139,7 @@ module.exports.formatArray = function(results) {
};

module.exports.isKeyComplete = function(key) {
var proto = keyToKeyProto(null, key);
var proto = keyToKeyProto(key);
for (var i = 0; i < proto.path.length; i++) {
if (!proto.path[i].kind) {
return false;
Expand Down Expand Up @@ -284,7 +281,7 @@ var queryToQueryProto = function(q) {
var filters = q.filters.map(function(f) {
var val = {};
if (f.name === '__key__') {
val.keyValue = keyToKeyProto(null, f.val);
val.keyValue = keyToKeyProto(f.val);
} else {
val = valueToProperty(f.val);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Transaction.prototype.get = function(keys, callback) {
keys = isMultipleRequest ? keys : [keys];
callback = callback || util.noop;
var req = {
keys: keys.map(entity.keyToKeyProto.bind(null, this.id))
keys: keys.map(entity.keyToKeyProto)
};
if (this.id) {
req.transaction = this.id;
Expand Down Expand Up @@ -137,7 +137,7 @@ Transaction.prototype.save = function(entities, callback) {
mode: MODE_NON_TRANSACTIONAL,
mutation: entities.reduce(function(acc, entityObject, index) {
var ent = entity.entityToEntityProto(entityObject.data);
ent.key = entity.keyToKeyProto(this.datasetId, entityObject.key);
ent.key = entity.keyToKeyProto(entityObject.key);
if (entity.isKeyComplete(entityObject.key)) {
acc.upsert.push(ent);
} else {
Expand Down Expand Up @@ -176,7 +176,7 @@ Transaction.prototype.delete = function(keys, callback) {
var req = {
mode: MODE_NON_TRANSACTIONAL,
mutation: {
delete: keys.map(entity.keyToKeyProto.bind(null, this.id))
delete: keys.map(entity.keyToKeyProto)
}
};
if (this.id) {
Expand Down
13 changes: 0 additions & 13 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,6 @@ var mockRespGet = require('../testdata/response_get.json');
var Transaction = require('../../lib/datastore/transaction.js');

describe('Dataset', function() {
it('should append ~s if ~s or ~e are not presented', function(done) {
var dsWithNoPrefix = new datastore.Dataset({ projectId: 'test' });
var dsWithSPrefix = new datastore.Dataset({ projectId: 's~name' });
var dsWithEPrefix = new datastore.Dataset({ projectId: 'e~name' });
assert.equal(dsWithNoPrefix.id, 's~test');
assert.equal(dsWithSPrefix.id, 's~name');
assert.equal(dsWithEPrefix.id, 'e~name');
done();
});

it('should get by key', function(done) {
var ds = new datastore.Dataset({ projectId: 'test' });
ds.transaction.makeReq = function(method, proto, callback) {
Expand Down Expand Up @@ -126,9 +116,6 @@ describe('Dataset', function() {
assert.equal(method, 'allocateIds');
assert.equal(proto.keys.length, 1);
assert.deepEqual(proto.keys[0], {
partitionId:{
datasetId: 's~test',
},
path :[{kind:'Kind'}]
});
callback(null, {
Expand Down
18 changes: 7 additions & 11 deletions test/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,8 @@ describe('keyFromKeyProto', function() {
describe('keyToKeyProto', function() {
it('should handle hierarchical key definitions', function(done) {
var key = ['Kind1', 1, 'Kind2', 'name'];
var proto = entity.keyToKeyProto('datasetId', key);
assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
assert.strictEqual(proto.partitionId.namespace, undefined);
var proto = entity.keyToKeyProto(key);
assert.strictEqual(proto.partitionId, undefined);
assert.strictEqual(proto.path[0].kind, 'Kind1');
assert.strictEqual(proto.path[0].id, 1);
assert.strictEqual(proto.path[0].name, undefined);
Expand All @@ -183,8 +182,7 @@ describe('keyToKeyProto', function() {

it('should detect the namespace of the hierarchical keys', function(done) {
var key = ['Namespace', 'Kind1', 1, 'Kind2', 'name'];
var proto = entity.keyToKeyProto('datasetId', key);
assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
var proto = entity.keyToKeyProto(key);
assert.strictEqual(proto.partitionId.namespace, 'Namespace');
assert.strictEqual(proto.path[0].kind, 'Kind1');
assert.strictEqual(proto.path[0].id, 1);
Expand All @@ -199,16 +197,14 @@ describe('keyToKeyProto', function() {
var key = ['Kind1', null];
var keyWithNS = ['Namespace', 'Kind1', null];

var proto = entity.keyToKeyProto('datasetId', key);
var protoWithNS = entity.keyToKeyProto('datasetId', keyWithNS);
var proto = entity.keyToKeyProto(key);
var protoWithNS = entity.keyToKeyProto(keyWithNS);

assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
assert.strictEqual(proto.partitionId.namespace, undefined);
assert.strictEqual(proto.partitionId, undefined);
assert.strictEqual(proto.path[0].kind, 'Kind1');
assert.strictEqual(proto.path[0].id, undefined);
assert.strictEqual(proto.path[0].name, undefined);

assert.strictEqual(protoWithNS.partitionId.datasetId, 'datasetId');
assert.strictEqual(protoWithNS.partitionId.namespace, 'Namespace');
assert.strictEqual(protoWithNS.path[0].kind, 'Kind1');
assert.strictEqual(protoWithNS.path[0].id, undefined);
Expand All @@ -218,7 +214,7 @@ describe('keyToKeyProto', function() {

it('should throw if key contains less than 2 items', function() {
assert.throws(function() {
entity.keyToKeyProto('datasetId', ['Kind']);
entity.keyToKeyProto(['Kind']);
});
});
});
Expand Down

0 comments on commit c977820

Please sign in to comment.