Skip to content

Commit fd60490

Browse files
author
Burcu Dogan
committed
Merge pull request #97 from rakyll/optional-partition
datastore: Key partition should be optional
2 parents 67e7b8e + c977820 commit fd60490

5 files changed

Lines changed: 18 additions & 42 deletions

File tree

lib/datastore/dataset.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ var SCOPES = [
3737
*/
3838
function Dataset(opts) {
3939
opts = opts || {};
40-
var id = opts.projectId || '';
41-
42-
if (id.indexOf('s~') === -1 && id.indexOf('e~') === -1) {
43-
id = 's~' + id;
44-
}
40+
var id = opts.projectId;
4541

4642
this.connection = new conn.Connection({
4743
keyFilename: opts.keyFilename,
@@ -109,7 +105,7 @@ Dataset.prototype.allocateIds = function(incompleteKey, n, callback) {
109105
}
110106
var incompleteKeys = [];
111107
for (var i = 0; i < n; i++) {
112-
incompleteKeys.push(entity.keyToKeyProto(this.id, incompleteKey));
108+
incompleteKeys.push(entity.keyToKeyProto(incompleteKey));
113109
}
114110
this.transaction.makeReq(
115111
'allocateIds', { keys: incompleteKeys }, function(err, resp) {

lib/datastore/entity.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ var keyFromKeyProto = function(proto) {
9191

9292
module.exports.keyFromKeyProto = keyFromKeyProto;
9393

94-
var keyToKeyProto = function(datasetId, key) {
94+
var keyToKeyProto = function(key) {
9595
if (key.length < 2) {
9696
throw new Error('A key should contain at least a kind and an identifier.');
9797
}
@@ -120,12 +120,9 @@ var keyToKeyProto = function(datasetId, key) {
120120
path: path
121121
};
122122
if (namespace) {
123-
proto.partitionId = {};
124-
proto.partitionId.namespace = namespace;
125-
}
126-
if (datasetId) {
127-
proto.partitionId = proto.partitionId || {};
128-
proto.partitionId.datasetId = datasetId;
123+
proto.partitionId = {
124+
namespace: namespace
125+
};
129126
}
130127
return proto;
131128
};
@@ -142,7 +139,7 @@ module.exports.formatArray = function(results) {
142139
};
143140

144141
module.exports.isKeyComplete = function(key) {
145-
var proto = keyToKeyProto(null, key);
142+
var proto = keyToKeyProto(key);
146143
for (var i = 0; i < proto.path.length; i++) {
147144
if (!proto.path[i].kind) {
148145
return false;
@@ -284,7 +281,7 @@ var queryToQueryProto = function(q) {
284281
var filters = q.filters.map(function(f) {
285282
var val = {};
286283
if (f.name === '__key__') {
287-
val.keyValue = keyToKeyProto(null, f.val);
284+
val.keyValue = keyToKeyProto(f.val);
288285
} else {
289286
val = valueToProperty(f.val);
290287
}

lib/datastore/transaction.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ Transaction.prototype.get = function(keys, callback) {
104104
keys = isMultipleRequest ? keys : [keys];
105105
callback = callback || util.noop;
106106
var req = {
107-
keys: keys.map(entity.keyToKeyProto.bind(null, this.id))
107+
keys: keys.map(entity.keyToKeyProto)
108108
};
109109
if (this.id) {
110110
req.transaction = this.id;
@@ -137,7 +137,7 @@ Transaction.prototype.save = function(entities, callback) {
137137
mode: MODE_NON_TRANSACTIONAL,
138138
mutation: entities.reduce(function(acc, entityObject, index) {
139139
var ent = entity.entityToEntityProto(entityObject.data);
140-
ent.key = entity.keyToKeyProto(this.datasetId, entityObject.key);
140+
ent.key = entity.keyToKeyProto(entityObject.key);
141141
if (entity.isKeyComplete(entityObject.key)) {
142142
acc.upsert.push(ent);
143143
} else {
@@ -176,7 +176,7 @@ Transaction.prototype.delete = function(keys, callback) {
176176
var req = {
177177
mode: MODE_NON_TRANSACTIONAL,
178178
mutation: {
179-
delete: keys.map(entity.keyToKeyProto.bind(null, this.id))
179+
delete: keys.map(entity.keyToKeyProto)
180180
}
181181
};
182182
if (this.id) {

test/datastore/dataset.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,6 @@ var mockRespGet = require('../testdata/response_get.json');
2424
var Transaction = require('../../lib/datastore/transaction.js');
2525

2626
describe('Dataset', function() {
27-
it('should append ~s if ~s or ~e are not presented', function(done) {
28-
var dsWithNoPrefix = new datastore.Dataset({ projectId: 'test' });
29-
var dsWithSPrefix = new datastore.Dataset({ projectId: 's~name' });
30-
var dsWithEPrefix = new datastore.Dataset({ projectId: 'e~name' });
31-
assert.equal(dsWithNoPrefix.id, 's~test');
32-
assert.equal(dsWithSPrefix.id, 's~name');
33-
assert.equal(dsWithEPrefix.id, 'e~name');
34-
done();
35-
});
36-
3727
it('should get by key', function(done) {
3828
var ds = new datastore.Dataset({ projectId: 'test' });
3929
ds.transaction.makeReq = function(method, proto, callback) {
@@ -126,9 +116,6 @@ describe('Dataset', function() {
126116
assert.equal(method, 'allocateIds');
127117
assert.equal(proto.keys.length, 1);
128118
assert.deepEqual(proto.keys[0], {
129-
partitionId:{
130-
datasetId: 's~test',
131-
},
132119
path :[{kind:'Kind'}]
133120
});
134121
callback(null, {

test/datastore/entity.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,8 @@ describe('keyFromKeyProto', function() {
169169
describe('keyToKeyProto', function() {
170170
it('should handle hierarchical key definitions', function(done) {
171171
var key = ['Kind1', 1, 'Kind2', 'name'];
172-
var proto = entity.keyToKeyProto('datasetId', key);
173-
assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
174-
assert.strictEqual(proto.partitionId.namespace, undefined);
172+
var proto = entity.keyToKeyProto(key);
173+
assert.strictEqual(proto.partitionId, undefined);
175174
assert.strictEqual(proto.path[0].kind, 'Kind1');
176175
assert.strictEqual(proto.path[0].id, 1);
177176
assert.strictEqual(proto.path[0].name, undefined);
@@ -183,8 +182,7 @@ describe('keyToKeyProto', function() {
183182

184183
it('should detect the namespace of the hierarchical keys', function(done) {
185184
var key = ['Namespace', 'Kind1', 1, 'Kind2', 'name'];
186-
var proto = entity.keyToKeyProto('datasetId', key);
187-
assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
185+
var proto = entity.keyToKeyProto(key);
188186
assert.strictEqual(proto.partitionId.namespace, 'Namespace');
189187
assert.strictEqual(proto.path[0].kind, 'Kind1');
190188
assert.strictEqual(proto.path[0].id, 1);
@@ -199,16 +197,14 @@ describe('keyToKeyProto', function() {
199197
var key = ['Kind1', null];
200198
var keyWithNS = ['Namespace', 'Kind1', null];
201199

202-
var proto = entity.keyToKeyProto('datasetId', key);
203-
var protoWithNS = entity.keyToKeyProto('datasetId', keyWithNS);
200+
var proto = entity.keyToKeyProto(key);
201+
var protoWithNS = entity.keyToKeyProto(keyWithNS);
204202

205-
assert.strictEqual(proto.partitionId.datasetId, 'datasetId');
206-
assert.strictEqual(proto.partitionId.namespace, undefined);
203+
assert.strictEqual(proto.partitionId, undefined);
207204
assert.strictEqual(proto.path[0].kind, 'Kind1');
208205
assert.strictEqual(proto.path[0].id, undefined);
209206
assert.strictEqual(proto.path[0].name, undefined);
210207

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

219215
it('should throw if key contains less than 2 items', function() {
220216
assert.throws(function() {
221-
entity.keyToKeyProto('datasetId', ['Kind']);
217+
entity.keyToKeyProto(['Kind']);
222218
});
223219
});
224220
});

0 commit comments

Comments
 (0)