Skip to content

Commit

Permalink
mongo的pairs编码支持
Browse files Browse the repository at this point in the history
1、mongo的pairs编码支持用于排序
2、修复挤出小BUG
  • Loading branch information
xiyoo0812 committed Nov 2, 2023
1 parent 8a9be57 commit 333d787
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 17 deletions.
16 changes: 16 additions & 0 deletions extend/lbson/src/bson.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ namespace lbson {
return lua_gettop(L);
}

int pairs(lua_State* L) {
m_buffer.clean();
size_t data_len = 0;
bson_value* value = lua_to_object<bson_value*>(L, -1);
if (value == nullptr) {
char* data = (char*)encode_pairs(L, &data_len);
value = new bson_value(bson_type::BSON_DOCUMENT, data, data_len);
} else {
lua_pop(L, 1);
char* data = (char*)encode_pairs(L, &data_len);
value->str = string(data, data_len);
}
lua_push_object(L, value);
return 1;
}

uint8_t* encode_pairs(lua_State* L, size_t* data_len) {
int n = lua_gettop(L);
if (n < 2 || n % 2 != 0) {
Expand Down
8 changes: 8 additions & 0 deletions extend/lbson/src/lbson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ namespace lbson {
static int decode(lua_State* L) {
return thread_bson.decode(L);
}
static int pairs(lua_State* L) {
return thread_bson.pairs(L);
}
static bson_value* doc() {
return new bson_value(bson_type::BSON_DOCUMENT, "");
}
static bson_value* int32(int32_t value) {
return new bson_value(bson_type::BSON_INT64, value);
}
Expand Down Expand Up @@ -48,7 +54,9 @@ namespace lbson {
llbson.set_function("timestamp", timestamp);
llbson.set_function("int32", int32);
llbson.set_function("int64", int64);
llbson.set_function("pairs", pairs);
llbson.set_function("date", date);
llbson.set_function("doc", doc);
kit_state.new_class<bson_value>(
"val", &bson_value::val,
"str", &bson_value::str,
Expand Down
2 changes: 1 addition & 1 deletion extend/lcodec/src/mysql.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ namespace lcodec {
void encode_args_value(lua_State* L, int index) {
switch (lua_type(L, index)) {
case LUA_TBOOLEAN:
m_buf->write<double>(lua_tointeger(L, index));
m_buf->write<uint8_t>(lua_toboolean(L, index) ? 1 : 0);
break;
case LUA_TNUMBER:
lua_isinteger(L, index) ? m_buf->write<uint64_t>(lua_tointeger(L, index)) : m_buf->write<double>(lua_tonumber(L, index));
Expand Down
2 changes: 1 addition & 1 deletion extend/lcodec/src/redis.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ namespace lcodec {
string_encode(L, idx);
break;
case LUA_TBOOLEAN:
integer_encode(lua_tointeger(L, idx));
integer_encode(lua_toboolean(L, idx) ? 1 : 0);
break;
case LUA_TNUMBER:
lua_isinteger(L, idx) ? integer_encode(lua_tointeger(L, idx)) : number_encode(lua_tonumber(L, idx));
Expand Down
8 changes: 4 additions & 4 deletions extend/lcodec/src/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ namespace lcodec {
body = (uint8_t*)lua_tolstring(L, index + 1, len);
}
m_buf->write<uint8_t>((0x80 | opcode));
if (*len < 126) {
if (*len < 0x7e) {
m_buf->write<uint8_t>(*len);
} else if (*len < 0xffff) {
m_buf->write<uint8_t>(126);
} else if (*len <= 0xffff) {
m_buf->write<uint8_t>(0x7e);
m_buf->write<uint16_t>(byteswap2(*len));
} else {
m_buf->write<uint8_t>(127);
m_buf->write<uint8_t>(0x7f);
m_buf->write<uint64_t>(byteswap8(*len));
}
m_buf->push_data(body, *len);
Expand Down
2 changes: 1 addition & 1 deletion extend/lualog/lualog/lualog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace logger {
case LUA_TUSERDATA: return "userdata";
case LUA_TLIGHTUSERDATA: return "userdata";
case LUA_TSTRING: return lua_tostring(L, index);
case LUA_TBOOLEAN: return (lua_tointeger(L, index) == 1) ? "true" : "false";
case LUA_TBOOLEAN: return lua_toboolean(L, index) ? "true" : "false";
case LUA_TTABLE:
if ((flag & 0x01) == 0x01) {
buf.clean();
Expand Down
36 changes: 26 additions & 10 deletions script/driver/mongo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local log_err = logger.err
local log_info = logger.info
local log_warn = logger.warn
local tinsert = table.insert
local tunpack = table.unpack
local qjoin = qtable.join
local tdelete = qtable.delete
local ssub = string.sub
Expand All @@ -20,6 +21,7 @@ local makechan = quanta.make_channel

local lmd5 = crypt.md5
local lsha1 = crypt.sha1
local bsonpairs = bson.pairs
local lrandomkey = crypt.randomkey
local lb64encode = crypt.b64_encode
local lb64decode = crypt.b64_decode
Expand Down Expand Up @@ -49,8 +51,8 @@ prop:reader("salted_pass", nil) --salted_pass
prop:reader("executer", nil) --执行者
prop:reader("timer_id", nil) --timer_id
prop:reader("cursor_id", nil) --cursor_id
prop:reader("sort_doc", nil) --sort_doc
prop:reader("connections", {}) --connections
prop:reader("readpref", nil) --readPreference
prop:reader("alives", {}) --alives
prop:reader("req_counter", nil)
prop:reader("res_counter", nil)
Expand All @@ -60,6 +62,7 @@ function MongoDB:__init(conf, id)
self.name = conf.db
self.user = conf.user
self.passwd = conf.passwd
self.sort_doc = bson.doc()
self.cursor_id = bson.int64(0)
self.codec = bson.mongocodec()
self:set_options(conf.opts)
Expand Down Expand Up @@ -87,11 +90,6 @@ function MongoDB:close()
end

function MongoDB:set_options(opts)
for key, value in pairs(opts) do
if key == "readPreference" then
self.readpref = { mode = value }
end
end
end

function MongoDB:setup_pool(hosts)
Expand Down Expand Up @@ -329,8 +327,12 @@ function MongoDB:drop_collection(co_name)
end

-- 参数说明
-- indexes={{key={open_id=1,platform_id=1},name="open_id-platform_id",unique=true}, }
-- indexes: {{key={open_id=1}, name="open_id", unique=true} }
-- indexes: {{key={open_id,1,platform_id,1}, name="open_id-platform_id", unique=true} }
function MongoDB:create_indexes(co_name, indexes)
for _, index in pairs(indexes) do
index.key = self:format_pairs(index.key)
end
local succ, doc = self:runCommand("createIndexes", co_name, "indexes", indexes)
if not succ then
return succ, doc
Expand Down Expand Up @@ -369,7 +371,7 @@ function MongoDB:count(co_name, query, limit, skip)
end

function MongoDB:find_one(co_name, query, projection)
local succ, reply = self:runCommand("find", co_name, "$readPreference", self.readpref, "filter", query, "projection", projection, "limit", 1)
local succ, reply = self:runCommand("find", co_name, "filter", query, "projection", projection, "limit", 1)
if not succ then
return succ, reply
end
Expand All @@ -380,9 +382,23 @@ function MongoDB:find_one(co_name, query, projection)
return succ
end

function MongoDB:format_pairs(args, doc)
if args then
if type(next(args)) == "string" then
return args
end
if doc then
tinsert(args, doc)
end
return bsonpairs(tunpack(args))
end
end

-- 参数说明
--sort: {k1=1} / {k1,1,k2,-1,k3,-1}
function MongoDB:find(co_name, query, projection, sortor, limit, skip)
local succ, reply = self:runCommand("find", co_name, "$readPreference", self.readpref, "filter",
query, "projection", projection, "sort", sortor, "limit", limit, "skip", skip)
local fsortor = self:format_pairs(sortor, self.sort_doc)
local succ, reply = self:runCommand("find", co_name, "filter", query, "projection", projection, "sort", fsortor, "limit", limit, "skip", skip)
if not succ then
return succ, reply
end
Expand Down

0 comments on commit 333d787

Please sign in to comment.