add sharedata.deepcopy

This commit is contained in:
Cloud Wu
2016-11-14 13:05:01 +08:00
parent ffe5de468e
commit c5fb8ef3f7
2 changed files with 56 additions and 1 deletions

View File

@@ -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

View File

@@ -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