mapserver/mapblockaccessor/mapblockaccessor.go

173 lines
3.7 KiB
Go
Raw Normal View History

2019-01-10 17:50:31 +01:00
package mapblockaccessor
import (
2019-01-13 16:37:03 +01:00
"fmt"
2019-01-10 17:50:31 +01:00
"mapserver/coords"
"mapserver/db"
2019-01-21 14:27:15 +01:00
"mapserver/layer"
2019-01-21 16:27:31 +01:00
"mapserver/mapblockparser"
2019-01-11 09:07:31 +01:00
"time"
cache "github.com/patrickmn/go-cache"
"github.com/sirupsen/logrus"
2019-01-10 17:50:31 +01:00
)
type MapBlockAccessor struct {
2019-01-18 15:10:46 +01:00
accessor db.DBAccessor
c *cache.Cache
2019-01-18 14:43:34 +01:00
listeners []MapBlockListener
}
type MapBlockListener interface {
2019-01-21 14:42:38 +01:00
OnParsedMapBlock(block *mapblockparser.MapBlock)
2019-01-11 09:07:31 +01:00
}
func getKey(pos coords.MapBlockCoords) string {
return fmt.Sprintf("Coord %d/%d/%d", pos.X, pos.Y, pos.Z)
2019-01-10 17:50:31 +01:00
}
func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor {
2019-01-19 16:50:05 +01:00
c := cache.New(100*time.Millisecond, 200*time.Millisecond)
2019-01-11 09:07:31 +01:00
return &MapBlockAccessor{accessor: accessor, c: c}
2019-01-10 17:50:31 +01:00
}
2019-01-18 14:43:34 +01:00
func (a *MapBlockAccessor) AddListener(l MapBlockListener) {
a.listeners = append(a.listeners, l)
}
2019-01-10 17:50:31 +01:00
func (a *MapBlockAccessor) Update(pos coords.MapBlockCoords, mb *mapblockparser.MapBlock) {
2019-01-11 09:07:31 +01:00
key := getKey(pos)
a.c.Set(key, mb, cache.DefaultExpiration)
2019-01-10 17:50:31 +01:00
}
2019-01-21 14:27:15 +01:00
func (a *MapBlockAccessor) FindLegacyMapBlocks(lastpos coords.MapBlockCoords, limit int, layerfilter []layer.Layer) (bool, *coords.MapBlockCoords, []*mapblockparser.MapBlock, error) {
2019-01-21 09:11:07 +01:00
blocks, err := a.accessor.FindLegacyBlocks(lastpos, limit)
if err != nil {
2019-01-21 14:27:15 +01:00
return false, nil, nil, err
2019-01-21 09:11:07 +01:00
}
mblist := make([]*mapblockparser.MapBlock, 0)
var newlastpos *coords.MapBlockCoords
2019-01-21 14:27:15 +01:00
hasMore := len(blocks) == limit
2019-01-21 09:11:07 +01:00
for _, block := range blocks {
2019-01-21 14:27:15 +01:00
newlastpos = &block.Pos
inLayer := false
for _, l := range layerfilter {
if (block.Pos.Y*16) >= l.From && (block.Pos.Y*16) <= l.To {
inLayer = true
}
}
if !inLayer {
continue
}
2019-01-21 09:11:07 +01:00
fields := logrus.Fields{
"x": block.Pos.X,
"y": block.Pos.Y,
"z": block.Pos.Z,
}
logrus.WithFields(fields).Debug("legacy mapblock")
key := getKey(block.Pos)
2019-01-21 11:31:50 +01:00
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos)
2019-01-21 09:11:07 +01:00
if err != nil {
2019-01-21 14:27:15 +01:00
return false, nil, nil, err
2019-01-21 09:11:07 +01:00
}
for _, listener := range a.listeners {
2019-01-21 14:42:38 +01:00
listener.OnParsedMapBlock(mapblock)
2019-01-21 09:11:07 +01:00
}
a.c.Set(key, mapblock, cache.DefaultExpiration)
mblist = append(mblist, mapblock)
}
2019-01-21 14:27:15 +01:00
return hasMore, newlastpos, mblist, nil
2019-01-21 09:11:07 +01:00
}
2019-01-21 14:27:15 +01:00
func (a *MapBlockAccessor) FindLatestMapBlocks(mintime int64, limit int, layerfilter []layer.Layer) ([]*mapblockparser.MapBlock, error) {
2019-01-18 13:07:01 +01:00
blocks, err := a.accessor.FindLatestBlocks(mintime, limit)
if err != nil {
return nil, err
}
mblist := make([]*mapblockparser.MapBlock, 0)
2019-01-18 13:50:59 +01:00
for _, block := range blocks {
2019-01-18 15:56:53 +01:00
2019-01-21 14:27:15 +01:00
inLayer := false
for _, l := range layerfilter {
if (block.Pos.Y*16) >= l.From && (block.Pos.Y*16) <= l.To {
inLayer = true
}
}
if !inLayer {
continue
}
2019-01-18 15:56:53 +01:00
fields := logrus.Fields{
"x": block.Pos.X,
"y": block.Pos.Y,
"z": block.Pos.Z,
}
logrus.WithFields(fields).Debug("updated mapblock")
2019-01-18 13:07:01 +01:00
key := getKey(block.Pos)
2019-01-21 11:31:50 +01:00
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos)
2019-01-18 13:07:01 +01:00
if err != nil {
return nil, err
}
2019-01-18 15:10:46 +01:00
for _, listener := range a.listeners {
2019-01-21 14:42:38 +01:00
listener.OnParsedMapBlock(mapblock)
2019-01-18 14:43:34 +01:00
}
2019-01-18 13:07:01 +01:00
a.c.Set(key, mapblock, cache.DefaultExpiration)
mblist = append(mblist, mapblock)
}
return mblist, nil
}
2019-01-10 17:50:31 +01:00
func (a *MapBlockAccessor) GetMapBlock(pos coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
2019-01-11 09:07:31 +01:00
key := getKey(pos)
cachedblock, found := a.c.Get(key)
if found {
return cachedblock.(*mapblockparser.MapBlock), nil
}
2019-01-10 17:50:31 +01:00
block, err := a.accessor.GetBlock(pos)
if err != nil {
return nil, err
}
2019-01-11 09:07:31 +01:00
if block == nil {
return nil, nil
}
2019-01-21 11:31:50 +01:00
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, pos)
2019-01-10 17:50:31 +01:00
if err != nil {
return nil, err
}
2019-01-18 15:10:46 +01:00
for _, listener := range a.listeners {
2019-01-21 14:42:38 +01:00
listener.OnParsedMapBlock(mapblock)
2019-01-18 14:43:34 +01:00
}
2019-01-11 09:07:31 +01:00
a.c.Set(key, mapblock, cache.DefaultExpiration)
2019-01-10 17:50:31 +01:00
return mapblock, nil
}