Skip to content
This repository has been archived by the owner on Dec 30, 2023. It is now read-only.

Commit

Permalink
refactor entity
Browse files Browse the repository at this point in the history
  • Loading branch information
fztcjjl committed Oct 23, 2015
1 parent 2a889b6 commit 1fac559
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 99 deletions.
18 changes: 9 additions & 9 deletions common/datacenter/accountdc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@ local skynet = require "skynet"
local snax = require "snax"
local EntityFactory = require "EntityFactory"

local EntAccount
local entAccount

function init(...)
EntAccount = EntityFactory.Get("d_account")
EntAccount:Init()
EntAccount:Load()
entAccount = EntityFactory.get("d_account")
entAccount:init()
entAccount:load()
end

function exit(...)

end

function response.add(row)
return EntAccount:Add(row)
return entAccount:add(row)
end

function response.delete(row)
return EntAccount:Delete(row)
return entAccount:delete(row)
end

function response.get(sdkid, pid)
return EntAccount:Get(sdkid, pid)
return entAccount:get(sdkid, pid)
end

function response.update(row)
return EntAccount:Update(row)
return entAccount:update(row)
end

function response.get_nextid()
return EntAccount:GetNextId()
return entAccount:getNextId()
end
36 changes: 18 additions & 18 deletions common/datacenter/userdc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,69 @@ local skynet = require "skynet"
local snax = require "snax"
local EntityFactory = require "EntityFactory"

local EntUser
local EntUserCustom
local entUser
local entUserCustom

function init(...)
EntUser = EntityFactory.Get("d_user")
EntUser:Init()
entUser = EntityFactory.get("d_user")
entUser:init()

EntUserCustom = EntityFactory.Get("d_user_custom")
EntUserCustom:Init()
EntUserCustom:Load()
entUserCustom = EntityFactory.get("d_user_custom")
entUserCustom:init()
entUserCustom:load()
end

function exit(...)
end

function response.load(uid)
if not uid then return end
EntUser:Load(uid)
entUser:load(uid)
end

function response.unload(uid)
if not uid then return end
EntUser:UnLoad(uid)
entUser:unload(uid)
end


function response.getvalue(uid, key)
return EntUser:GetValue(uid, key)
return entUser:getValue(uid, key)
end

function response.setvalue(uid, key, value)
return EntUser:SetValue(uid, key, value)
return entUser:setValue(uid, key, value)
end


function response.add(row)
return EntUser:Add(row)
return entUser:add(row)
end

function response.delete(row)
return EntUser:Delete(row)
return entUser:delete(row)
end

function response.user_addvalue(uid, key, n)
local value = EntUser:GetValue(uid, key)
local value = entUser:getValue(uid, key)
value = value + n
local ret = EntUser:SetValue(uid, key, value)
local ret = entUser:setValue(uid, key, value)
return ret, value
end

function response.get(uid)
return EntUser:Get(uid)
return entUser:get(uid)
end

function response.check_rolename_exists(name)
if table.empty(EntUserCustom:Get(name)) then
if table.empty(entUserCustom:get(name)) then
return false
end
return true
end

function response.check_role_exists(uid)
if not EntUser:GetValue(uid, "uid") then
if not entUser:getValue(uid, "uid") then
return false
end
return true
Expand Down
39 changes: 18 additions & 21 deletions common/entitybase/CommonEntity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,12 @@ function CommonEntity:ctor()
self.type = 3
end

function CommonEntity:Init()
function CommonEntity:init()
self.pk, self.key, self.indexkey = skynet.call("dbmgr", "lua", "get_table_key", self.tbname, self.type)
end

function CommonEntity:dtor()
end

-- 加载整张表数据
function CommonEntity:Load()
function CommonEntity:load()
if table.empty(self.recordset) then
local rs = skynet.call("dbmgr", "lua", "get_common", self.tbname)
if rs then
Expand All @@ -42,19 +39,19 @@ end

-- row中包含pk字段,row为k,v形式table
-- 内存中不存在,则添加,并同步到redis
function CommonEntity:Add(row, nosync)
function CommonEntity:add(row, nosync)
if row.id and self.recordset[row.id] then return end -- 记录已经存在,返回

local id = row[self.pk]
if not id or id == 0 then
id = self:GetNextId()
id = self:getNextId()
row[self.pk] = id
end

local ret = skynet.call("dbmgr", "lua", "add", self.tbname, row, self.type, nosync)

if ret then
key = self:GetKey(row)
key = self:getKey(row)
self.recordset[key] = row
end

Expand All @@ -63,14 +60,14 @@ end

