summaryrefslogtreecommitdiffstats
path: root/acf/persistence/backends
diff options
context:
space:
mode:
Diffstat (limited to 'acf/persistence/backends')
-rw-r--r--acf/persistence/backends/json.lua49
-rw-r--r--acf/persistence/backends/volatile.lua52
2 files changed, 60 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
diff --git a/acf/persistence/backends/volatile.lua b/acf/persistence/backends/volatile.lua
new file mode 100644
index 0000000..63d7265
--- /dev/null
+++ b/acf/persistence/backends/volatile.lua
@@ -0,0 +1,52 @@
+--[[
+Copyright (c) 2012-2013 Kaarle Ritvanen
+See LICENSE file for license details
+--]]
+
+module(..., package.seeall)
+
+local copy = require('acf.util').copy
+
+
+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(data) self.data = data or {} end
+
+function backend:_get(path)
+ local res = self.data
+ for _, comp in ipairs(path) do
+ if res == nil then return end
+ assert(type(res) == 'table')
+ res = res[tonumber(comp) or comp]
+ end
+ return res
+end
+
+function backend:get(path)
+ local res = self:_get(path)
+ return type(res) == 'table' and keys(res) or res
+end
+
+function backend:_set(path, tpe, value)
+ if tpe == 'table' then value = {} end
+
+ if #path == 0 then self.data = value
+
+ else
+ local comps = copy(path)
+ local name = comps[#comps]
+ table.remove(comps)
+ self:_get(comps)[tonumber(name) or name] = value
+ end
+end
+
+function backend:set(mods)
+ for _, mod in ipairs(mods) do self:_set(unpack(mod)) end
+end