mapserver/server/mapblockaccessor/mapblockaccessor.go

219 lines
4.6 KiB
Go
Raw Normal View History

2019-01-10 19:50:31 +03:00
package mapblockaccessor
import (
2019-01-13 18:37:03 +03:00
"fmt"
2019-01-10 19:50:31 +03:00
"mapserver/coords"
"mapserver/db"
2019-01-28 15:31:48 +03:00
"mapserver/eventbus"
2019-01-21 16:27:15 +03:00
"mapserver/layer"
2019-01-21 18:27:31 +03:00
"mapserver/mapblockparser"
2019-01-28 15:31:48 +03:00
2019-01-11 11:07:31 +03:00
"time"
cache "github.com/patrickmn/go-cache"
"github.com/sirupsen/logrus"
2019-01-10 19:50:31 +03:00
)
type MapBlockAccessor struct {
2019-01-28 15:31:48 +03:00
accessor db.DBAccessor
c *cache.Cache
Eventbus *eventbus.Eventbus
2019-01-11 11:07:31 +03:00
}
2019-02-06 10:38:08 +03:00
func getKey(pos *coords.MapBlockCoords) string {
2019-01-11 11:07:31 +03:00
return fmt.Sprintf("Coord %d/%d/%d", pos.X, pos.Y, pos.Z)
2019-01-10 19:50:31 +03:00
}
func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor {
2019-01-22 15:17:30 +03:00
c := cache.New(500*time.Millisecond, 1000*time.Millisecond)
2019-01-11 11:07:31 +03:00
2019-01-28 15:31:48 +03:00
return &MapBlockAccessor{
accessor: accessor,
c: c,
Eventbus: eventbus.New(),
}
2019-01-18 16:43:34 +03:00
}
2019-02-06 10:38:08 +03:00
func (a *MapBlockAccessor) Update(pos *coords.MapBlockCoords, mb *mapblockparser.MapBlock) {
2019-01-11 11:07:31 +03:00
key := getKey(pos)
a.c.Set(key, mb, cache.DefaultExpiration)
2019-01-10 19:50:31 +03:00
}
2019-01-24 09:24:28 +03:00
type FindMapBlocksResult struct {
2019-01-23 15:13:32 +03:00
HasMore bool
LastPos *coords.MapBlockCoords
2019-01-24 09:52:25 +03:00
LastMtime int64
2019-01-23 15:13:32 +03:00
List []*mapblockparser.MapBlock
2019-01-23 14:46:45 +03:00
UnfilteredCount int
}
2019-01-24 17:56:37 +03:00
func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, layerfilter []layer.Layer) (*FindMapBlocksResult, error) {
2019-01-21 11:11:07 +03:00
2019-01-24 09:52:25 +03:00
fields := logrus.Fields{
"lastmtime": lastmtime,
"limit": limit,
}
2019-01-24 17:56:37 +03:00
logrus.WithFields(fields).Debug("FindMapBlocksByMtime")
2019-01-24 09:52:25 +03:00
2019-01-24 17:56:37 +03:00
blocks, err := a.accessor.FindBlocksByMtime(lastmtime, limit)
2019-01-21 11:11:07 +03:00
if err != nil {
2019-01-23 14:46:45 +03:00
return nil, err
2019-01-21 11:11:07 +03:00
}
2019-01-24 09:24:28 +03:00
result := FindMapBlocksResult{}
2019-01-23 14:46:45 +03:00
2019-01-21 11:11:07 +03:00
mblist := make([]*mapblockparser.MapBlock, 0)
var newlastpos *coords.MapBlockCoords
2019-01-23 14:46:45 +03:00
result.HasMore = len(blocks) == limit
result.UnfilteredCount = len(blocks)
2019-01-21 11:11:07 +03:00
for _, block := range blocks {
2019-02-06 10:38:08 +03:00
newlastpos = block.Pos
2019-01-24 09:52:25 +03:00
if result.LastMtime < block.Mtime {
result.LastMtime = block.Mtime
}
2019-01-21 16:27:15 +03:00
inLayer := false
for _, l := range layerfilter {
if (block.Pos.Y*16) >= l.From && (block.Pos.Y*16) <= l.To {
inLayer = true
2019-01-22 15:17:30 +03:00
break
2019-01-21 16:27:15 +03:00
}
}
if !inLayer {
continue
}
2019-01-21 11:11:07 +03:00
fields := logrus.Fields{
"x": block.Pos.X,
"y": block.Pos.Y,
"z": block.Pos.Z,
}
2019-01-24 17:56:37 +03:00
logrus.WithFields(fields).Debug("mapblock")
key := getKey(block.Pos)
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos)
if err != nil {
return nil, err
}
2019-01-28 15:31:48 +03:00
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
2019-01-24 17:56:37 +03:00
a.c.Set(key, mapblock, cache.DefaultExpiration)
mblist = append(mblist, mapblock)
}
result.LastPos = newlastpos
result.List = mblist
return &result, nil
}
2019-02-06 10:38:08 +03:00
func (a *MapBlockAccessor) FindMapBlocksByPos(lastpos *coords.MapBlockCoords, limit int, layerfilter []layer.Layer) (*FindMapBlocksResult, error) {
2019-01-24 17:56:37 +03:00
fields := logrus.Fields{
"x": lastpos.X,
"y": lastpos.Y,
"z": lastpos.Z,
"limit": limit,
}
logrus.WithFields(fields).Debug("FindMapBlocksByPos")
blocks, err := a.accessor.FindLegacyBlocksByPos(lastpos, limit)
if err != nil {
return nil, err
}
result := FindMapBlocksResult{}
mblist := make([]*mapblockparser.MapBlock, 0)
var newlastpos *coords.MapBlockCoords
result.HasMore = len(blocks) == limit
result.UnfilteredCount = len(blocks)
for _, block := range blocks {
2019-02-06 10:38:08 +03:00
newlastpos = block.Pos
2019-01-24 17:56:37 +03:00
if result.LastMtime < block.Mtime {
result.LastMtime = block.Mtime
}
inLayer := false
for _, l := range layerfilter {
if (block.Pos.Y*16) >= l.From && (block.Pos.Y*16) <= l.To {
inLayer = true
break
}
}
if !inLayer {
continue
}
fields := logrus.Fields{
"x": block.Pos.X,
"y": block.Pos.Y,
"z": block.Pos.Z,
}
logrus.WithFields(fields).Trace("mapblock")
2019-01-21 11:11:07 +03:00
key := getKey(block.Pos)
2019-01-21 13:31:50 +03:00
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos)
2019-01-21 11:11:07 +03:00
if err != nil {
2019-01-23 14:46:45 +03:00
return nil, err
2019-01-21 11:11:07 +03:00
}
2019-01-28 15:31:48 +03:00
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
2019-01-21 11:11:07 +03:00
a.c.Set(key, mapblock, cache.DefaultExpiration)
mblist = append(mblist, mapblock)
}
2019-01-23 14:46:45 +03:00
result.LastPos = newlastpos
result.List = mblist
2019-02-08 10:11:06 +03:00
fields = logrus.Fields{
2019-02-08 18:02:24 +03:00
"len(List)": len(result.List),
2019-02-08 10:11:06 +03:00
"unfilteredCount": result.UnfilteredCount,
2019-02-08 18:02:24 +03:00
"hasMore": result.HasMore,
"limit": limit,
2019-02-08 10:11:06 +03:00
}
logrus.WithFields(fields).Debug("FindMapBlocksByPos:Result")
2019-01-23 14:46:45 +03:00
return &result, nil
2019-01-21 11:11:07 +03:00
}
2019-02-06 10:38:08 +03:00
func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) {
2019-01-11 11:07:31 +03:00
key := getKey(pos)
cachedblock, found := a.c.Get(key)
if found {
return cachedblock.(*mapblockparser.MapBlock), nil
}
2019-01-10 19:50:31 +03:00
block, err := a.accessor.GetBlock(pos)
if err != nil {
return nil, err
}
2019-01-11 11:07:31 +03:00
if block == nil {
return nil, nil
}
2019-01-21 13:31:50 +03:00
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, pos)
2019-01-10 19:50:31 +03:00
if err != nil {
return nil, err
}
2019-01-28 15:31:48 +03:00
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
2019-01-18 16:43:34 +03:00
2019-01-11 11:07:31 +03:00
a.c.Set(key, mapblock, cache.DefaultExpiration)
2019-01-10 19:50:31 +03:00
return mapblock, nil
}