diff options
author | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-10-08 18:50:56 +0300 |
---|---|---|
committer | Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi> | 2013-10-08 18:50:56 +0300 |
commit | 33728ad3382d74281412d4556561d479bb88832b (patch) | |
tree | e8a9b2798dec96d820715f729989e91f9e7d5e12 /acf2/persistence/init.lua | |
parent | 3e48dd63e8bdf0c2641cfb73e6b20bea8c466ff8 (diff) | |
download | aconf-2b81c00bf56616357997f3ec41c9337fec0da8c0.tar.bz2 aconf-2b81c00bf56616357997f3ec41c9337fec0da8c0.tar.xz |
changed module paths from acf to acf2v0.1.0
Diffstat (limited to 'acf2/persistence/init.lua')
-rw-r--r-- | acf2/persistence/init.lua | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/acf2/persistence/init.lua b/acf2/persistence/init.lua new file mode 100644 index 0000000..1dca61d --- /dev/null +++ b/acf2/persistence/init.lua @@ -0,0 +1,79 @@ +--[[ +Copyright (c) 2012-2013 Kaarle Ritvanen +See LICENSE file for license details +--]] + +local M = {} + +local loadmods = require('acf2.loader') +local topology = require('acf2.model.root').topology +local object = require('acf2.object') +local pth = require('acf2.path') +local util = require('acf2.util') + +local stringy = require('stringy') + + +local DataStore = object.class( + require('acf2.transaction.backend').TransactionBackend +) + +function DataStore:init() + object.super(self, DataStore):init() + self.backends = util.map( + function(m) return m() end, + loadmods('persistence/backends') + ) +end + +function DataStore:split_path(path) + local comps = pth.split(path) + local backend = self.backends[comps[1]] + assert(backend) + table.remove(comps, 1) + return backend, comps +end + +function DataStore:get(path) + local backend, comps = self:split_path(path) + local top = topology(path) + + local res = backend:get(comps, top) + + if top then + local t = top.type + if t and res ~= nil then + local atype = type(res) + + if t == 'table' then assert(atype == 'table') + + else + assert(atype ~= 'table') + + if t == 'string' then res = tostring(res) + elseif t == 'number' then res = tonumber(res) + elseif t == 'boolean' then + res = (res and res ~= 'false') and true or false + elseif t == 'reference' then assert(atype == 'string') + else assert(false) end + end + end + end + + return util.copy(res), self.mod_time[path] or 0 +end + +function DataStore:_set_multiple(mods) + local bms = {} + + for _, mod in ipairs(mods) do + local path, value = unpack(mod) + local backend, comps = self:split_path(path) + table.insert(util.setdefault(bms, backend, {}), {comps, value}) + end + + for backend, bm in pairs(bms) do backend:set(bm) end +end + + +return DataStore() |