1
0
forked from MTSR/mapserver
mapserver/server/mapblockaccessor/mtime.go

99 lines
2.1 KiB
Go
Raw Normal View History

2019-02-10 22:27:53 +03:00
package mapblockaccessor
import (
"mapserver/coords"
"mapserver/eventbus"
"mapserver/layer"
"mapserver/mapblockparser"
cache "github.com/patrickmn/go-cache"
2019-02-15 10:36:48 +03:00
"github.com/prometheus/client_golang/prometheus"
2019-02-15 11:08:22 +03:00
"github.com/sirupsen/logrus"
2019-02-10 22:27:53 +03:00
)
2019-02-10 22:34:11 +03:00
type FindMapBlocksByMtimeResult struct {
HasMore bool
LastPos *coords.MapBlockCoords
LastMtime int64
List []*mapblockparser.MapBlock
UnfilteredCount int
}
2019-02-14 10:50:54 +03:00
func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, layerfilter []*layer.Layer) (*FindMapBlocksByMtimeResult, error) {
2019-02-10 22:27:53 +03:00
fields := logrus.Fields{
"lastmtime": lastmtime,
"limit": limit,
}
logrus.WithFields(fields).Debug("FindMapBlocksByMtime")
2019-02-15 10:36:48 +03:00
timer := prometheus.NewTimer(dbGetMtimeDuration)
2019-02-10 22:27:53 +03:00
blocks, err := a.accessor.FindBlocksByMtime(lastmtime, limit)
2019-02-15 10:36:48 +03:00
timer.ObserveDuration()
2019-02-10 22:27:53 +03:00
changedBlockCount.Add(float64(len(blocks)))
2019-02-10 22:27:53 +03:00
if err != nil {
return nil, err
}
2019-02-10 22:34:11 +03:00
result := FindMapBlocksByMtimeResult{}
2019-02-10 22:27:53 +03:00
mblist := make([]*mapblockparser.MapBlock, 0)
var newlastpos *coords.MapBlockCoords
result.HasMore = len(blocks) == limit
result.UnfilteredCount = len(blocks)
for _, block := range blocks {
newlastpos = block.Pos
if result.LastMtime < block.Mtime {
result.LastMtime = block.Mtime
}
inLayer := false
for _, l := range layerfilter {
if block.Pos.Y >= l.From && block.Pos.Y <= l.To {
2019-02-10 22:27:53 +03:00
inLayer = true
break
}
}
if !inLayer {
continue
}
fields := logrus.Fields{
"x": block.Pos.X,
"y": block.Pos.Y,
"z": block.Pos.Z,
}
logrus.WithFields(fields).Debug("mapblock")
key := getKey(block.Pos)
mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos)
if err != nil {
fields := logrus.Fields{
"x": block.Pos.X,
"y": block.Pos.Y,
"z": block.Pos.Z,
"err": err,
}
logrus.WithFields(fields).Error("parse error")
continue
2019-02-10 22:27:53 +03:00
}
a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock)
2019-03-13 12:41:37 +03:00
a.blockcache.Set(key, mapblock, cache.DefaultExpiration)
cacheBlockCount.Inc()
2019-02-10 22:27:53 +03:00
mblist = append(mblist, mapblock)
}
result.LastPos = newlastpos
result.List = mblist
return &result, nil
}