From 6e9a052aa877222a1a494ddcdc5507b5bcc3ea68 Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sun, 2 Apr 2023 01:28:21 +0100 Subject: minor cleaning --- server/chunk.lua | 5 ++--- server/db.lua | 4 ++++ server/map.lua | 5 +++-- server/server.lua | 25 ++++--------------------- 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/server/chunk.lua b/server/chunk.lua index 852f1fe..bb30945 100644 --- a/server/chunk.lua +++ b/server/chunk.lua @@ -34,12 +34,11 @@ function ChunkS.load_from_disk(cls,cp) return self end -function ChunkS.save_if_dirty(self) +function ChunkS.save_if_dirty(self,txn) if self.dirty then print("saving chunk",self.cp) - local txn,dbi = db.get_db('chunks',true) + local dbi = txn:open'chunks' dbi[tostring(self.cp)] = self:data_packet() - txn:commit() self.dirty = false end end diff --git a/server/db.lua b/server/db.lua index 4208aeb..a4cd8a7 100644 --- a/server/db.lua +++ b/server/db.lua @@ -7,9 +7,13 @@ local function get_db(dbname, writeable) local the_db = assert(txn:open(dbname), "couldn't open db") return txn,the_db end +local function txn(writeable) + return assert(env:txn_begin(writeable), "couldn't begin txn") +end return { env=env, + txn=txn, get_db=get_db, } diff --git a/server/map.lua b/server/map.lua index 3d7f377..7b2e62b 100644 --- a/server/map.lua +++ b/server/map.lua @@ -28,13 +28,14 @@ function MapS.obtain(self,cp) end end -function MapS.save_chunk(self,cp) + +function MapS.save_chunk(self,cp,txn) -- will only actually save anything if anything needs saving. -- any attempt to save not-loaded chunks is silently ignored local ch = self:chunk(cp) if not ch then return end - ch:save_if_dirty() + ch:save_if_dirty(txn) end return {MapS=MapS} diff --git a/server/server.lua b/server/server.lua index 2da7988..701551d 100644 --- a/server/server.lua +++ b/server/server.lua @@ -9,6 +9,7 @@ local MapS = require"map".MapS local Player=require'player'.Player local posix_time = require"posix.time" local posix_signal = require"posix.signal" +local db = require'db' math.randomseed(os.time()) @@ -138,29 +139,9 @@ local function timenow() -- this discards some precision but i don't care return tv.tv_sec + (tv.tv_nsec/1000000000) end --- do something only every x seconds --- returns function(dt) -> bool --- which returns true if you should, and false otherwise -local function every(interval) - local time_since = interval+100 - return function(dt) - time_since = time_since + dt - if time_since > interval then - time_since = 0 - return true - else - return false - end - end -end local tick_interval = 1 -- seconds local last_tick_time = timenow() - --- average tps calculation -local dts = {} -local ndt = 1 -local ndts = 100 local ntick = 0 @@ -180,13 +161,15 @@ local function player_near_chunk(cp) end local function save_things() + local txn = db.txn(true) for cp,ch in map:iter_chunks() do - map:save_chunk(cp) + map:save_chunk(cp,txn) if not player_near_chunk(cp) then -- print("unloading chunk",cp) map:remove_chunk(cp) end end + txn:commit() end local function tick(ntick) -- cgit v1.2.3