From dab5a4e3081f5184914ef6fbae82ebed42c009a4 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 20 Apr 2017 18:37:27 +0800 Subject: [PATCH] mongo.sort --- lualib/mongo.lua | 19 ++++++++++++------- lualib/socketchannel.lua | 2 +- service/console.lua | 2 -- test/testmongodb.lua | 10 +++++++++- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/lualib/mongo.lua b/lualib/mongo.lua index 3be7d18a..393f4334 100644 --- a/lualib/mongo.lua +++ b/lualib/mongo.lua @@ -396,16 +396,21 @@ function mongo_collection:find(query, selector) } , cursor_meta) 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}) function mongo_cursor:sort(key, key_v, ...) if key_v then - local key_list = {} - 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 + local key_list = unfold({}, key, key_v , ...) key = bson_encode_order(table.unpack(key_list)) end self.__sortquery = bson_encode {['$query'] = self.__query, ['$orderby'] = key} diff --git a/lualib/socketchannel.lua b/lualib/socketchannel.lua index fa72b891..89290ec0 100644 --- a/lualib/socketchannel.lua +++ b/lualib/socketchannel.lua @@ -158,7 +158,7 @@ local function dispatch_by_order(self) local func, co = pop_response(self) if not co then -- close signal - wakeup_all(self, errmsg) + wakeup_all(self, "channel_closed") break end local ok, result_ok, result_data, padding = pcall(func, self.__sock) diff --git a/service/console.lua b/service/console.lua index 90fe62b0..e0ce1453 100644 --- a/service/console.lua +++ b/service/console.lua @@ -12,7 +12,6 @@ end local function console_main_loop() local stdin = socket.stdin() - socket.lock(stdin) while true do local cmdline = socket.readline(stdin, "\n") local split = split_cmdline(cmdline) @@ -23,7 +22,6 @@ local function console_main_loop() pcall(skynet.newservice, cmdline) end end - socket.unlock(stdin) end skynet.start(function() diff --git a/test/testmongodb.lua b/test/testmongodb.lua index bce37683..e8dc5887 100644 --- a/test/testmongodb.lua +++ b/test/testmongodb.lua @@ -7,10 +7,18 @@ local host, db_name = ... function test_insert_without_index() 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:drop() 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) local ret = db[db_name].testdb:safe_insert({test_key = 1}); @@ -85,7 +93,7 @@ function test_expire_index() for i = 1, 1000 do skynet.sleep(11); - + local ret = db[db_name].testdb:findOne({test_key = 1}) if ret == nil then return