diff options
Diffstat (limited to 'acf/persistence/backends/json.lua')
-rw-r--r-- | acf/persistence/backends/json.lua | 49 |
1 files changed, 8 insertions, 41 deletions
diff --git a/acf/persistence/backends/json.lua b/acf/persistence/backends/json.lua index 6500d13..299cf72 100644 --- a/acf/persistence/backends/json.lua +++ b/acf/persistence/backends/json.lua @@ -6,6 +6,7 @@ See LICENSE file for license details module(..., package.seeall) local pth = require('acf.path') +local Cache = require('acf.persistence.backends.volatile').backend local util = require('acf.persistence.util') local copy = require('acf.util').copy @@ -13,13 +14,6 @@ require 'json' require 'lfs' -local function keys(tbl) - local res = {} - for k, v in pairs(tbl) do table.insert(res, k) end - return res -end - - backend = require('acf.object').class() function backend:init() @@ -57,54 +51,27 @@ function backend:split_path(path) end end -function backend:_get(path) +function backend:get(path) local fpath, jpath = self:split_path(path) - if not self.cache[fpath] then - self.cache[fpath] = json.decode(util.read_file(fpath)) - end - - local res = self.cache[fpath] - - while #jpath > 0 do - if res == nil then return end - assert(type(res) == 'table') - local next = jpath[1] - res = res[tonumber(next) or next] - table.remove(jpath, 1) + self.cache[fpath] = Cache(json.decode(util.read_file(fpath))) end - - return res -end - -function backend:get(path) - local res = self:_get(path) - return type(res) == 'table' and keys(res) or res + return self.cache[fpath]:get(jpath) end function backend:set(mods) local dirty = {} for _, mod in ipairs(mods) do - local p, t, value = unpack(mod) - local fpath, jpath = self:split_path(p) - if t == 'table' then value = {} end - - if #jpath == 0 then self.cache[fpath] = value - - else - local comps = copy(p) - local name = comps[#comps] - table.remove(comps) - self:_get(comps)[tonumber(name) or name] = value - end - + local path, tpe, value = unpack(mod) + local fpath, jpath = self:split_path(path) + self.cache[fpath]:_set(jpath, tpe, value) dirty[fpath] = true end for path, _ in pairs(dirty) do local file = util.open_file(path, 'w') - file:write(json.encode(self.cache[path])) + file:write(json.encode(self.cache[path]:_get{})) file:close() end end |