2019-02-10 22:27:53 +03:00
|
|
|
package mapblockaccessor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"mapserver/coords"
|
|
|
|
"mapserver/eventbus"
|
2022-01-30 15:29:47 +03:00
|
|
|
"mapserver/types"
|
2019-03-22 19:32:43 +03:00
|
|
|
"sync"
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2021-10-12 08:44:07 +03:00
|
|
|
"github.com/minetest-go/mapparser"
|
2019-02-10 22:27:53 +03:00
|
|
|
cache "github.com/patrickmn/go-cache"
|
2019-02-15 10:36:48 +03:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2019-03-13 11:12:22 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2019-02-10 22:27:53 +03:00
|
|
|
)
|
|
|
|
|
2019-03-22 19:32:43 +03:00
|
|
|
var lock = &sync.RWMutex{}
|
|
|
|
|
2021-10-12 08:44:07 +03:00
|
|
|
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapparser.MapBlock, error) {
|
2023-10-11 18:33:41 +03:00
|
|
|
cache_enabled := a.maxcount > 0
|
2019-02-10 22:27:53 +03:00
|
|
|
key := getKey(pos)
|
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
if cache_enabled {
|
2019-03-13 11:12:22 +03:00
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
//maintenance
|
|
|
|
cacheBlocks.Set(float64(a.blockcache.ItemCount()))
|
|
|
|
if a.blockcache.ItemCount() > a.maxcount {
|
|
|
|
//flush cache
|
|
|
|
fields := logrus.Fields{
|
|
|
|
"cached items": a.blockcache.ItemCount(),
|
|
|
|
"maxcount": a.maxcount,
|
|
|
|
}
|
|
|
|
logrus.WithFields(fields).Debug("Flushing cache")
|
2019-03-22 19:32:43 +03:00
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
a.blockcache.Flush()
|
2019-03-13 23:14:04 +03:00
|
|
|
}
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
//read section
|
|
|
|
lock.RLock()
|
2019-03-22 19:32:43 +03:00
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
cachedblock, found := a.blockcache.Get(key)
|
|
|
|
if found {
|
|
|
|
defer lock.RUnlock()
|
2019-02-15 10:36:48 +03:00
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
getCacheHitCount.Inc()
|
|
|
|
if cachedblock == nil {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return cachedblock.(*mapparser.MapBlock), nil
|
|
|
|
}
|
|
|
|
}
|
2019-03-22 19:32:43 +03:00
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
//end read
|
|
|
|
lock.RUnlock()
|
|
|
|
|
|
|
|
timer := prometheus.NewTimer(dbGetDuration)
|
|
|
|
defer timer.ObserveDuration()
|
|
|
|
|
|
|
|
//write section
|
|
|
|
lock.Lock()
|
|
|
|
defer lock.Unlock()
|
|
|
|
|
|
|
|
//try read
|
|
|
|
cachedblock, found = a.blockcache.Get(key)
|
|
|
|
if found {
|
|
|
|
getCacheHitCount.Inc()
|
|
|
|
if cachedblock == nil {
|
|
|
|
return nil, nil
|
|
|
|
} else {
|
|
|
|
return cachedblock.(*mapparser.MapBlock), nil
|
|
|
|
}
|
2019-03-22 19:32:43 +03:00
|
|
|
}
|
2023-10-11 18:33:41 +03:00
|
|
|
|
2019-03-22 19:32:43 +03:00
|
|
|
}
|
|
|
|
|
2019-02-10 22:27:53 +03:00
|
|
|
block, err := a.accessor.GetBlock(pos)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if block == nil {
|
2019-03-13 12:41:37 +03:00
|
|
|
//no mapblock here
|
2023-10-11 18:33:41 +03:00
|
|
|
if cache_enabled {
|
|
|
|
cacheBlockCount.Inc()
|
|
|
|
a.blockcache.Set(key, nil, cache.DefaultExpiration)
|
|
|
|
}
|
2019-02-10 22:27:53 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
if cache_enabled {
|
|
|
|
getCacheMissCount.Inc()
|
|
|
|
}
|
2019-03-13 12:41:37 +03:00
|
|
|
|
2021-10-12 08:44:07 +03:00
|
|
|
mapblock, err := mapparser.Parse(block.Data)
|
2019-02-10 22:27:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-01-30 15:29:47 +03:00
|
|
|
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, types.NewParsedMapblock(mapblock, pos))
|
2019-02-10 22:27:53 +03:00
|
|
|
|
2023-10-11 18:33:41 +03:00
|
|
|
if cache_enabled {
|
|
|
|
cacheBlockCount.Inc()
|
|
|
|
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
|
|
|
|
}
|
2019-02-10 22:27:53 +03:00
|
|
|
|
|
|
|
return mapblock, nil
|
|
|
|
}
|