summaryrefslogtreecommitdiffstats
path: root/acf/persistence/backends/json.lua
diff options
context:
space:
mode:
authorKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-08 18:50:56 +0300
committerKaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>2013-10-08 18:50:56 +0300
commit33728ad3382d74281412d4556561d479bb88832b (patch)
treee8a9b2798dec96d820715f729989e91f9e7d5e12 /acf/persistence/backends/json.lua
parent3e48dd63e8bdf0c2641cfb73e6b20bea8c466ff8 (diff)
downloadaconf-33728ad3382d74281412d4556561d479bb88832b.tar.bz2
aconf-33728ad3382d74281412d4556561d479bb88832b.tar.xz
changed module paths from acf to acf2v0.1.0
Diffstat (limited to 'acf/persistence/backends/json.lua')
-rw-r--r--acf/persistence/backends/json.lua79
1 files changed, 0 insertions, 79 deletions
diff --git a/acf/persistence/backends/json.lua b/acf/persistence/backends/json.lua
deleted file mode 100644
index e0cd66e..0000000
--- a/acf/persistence/backends/json.lua
+++ /dev/null
@@ -1,79 +0,0 @@
---[[
-Copyright (c) 2012-2013 Kaarle Ritvanen
-See LICENSE file for license details
---]]
-
-local pth = require('acf.path')
-local Cache = require('acf.persistence.backends.volatile')
-local util = require('acf.persistence.util')
-local copy = require('acf.util').copy
-
-local json = require('cjson')
-local posix = require('posix')
-
-
-local backend = require('acf.object').class()
-
-function backend:init()
- -- TODO cache expiration
- self.cache = {}
- self.dirty = {}
-end
-
-function backend:split_path(path)
- local fpath = copy(path)
- local jpath = {}
- local res
-
- while #fpath > 0 do
- local fp = pth.join('/', unpack(fpath))
- if self.cache[fp] then return fp, jpath end
- table.insert(jpath, 1, fpath[#fpath])
- table.remove(fpath)
- end
-
- fpath = '/'
-
- while true do
- fpath = pth.join(fpath, jpath[1])
- table.remove(jpath, 1)
-
- local t = posix.stat(fpath, 'type')
- if t == 'link' then t = posix.stat(posix.readlink(fpath), 'type') end
- if not t or not ({directory=true, regular=true})[t] then
- error('File or directory does not exist: '..fpath)
- end
-
- if t == 'regular' then return fpath, jpath end
-
- assert(#jpath > 0)
- end
-end
-
-function backend:get(path, top)
- local fpath, jpath = self:split_path(path)
- if not self.cache[fpath] then
- self.cache[fpath] = Cache(json.decode(util.read_file(fpath)))
- end
- return self.cache[fpath]:get(jpath, top)
-end
-
-function backend:set(mods)
- local dirty = {}
-
- for _, mod in ipairs(mods) do
- local path, value = unpack(mod)
- local fpath, jpath = self:split_path(path)
- self.cache[fpath]:_set(jpath, 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]:_get{}))
- file:close()
- end
-end
-
-
-return backend