summaryrefslogtreecommitdiffstats
path: root/acf/transaction/init.lua
blob: eb92c34f012be883da3e8e26928eece3cb91a6cd (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
--[[
Copyright (c) 2012-2013 Kaarle Ritvanen
See LICENSE file for license details
--]]

module(..., package.seeall)

local ErrorDict = require('acf.error').ErrorDict
local RootModel = require('acf.model').RootModel
local object = require('acf.object')
local super = object.super
local pth = require('acf.path')
local be_mod = require('acf.transaction.backend')
local copy = require('acf.util').copy


local function remove_list_value(list, value)
   value = tostring(value)

   for i, v in ipairs(list) do
      if tostring(v) == value then
	 table.remove(list, i)
	 return
      end
   end

   assert(false)
end


local Transaction = object.class(be_mod.TransactionBackend)

function Transaction:init(backend)
   super(self, Transaction):init()

   self.backend = backend

   self.started = be_mod.gen_number()
   self.access_time = {}

   self.added = {}
   self.modified = {}
   self.deleted = {}

   self.validate = {}

   self.root = RootModel(self)
end

function Transaction:check()
   if not self.backend then error('Transaction already committed') end
end

function Transaction:get(path)
   self:check()

   if self.deleted[path] then return nil, self.mod_time[path] end
   for _, tbl in ipairs{self.added, self.modified} do
      if tbl[path] ~= nil then
	 return copy(tbl[path][2]), self.mod_time[path]
      end
   end
   
   local value, timestamp = self.backend:get_if_older(path, self.started)
   self.access_time[path] = timestamp
   return value, timestamp
end

function Transaction:set_multiple(mods)
   super(self, Transaction):set_multiple(mods)

   local function set(path, t, value, new)
      local delete = value == nil
      value = not delete and {t, value} or nil

      if self.added[path] == nil and (not new or self.deleted[path]) then
	 self.modified[path] = value
	 self.deleted[path] = delete
      else self.added[path] = value end
   end

   for _, mod in ipairs(mods) do
      local path, t, value = unpack(mod)

      local ppath = pth.parent(path)
      local parent = self:get(ppath)
      if parent == nil then
	 self:set(ppath, 'table', true)
	 parent = {}
      end

      local name = pth.name(path)
      local old = self:get(path)

      local is_table = t == 'table'
      local delete = not is_table and value == nil

      if type(old) == 'table' then
	 if delete then
	    for _, child in ipairs(old) do self:set(pth.join(path, child)) end
	 elseif is_table then return
	 elseif #old > 0 then
	    error('Cannot assign a primitive value to non-leaf node '..path)
	 end
      end

      if is_table then value = {} end
      set(path, not delete and t or nil, value, old == nil)

      if old == nil and not delete then
	 table.insert(parent, name)
	 set(ppath, 'table', parent)
      elseif old ~= nil and delete then
	 remove_list_value(parent, name)
	 set(ppath, 'table', parent)
      end
   end
end

function Transaction:search(path) return self.root:search(pth.split(path)) end

function Transaction:commit()
   self:check()

   local errors = ErrorDict()
   for path, func in pairs(self.validate) do
      if not self.deleted[path] then errors:collect(func) end
   end
   errors:raise()

   local mods = {}
   local handled = {}

   local function insert(path, t, value)
      assert(not handled[path])
      table.insert(mods, {path, t, value})
      handled[path] = true
   end

   local function insert_add(path)
      if not handled[path] then
	 local pp = pth.parent(path)
	 if self.added[pp] then insert_add(pp) end

	 local t, value = unpack(self.added[path])
	 if t == 'table' then value = nil end
	 insert(path, t, value)
      end
   end

   local function insert_del(path)
      if not handled[path] then
	 local value = self.backend:get(path)
	 if type(value) == 'table' then
	    for _, child in ipairs(value) do
	       local cp = pth.join(path, child)
	       assert(self.deleted[cp])
	       insert_del(cp)
	    end
	 end
	 insert(path)
      end
   end

   for path, deleted in pairs(self.deleted) do
      if deleted then insert_del(path) end
   end

   for path, value in pairs(self.modified) do
      local t, v = unpack(value)
      if t ~= 'table' then insert(path, t, v) end
   end

   for path, _ in pairs(self.added) do insert_add(path) end

   self.backend:comp_and_setm(self.access_time, mods)
   self.backend = nil
end

local store = require('acf.persistence').DataStore()
function start(txn) return Transaction(txn or store) end