summaryrefslogtreecommitdiffstats
path: root/acf/persistence/init.lua
diff options
context:
space:
mode:
Diffstat (limited to 'acf/persistence/init.lua')
-rw-r--r--acf/persistence/init.lua47
1 files changed, 47 insertions, 0 deletions
diff --git a/acf/persistence/init.lua b/acf/persistence/init.lua
new file mode 100644
index 0000000..f674e9c
--- /dev/null
+++ b/acf/persistence/init.lua
@@ -0,0 +1,47 @@
+--[[
+Copyright (c) 2012 Kaarle Ritvanen
+See LICENSE file for license details
+--]]
+
+module(..., package.seeall)
+
+local object = require('acf.object')
+local super = object.super
+local pth = require('acf.path')
+local util = require('acf.util')
+
+
+DataStore = object.class(require('acf.transaction.backend').TransactionBackend)
+
+function DataStore:init()
+ super(self, DataStore):init()
+ self.backends = util.map(function(m) return m.backend() end,
+ util.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)
+ return util.copy(backend:get(comps)), self.mod_time[path] or 0
+end
+
+function DataStore:set_multiple(mods)
+ super(self, 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)
+ if not bms[backend] then bms[backend] = {} end
+ table.insert(bms[backend], {comps, t, value})
+ end
+
+ for backend, bm in pairs(bms) do backend:set(bm) end
+end