mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
add sharedata.deepcopy
This commit is contained in:
@@ -55,4 +55,16 @@ function sharedata.flush()
|
||||
collectgarbage()
|
||||
end
|
||||
|
||||
function sharedata.deepcopy(name, ...)
|
||||
if cache[name] then
|
||||
local cobj = cache[name].__obj
|
||||
return sd.copy(cobj, ...)
|
||||
end
|
||||
|
||||
local cobj = skynet.call(service, "lua", "query", name)
|
||||
local ret = sd.copy(cobj, ...)
|
||||
skynet.send(service, "lua", "confirm" , cobj)
|
||||
return ret
|
||||
end
|
||||
|
||||
return sharedata
|
||||
|
||||
@@ -20,6 +20,7 @@ local isdirty = core.isdirty
|
||||
local index = core.index
|
||||
local needupdate = core.needupdate
|
||||
local len = core.len
|
||||
local core_nextkey = core.nextkey
|
||||
|
||||
local function findroot(self)
|
||||
while self.__parent do
|
||||
@@ -106,7 +107,7 @@ end
|
||||
|
||||
function conf.next(obj, key)
|
||||
local cobj = getcobj(obj)
|
||||
local nextkey = core.nextkey(cobj, key)
|
||||
local nextkey = core_nextkey(cobj, key)
|
||||
if nextkey then
|
||||
return nextkey, obj[nextkey]
|
||||
end
|
||||
@@ -132,4 +133,46 @@ function conf.flush(obj)
|
||||
getcobj(obj)
|
||||
end
|
||||
|
||||
local function clone_table(cobj)
|
||||
local obj = {}
|
||||
local key
|
||||
while true do
|
||||
key = core_nextkey(cobj, key)
|
||||
if key == nil then
|
||||
break
|
||||
end
|
||||
local v = index(cobj, key)
|
||||
if type(v) == "userdata" then
|
||||
v = clone_table(v)
|
||||
end
|
||||
obj[key] = v
|
||||
end
|
||||
return obj
|
||||
end
|
||||
|
||||
local function find_node(cobj, key, ...)
|
||||
if key == nil then
|
||||
return cobj
|
||||
end
|
||||
local cobj = index(cobj, key)
|
||||
if cobj == nil then
|
||||
return nil
|
||||
end
|
||||
if type(cobj) == "userdata" then
|
||||
return find_node(cobj, ...)
|
||||
end
|
||||
return cobj
|
||||
end
|
||||
|
||||
function conf.copy(cobj, ...)
|
||||
cobj = find_node(cobj, ...)
|
||||
if cobj then
|
||||
if type(cobj) == "userdata" then
|
||||
return clone_table(cobj)
|
||||
else
|
||||
return cobj
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
return conf
|
||||
|
||||
Reference in New Issue
Block a user