summaryrefslogtreecommitdiffstats
path: root/acf/transaction/backend.lua
blob: 5b8c53f7de3f2b40c2ef3ac8db31c06ce9d56004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
--[[
Copyright (c) 2012 Kaarle Ritvanen
See LICENSE file for license details
--]]

module(..., package.seeall)

-- TODO each transaction backend (i.e. persistence manager or
-- transaction proper) should be implemented as a thread or have its
-- internal state stored in shared storage (with appropriate locking)


local generation = 0
function gen_number()
   generation = generation + 1
   return generation
end


TransactionBackend = require('acf.object').class()

function TransactionBackend:init() self.mod_time = {} end

function TransactionBackend:get_if_older(path, timestamp)
   local value, ts = self:get(path)
   if ts > timestamp then error('Concurrent modification: '..path) end
   return value, ts
end

function TransactionBackend:set(path, t, value)
   self:set_multiple{{path, t, value}}
end

function TransactionBackend:set_multiple(mods)
   -- TODO delegate to PM backends?
   local timestamp = gen_number()
   for _, mod in ipairs(mods) do
      self.mod_time[mod[1]] = timestamp
   end
end

-- TODO should be atomic, mutex with set_multiple
function TransactionBackend:comp_and_setm(accessed, mods)
   for path, timestamp in pairs(accessed) do
      self:get_if_older(path, timestamp)
   end
   self:set_multiple(mods)
end