Skip to content

Commit 8a06ae2

Browse files
committed
attempt to migrate databases
1 parent d99c3be commit 8a06ae2

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

statistics.lua

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
local S = smartshop.S
99

10+
smartshop.db_version = 5
11+
1012
-- Interval for globalstep to write report file.
1113
smartshop.report_interval = tonumber(core.settings:get(
1214
"smartshop.report_interval")) or 0
@@ -30,6 +32,68 @@ smartshop.item_prices = {}
3032

3133
local worldpath = smartshop.worldpath
3234

35+
-- Attempt to update databas to reduce garbage.
36+
local function update_database()
37+
local new_stats = { smartshop_db_version = { version = smartshop.db_version } }
38+
local new_prices = {}
39+
local next_index
40+
-- smartshop.item_stats[item_name][spos] = count
41+
for item_name, dict in pairs(smartshop.item_stats) do
42+
next_index = 1
43+
for spos, count in pairs(dict) do
44+
if ")" == spos:sub(-1) then
45+
-- Give the position any unique index.
46+
-- There is a high probability that next time
47+
-- the corresponding shop is updated, this will
48+
-- be rectified, if the shop even still exists.
49+
spos = spos .. next_index
50+
next_index = next_index + 1
51+
-- only log info once per item and position combo
52+
if 6 == next_index then
53+
print('-------------'.. next_index)
54+
core.log("info", "[smartshop] detected unusual occurance in "
55+
.. "item-count database for " .. item_name .. " at "
56+
.. spos:sub(1, -2))
57+
end
58+
end
59+
if not new_stats[item_name] then
60+
new_stats[item_name] = {}
61+
end
62+
new_stats[item_name][spos] = count
63+
end
64+
end
65+
smartshop.item_stats = new_stats
66+
67+
-- smartshop.item_prices[item_name][spos] = price
68+
for item_name, dict in pairs(smartshop.item_prices) do
69+
next_index = 1
70+
for spos, price in pairs(dict) do
71+
if ")" == spos:sub(-1) then
72+
-- Give the position any unique index.
73+
-- There is a high probability that next time
74+
-- the corresponding shop is updated, this will
75+
-- be rectified, if the shop even still exists.
76+
spos = spos .. next_index
77+
next_index = next_index + 1
78+
-- only log info once per item and position combo
79+
if 6 == next_index then
80+
core.log("info", "[smartshop] detected unusual occurance in "
81+
.. "item-price database for " .. item_name .. " at "
82+
.. spos:sub(1, -2))
83+
end
84+
end
85+
if not new_prices[item_name] then
86+
new_prices[item_name] = {}
87+
end
88+
new_prices[item_name][spos] = price
89+
end
90+
end
91+
smartshop.item_prices = new_prices
92+
smartshop.save_counts()
93+
smartshop.save_prices()
94+
end
95+
96+
3397
-- Load item stats
3498
local function load_statistics()
3599
local data
@@ -50,6 +114,10 @@ local function load_statistics()
50114
end
51115
file:close()
52116
end
117+
118+
if not smartshop.item_stats.version then
119+
update_database()
120+
end
53121
end
54122

55123

0 commit comments

Comments
 (0)