mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 04:33:05 +00:00
1、修改mongo_collection:createIndex的bug
2、增加mongo_collection:drop和mongo_collection:dropIndex(方便测试代码清理环境) 3、根据自己项目用到的功能,增加了测试代码
This commit is contained in:
@@ -282,25 +282,39 @@ end
|
|||||||
|
|
||||||
-- collection:createIndex({username = 1}, {unique = true})
|
-- collection:createIndex({username = 1}, {unique = true})
|
||||||
function mongo_collection:createIndex(keys, option)
|
function mongo_collection:createIndex(keys, option)
|
||||||
local name
|
local name = option.name
|
||||||
for k, v in pairs(keys) do
|
option.name = nil
|
||||||
assert(v == 1)
|
|
||||||
name = (name == nil) and k or (name .. "_" .. k)
|
if not name then
|
||||||
|
for k, v in pairs(keys) do
|
||||||
|
name = (name == nil) and k or (name .. "_" .. k)
|
||||||
|
name = name .. "_" .. v
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
local doc = {};
|
local doc = {};
|
||||||
doc.name = name
|
doc.name = name
|
||||||
doc.key = keys
|
doc.key = keys
|
||||||
for k, v in pairs(option) do
|
for k, v in pairs(option) do
|
||||||
if v then
|
doc[k] = v
|
||||||
doc[k] = true
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
return self.database:runCommand("createIndexes", self.name, "indexes", {doc})
|
return self.database:runCommand("createIndexes", self.name, "indexes", {doc})
|
||||||
end
|
end
|
||||||
|
|
||||||
mongo_collection.ensureIndex = mongo_collection.createIndex;
|
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}}, })
|
-- collection:findAndModify({query = {name = "userid"}, update = {["$inc"] = {nextid = 1}}, })
|
||||||
-- keys, value type
|
-- keys, value type
|
||||||
-- query, table
|
-- query, table
|
||||||
|
|||||||
90
test/testmongodb.lua
Normal file
90
test/testmongodb.lua
Normal file
@@ -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)
|
||||||
Reference in New Issue
Block a user