Skip to content

Commit

Permalink
orm增加table类型 ,orm mysql改为prepare预处理执行方式
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Oct 10, 2024
1 parent 5d46326 commit 3dd2edf
Show file tree
Hide file tree
Showing 8 changed files with 1,006 additions and 349 deletions.
71 changes: 70 additions & 1 deletion lualib/skynet-fly/db/mysqlf.lua
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
local skynet = require "skynet"
local contriner_client = require "skynet-fly.client.contriner_client"
local mysql = require "skynet.db.mysql"
local log = require "skynet-fly.log"
local timer = require "skynet-fly.timer"

local assert = assert
local setmetatable = setmetatable
local pcall = pcall
local next = next

contriner_client:register("mysql_m")
contriner_client:register("mysql_m", "share_config_m")

local g_instance = nil
local g_instance_map = {}

local M = {}
local mt = {__index = M}

---------------------------------mysql_m--------------------------------------------

function M:new(db_name)
local client = contriner_client:new("mysql_m",db_name)
local t = {
Expand Down Expand Up @@ -52,4 +59,66 @@ function M:max_packet_size()
end
end

---------------------------------mysql_m--------------------------------------------

---------------------------------本服直连模式----------------------------------------
local function keep_alive(week_t)
local t = next(week_t)
if not t then return end
local conn = t.conn
if conn then
local ok,ret = pcall(conn.ping, conn)
if not ok then
log.error("keep_alive err ", ret)
end
else
log.error("keep_alive not conn ", conn)
end
end

local g_lmt = {__gc = function(t)
if t.keep_time then
t.keep_time:cancel()
end
end}

local week_mt = {__mode = "kv"}

function M.l_new_client(db_name)
local cli = contriner_client:new('share_config_m')
local conf_map = cli:mod_call('query','mysql')
assert(conf_map and conf_map[db_name],"not mysql conf")

local conf = conf_map[db_name]
local database = conf.database
conf.database = nil
local conn = mysql.connect(conf)
conn:query('CREATE DATABASE IF NOT EXISTS ' .. database .. ';')
conn:disconnect()
conf.database = database
conn = mysql.connect(conf)

local t = {
conf = conf,
conn = conn,
}

local week_t = setmetatable({}, week_mt)
week_t[t] = true

t.keep_time = timer:new(timer.second * 10,timer.loop, keep_alive, week_t)
t.keep_time:after_next()

setmetatable(t, g_lmt)
return t
end

function M.l_instance(db_name)
if not g_instance_map[db_name] then
g_instance_map[db_name] = M.l_new_client(db_name)
end

return g_instance_map[db_name]
end

return M
62 changes: 36 additions & 26 deletions lualib/skynet-fly/db/orm/ormtable.lua
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ local FIELD_TYPE = {

text = 51,
blob = 52,
table = 53,
}

local INVAILD_POINT = {count = 0, total_count = 0} --无效叶点
Expand All @@ -68,6 +69,7 @@ local FIELD_LUA_DEFAULT = {

[FIELD_TYPE.text] = "",
[FIELD_TYPE.blob] = "",
[FIELD_TYPE.table] = {},
}

local function create_check_str(len)
Expand Down Expand Up @@ -98,6 +100,7 @@ local FIELD_TYPE_CHECK_FUNC = {

[FIELD_TYPE.text] = function(str) return type(str) == 'string' end,
[FIELD_TYPE.blob] = function(str) return type(str) == 'string' end,
[FIELD_TYPE.table] = function(tab) return type(tab) == 'table' end,
}

local function add_field_name_type(t,field_name,field_type)
Expand Down Expand Up @@ -355,7 +358,11 @@ local function init_entry_data(t, entry_data, is_old)
if entry_data[fn] then
new_entry_data[fn] = entry_data[fn]
else
new_entry_data[fn] = FIELD_LUA_DEFAULT[ft]
if ft ~= FIELD_TYPE.table then
new_entry_data[fn] = FIELD_LUA_DEFAULT[ft]
else
new_entry_data[fn] = {}
end
end
end
return new_entry_data
Expand Down Expand Up @@ -752,35 +759,38 @@ local function get_entry_by_in(t, in_values, key_values)
else
key_values[kv_len + 1] = nil
local entry_data_list = t._adapterinterface:get_entry_by_in(in_values, key_values)
if not entry_data_list or not next(entry_data_list) then
--添加无效条目站位,防止缓存穿透
for i = #in_values, 1, -1 do
local v = in_values[i]
key_values[kv_len + 1] = v
local invaild_entry = create_invaild_entry(t, key_values)
add_key_select(t, invaild_entry)
set_total_count(t, key_values, 0)
end
return res_entry_list, false
else
local in_v_count_map = {}
for i = 1,#entry_data_list do
local entry_data = init_entry_data(t, entry_data_list[i], true)
local entry = ormentry:new(t, entry_data)
tinsert(res_entry_list, add_key_select(t, entry))
local inv = entry_data[in_field_name]
if not in_v_count_map[inv] then
in_v_count_map[inv] = 0
end
in_v_count_map[inv] = in_v_count_map[inv] + 1
local in_v_count_map = {}
local in_v_cnt = 0
for i = 1,#entry_data_list do
local entry_data = init_entry_data(t, entry_data_list[i], true)
local entry = ormentry:new(t, entry_data)
tinsert(res_entry_list, add_key_select(t, entry))
local inv = entry_data[in_field_name]
if not in_v_count_map[inv] then
in_v_count_map[inv] = 0
in_v_cnt = in_v_cnt + 1
end
in_v_count_map[inv] = in_v_count_map[inv] + 1
end

for inv, count in pairs(in_v_count_map) do
key_values[kv_len + 1] = inv
set_total_count(t, key_values, count)
for inv, count in pairs(in_v_count_map) do
key_values[kv_len + 1] = inv
set_total_count(t, key_values, count)
end

--添加无效条目站位,防止缓存穿透
if in_v_cnt ~= #in_values then
for i = 1, #in_values do
local v = in_values[i]
if not in_v_count_map[v] then
key_values[kv_len + 1] = v
local invaild_entry = create_invaild_entry(t, key_values)
add_key_select(t, invaild_entry)
set_total_count(t, key_values, 0)
end
end
return res_entry_list, false
end
return res_entry_list, false
end
end

Expand Down
Loading

0 comments on commit 3dd2edf

Please sign in to comment.