Repository navigation
Expand file tree
/
Copy pathcache.lua
More file actions
168 lines (148 loc) · 4.86 KB
/
Copy pathcache.lua
File metadata and controls
168 lines (148 loc) · 4.86 KB
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
-- Implementation of a reinforcement cache
local chunk_size = 16
local function coord_chunk(n)
return math.floor(n / chunk_size) * chunk_size
end
local function get_pos_chunk(pos)
return vector.new(
coord_chunk(pos.x),
coord_chunk(pos.y),
coord_chunk(pos.z)
)
end
--[[
cache:
chunk coords --> block coords --> reinforcement
]]--
local chunk_reinf_cache = {}
local cache_chunk_expiry = 30 -- debug, can be set to 60+
function ct.flush_reinf(reinf)
local reinf_value = reinf.value
local x, y, z = reinf.x, reinf.y, reinf.z
if reinf.new then
-- minetest.chat_send_all(" Registered: (" .. ptos(x, y, z) .. ")")
ctdb.remove_reinforcement(vector.new(x, y, z))
-- TODO: cleanup this remove-then-register hack
ctdb.register_reinforcement(
vector.new(x, y, z),
reinf.ctgroup_id,
reinf.material,
reinf.last_update,
reinf.creation_date,
reinf.last_stacked
)
reinf.new = false
end
if reinf_value < 1 then
-- minetest.chat_send_all(" Removed: (" .. ptos(x, y, z) .. ")")
ctdb.remove_reinforcement(vector.new(x, y, z))
else
-- minetest.chat_send_all(" Updated: (" .. ptos(x, y, z) .. ")")
ctdb.update_reinforcement(
vector.new(x, y, z),
reinf_value,
reinf.last_update,
reinf.creation_date,
reinf.last_stacked
)
end
end
function ct.try_flush_cache()
local current_time = os.time(os.date("!*t"))
ctdb.begin_transaction()
for key, chunk in pairs(chunk_reinf_cache) do
if (chunk.time_added + cache_chunk_expiry) < current_time then
minetest.log("verbose", "Flushing chunk (" .. key .. ") to db.")
for _, reinf in pairs(chunk.reinforcements) do
ct.flush_reinf(reinf)
end
chunk_reinf_cache[key] = nil
end
end
ctdb.end_transaction()
end
function ct.force_flush_cache()
ctdb.begin_transaction()
for key, chunk in pairs(chunk_reinf_cache) do
minetest.log("verbose", "Force flushing chunk (" .. key .. ") to db.")
for _, reinf in pairs(chunk.reinforcements) do
ct.flush_reinf(reinf)
end
chunk_reinf_cache[key] = nil
end
ctdb.end_transaction()
end
function ct.chunk_ensure_cached(pos)
local vchunk_start = get_pos_chunk(pos)
local vchunk_end = vector.add(vchunk_start, chunk_size)
local chunk_reinf = chunk_reinf_cache[vtos(vchunk_start)]
if not chunk_reinf then
ctdb.get_reinforcements_for_cache(
chunk_reinf_cache,
vchunk_start,
vchunk_end
)
end
end
function ct.get_reinforcement(pos)
ct.try_flush_cache()
ct.chunk_ensure_cached(pos)
local vchunk_start = get_pos_chunk(pos)
local chunk_reinf = chunk_reinf_cache[vtos(vchunk_start)]
local reinf = chunk_reinf.reinforcements[vtos(pos)]
-- Apply warmup/decay to this reinforcement
ct.try_catchup_reinforcement(pos, reinf)
return reinf
end
function ct.modify_reinforcement(pos, value, last_update)
ct.try_flush_cache()
ct.chunk_ensure_cached(pos)
local vchunk_start = get_pos_chunk(pos)
local chunk_reinf = chunk_reinf_cache[vtos(vchunk_start)]
if value == 0 then
-- We have to force a flush to get the DB in sync with the removal of the
-- node's entry in the cache.
--
-- If this isn't done, Citadella would reload the value from the DB, which
-- is almost certainly not coherent with the current state of the cache.
if chunk_reinf.reinforcements[vtos(pos)] then
chunk_reinf.reinforcements[vtos(pos)].value = 0
ct.flush_reinf(chunk_reinf.reinforcements[vtos(pos)])
-- Once the cache has been flushed, this reinforcement entry is removed.
chunk_reinf.reinforcements[vtos(pos)] = nil
end
elseif chunk_reinf.reinforcements[vtos(pos)] then
chunk_reinf.reinforcements[vtos(pos)].value = value
if last_update then
chunk_reinf.reinforcements[vtos(pos)].last_update = last_update
end
end
return value
end
function ct.register_reinforcement(pos, ctgroup_id, item_name, resource_limit)
local reinf = ct.get_reinforcement(pos)
if not reinf then
local time = os.time(os.date("!*t"))
local vchunk = get_pos_chunk(pos)
chunk_reinf_cache[vtos(vchunk)].reinforcements[vtos(pos)] = {
x = pos.x, y = pos.y, z = pos.z,
value = 1,
material = item_name,
ctgroup_id = ctgroup_id,
last_update = time,
creation_date = time,
last_stacked = time,
new = true
}
end
end
function ct.update_reinforcement_group(reinf, pos, ctgroup_id)
ctdb.update_reinforcement_group(
pos, reinf.ctgroup_id, ctgroup_id
)
local vchunk = get_pos_chunk(pos)
local cached_reinf = chunk_reinf_cache[vtos(vchunk)].reinforcements[vtos(pos)]
if cached_reinf then
cached_reinf.ctgroup_id = ctgroup_id
end
end