diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-03-20 19:53:51 +0200 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-03-25 23:13:11 +0200 |
commit | f4c5db6fb7d128ba5ed9c2078524f65ae7cce3d3 (patch) | |
tree | 65325dde367a8a373e7ac044fdeba23015523384 | |
parent | 395da378214f4537901860b76c15ab1e405d1d3f (diff) | |
download | aconf-f4c5db6fb7d128ba5ed9c2078524f65ae7cce3d3.tar.bz2 aconf-f4c5db6fb7d128ba5ed9c2078524f65ae7cce3d3.tar.xz |
volatile backend for persistence manager
set as default backend
possibility to switch backend in field definition
-rw-r--r-- | acf/model/init.lua | 19 | ||||
-rw-r--r-- | acf/model/model.lua | 8 | ||||
-rw-r--r-- | acf/model/node.lua | 2 | ||||
-rw-r--r-- | acf/persistence/backends/json.lua | 49 | ||||
-rw-r--r-- | acf/persistence/backends/volatile.lua | 52 |
5 files changed, 74 insertions, 56 deletions
diff --git a/acf/model/init.lua b/acf/model/init.lua index 2800ab6..71d0744 100644 --- a/acf/model/init.lua +++ b/acf/model/init.lua @@ -205,8 +205,9 @@ function Set.save_member(node, k, v) set.add(node, v) end -- experimental Mixed = class(Collection) -function Mixed:init() - super(self, Mixed):init({type=Mixed}, node.Mixed) +function Mixed:init(params) + params.type = Mixed + super(self, Mixed):init(params, node.Mixed) end function Mixed:load(context) @@ -224,14 +225,10 @@ end RootModel = new() -function RootModel:init(txn) super(self, RootModel):init{txn=txn, path='/'} end +function RootModel:init(txn) + super(self, RootModel):init{txn=txn, path='/', addr='/volatile'} +end -function register(name, path, field) - local field = to_field(field) - function field:compute(context) - context.path = '/'..name - context.addr = path - return self:load(context) - end - RootModel[name] = field +function register(name, addr, field) + RootModel[name] = to_field(field, addr) end diff --git a/acf/model/model.lua b/acf/model/model.lua index 7c27b50..0a312a3 100644 --- a/acf/model/model.lua +++ b/acf/model/model.lua @@ -20,9 +20,11 @@ local isinstance = object.isinstance local util = require('acf.util') -function to_field(obj) - if object.issubclass(obj, Model) then return fld.Model{model=obj} end - return getmetatable(obj).class and obj or obj() +function to_field(obj, addr) + if object.issubclass(obj, Model) then + return fld.Model{model=obj, addr=addr} + end + return getmetatable(obj).class and obj or obj{addr=addr} end diff --git a/acf/model/node.lua b/acf/model/node.lua index cf3111d..3960b90 100644 --- a/acf/model/node.lua +++ b/acf/model/node.lua @@ -36,7 +36,7 @@ function BoundField:init(parent, field) txn=pmt.txn, parent=parent, path=pth.join(pmt.path, name), - addr=pmt.addr and pth.join(pmt.addr, name) + addr=field.addr or pth.join(pmt.addr, name) }, unpack(arg) ) 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 |