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 1 commit
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
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
31 changes: 14 additions & 17 deletions test/datastore/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,26 @@ describe('keyFromKeyProto', function() {

it('should handle keys hierarchically', function(done) {
var key = entity.keyFromKeyProto(protoH);
assert.deepEqual(key, ['Test', 'Kind', 111, 'Kind2', 'name']);
assert.deepEqual(key, datastore.Key('Test', 'Kind', 111, 'Kind2', 'name'));
done();
});

it('should handle incomplete keys hierarchically', function(done) {
var key = entity.keyFromKeyProto(protoHIncomplete);
assert.deepEqual(key, ['Test', 'Kind', null, 'Kind2', null]);
assert.deepEqual(key, datastore.Key('Test', 'Kind', null, 'Kind2', null));
done();
});

it('should not set namespace if default', function(done) {
var key = entity.keyFromKeyProto(proto);
assert.deepEqual(key, ['Kind', 'Name']);
assert.deepEqual(key, datastore.Key('Kind', 'Name'));
done();
});
});

describe('keyToKeyProto', function() {
it('should handle hierarchical key definitions', function(done) {
var key = ['Kind1', 1, 'Kind2', 'name'];
var key = datastore.Key('Kind1', 1, 'Kind2', 'name');
var proto = entity.keyToKeyProto(key);
assert.strictEqual(proto.partitionId, undefined);
assert.strictEqual(proto.path[0].kind, 'Kind1');
Expand All @@ -181,7 +181,7 @@ describe('keyToKeyProto', function() {
});

it('should detect the namespace of the hierarchical keys', function(done) {
var key = ['Namespace', 'Kind1', 1, 'Kind2', 'name'];
var key = datastore.Key('Namespace', 'Kind1', 1, 'Kind2', 'name');
var proto = entity.keyToKeyProto(key);
assert.strictEqual(proto.partitionId.namespace, 'Namespace');
assert.strictEqual(proto.path[0].kind, 'Kind1');
Expand All @@ -194,8 +194,8 @@ describe('keyToKeyProto', function() {
});

it('should handle incomplete keys with & without namespaces', function(done) {
var key = ['Kind1', null];
var keyWithNS = ['Namespace', 'Kind1', null];
var key = datastore.Key('Kind1', null);
var keyWithNS = datastore.Key('Namespace', 'Kind1', null);

var proto = entity.keyToKeyProto(key);
var protoWithNS = entity.keyToKeyProto(keyWithNS);
Expand All @@ -222,10 +222,10 @@ describe('keyToKeyProto', function() {
describe('isKeyComplete', function() {
it('should ret true if kind and an identifier have !0 vals', function(done) {
[
{ key: ['Kind1', null], expected: false },
{ key: ['Kind1', 3], expected: true },
{ key: ['Namespace', 'Kind1', null], expected: false },
{ key: ['Namespace', 'Kind1', 'name'], expected: true }
{ key: datastore.Key('Kind1', null), expected: false },
{ key: datastore.Key('Kind1', 3), expected: true },
{ key: datastore.Key('Namespace', 'Kind1', null), expected: false },
{ key: datastore.Key('Namespace', 'Kind1', 'name'), expected: true }
].forEach(function(test) {
assert.strictEqual(entity.isKeyComplete(test.key), test.expected);
});
Expand All @@ -234,15 +234,12 @@ describe('isKeyComplete', function() {
});

describe('entityFromEntityProto', function() {
it(
'should support boolean, integer, double, string, entity and list values',
it('should support boolean, integer, double, string, entity and list values',
function(done) {
var obj = entity.entityFromEntityProto(entityProto);
assert.strictEqual(
obj.createdAt.getTime(), new Date('2001-01-01').getTime());
assert.strictEqual(obj.linkedTo.ns, undefined);
assert.strictEqual(obj.linkedTo[0], 'Kind');
assert.strictEqual(obj.linkedTo[1], 4790047639339008);
assert.deepEqual(obj.linkedTo, datastore.Key('Kind', 4790047639339008));
assert.strictEqual(obj.name, 'Name');
assert.strictEqual(obj.flagged, true);
assert.strictEqual(obj.count, 5);
Expand Down Expand Up @@ -297,7 +294,7 @@ describe('queryToQueryProto', function() {
var ds = new datastore.Dataset({ projectId: 'project-id' });
var q = ds.createQuery('Kind1')
.filter('name =', 'John')
.hasAncestor(['Kind2', 'somename']);
.hasAncestor(datastore.Key('Kind2', 'somename'));
var proto = entity.queryToQueryProto(q);
assert.deepEqual(proto, queryFilterProto);
done();
Expand Down