consolidate objects and tiles

This commit is contained in:
NatureFreshMilk 2019-01-21 15:13:33 +01:00
parent 7e28727ec0
commit b3468536a7
15 changed files with 62 additions and 88 deletions

View File

@ -6,7 +6,7 @@ import (
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/params"
"mapserver/tiledb"
"mapserver/mapobjectdb"
"mapserver/tilerenderer"
"mapserver/worldconfig"
)
@ -21,7 +21,7 @@ type App struct {
Worldconfig worldconfig.WorldConfig
Blockdb db.DBAccessor
Tiledb tiledb.DBAccessor
Objectdb mapobjectdb.DBAccessor
BlockAccessor *mapblockaccessor.MapBlockAccessor
Colormapping *colormapping.ColorMapping

View File

@ -6,7 +6,7 @@ import (
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/params"
"mapserver/tiledb"
"mapserver/mapobjectdb"
"mapserver/tilerenderer"
"mapserver/worldconfig"
@ -58,7 +58,7 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
//tile database
a.Tiledb, err = tiledb.NewSqliteAccessor("tiles.sqlite")
a.Objectdb, err = mapobjectdb.NewSqliteAccessor("mapserver.sqlite")
if err != nil {
return nil, err
@ -66,7 +66,7 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
//migrate tile database
err = a.Tiledb.Migrate()
err = a.Objectdb.Migrate()
if err != nil {
return nil, err
@ -75,7 +75,7 @@ func Setup(p params.ParamsType, cfg *Config) (*App, error) {
//setup tile renderer
a.Tilerenderer = tilerenderer.NewTileRenderer(
a.Mapblockrenderer,
a.Tiledb,
a.Objectdb,
a.Blockdb,
a.Config.Layers,
)

View File

@ -42,7 +42,7 @@ func Job(ctx *app.App) {
for tc.Zoom > 1 {
tc = tc.GetZoomedOutTile()
ctx.Tiledb.RemoveTile(tc)
ctx.Objectdb.RemoveTile(tc)
}
}

View File

@ -4,6 +4,13 @@ import (
"mapserver/coords"
)
type Tile struct {
Pos *coords.TileCoords
Data []byte
Mtime int64
}
type MapObject struct {
//mapblock position
MBPos coords.MapBlockCoords
@ -24,7 +31,14 @@ type SearchQuery struct {
type DBAccessor interface {
Migrate() error
//Generic map objects (poi, etc)
GetMapData(q SearchQuery) ([]MapObject, error)
RemoveMapData(pos coords.MapBlockCoords) error
AddMapData(data MapObject) error
//tile data
GetTile(pos *coords.TileCoords) (*Tile, error)
SetTile(tile *Tile) error
RemoveTile(pos *coords.TileCoords) error
}

View File

@ -0,0 +1,18 @@
package mapobjectdb
import (
"mapserver/coords"
)
func (db *Sqlite3Accessor) GetMapData(q SearchQuery) ([]MapObject, error){
return nil, nil
}
func (db *Sqlite3Accessor) RemoveMapData(pos coords.MapBlockCoords) error {
return nil
}
func (db *Sqlite3Accessor) AddMapData(data MapObject) error {
return nil
}

View File

@ -9,7 +9,7 @@ import (
const migrateScript = `
create table if not exists objects(
id bigint primary key auto_increment,
id integer primary key autoincrement,
x int,
y int,
z int,
@ -18,7 +18,17 @@ create table if not exists objects(
posz int,
type varchar,
data blob,
mtime bigint
);
create table if not exists tiles(
data blob,
mtime bigint,
layerid int,
x int,
y int,
zoom int,
primary key(x,y,zoom,layerid)
);
`

View File

@ -1,4 +1,4 @@
package tiledb
package mapobjectdb
import (
"io/ioutil"

View File

@ -1,43 +1,11 @@
package tiledb
package mapobjectdb
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus"
"mapserver/coords"
"time"
)
const migrateScript = `
create table if not exists tiles(
data blob,
mtime bigint,
layerid int,
x int,
y int,
zoom int,
primary key(x,y,zoom,layerid)
);
`
type Sqlite3Accessor struct {
db *sql.DB
filename string
}
func (db *Sqlite3Accessor) Migrate() error {
log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database")
start := time.Now()
_, err := db.db.Exec(migrateScript)
if err != nil {
return err
}
t := time.Now()
elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
return nil
}
const getTileQuery = `
select data,mtime from tiles t

BIN
mapserver.sqlite Normal file

Binary file not shown.

View File

@ -1,18 +0,0 @@
package tiledb
import (
"mapserver/coords"
)
type Tile struct {
Pos *coords.TileCoords
Data []byte
Mtime int64
}
type DBAccessor interface {
Migrate() error
GetTile(pos *coords.TileCoords) (*Tile, error)
SetTile(tile *Tile) error
RemoveTile(pos *coords.TileCoords) error
}

View File

@ -1,11 +0,0 @@
package tiledb
import (
"github.com/sirupsen/logrus"
)
var log *logrus.Entry
func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "tiledb"})
}

View File

@ -10,7 +10,7 @@ import (
"mapserver/db"
"mapserver/layer"
"mapserver/mapblockrenderer"
"mapserver/tiledb"
"mapserver/mapobjectdb"
"time"
"github.com/disintegration/imaging"
@ -20,12 +20,12 @@ import (
type TileRenderer struct {
mapblockrenderer *mapblockrenderer.MapBlockRenderer
layers []layer.Layer
tdb tiledb.DBAccessor
tdb mapobjectdb.DBAccessor
dba db.DBAccessor
}
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
tdb tiledb.DBAccessor,
tdb mapobjectdb.DBAccessor,
dba db.DBAccessor,
layers []layer.Layer) *TileRenderer {
@ -188,7 +188,7 @@ func (tr *TileRenderer) RenderImage(tc *coords.TileCoords, cachedOnly bool) (*im
png.Encode(buf, img)
}
tile := tiledb.Tile{Pos: tc, Data: buf.Bytes(), Mtime: time.Now().Unix()}
tile := mapobjectdb.Tile{Pos: tc, Data: buf.Bytes(), Mtime: time.Now().Unix()}
tr.tdb.SetTile(&tile)
return img, nil

View File

@ -11,7 +11,7 @@ import (
"mapserver/mapblockaccessor"
"mapserver/mapblockrenderer"
"mapserver/testutils"
"mapserver/tiledb"
"mapserver/mapobjectdb"
"os"
"testing"
)
@ -49,7 +49,7 @@ func TestTileRender(t *testing.T) {
tiletmpfile, err := ioutil.TempFile("", "TestTileRenderTiles.*.sqlite")
defer os.Remove(tiletmpfile.Name())
tdb, _ := tiledb.NewSqliteAccessor(tiletmpfile.Name())
tdb, _ := mapobjectdb.NewSqliteAccessor(tiletmpfile.Name())
tdb.Migrate()
layers := []layer.Layer{

View File

@ -4,7 +4,6 @@ import (
"github.com/sirupsen/logrus"
"mapserver/app"
"mapserver/coords"
"mapserver/mapblockparser"
"time"
)
@ -23,9 +22,6 @@ func Job(ctx *app.App) {
panic(err)
}
//only mapblocks with valid layer
validmblist := make([]*mapblockparser.MapBlock, 0)
for _, mb := range mblist {
if mb.Mtime > rstate.LastMtime {
rstate.LastMtime = mb.Mtime
@ -34,19 +30,17 @@ func Job(ctx *app.App) {
tc := coords.GetTileCoordsFromMapBlock(mb.Pos, ctx.Config.Layers)
if tc == nil {
continue
panic("tile not in any layer")
}
validmblist = append(validmblist, mb)
for tc.Zoom > 1 {
tc = tc.GetZoomedOutTile()
ctx.Tiledb.RemoveTile(tc)
ctx.Objectdb.RemoveTile(tc)
}
}
//Render zoom 12-1
for _, mb := range validmblist {
for _, mb := range mblist {
tc := coords.GetTileCoordsFromMapBlock(mb.Pos, ctx.Config.Layers)
for tc.Zoom > 1 {
tc = tc.GetZoomedOutTile()
@ -71,7 +65,6 @@ func Job(ctx *app.App) {
if len(mblist) > 0 {
fields = logrus.Fields{
"count": len(mblist),
"validcount": len(validmblist),
"lastmtime": rstate.LastMtime,
}
logrus.WithFields(fields).Info("incremental update")

View File

@ -27,7 +27,7 @@ func (t *Tiles) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
zoom, _ := strconv.Atoi(parts[3])
c := coords.NewTileCoords(x, y, zoom, layerid)
tile, err := t.ctx.Tiledb.GetTile(c)
tile, err := t.ctx.Objectdb.GetTile(c)
if err != nil {
resp.WriteHeader(500)