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

datastore: Introduce Key type #101

Merged
merged 6 commits into from
Aug 8, 2014
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 18 additions & 11 deletions lib/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ var signToOrderDict = {
'+': 'ASCENDING'
};

function Key(path) {
this.path_ = path;

This comment was marked as spam.

This comment was marked as spam.

}

module.exports.Key = Key;

function Int(val) {
this.val = val;
}
Expand Down Expand Up @@ -78,34 +84,35 @@ var entityFromEntityProto = function(proto) {
module.exports.entityFromEntityProto = entityFromEntityProto;

var keyFromKeyProto = function(proto) {
var key = [];
var path = [];
if (proto.partitionId.namespace) {
key.push(proto.partitionId.namespace);
path.push(proto.partitionId.namespace);
}
proto.path.forEach(function(p) {
key.push(p.kind);
key.push(Number(p.id) || p.name || null);
path.push(p.kind);
path.push(Number(p.id) || p.name || null);
});
return key;
return new Key(path);
};

module.exports.keyFromKeyProto = keyFromKeyProto;

var keyToKeyProto = function(key) {
if (key.length < 2) {
var keyPath = key.path_;
if (keyPath.length < 2) {
throw new Error('A key should contain at least a kind and an identifier.');
}
var namespace = null;
var start = 0;
if (key.length % 2 === 1) {
if (keyPath.length % 2 === 1) {
// the first item is the namespace
namespace = key[0];
namespace = keyPath[0];
start = 1;
}
var path = [];
for (var i = start; i < (key.length - start); i += 2) {
var p = { kind: key[i] };
var val = key[i+1];
for (var i = start; i < (keyPath.length - start); i += 2) {
var p = { kind: keyPath[i] };
var val = keyPath[i+1];
if (val) {
// if not numeric, set key name.
if (isNaN(val)) {
Expand Down
7 changes: 7 additions & 0 deletions lib/datastore/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ var entity = require('./entity');

module.exports = {
Dataset: require('./dataset'),
Key: function(path) {
// TODO(jbd): Handle arguments in entity.Key constructor.
if (Array.isArray(path)) {
return new entity.Key(path);
}
return new entity.Key([].slice.call(arguments));
},

This comment was marked as spam.

This comment was marked as spam.

Int: function(value) {
return new entity.Int(value);
},
Expand Down
5 changes: 3 additions & 2 deletions lib/datastore/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Transaction.prototype.finalize = function(callback) {
* @param {Function} callback
*/
Transaction.prototype.get = function(keys, callback) {
var isMultipleRequest = Array.isArray(keys[0]);
var isMultipleRequest = Array.isArray(keys);
keys = isMultipleRequest ? keys : [keys];
callback = callback || util.noop;
var req = {
Expand Down Expand Up @@ -170,9 +170,10 @@ Transaction.prototype.save = function(entities, callback) {
* @param {Function} callback
*/
Transaction.prototype.delete = function(keys, callback) {
var isMultipleRequest = Array.isArray(keys[0]);
var isMultipleRequest = Array.isArray(keys);
keys = isMultipleRequest ? keys : [keys];
callback = callback || util.noop;

var req = {
mode: MODE_NON_TRANSACTIONAL,
mutation: {
Expand Down
67 changes: 36 additions & 31 deletions regression/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ describe('datastore', function() {
};

it('should save/get/delete with a key name', function(done) {
var postKeyName = 'post1';

ds.save({ key: ['Post', postKeyName], data: post }, function(err, key) {
var postKey = datastore.Key('Post', 'post1');
ds.save({ key: postKey, data: post }, function(err, key) {
assert.ifError(err);
assert.equal(key[1], postKeyName);
ds.get(['Post', postKeyName], function(err, entity) {
assert.equal(key.path_[1], 'post1');
ds.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
ds.delete(['Post', postKeyName], function(err) {
ds.delete(key, function(err) {
assert.ifError(err);
done();
});
Expand All @@ -54,15 +53,17 @@ describe('datastore', function() {
});

it('should save/get/delete with a numeric key id', function(done) {
var postKeyId = '123456789';

ds.save({ key: ['Post', postKeyId], data: post }, function(err, key) {
var postKey = datastore.Key('Post', 123456789)

This comment was marked as spam.

This comment was marked as spam.

ds.save({
key: postKey,
data: post
}, function(err, key) {
assert.ifError(err);
assert.equal(key[1], postKeyId);
ds.get(['Post', postKeyId], function(err, entity) {
assert.equal(key.path_[1], 123456789);
ds.get(key, function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
ds.delete(['Post', postKeyId], function(err) {
ds.delete(key, function(err) {
assert.ifError(err);
done();
});
Expand All @@ -71,14 +72,17 @@ describe('datastore', function() {
});

it('should save/get/delete with a generated key id', function(done) {
ds.save({ key: ['Post', null], data: post }, function(err, key) {
ds.save({
key: datastore.Key('Post', null),
data: post
}, function(err, key) {
assert.ifError(err);
assert(key[1]);
var assignedId = key[1];
ds.get(['Post', assignedId], function(err, entity) {
var assignedId = key.path_[1];
assert(assignedId);
ds.get(datastore.Key('Post', assignedId), function(err, entity) {
assert.ifError(err);
assert.deepEqual(entity.data, post);
ds.delete(['Post', assignedId], function(err) {
ds.delete(datastore.Key('Post', assignedId), function(err) {
assert.ifError(err);
done();
});
Expand All @@ -96,15 +100,15 @@ describe('datastore', function() {
wordCount: 450,
rating: 4.5,
};
var key = ['Post', null];
var key = datastore.Key('Post', null);
ds.save([
{ key: key, data: post },
{ key: key, data: post2 }
], function(err, keys) {
assert.ifError(err);
assert.equal(keys.length,2);
var firstKey = ['Post', keys[0][1]];
var secondKey = ['Post', keys[1][1]];
var firstKey = datastore.Key('Post', keys[0].path_[1]);
var secondKey = datastore.Key('Post', keys[1].path_[1]);
ds.get([firstKey, secondKey], function(err, entities) {
assert.ifError(err);
assert.equal(entities.length, 2);
Expand All @@ -121,14 +125,14 @@ describe('datastore', function() {
describe('querying the datastore', function() {

var keys = [
['Character', 'Rickard'],
['Character', 'Rickard', 'Character', 'Eddard'],
['Character', 'Catelyn'],
['Character', 'Eddard', 'Character', 'Arya'],
['Character', 'Eddard', 'Character', 'Sansa'],
['Character', 'Eddard', 'Character', 'Robb'],
['Character', 'Eddard', 'Character', 'Bran'],
['Character', 'Eddard', 'Character', 'Jon Snow']
datastore.Key('Character', 'Rickard'),
datastore.Key('Character', 'Rickard', 'Character', 'Eddard'),
datastore.Key('Character', 'Catelyn'),
datastore.Key('Character', 'Eddard', 'Character', 'Arya'),
datastore.Key('Character', 'Eddard', 'Character', 'Sansa'),
datastore.Key('Character', 'Eddard', 'Character', 'Robb'),
datastore.Key('Character', 'Eddard', 'Character', 'Bran'),
datastore.Key('Character', 'Eddard', 'Character', 'Jon Snow')
];

var characters = [{
Expand Down Expand Up @@ -227,7 +231,8 @@ describe('datastore', function() {
});

it('should filter by ancestor', function(done) {
var q = ds.createQuery('Character').hasAncestor(['Character', 'Eddard']);
var q = ds.createQuery('Character')
.hasAncestor(datastore.Key('Character', 'Eddard'));
ds.runQuery(q, function(err, entities) {
assert.ifError(err);
assert.equal(entities.length, 5);
Expand All @@ -237,7 +242,7 @@ describe('datastore', function() {

it('should filter by key', function(done) {
var q = ds.createQuery('Character')
.filter('__key__ =', ['Character', 'Rickard']);
.filter('__key__ =', datastore.Key('Character', 'Rickard'));
ds.runQuery(q, function(err, entities) {
assert.ifError(err);
assert.equal(entities.length, 1);
Expand Down Expand Up @@ -332,7 +337,7 @@ describe('datastore', function() {
describe('transactions', function() {

it('should run in a transaction', function(done) {
var key = ['Company', 'Google'];
var key = datastore.Key('Company', 'Google');
var obj = {
url: 'www.google.com'
};
Expand Down
30 changes: 16 additions & 14 deletions test/datastore/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ describe('Dataset', function() {
assert.equal(proto.keys.length, 1);
callback(null, mockRespGet);
};
ds.get(['Kind', 123], function(err, entity) {
ds.get(datastore.Key('Kind', 123), function(err, entity) {
var properties = entity.data;
assert.deepEqual(entity.key, ['Kind', 5732568548769792]);
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.strictEqual(properties.name, 'Burcu');
assert.deepEqual(properties.bytes, new Buffer('hello'));
assert.strictEqual(properties.done, false);
Expand All @@ -50,10 +50,11 @@ describe('Dataset', function() {
assert.equal(proto.keys.length, 1);
callback(null, mockRespGet);
};
ds.get([['Kind', 123]], function(err, entities) {
var key = datastore.Key('Kind', 5732568548769792);
ds.get([key], function(err, entities) {
var entity = entities[0];
var properties = entity.data;
assert.deepEqual(entity.key, ['Kind', 5732568548769792]);
assert.deepEqual(entity.key.path_, ['Kind', 5732568548769792]);
assert.strictEqual(properties.name, 'Burcu');
assert.deepEqual(properties.bytes, new Buffer('hello'));
assert.strictEqual(properties.done, false);
Expand All @@ -70,7 +71,7 @@ describe('Dataset', function() {
assert.equal(!!proto.mutation.delete, true);
callback();
};
ds.delete(['Kind', 123], done);
ds.delete(datastore.Key('Kind', 123), done);
});

it('should multi delete by keys', function(done) {
Expand All @@ -81,8 +82,8 @@ describe('Dataset', function() {
callback();
};
ds.delete([
['Kind', 123],
['Kind', 345]
datastore.Key('Kind', 123),
datastore.Key('Kind', 345)
], done);
});

Expand All @@ -93,7 +94,8 @@ describe('Dataset', function() {
assert.equal(proto.mutation.insertAutoId.length, 1);
callback();
};
ds.save({ key: ['Kind', 123, null], data: {} }, done);
var key = datastore.Key('Kind', 123, null);
ds.save({ key: key, data: {} }, done);
});

it('should save with keys', function(done) {
Expand All @@ -105,8 +107,8 @@ describe('Dataset', function() {
callback();
};
ds.save([
{ key: ['Kind', 123], data: { k: 'v' } },
{ key: ['Kind', 456], data: { k: 'v' } }
{ key: datastore.Key('Kind', 123), data: { k: 'v' } },
{ key: datastore.Key('Kind', 456), data: { k: 'v' } }
], done);
});

Expand All @@ -127,16 +129,16 @@ describe('Dataset', function() {
]
});
};
ds.allocateIds(['Kind', null], 1, function(err, ids) {
assert.deepEqual(ids[0], ['Kind', 123]);
ds.allocateIds(datastore.Key('Kind', null), 1, function(err, ids) {
assert.deepEqual(ids[0], datastore.Key('Kind', 123));
done();
});
});

it('should throw if trying to allocate IDs with complete keys', function() {
var ds = new datastore.Dataset({ projectId: 'test' });
assert.throws(function() {
ds.allocateIds(['Kind', 123]);
ds.allocateIds(datastore.Key('Kind', 123));
});
});

Expand Down Expand Up @@ -229,7 +231,7 @@ describe('Dataset', function() {
assert.ifError(err);

var properties = entities[0].data;
assert.deepEqual(entities[0].key, ['Kind', 5732568548769792]);
assert.deepEqual(entities[0].key.path_, ['Kind', 5732568548769792]);
assert.strictEqual(properties.name, 'Burcu');
assert.deepEqual(properties.bytes, new Buffer('hello'));
assert.strictEqual(properties.done, false);
Expand Down
Loading