-- row中包含pk字段,row为k,v形式table
-- 从内存中删除,并同步到redis
function CommonEntity:Delete(row, nosync)
function CommonEntity:delete(row, nosync)
local id = row[self.pk]
if not self.recordset[id] then return end -- 记录不存在,返回

local ret = skynet.call("dbmgr", "lua", "delete", self.tbname, row, self.type, nosync)

if ret then
key = self:GetKey(row)
key = self:getKey(row)
self.recordset[key] = nil
end

Expand All @@ -79,26 +76,26 @@ end

-- row中包含pk字段,row为k,v形式table
-- 仅从内存中移除,但不同步到redis
function CommonEntity:Remove(row)
function CommonEntity:remove(row)
local id = row[self.pk]
if not self.recordset[id] then return end -- 记录不存在,返回

key = self:GetKey(row)
key = self:getKey(row)
self.recordset[key] = nil

return true
end

-- row中包含pk字段,row为k,v形式table
function CommonEntity:Update(row, nosync)
function CommonEntity:update(row, nosync)
local id = row[self.pk]
if not self.recordset[id] then return end -- 记录不存在,返回


local ret = skynet.call("dbmgr", "lua", "update", self.tbname, row, self.type, nosync)

if ret then
key = self:GetKey(row)
key = self:getKey(row)
for k, v in pairs(row) do
self.recordset[key][k] = v
end
Expand All @@ -107,7 +104,7 @@ function CommonEntity:Update(row, nosync)
return true
end

function CommonEntity:Get(...)
function CommonEntity:get(...)
local t = { ... }
assert(#t > 0)
local key
Expand All @@ -132,21 +129,21 @@ function CommonEntity:Set(id, row)
end
--]]

function CommonEntity:GetValue(id, field)
local record = self:Get(id)
function CommonEntity:getValue(id, field)
local record = self:get(id)
if not record then
return record[field]
end
end

function CommonEntity:SetValue(id, field, data)
function CommonEntity:setValue(id, field, data)
local record = {}
record[self.pkfield] = id
record[field] = data
self:Update(record)
self:update(record)
end

function CommonEntity:GetKey(row)
function CommonEntity:getKey(row)
local fields = string.split(self.key, ",")
local key
for i=1, #fields do
Expand All @@ -160,7 +157,7 @@ function CommonEntity:GetKey(row)
return tonumber(key) or key
end

function CommonEntity:GetAll( )
function CommonEntity:getAll( )
return self.recordset
end

Expand Down
12 changes: 4 additions & 8 deletions common/entitybase/ConfigEntity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,11 @@ function ConfigEntity:ctor()
self.type = 1
end

function ConfigEntity:dtor()
function ConfigEntity:init()

end

function ConfigEntity:Init()

end

function ConfigEntity:Load()
function ConfigEntity:load()
--if table.empty(self.recordset) then
local rs = skynet.call("dbmgr", "lua", "get_config", self.tbname)
if rs then
Expand All @@ -26,7 +22,7 @@ function ConfigEntity:Load()
--end
end

function ConfigEntity:Get(...)
function ConfigEntity:get(...)
local t = { ... }
assert(#t > 0)
local key
Expand All @@ -46,7 +42,7 @@ function ConfigEntity:Get(...)
end


function ConfigEntity:GetAll( )
function ConfigEntity:getAll( )
return self.recordset
end

Expand Down
7 changes: 2 additions & 5 deletions common/entitybase/Entity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,12 @@ function Entity:ctor()
self.type = 0 -- 表类型:1、config,2、user,3、common
end

function Entity:dtor()
end

-- 获取redis下一个编号
function Entity:GetNextId()
function Entity:getNextId()
return do_redis({ "incr", self.tbname .. ":" .. self.pk })
end

function Entity:Init()
function Entity:init()
self.pk, self.key, self.indexkey = skynet.call("dbmgr", "lua", "get_table_key", self.tbname, self.type)
--self.tbschema = skynet.call("dbmgr", "lua", "get_schema", self.tbname)
end
Expand Down
2 changes: 1 addition & 1 deletion common/entitybase/EntityFactory.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ local M = {}
local entities = {} -- 保存实体对象

-- 工厂方法,获取具体对象,name为表名
function M.Get(name)
function M.get(name)
if entities[name] then
return entities[name]
end
Expand Down
3 changes: 0 additions & 3 deletions common/entitybase/UserEntity.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,4 @@ function UserEntity:ctor()
self.type = 2
end

function UserEntity:dtor()
end

return UserEntity
Loading

0 comments on commit 1fac559

Please sign in to comment.