From c977820c412874af8049e1e75f58844643b6c5f6 Mon Sep 17 00:00:00 2001 From: Burcu Dogan Date: Wed, 6 Aug 2014 14:37:18 -0700 Subject: [PATCH] datastore: Key partition should only include namespace and should be optional. --- lib/datastore/dataset.js | 8 ++------ lib/datastore/entity.js | 15 ++++++--------- lib/datastore/transaction.js | 6 +++--- test/datastore/dataset.js | 13 ------------- test/datastore/entity.js | 18 +++++++----------- 5 files changed, 18 insertions(+), 42 deletions(-) diff --git a/lib/datastore/dataset.js b/lib/datastore/dataset.js index 163ca353343..ee5f0b7a779 100644 --- a/lib/datastore/dataset.js +++ b/lib/datastore/dataset.js @@ -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, @@ -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) { diff --git a/lib/datastore/entity.js b/lib/datastore/entity.js index 11a71d82b1e..e0b1bcbefc8 100644 --- a/lib/datastore/entity.js +++ b/lib/datastore/entity.js @@ -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.'); } @@ -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; }; @@ -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; @@ -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); } diff --git a/lib/datastore/transaction.js b/lib/datastore/transaction.js index 54dea3c0d18..bbcb7c3593c 100644 --- a/lib/datastore/transaction.js +++ b/lib/datastore/transaction.js @@ -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; @@ -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 { @@ -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) { diff --git a/test/datastore/dataset.js b/test/datastore/dataset.js index 4977a71b28a..305a6d19a53 100644 --- a/test/datastore/dataset.js +++ b/test/datastore/dataset.js @@ -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) { @@ -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, { diff --git a/test/datastore/entity.js b/test/datastore/entity.js index 324cc3dedb5..224f23d83ae 100644 --- a/test/datastore/entity.js +++ b/test/datastore/entity.js @@ -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); @@ -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); @@ -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); @@ -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']); }); }); });