From f1c9f9b9dc5b4daa1c017ce2533513a826abd23b Mon Sep 17 00:00:00 2001 From: dpull Date: Thu, 11 Dec 2014 10:41:59 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E4=BF=AE=E6=94=B9mongo=5Fcollection:?= =?UTF-8?q?createIndex=E7=9A=84bug=202=E3=80=81=E5=A2=9E=E5=8A=A0mongo=5Fc?= =?UTF-8?q?ollection:drop=E5=92=8Cmongo=5Fcollection:dropIndex=EF=BC=88?= =?UTF-8?q?=E6=96=B9=E4=BE=BF=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81=E6=B8=85?= =?UTF-8?q?=E7=90=86=E7=8E=AF=E5=A2=83=EF=BC=89=203=E3=80=81=E6=A0=B9?= =?UTF-8?q?=E6=8D=AE=E8=87=AA=E5=B7=B1=E9=A1=B9=E7=9B=AE=E7=94=A8=E5=88=B0?= =?UTF-8?q?=E7=9A=84=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=A2=9E=E5=8A=A0=E4=BA=86?= =?UTF-8?q?=E6=B5=8B=E8=AF=95=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lualib/mongo.lua | 28 ++++++++++---- test/testmongodb.lua | 90 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 test/testmongodb.lua diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 38ac20dd..6399465d 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -282,25 +282,39 @@ end -- collection:createIndex({username = 1}, {unique = true}) function mongo_collection:createIndex(keys, option) - local name - for k, v in pairs(keys) do - assert(v == 1) - name = (name == nil) and k or (name .. "_" .. k) + local name = option.name + option.name = nil + + if not name then + for k, v in pairs(keys) do + name = (name == nil) and k or (name .. "_" .. k) + name = name .. "_" .. v + end end + local doc = {}; doc.name = name doc.key = keys for k, v in pairs(option) do - if v then - doc[k] = true - end + doc[k] = v end return self.database:runCommand("createIndexes", self.name, "indexes", {doc}) end mongo_collection.ensureIndex = mongo_collection.createIndex; + +function mongo_collection:drop() + return self.database:runCommand("drop", self.name) +end + +-- collection:dropIndex("age_1") +-- collection:dropIndex("*") +function mongo_collection:dropIndex(indexName) + return self.database:runCommand("dropIndexes", self.name, "index", indexName) +end + -- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, }) -- keys, value type -- query, table diff --git a/test/testmongodb.lua b/test/testmongodb.lua new file mode 100644 index 00000000..f1fa81e0 --- /dev/null +++ b/test/testmongodb.lua @@ -0,0 +1,90 @@ +local skynet = require "skynet" +local mongo = require "mongo" +local bson = require "bson" + +local host, db_name = ... + +function test_insert_without_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}); + assert(ret and ret.n == 1) +end + +function test_insert_with_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 0) +end + +function test_find_and_remove() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index"}) + + local ret = db[db_name].testdb:safe_insert({test_key = 1}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret and ret.test_key == 1) + + db[db_name].testdb:delete({test_key = 1}) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret == nil) +end + + +function test_expire_index() + local db = mongo.client({host = host}) + + db[db_name].testdb:dropIndex("*") + db[db_name].testdb:drop() + + db[db_name].testdb:ensureIndex({test_key = 1}, {unique = true, name = "test_key_index", expireAfterSeconds = 1, }) + db[db_name].testdb:ensureIndex({test_date = 1}, {expireAfterSeconds = 1, }) + + local ret = db[db_name].testdb:safe_insert({test_key = 1, test_date = bson.date(os.time())}) + assert(ret and ret.n == 1) + + local ret = db[db_name].testdb:findOne({test_key = 1}) + assert(ret and ret.test_key == 1) + + for i = 1, 1000 do + skynet.sleep(11); + + local ret = db[db_name].testdb:findOne({test_key = 1}) + if ret == nil then + return + end + end + + assert(false, "test expire index failed"); +end + +skynet.start(function() + test_insert_without_index() + test_insert_with_index() + test_find_and_remove() + test_expire_index() + + print("mongodb test finish."); +end)