mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
feat: add mongo self batch insert (#1456)
* feat: add mongo self batch insert * perf: add safe batch insert test * perf: modify some words
This commit is contained in:
@@ -365,6 +365,18 @@ function mongo_collection:batch_insert(docs)
|
|||||||
sock:request(pack)
|
sock:request(pack)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function mongo_collection:safe_batch_insert(docs)
|
||||||
|
for i = 1, #docs do
|
||||||
|
if docs[i]._id == nil then
|
||||||
|
docs[i]._id = bson.objectid()
|
||||||
|
end
|
||||||
|
docs[i] = bson_encode(docs[i])
|
||||||
|
end
|
||||||
|
|
||||||
|
local r = self.database:runCommand("insert", self.name, "documents", docs)
|
||||||
|
return werror(r)
|
||||||
|
end
|
||||||
|
|
||||||
function mongo_collection:update(selector,update,upsert,multi)
|
function mongo_collection:update(selector,update,upsert,multi)
|
||||||
local flags = (upsert and 1 or 0) + (multi and 2 or 0)
|
local flags = (upsert and 1 or 0) + (multi and 2 or 0)
|
||||||
local sock = self.connection.__sock
|
local sock = self.connection.__sock
|
||||||
|
|||||||
@@ -168,6 +168,24 @@ function test_expire_index()
|
|||||||
assert(false, "test expire index failed");
|
assert(false, "test expire index failed");
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function test_safe_batch_insert()
|
||||||
|
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)
|
||||||
|
|
||||||
|
local ret = db.testcoll:find()
|
||||||
|
assert(length == ret:count(), "test safe batch insert failed")
|
||||||
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
if username then
|
if username then
|
||||||
print("Test auth")
|
print("Test auth")
|
||||||
@@ -183,5 +201,7 @@ skynet.start(function()
|
|||||||
test_runcommand()
|
test_runcommand()
|
||||||
print("Test expire index")
|
print("Test expire index")
|
||||||
test_expire_index()
|
test_expire_index()
|
||||||
|
print("test safe batch insert")
|
||||||
|
test_safe_batch_insert()
|
||||||
print("mongodb test finish.");
|
print("mongodb test finish.");
|
||||||
end)
|
end)
|
||||||
|
|||||||
Reference in New Issue
Block a user