diff --git a/server/mapblockaccessor/get.go b/server/mapblockaccessor/get.go new file mode 100644 index 0000000..ea663b8 --- /dev/null +++ b/server/mapblockaccessor/get.go @@ -0,0 +1,38 @@ +package mapblockaccessor + +import ( + "mapserver/coords" + "mapserver/eventbus" + "mapserver/mapblockparser" + + cache "github.com/patrickmn/go-cache" +) + +func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) { + key := getKey(pos) + + cachedblock, found := a.c.Get(key) + if found { + return cachedblock.(*mapblockparser.MapBlock), nil + } + + block, err := a.accessor.GetBlock(pos) + if err != nil { + return nil, err + } + + if block == nil { + return nil, nil + } + + mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, pos) + if err != nil { + return nil, err + } + + a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) + + a.c.Set(key, mapblock, cache.DefaultExpiration) + + return mapblock, nil +} diff --git a/server/mapblockaccessor/legacyblocks.go b/server/mapblockaccessor/legacyblocks.go new file mode 100644 index 0000000..98b85cc --- /dev/null +++ b/server/mapblockaccessor/legacyblocks.go @@ -0,0 +1,87 @@ +package mapblockaccessor + +import ( + "mapserver/coords" + "mapserver/eventbus" + "mapserver/layer" + "mapserver/mapblockparser" + + cache "github.com/patrickmn/go-cache" + "github.com/sirupsen/logrus" +) + +func (a *MapBlockAccessor) FindMapBlocksByPos(lastpos *coords.MapBlockCoords, limit int, layerfilter []layer.Layer) (*FindMapBlocksResult, error) { + + 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 { + newlastpos = block.Pos + 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") + + key := getKey(block.Pos) + + mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos) + if err != nil { + return nil, err + } + + a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) + + a.c.Set(key, mapblock, cache.DefaultExpiration) + mblist = append(mblist, mapblock) + + } + + result.LastPos = newlastpos + result.List = mblist + + fields = logrus.Fields{ + "len(List)": len(result.List), + "unfilteredCount": result.UnfilteredCount, + "hasMore": result.HasMore, + "limit": limit, + } + logrus.WithFields(fields).Debug("FindMapBlocksByPos:Result") + + return &result, nil +} diff --git a/server/mapblockaccessor/mapblockaccessor.go b/server/mapblockaccessor/mapblockaccessor.go index 8de9919..14f985e 100644 --- a/server/mapblockaccessor/mapblockaccessor.go +++ b/server/mapblockaccessor/mapblockaccessor.go @@ -5,13 +5,11 @@ import ( "mapserver/coords" "mapserver/db" "mapserver/eventbus" - "mapserver/layer" "mapserver/mapblockparser" "time" cache "github.com/patrickmn/go-cache" - "github.com/sirupsen/logrus" ) type MapBlockAccessor struct { @@ -33,10 +31,6 @@ func NewMapBlockAccessor(accessor db.DBAccessor) *MapBlockAccessor { Eventbus: eventbus.New(), } } -func (a *MapBlockAccessor) Update(pos *coords.MapBlockCoords, mb *mapblockparser.MapBlock) { - key := getKey(pos) - a.c.Set(key, mb, cache.DefaultExpiration) -} type FindMapBlocksResult struct { HasMore bool @@ -45,174 +39,3 @@ type FindMapBlocksResult struct { List []*mapblockparser.MapBlock UnfilteredCount int } - -func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, layerfilter []layer.Layer) (*FindMapBlocksResult, error) { - - fields := logrus.Fields{ - "lastmtime": lastmtime, - "limit": limit, - } - logrus.WithFields(fields).Debug("FindMapBlocksByMtime") - - blocks, err := a.accessor.FindBlocksByMtime(lastmtime, 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 { - newlastpos = block.Pos - 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).Debug("mapblock") - - key := getKey(block.Pos) - - mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos) - if err != nil { - return nil, err - } - - a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) - - a.c.Set(key, mapblock, cache.DefaultExpiration) - mblist = append(mblist, mapblock) - - } - - result.LastPos = newlastpos - result.List = mblist - - return &result, nil -} - -func (a *MapBlockAccessor) FindMapBlocksByPos(lastpos *coords.MapBlockCoords, limit int, layerfilter []layer.Layer) (*FindMapBlocksResult, error) { - - 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 { - newlastpos = block.Pos - 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") - - key := getKey(block.Pos) - - mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos) - if err != nil { - return nil, err - } - - a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) - - a.c.Set(key, mapblock, cache.DefaultExpiration) - mblist = append(mblist, mapblock) - - } - - result.LastPos = newlastpos - result.List = mblist - - fields = logrus.Fields{ - "len(List)": len(result.List), - "unfilteredCount": result.UnfilteredCount, - "hasMore": result.HasMore, - "limit": limit, - } - logrus.WithFields(fields).Debug("FindMapBlocksByPos:Result") - - return &result, nil -} - -func (a *MapBlockAccessor) GetMapBlock(pos *coords.MapBlockCoords) (*mapblockparser.MapBlock, error) { - key := getKey(pos) - - cachedblock, found := a.c.Get(key) - if found { - return cachedblock.(*mapblockparser.MapBlock), nil - } - - block, err := a.accessor.GetBlock(pos) - if err != nil { - return nil, err - } - - if block == nil { - return nil, nil - } - - mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, pos) - if err != nil { - return nil, err - } - - a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) - - a.c.Set(key, mapblock, cache.DefaultExpiration) - - return mapblock, nil -} diff --git a/server/mapblockaccessor/mtime.go b/server/mapblockaccessor/mtime.go new file mode 100644 index 0000000..4dba785 --- /dev/null +++ b/server/mapblockaccessor/mtime.go @@ -0,0 +1,77 @@ +package mapblockaccessor + +import ( + "mapserver/coords" + "mapserver/eventbus" + "mapserver/layer" + "mapserver/mapblockparser" + + cache "github.com/patrickmn/go-cache" + "github.com/sirupsen/logrus" +) + +func (a *MapBlockAccessor) FindMapBlocksByMtime(lastmtime int64, limit int, layerfilter []layer.Layer) (*FindMapBlocksResult, error) { + + fields := logrus.Fields{ + "lastmtime": lastmtime, + "limit": limit, + } + logrus.WithFields(fields).Debug("FindMapBlocksByMtime") + + blocks, err := a.accessor.FindBlocksByMtime(lastmtime, 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 { + newlastpos = block.Pos + 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).Debug("mapblock") + + key := getKey(block.Pos) + + mapblock, err := mapblockparser.Parse(block.Data, block.Mtime, block.Pos) + if err != nil { + return nil, err + } + + a.Eventbus.Emit(eventbus.MAPBLOCK_RENDERED, mapblock) + + a.c.Set(key, mapblock, cache.DefaultExpiration) + mblist = append(mblist, mapblock) + + } + + result.LastPos = newlastpos + result.List = mblist + + return &result, nil +} diff --git a/server/mapblockaccessor/update.go b/server/mapblockaccessor/update.go new file mode 100644 index 0000000..7c971f4 --- /dev/null +++ b/server/mapblockaccessor/update.go @@ -0,0 +1,13 @@ +package mapblockaccessor + +import ( + "mapserver/coords" + "mapserver/mapblockparser" + + cache "github.com/patrickmn/go-cache" +) + +func (a *MapBlockAccessor) Update(pos *coords.MapBlockCoords, mb *mapblockparser.MapBlock) { + key := getKey(pos) + a.c.Set(key, mb, cache.DefaultExpiration) +}