1
0
forked from MTSR/mapserver

sqlite impl

This commit is contained in:
NatureFreshMilk 2019-02-14 08:34:16 +01:00
parent f9d0b78013
commit 0e169d2b45
8 changed files with 61 additions and 22 deletions

View File

@ -15,7 +15,7 @@ type Config struct {
EnableRendering bool `json:"enablerendering"` EnableRendering bool `json:"enablerendering"`
Webdev bool `json:"webdev"` Webdev bool `json:"webdev"`
WebApi *WebApiConfig `json:"webapi"` WebApi *WebApiConfig `json:"webapi"`
Layers []layer.Layer `json:"layers"` Layers []*layer.Layer `json:"layers"`
RenderingFetchLimit int `json:"renderingfetchlimit"` RenderingFetchLimit int `json:"renderingfetchlimit"`
RenderingJobs int `json:"renderingjobs"` RenderingJobs int `json:"renderingjobs"`
RenderingQueue int `json:"renderingqueue"` RenderingQueue int `json:"renderingqueue"`
@ -69,8 +69,8 @@ func ParseConfig(filename string) (*Config, error) {
SecretKey: RandStringRunes(16), SecretKey: RandStringRunes(16),
} }
layers := []layer.Layer{ layers := []*layer.Layer{
layer.Layer{ &layer.Layer{
Id: 0, Id: 0,
Name: "Base", Name: "Base",
From: -16, From: -16,

View File

@ -7,6 +7,8 @@ const (
maxPositive = modulo / 2 maxPositive = modulo / 2
minValue = -1 << (numBitsPerComponent - 1) minValue = -1 << (numBitsPerComponent - 1)
maxValue = 1<<(numBitsPerComponent-1) - 1 maxValue = 1<<(numBitsPerComponent-1) - 1
MinPlainCoord = -34351347711
) )
func CoordToPlain(c *MapBlockCoords) int64 { func CoordToPlain(c *MapBlockCoords) int64 {

View File

@ -12,6 +12,16 @@ func init() {
log = logrus.WithFields(logrus.Fields{"prefix": "coords/convert_test"}) log = logrus.WithFields(logrus.Fields{"prefix": "coords/convert_test"})
} }
func TestMinCoord(t *testing.T){
c := NewMapBlockCoords(MinCoord, MinCoord, MinCoord)
pc := CoordToPlain(c)
log.WithFields(logrus.Fields{"coords": c, "plain": pc}).Info("TestMinCoord")
if pc != MinPlainCoord {
t.Fatal("no min match")
}
}
func testCoordConvert(t *testing.T, mb *MapBlockCoords) { func testCoordConvert(t *testing.T, mb *MapBlockCoords) {
log.WithFields(logrus.Fields{"coords": mb}).Info("MapblockCoords") log.WithFields(logrus.Fields{"coords": mb}).Info("MapblockCoords")

View File

@ -14,6 +14,7 @@ type Block struct {
type InitialBlocksResult struct { type InitialBlocksResult struct {
List []*Block List []*Block
UnfilteredCount int
HasMore bool HasMore bool
} }

View File

@ -3,6 +3,8 @@ package postgres
import ( import (
"database/sql" "database/sql"
"mapserver/coords" "mapserver/coords"
"mapserver/settings"
"mapserver/layer"
"mapserver/db" "mapserver/db"
"time" "time"
@ -68,7 +70,7 @@ func (this *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db
return blocks, nil return blocks, nil
} }
func (this *PostgresAccessor) FindNextInitialBlocks(lastpos *coords.MapBlockCoords, limit int) (*db.InitialBlocksResult, error) { func (this *PostgresAccessor) FindNextInitialBlocks(s settings.Settings, layers []*layer.Layer, limit int) (*db.InitialBlocksResult, error) {
return nil, nil return nil, nil
} }

View File

@ -5,39 +5,41 @@ import (
"mapserver/coords" "mapserver/coords"
"mapserver/db" "mapserver/db"
"mapserver/settings" "mapserver/settings"
"mapserver/layer"
)
const (
SETTING_LAST_POS = "last_pos"
) )
const getLastBlockQuery = ` const getLastBlockQuery = `
select pos,data,mtime select pos,data,mtime
from blocks b from blocks b
where b.mtime = 0 where b.mtime = 0
and b.pos > ? and b.pos >= ?
order by b.pos asc, b.mtime asc order by b.pos asc, b.mtime asc
limit ? limit ?
` `
func (this *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers []layer.Layer, limit int) (*db.InitialBlocksResult, error) { func (this *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers []*layer.Layer, limit int) (*db.InitialBlocksResult, error) {
result := &db.InitialBlocksResult{} result := &db.InitialBlocksResult{}
blocks := make([]*db.Block, 0) blocks := make([]*db.Block, 0)
lastx := s.GetInt(settings.SETTING_LASTX, coords.MinCoord-1) lastpos := s.GetInt64(SETTING_LAST_POS, coords.MinPlainCoord)
lasty := s.GetInt(settings.SETTING_LASTY, coords.MinCoord-1)
lastz := s.GetInt(settings.SETTING_LASTZ, coords.MinCoord-1)
lastcoords := coords.NewMapBlockCoords(lastx, lasty, lastz) rows, err := this.db.Query(getLastBlockQuery, lastpos, limit)
pc := coords.CoordToPlain(lastpos)
rows, err := this.db.Query(getLastBlockQuery, pc, limit)
if err != nil { if err != nil {
return nil, err return nil, err
} }
defer rows.Close() defer rows.Close()
var newlastpos *coords.MapBlockCoords
for rows.Next() { for rows.Next() {
result.HasMore = true
result.UnfilteredCount++
var pos int64 var pos int64
var data []byte var data []byte
var mtime int64 var mtime int64
@ -48,17 +50,22 @@ func (this *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers [
} }
mb := convertRows(pos, data, mtime) mb := convertRows(pos, data, mtime)
newlastpos = mb.Pos
// new position
lastpos = pos
blockcoordy := mb.Pos.Y * 16
currentlayer := layer.FindLayerByY(layers, blockcoordy)
if currentlayer != nil {
blocks = append(blocks, mb) blocks = append(blocks, mb)
} }
}
result.List = blocks result.List = blocks
//Save current positions of initial run //Save current positions of initial run
s.SetInt(settings.SETTING_LASTX, newlastpos.X) s.SetInt64(SETTING_LAST_POS, lastpos)
s.SetInt(settings.SETTING_LASTY, newlastpos.Y)
s.SetInt(settings.SETTING_LASTZ, newlastpos.Z)
return result, nil return result, nil
} }

View File

@ -6,3 +6,21 @@ type Layer struct {
To int `json:"to"` To int `json:"to"`
From int `json:"from"` From int `json:"from"`
} }
func FindLayerById(layers []*Layer, id int) *Layer {
for _, l := range layers {
if l.Id == id {
return l
}
}
return nil
}
func FindLayerByY(layers []*Layer, y int) *Layer {
for _, l := range layers {
if y >= l.From && y <= l.To {
return l
}
}
return nil
}

View File

@ -4,6 +4,7 @@ import (
"mapserver/coords" "mapserver/coords"
"mapserver/eventbus" "mapserver/eventbus"
"mapserver/layer" "mapserver/layer"
"mapserver/settings"
"mapserver/mapblockparser" "mapserver/mapblockparser"
cache "github.com/patrickmn/go-cache" cache "github.com/patrickmn/go-cache"
@ -12,13 +13,11 @@ import (
type FindNextLegacyBlocksResult struct { type FindNextLegacyBlocksResult struct {
HasMore bool HasMore bool
LastPos *coords.MapBlockCoords
LastMtime int64
List []*mapblockparser.MapBlock List []*mapblockparser.MapBlock
UnfilteredCount int UnfilteredCount int
} }
func (a *MapBlockAccessor) FindNextLegacyBlocks(lastpos *coords.MapBlockCoords, limit int, layerfilter []layer.Layer) (*FindNextLegacyBlocksResult, error) { func (a *MapBlockAccessor) FindNextLegacyBlocks(s settings.Settings, layers []layer.Layer, limit int) (*FindNextLegacyBlocksResult, error) {
fields := logrus.Fields{ fields := logrus.Fields{
"x": lastpos.X, "x": lastpos.X,