--[[ Copyright (c) 2012-2013 Kaarle Ritvanen See LICENSE file for license details --]] module(..., package.seeall) local loadmods = require('acf.loader').loadmods local object = require('acf.object') local pth = require('acf.path') local util = require('acf.util') DataStore = object.class(require('acf.transaction.backend').TransactionBackend) function DataStore:init() object.super(self, DataStore):init() self.backends = util.map( function(m) return m.backend() 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, t) local backend, comps = self:split_path(path) local res = backend:get(comps, t) if t ~= nil 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 true or false elseif t == 'reference' then assert(atype == 'string') else assert(false) 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, t, value = unpack(mod) local backend, comps = self:split_path(path) table.insert(util.setdefault(bms, backend, {}), {comps, t, value}) end for backend, bm in pairs(bms) do backend:set(bm) end end