mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
mongo.sort
This commit is contained in:
@@ -396,16 +396,21 @@ function mongo_collection:find(query, selector)
|
|||||||
} , cursor_meta)
|
} , cursor_meta)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local function unfold(list, key, ...)
|
||||||
|
if key == nil then
|
||||||
|
return list
|
||||||
|
end
|
||||||
|
local next_func, t = pairs(key)
|
||||||
|
local k, v = next_func(t) -- The first key pair
|
||||||
|
table.insert(list, k)
|
||||||
|
table.insert(list, v)
|
||||||
|
return unfold(list, ...)
|
||||||
|
end
|
||||||
|
|
||||||
-- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1})
|
-- cursor:sort { key = 1 } or cursor:sort( {key1 = 1}, {key2 = -1})
|
||||||
function mongo_cursor:sort(key, key_v, ...)
|
function mongo_cursor:sort(key, key_v, ...)
|
||||||
if key_v then
|
if key_v then
|
||||||
local key_list = {}
|
local key_list = unfold({}, key, key_v , ...)
|
||||||
for _, kp in ipairs {key, key_v, ...} do
|
|
||||||
local next_func, t = pairs(kp)
|
|
||||||
local k, v = next_func(t, v) -- The first key pair
|
|
||||||
table.insert(key_list, k)
|
|
||||||
table.insert(key_list, v)
|
|
||||||
end
|
|
||||||
key = bson_encode_order(table.unpack(key_list))
|
key = bson_encode_order(table.unpack(key_list))
|
||||||
end
|
end
|
||||||
self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key}
|
self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key}
|
||||||
|
|||||||
@@ -158,7 +158,7 @@ local function dispatch_by_order(self)
|
|||||||
local func, co = pop_response(self)
|
local func, co = pop_response(self)
|
||||||
if not co then
|
if not co then
|
||||||
-- close signal
|
-- close signal
|
||||||
wakeup_all(self, errmsg)
|
wakeup_all(self, "channel_closed")
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
local ok, result_ok, result_data, padding = pcall(func, self.__sock)
|
local ok, result_ok, result_data, padding = pcall(func, self.__sock)
|
||||||
|
|||||||
@@ -12,7 +12,6 @@ end
|
|||||||
|
|
||||||
local function console_main_loop()
|
local function console_main_loop()
|
||||||
local stdin = socket.stdin()
|
local stdin = socket.stdin()
|
||||||
socket.lock(stdin)
|
|
||||||
while true do
|
while true do
|
||||||
local cmdline = socket.readline(stdin, "\n")
|
local cmdline = socket.readline(stdin, "\n")
|
||||||
local split = split_cmdline(cmdline)
|
local split = split_cmdline(cmdline)
|
||||||
@@ -23,7 +22,6 @@ local function console_main_loop()
|
|||||||
pcall(skynet.newservice, cmdline)
|
pcall(skynet.newservice, cmdline)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
socket.unlock(stdin)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
skynet.start(function()
|
skynet.start(function()
|
||||||
|
|||||||
@@ -7,10 +7,18 @@ local host, db_name = ...
|
|||||||
function test_insert_without_index()
|
function test_insert_without_index()
|
||||||
local db = mongo.client({host = host})
|
local db = mongo.client({host = host})
|
||||||
|
|
||||||
|
|
||||||
|
local r = db:runCommand("buildInfo",1)
|
||||||
|
for k, v in pairs(r) do
|
||||||
|
print(k,v)
|
||||||
|
end
|
||||||
db[db_name].testdb:dropIndex("*")
|
db[db_name].testdb:dropIndex("*")
|
||||||
db[db_name].testdb:drop()
|
db[db_name].testdb:drop()
|
||||||
|
|
||||||
local ret = db[db_name].testdb:safe_insert({test_key = 1});
|
local ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||||
|
for k,v in pairs(ret) do
|
||||||
|
print(k,v)
|
||||||
|
end
|
||||||
assert(ret and ret.n == 1)
|
assert(ret and ret.n == 1)
|
||||||
|
|
||||||
local ret = db[db_name].testdb:safe_insert({test_key = 1});
|
local ret = db[db_name].testdb:safe_insert({test_key = 1});
|
||||||
|
|||||||
Reference in New Issue
Block a user