Skip to content

Commit

Permalink
feat(server): add bulkstashsave convar to toggle old-saving
Browse files Browse the repository at this point in the history
Not recommended but a simple workaround for #1510 if
updating max_allowed_packet somehow isn't a viable solution.
  • Loading branch information
thelindat committed Nov 15, 2023
1 parent 03025f3 commit e5102f7
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
1 change: 1 addition & 0 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ end

if IsDuplicityVersion() then
server = {
bulkstashsave = GetConvarInt('inventory:bulkstashsave', 1) == 1,
loglevel = GetConvarInt('inventory:loglevel', 1),
randomprices = GetConvarInt('inventory:randomprices', 0) == 1,
randomloot = GetConvarInt('inventory:randomloot', 1) == 1,
Expand Down
2 changes: 1 addition & 1 deletion modules/inventory/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2260,7 +2260,7 @@ local function saveInventories(clearInventories)
if index and data then
total[5] += 1

if index == 4 then
if index == 4 and server.bulkstashsave then
for i = 1, 3 do
total[index] += 1
parameters[index][total[index]] = data[i]
Expand Down
44 changes: 31 additions & 13 deletions modules/mysql/server.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,22 +207,40 @@ function db.saveInventories(players, trunks, gloveboxes, stashes, total)
end

if total[4] > 0 then
total[4] /= 3
local p = promise.new()
promises[#promises + 1] = p

MySQL.query(Query.UPSERT_STASH:gsub('%(%?, %?, %?%)', string.rep('(?, ?, ?)', total[4], ', ')), stashes, function(resp)
local affectedRows = resp.affectedRows

if total[4] == 1 then
if affectedRows == 2 then affectedRows = 1 end
else
affectedRows -= tonumber(resp.info:match('Duplicates: (%d+)'), 10) or 0
end

shared.info(('Saved %d/%d stashes (%.4f ms)'):format(affectedRows, total[4], (os.nanotime() - start) / 1e6))
p:resolve()
end)
if server.bulkstashsave then
total[4] /= 3

MySQL.query(Query.UPSERT_STASH:gsub('%(%?, %?, %?%)', string.rep('(?, ?, ?)', total[4], ', ')), stashes, function(resp)
local affectedRows = resp.affectedRows

if total[4] == 1 then
if affectedRows == 2 then affectedRows = 1 end
else
affectedRows -= tonumber(resp.info:match('Duplicates: (%d+)'), 10) or 0
end

shared.info(('Saved %d/%d stashes (%.4f ms)'):format(affectedRows, total[4], (os.nanotime() - start) / 1e6))
p:resolve()
end)
else
MySQL.rawExecute(Query.UPSERT_STASH, stashes, function(resp)
local affectedRows = 0

if table.type(resp) == 'hash' then
if resp.affectedRows > 0 then affectedRows = 1 end
else
for i = 1, #resp do
if resp[i].affectedRows > 0 then affectedRows += 1 end
end
end

shared.info(('Saved %s/%s stashes (%.4f ms)'):format(affectedRows, total[4], (os.nanotime() - start) / 1e6))
p:resolve()
end)
end
end

-- All queries must run asynchronously on resource stop, so we'll await multiple promises instead.
Expand Down

0 comments on commit e5102f7

Please sign in to comment.