Permalink
Browse files

Select: 完善基于ID的查找和删除; 在select中添加记录集总数

  • Loading branch information...
1 parent 65f6a87 commit 5e8da9ba1366dbfc5b1683747e1f04fcc17a78a6 @newghost committed Nov 14, 2015
Showing with 159 additions and 84 deletions.
  1. +129 −80 node_modules/redblade/redblade.js
  2. +30 −4 node_modules/redblade/test/testRedblade.js
@@ -702,6 +702,8 @@ var select = function(schemaName, where, cb, options) {
options = options || {}
var schema = SCHEMA[schemaName]
+ , _idField = schema._idField_
+ , _idValue = where[_idField]
, conditions = Object.keys(where)
//总记录条数
, countObjs = 0
@@ -825,7 +827,7 @@ var select = function(schemaName, where, cb, options) {
/*
查询的字段在schema中有无定义
condition: email
- rules: { minlen: [4], index: ['user_email'], keywords: ['key'] }
+ rules: { minlen:[4], index:['user_email'], keywords:['key'] }
*/
var rules = schema[condition]
if (!rules) {
@@ -835,104 +837,108 @@ var select = function(schemaName, where, cb, options) {
return
}
- /*
- 若是ID字段则无需根据索引字段查找出ID, where[condition]的值即为ID
- */
- if (rules.id) {
- var idArr = [ where[condition] ]
- skeys = null
- zkeys = null
- } else {
- //只有定义在schema中的index或keywords,才能让where条件起作用
- var conditionSchema = rules.index || rules.keywords
- if (!conditionSchema) {
- var err = new Error('select error ' + schemaName + ':' + condition + ' is not an index field')
- console.error(err)
- cb && cb(err, [])
- return
- }
- var conditionKey = conditionSchema[0] + ':' + where[condition]
+ //只有定义在schema中的index或keywords,才能让where条件起作用
+ var conditionSchema = rules.index || rules.keywords
+ if (!conditionSchema) {
+ var err = new Error('select error ' + schemaName + ':' + condition + ' is not an index field')
+ console.error(err)
+ cb && cb(err, [])
+ return
+ }
- //schema定义中只有一个参数为无序集合(set); 两个参数为有序集合, 第二个参数为排序权重
- if (conditionSchema.length > 1) {
+ var conditionKey = conditionSchema[0] + ':' + where[condition]
- desc === true
- ? multi.zrevrange(conditionKey, from, to)
- : multi.zrange(conditionKey, from, to)
+ //schema定义中只有一个参数为无序集合(set); 两个参数为有序集合, 第二个参数为排序权重
+ if (conditionSchema.length > 1) {
+ desc === true
+ ? multi.zrevrange(conditionKey, from, to)
+ : multi.zrange(conditionKey, from, to)
- zkeys && zkeys.push(conditionKey)
- skeys = null
- } else {
- multi.smembers(conditionKey)
- zkeys && zkeys.push(conditionKey)
- skeys && skeys.push(conditionKey)
- }
+ zkeys && zkeys.push(conditionKey)
+ skeys = null
+ } else {
+ multi.smembers(conditionKey)
+ zkeys && zkeys.push(conditionKey)
+ skeys && skeys.push(conditionKey)
}
-
if (count1 == conditions.length) {
/*
如果所有条件均为无序集合,则使用sinter函数求多个条件交集
*/
- if (skeys && skeys.length > 1) {
- multi.discard()
- skeys.push(function(err, IDs) {
- if (err) {
- cb && cb(err)
- return
- }
+ if (skeys) {
+ if (skeys.length > 1) {
+ multi.discard()
+ skeys.push(function(err, IDs) {
+ if (err) {
+ cb && cb(err)
+ return
+ }
- //无序集合的交集会返回所有ID,取其长度即可
- countObjs = IDs.length
+ //无序集合的交集会返回所有ID,取其长度即可
+ countObjs = IDs.length
- //to + 1: 使之与zrevrange zrange的结果保持一致
- getFromIDs(IDs.sort().slice(from, to + 1))
- })
+ //to + 1: 使之与zrevrange zrange的结果保持一致
+ getFromIDs(IDs.sort().slice(from, to + 1))
+ })
- client.sinter.apply(client, skeys)
+ client.sinter.apply(client, skeys)
+ } else {
+ multi.scard(conditionKey)
+ multi.exec(function(err, results) {
+ countObjs = parseInt(results[1]) || 0
+ getFromIDs(results[0] || [])
+ })
+ }
}
/*
索引类型中有无序集合则使用; zinterstore求其交集
http://blog.redsmin.com/post/55718899396/how-to-use-zinterstore-with-both-zsets-and-sets
*/
- else if (zkeys && zkeys.length > 1) {
- multi.discard()
-
- var transaction = client.multi()
- //生成一个随机的keys作为临时查询结果存储地
- , tempStoreKey = '_tmp_ix_' + (+new Date()).toString(36) + (Math.random() * 100000000 | 0).toString(36)
-
- zkeys.unshift(tempStoreKey, zkeys.length)
-
- transaction.zinterstore.apply(transaction, zkeys)
-
- desc === true
- ? transaction.zrevrange(conditionKey, from, to)
- : transaction.zrange(conditionKey, from, to)
-
- transaction.zcard(tempStoreKey)
- transaction.del(tempStoreKey)
- transaction.exec(function(err, results) {
- if (err) {
- cb && cb(err)
- return
- }
-
- /*
- 事务结果排序:
- 0: zinterstore 指令对应的结果
- 1: zrange/ zrevrange 查询结果集
- 2: zcard 查看临时结果集合的元素个数
- 3: del 删除临时结果集合
- */
- countObjs = parseInt(results[2]) || 0
-
- getFromIDs(results[1] || [])
- })
+ else if (zkeys) {
+ if (zkeys.length > 1) {
+ multi.discard()
+
+ var transaction = client.multi()
+ //生成一个随机的keys作为临时查询结果存储地
+ , tempStoreKey = '_tmp_ix_' + (+new Date()).toString(36) + (Math.random() * 100000000 | 0).toString(36)
+
+ zkeys.unshift(tempStoreKey, zkeys.length)
+ transaction.zinterstore.apply(transaction, zkeys)
+
+ desc === true
+ ? transaction.zrevrange(conditionKey, from, to)
+ : transaction.zrange(conditionKey, from, to)
+
+ transaction.zcard(tempStoreKey)
+ transaction.del(tempStoreKey)
+ transaction.exec(function(err, results) {
+ if (err) {
+ cb && cb(err)
+ return
+ }
- //console.log(transaction.queue)
+ /*
+ 事务结果排序:
+ 0: zinterstore 指令对应的结果
+ 1: zrange/ zrevrange 查询结果集
+ 2: zcard 查看临时结果集合的元素个数
+ 3: del 删除临时结果集合
+ */
+ countObjs = parseInt(results[2]) || 0
+ getFromIDs(results[1] || [])
+ })
+
+ //console.log(transaction.queue)
+ } else {
+ multi.zcard(conditionKey)
+ multi.exec(function(err, results) {
+ countObjs = parseInt(results[1]) || 0
+ getFromIDs(results[0] || [])
+ })
+ }
}
/*
@@ -973,7 +979,50 @@ var select = function(schemaName, where, cb, options) {
resultHandler([keys])
})
- } else {
+ }
+
+ /*
+ 若是含有ID字段
+ */
+ else if (_idValue) {
+ var key = schemaName + ':' + _idValue
+
+ client.hgetall(key, function(err, object) {
+ if (err) {
+ return cb && cb(err)
+ }
+
+ object = object || {}
+
+ for (var i = 0; i < conditions.length; i++) {
+ var field = conditions[i]
+ , value = where[field]
+
+ if (object[field] != value) {
+ if (options.mode == 'remove') {
+ cb && cb(null, 0, 0)
+ } else {
+ cb && cb(null, [], 0)
+ }
+ return
+ }
+ }
+
+ if (options.mode == 'remove' && object) {
+ removeRelate(schemaName, object, function(err, result) {
+ if (err) {
+ console.error(err)
+ }
+ client.del(key, cb)
+ })
+ } else {
+ countObjs = 1
+ cb && cb(null, [object], 1)
+ }
+ })
+ }
+
+ else {
conditions.forEach(conditionHandler)
}
}
@@ -310,6 +310,20 @@ describe('Redblade 测试用例', function() {
})
})
+ it('Redblade: 查询带ID的document', function(done) {
+ redblade.select('document', {
+ docid : '3'
+ , poster : 'airjd'
+ , folder : 'var'
+ , keywords : 'NoSQL'
+ }, function(err, objs) {
+ assert.equal(err, null)
+ assert.equal(objs[0].content, 'document 3')
+ done()
+ })
+ })
+
+
it('Redblade: 查询不存在的字段 fodler (应该为folder)', function(done) {
redblade.select('document', { poster:'ourjs', fodler: 'www' }, function(err, documents) {
assert.equal(documents.length, 0)
@@ -326,7 +340,6 @@ describe('Redblade 测试用例', function() {
it('Redblade: 插入大量document数据以便查询', function(done) {
-
for (var i = 0; i < 1000; i++) {
redblade.insert('document', {
docid : 'TEST' + i
@@ -350,10 +363,17 @@ describe('Redblade 测试用例', function() {
done()
}, 400)
})
-
})
+ it('Redblade: 查询一个条件(有序)', function(done) {
+ redblade.select('document', { poster: 'airjd' }, function(err, docs) {
+ assert.equal(err, null)
+ assert.ok(docs.length > 1)
+ done()
+ })
+ })
+
it('Redblade: select 前后数据并比较', function(done) {
var where = { poster: 'airjd', folder: 'var', keywords: 'NoSQL' }
@@ -473,6 +493,14 @@ describe('Redblade 测试用例', function() {
}, { from: 0, to: 10 })
})
})
+
+ it('Redblade: 查询一个条件(无序)', function(done) {
+ redblade.select('people', { gender: '' }, function(err, docs) {
+ assert.equal(err, null)
+ assert.ok(docs.length > 1)
+ done()
+ })
+ })
})
@@ -541,7 +569,6 @@ describe('Redblade 测试用例', function() {
it('redblade:remove 检测删除文章后是否从关键字集合中移除,如 articlekeys:NoSQL', function(done) {
redblade.remove('article', { poster: 'airjd', keywords: 'NoSQL' }, function(err, num) {
redblade.client.zrange('articlekeys:NoSQL', 0, 100, function(err, members) {
- console.log(members)
assert.equal(members.length, 0)
done()
})
@@ -552,7 +579,6 @@ describe('Redblade 测试用例', function() {
it('删除自动创建的索引后查看数据库集是否为空', function(done) {
redblade.client.del('articlekeys', 'articlekeys:NoSQL', 'articlekeys:信息技术', 'articlekeys:JavaScript', function() {
redblade.client.keys('*', function(err, keys) {
- console.log(keys)
assert.equal(keys.length, 0)
done()
})

0 comments on commit 5e8da9b

Please sign in to comment.