From 08407ce0da657e5e4df2750b87299399b3db1e0a Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Fri, 18 Jan 2019 13:50:59 +0100 Subject: [PATCH] mapdb stub --- app/config.go | 18 +++++----- main.go | 4 +-- mapblockaccessor/mapblockaccessor.go | 4 +-- mapblockparser/mapblock.go | 2 +- mapdb/accessor.go | 27 +++++++++++++++ tileupdate/job.go | 52 ++++++++++++++-------------- 6 files changed, 67 insertions(+), 40 deletions(-) create mode 100644 mapdb/accessor.go diff --git a/app/config.go b/app/config.go index c6f2a33..09238da 100644 --- a/app/config.go +++ b/app/config.go @@ -7,11 +7,11 @@ import ( ) type Config struct { - Port int `json:"port"` - EnableInitialRendering bool `json:"enableinitialrendering"` - EnableIncrementalUpdate bool `json:"enableincrementalupdate"` - Webdev bool `json:"webdev"` - WebApi WebApiConfig `json:"webapi"` + Port int `json:"port"` + EnableInitialRendering bool `json:"enableinitialrendering"` + EnableIncrementalUpdate bool `json:"enableincrementalupdate"` + Webdev bool `json:"webdev"` + WebApi WebApiConfig `json:"webapi"` } type WebApiConfig struct { @@ -24,11 +24,11 @@ func ParseConfig(filename string) (*Config, error) { } cfg := Config{ - Port: 8080, - EnableInitialRendering: true, + Port: 8080, + EnableInitialRendering: true, EnableIncrementalUpdate: true, - Webdev: false, - WebApi: webapi, + Webdev: false, + WebApi: webapi, } info, err := os.Stat(filename) diff --git a/main.go b/main.go index 6703d13..108507c 100644 --- a/main.go +++ b/main.go @@ -2,14 +2,14 @@ package main import ( "encoding/json" + "fmt" "github.com/sirupsen/logrus" "mapserver/app" "mapserver/initialrenderer" "mapserver/layerconfig" "mapserver/params" - "mapserver/web" "mapserver/tileupdate" - "fmt" + "mapserver/web" ) func main() { diff --git a/mapblockaccessor/mapblockaccessor.go b/mapblockaccessor/mapblockaccessor.go index 267b369..911b7dd 100644 --- a/mapblockaccessor/mapblockaccessor.go +++ b/mapblockaccessor/mapblockaccessor.go @@ -29,7 +29,7 @@ func (a *MapBlockAccessor) Update(pos coords.MapBlockCoords, mb *mapblockparser. a.c.Set(key, mb, cache.DefaultExpiration) } -func (a *MapBlockAccessor) FindLatestMapBlocks(mintime int64, limit int) ([]*mapblockparser.MapBlock, error){ +func (a *MapBlockAccessor) FindLatestMapBlocks(mintime int64, limit int) ([]*mapblockparser.MapBlock, error) { blocks, err := a.accessor.FindLatestBlocks(mintime, limit) if err != nil { @@ -38,7 +38,7 @@ func (a *MapBlockAccessor) FindLatestMapBlocks(mintime int64, limit int) ([]*map mblist := make([]*mapblockparser.MapBlock, 0) - for _, block := range(blocks) { + for _, block := range blocks { key := getKey(block.Pos) mapblock, err := mapblockparser.Parse(block.Data, block.Mtime) diff --git a/mapblockparser/mapblock.go b/mapblockparser/mapblock.go index ecd5c09..b59ebd4 100644 --- a/mapblockparser/mapblock.go +++ b/mapblockparser/mapblock.go @@ -6,7 +6,7 @@ type MapBlock struct { Mapdata []byte `json:"mapdata"` Metadata Metadata `json:"metadata"` BlockMapping map[int]string `json:"blockmapping"` - Mtime int64 `json:"mtime"` + Mtime int64 `json:"mtime"` } type Metadata struct { diff --git a/mapdb/accessor.go b/mapdb/accessor.go new file mode 100644 index 0000000..24c0f3f --- /dev/null +++ b/mapdb/accessor.go @@ -0,0 +1,27 @@ +package mapdb + +import ( + "mapserver/coords" +) + +type MapData struct { + //mapblock position + MBPos coords.MapBlockCoords + //block position + X, Y, Z int + Type string + Data string + Mtime int64 +} + +type SearchQuery struct { + //block position (not mapblock) + Pos1, Pos2 coords.MapBlockCoords + Type string +} + +type DBAccessor interface { + Migrate() error + GetMapData(q SearchQuery) ([]MapData, error) + SetMapData(pos coords.MapBlockCoords, data []MapData) error +} diff --git a/tileupdate/job.go b/tileupdate/job.go index 7f4b82a..d43176d 100644 --- a/tileupdate/job.go +++ b/tileupdate/job.go @@ -1,38 +1,38 @@ package tileupdate import ( - "mapserver/app" - "github.com/sirupsen/logrus" - "time" + "github.com/sirupsen/logrus" + "mapserver/app" + "time" ) -func Job(ctx *app.App){ - t := time.Now().Unix() +func Job(ctx *app.App) { + t := time.Now().Unix() - fields := logrus.Fields{ - "time": t, - } - logrus.WithFields(fields).Info("Starting incremental update") + fields := logrus.Fields{ + "time": t, + } + logrus.WithFields(fields).Info("Starting incremental update") - for true { - mblist, err := ctx.BlockAccessor.FindLatestMapBlocks(t, 1000) + for true { + mblist, err := ctx.BlockAccessor.FindLatestMapBlocks(t, 1000) - if err != nil { - panic(err) - } + if err != nil { + panic(err) + } - fields = logrus.Fields{ - "count": len(mblist), - "time": t, - } - logrus.WithFields(fields).Info("incremental update") + for _, mb := range mblist { + if mb.Mtime > t { + t = mb.Mtime + 1 + } + } - for _, mb := range(mblist) { - if mb.Mtime > t { - t = mb.Mtime+1 - } - } + fields = logrus.Fields{ + "count": len(mblist), + "time": t, + } + logrus.WithFields(fields).Info("incremental update") - time.Sleep(5 * time.Second) - } + time.Sleep(5 * time.Second) + } }