Permalink
Browse files

Redis indexes intersections

  • Loading branch information...
1602 committed Oct 5, 2011
1 parent 5911ed3 commit c5da2b56a759fd896fcb5a955cf121ffece6b3a4
Showing with 56 additions and 343 deletions.
  1. +14 −14 lib/redis.js
  2. +32 −3 test/common_test.js
  3. +0 −317 test/datamapper_test.js
  4. +10 −9 test/perf_test.coffee
View
@@ -58,7 +58,9 @@ BridgeToRedis.prototype.updateIndexes = function (model, data, callback) {
}.bind(this));
if (schedule.length) {
- this.client.multi(schedule).exec(callback);
+ this.client.multi(schedule).exec(function (err) {
+ callback(err);
+ });
} else {
callback(null);
}
@@ -100,13 +102,13 @@ BridgeToRedis.prototype.destroy = function destroy(model, id, callback) {
});
};
-BridgeToRedis.prototype.possibleIndex = function possibleIndex(model, filter) {
+BridgeToRedis.prototype.possibleIndexes = function (model, filter) {
if (!filter || Object.keys(filter).length === 0) return false;
- var foundIndex = false;
+ var foundIndex = [];
Object.keys(filter).forEach(function (key) {
- if (!foundIndex && this.indexes[model][key]) {
- foundIndex = key;
+ if (this.indexes[model][key]) {
+ foundIndex.push('i:' + model + ':' + key + ':' + filter[key]);
}
}.bind(this));
@@ -115,26 +117,24 @@ BridgeToRedis.prototype.possibleIndex = function possibleIndex(model, filter) {
BridgeToRedis.prototype.all = function all(model, filter, callback) {
var ts = Date.now();
+ var client = this.client;
- var index = this.possibleIndex(model, filter);
- if (false) {
- // console.log('using index!', filter);
- this.client.smembers('i:' + model + ':' + index + ':' + filter[index],
- handleKeys.bind(this));
+ var indexes = this.possibleIndexes(model, filter);
+ if (indexes.length) {
+ indexes.push(handleKeys);
+ client.sinter.apply(client, indexes);
} else {
- // console.log('without index', filter);
- this.client.keys(model + ':*', handleKeys.bind(this));
+ client.keys(model + ':*', handleKeys);
}
function handleKeys(err, keys) {
- // console.log(arguments);
if (err) {
return callback(err, []);
}
var query = keys.map(function (key) {
return ['hgetall', key];
});
- this.client.multi(query).exec(function (err, replies) {
+ client.multi(query).exec(function (err, replies) {
// console.log('Redis time: %dms', Date.now() - ts);
callback(err, filter ? replies.filter(applyFilter(filter)) : replies);
});
View
@@ -27,12 +27,12 @@ Object.keys(schemas).forEach(function (schemaName) {
function testOrm(schema) {
- var Post;
+ var Post, User;
var start = Date.now();
it('should define class', function (test) {
- var User = schema.define('User', {
+ User = schema.define('User', {
name: String,
bio: Text,
approved: Boolean,
@@ -47,6 +47,18 @@ function testOrm(schema) {
published: { type: Boolean, default: false }
});
+ User.hasMany(Post, {as: 'posts', foreignKey: 'userId'});
+ // creates instance methods:
+ // user.posts(conds)
+ // user.buildPost(data) // like new Post({userId: user.id});
+ // user.createPost(data) // build and save
+
+ Post.belongsTo(User, {as: 'author', foreignKey: 'userId'});
+ // creates instance methods:
+ // post.author(callback) -- getter when called with function
+ // post.author() -- sync getter when called without params
+ // post.author(user) -- setter when called with object
+
var user = new User;
test.ok(User instanceof Function);
@@ -82,7 +94,7 @@ function testOrm(schema) {
it('should be expoted to JSON', function (test) {
test.equal(JSON.stringify(new Post({id: 1, title: 'hello, json'})),
- '{"id":1,"title":"hello, json","content":null,"date":null,"published":null}');
+ '{"id":1,"title":"hello, json","content":null,"date":null,"published":null,"userId":null}');
test.done();
});
@@ -260,6 +272,23 @@ function testOrm(schema) {
});
+ it('should handle hasMany relationship', function (test) {
+ User.create(function (err, u) {
+ if (err) return console.log(err);
+ test.ok(u.posts, 'Method defined: posts');
+ test.ok(u.buildPost, 'Method defined: buildPost');
+ test.ok(u.createPost, 'Method defined: createPost');
+ u.createPost(function (err, post) {
+ if (err) return console.log(err);
+ test.ok(post.author(), u.id);
+ u.posts(function (err, posts) {
+ test.strictEqual(posts.pop(), post);
+ test.done();
+ });
+ });
+ });
+ });
+
it('should destroy all records', function (test) {
Post.destroyAll(function (err) {
Post.all(function (err, posts) {
Oops, something went wrong.

0 comments on commit c5da2b5

Please sign in to comment.