mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
feat: add mongo safe batch delete (#1577)
This commit is contained in:
@@ -423,6 +423,18 @@ function mongo_collection:safe_delete(selector, single)
|
|||||||
return werror(r)
|
return werror(r)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mongo_collection:safe_batch_delete(selectors, single)
|
||||||
|
local delete_tb = {}
|
||||||
|
for i = 1, #selectors do
|
||||||
|
delete_tb[i] = bson_encode({
|
||||||
|
q = selectors[i],
|
||||||
|
limit = single and 1 or 0,
|
||||||
|
})
|
||||||
|
end
|
||||||
|
local r = self.database:runCommand("delete", self.name, "deletes", delete_tb)
|
||||||
|
return werror(r)
|
||||||
|
end
|
||||||
|
|
||||||
function mongo_collection:findOne(query, selector)
|
function mongo_collection:findOne(query, selector)
|
||||||
local conn = self.connection
|
local conn = self.connection
|
||||||
local request_id = conn:genId()
|
local request_id = conn:genId()
|
||||||
|
|||||||
@@ -186,6 +186,32 @@ local function test_safe_batch_insert()
|
|||||||
assert(length == ret:count(), "test safe batch insert failed")
|
assert(length == ret:count(), "test safe batch insert failed")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function test_safe_batch_delete()
|
||||||
|
local ok, err, ret
|
||||||
|
local c = _create_client()
|
||||||
|
local db = c[db_name]
|
||||||
|
|
||||||
|
db.testcoll:drop()
|
||||||
|
|
||||||
|
local docs, length = {}, 10
|
||||||
|
for i = 1, length do
|
||||||
|
table.insert(docs, {test_key = i})
|
||||||
|
end
|
||||||
|
|
||||||
|
db.testcoll:safe_batch_insert(docs)
|
||||||
|
|
||||||
|
docs = {}
|
||||||
|
local del_num = 5
|
||||||
|
for i = 1, del_num do
|
||||||
|
table.insert(docs, {test_key = i})
|
||||||
|
end
|
||||||
|
|
||||||
|
db.testcoll:safe_batch_delete(docs)
|
||||||
|
|
||||||
|
local ret = db.testcoll:find()
|
||||||
|
assert(length == ret:count(), "test safe batch delete failed")
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
if username then
|
if username then
|
||||||
print("Test auth")
|
print("Test auth")
|
||||||
@@ -203,5 +229,7 @@ skynet.start(function()
|
|||||||
test_expire_index()
|
test_expire_index()
|
||||||
print("test safe batch insert")
|
print("test safe batch insert")
|
||||||
test_safe_batch_insert()
|
test_safe_batch_insert()
|
||||||
|
print("test safe batch delete")
|
||||||
|
test_safe_batch_delete()
|
||||||
print("mongodb test finish.");
|
print("mongodb test finish.");
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user