common sql accessor

This commit is contained in:
NatureFreshMilk 2019-02-13 15:44:05 +01:00
parent 2119b09f85
commit f9d0b78013
3 changed files with 28 additions and 11 deletions

View File

@ -2,6 +2,8 @@ package db
import (
"mapserver/coords"
"mapserver/settings"
"mapserver/layer"
)
type Block struct {
@ -19,7 +21,7 @@ type DBAccessor interface {
Migrate() error
FindBlocksByMtime(gtmtime int64, limit int) ([]*Block, error)
FindNextInitialBlocks(lastpos *coords.MapBlockCoords, limit int) (*InitialBlocksResult, error)
FindNextInitialBlocks(s settings.Settings, layers []layer.Layer, limit int) (*InitialBlocksResult, error)
CountBlocks(frommtime, tomtime int64) (int, error)
GetBlock(pos *coords.MapBlockCoords) (*Block, error)

View File

@ -4,13 +4,29 @@ import (
_ "github.com/mattn/go-sqlite3"
"mapserver/coords"
"mapserver/db"
"mapserver/settings"
)
func (this *Sqlite3Accessor) FindNextInitialBlocks(lastpos *coords.MapBlockCoords, limit int) (*db.InitialBlocksResult, error) {
const getLastBlockQuery = `
select pos,data,mtime
from blocks b
where b.mtime = 0
and b.pos > ?
order by b.pos asc, b.mtime asc
limit ?
`
func (this *Sqlite3Accessor) FindNextInitialBlocks(s settings.Settings, layers []layer.Layer, limit int) (*db.InitialBlocksResult, error) {
result := &db.InitialBlocksResult{}
blocks := make([]*db.Block, 0)
lastx := s.GetInt(settings.SETTING_LASTX, coords.MinCoord-1)
lasty := s.GetInt(settings.SETTING_LASTY, coords.MinCoord-1)
lastz := s.GetInt(settings.SETTING_LASTZ, coords.MinCoord-1)
lastcoords := coords.NewMapBlockCoords(lastx, lasty, lastz)
pc := coords.CoordToPlain(lastpos)
rows, err := this.db.Query(getLastBlockQuery, pc, limit)
@ -19,6 +35,7 @@ func (this *Sqlite3Accessor) FindNextInitialBlocks(lastpos *coords.MapBlockCoord
}
defer rows.Close()
var newlastpos *coords.MapBlockCoords
for rows.Next() {
var pos int64
@ -31,10 +48,17 @@ func (this *Sqlite3Accessor) FindNextInitialBlocks(lastpos *coords.MapBlockCoord
}
mb := convertRows(pos, data, mtime)
newlastpos = mb.Pos
blocks = append(blocks, mb)
}
result.List = blocks
//Save current positions of initial run
s.SetInt(settings.SETTING_LASTX, newlastpos.X)
s.SetInt(settings.SETTING_LASTY, newlastpos.Y)
s.SetInt(settings.SETTING_LASTZ, newlastpos.Z)
return result, nil
}

View File

@ -8,15 +8,6 @@ order by b.mtime asc
limit ?
`
const getLastBlockQuery = `
select pos,data,mtime
from blocks b
where b.mtime = 0
and b.pos > ?
order by b.pos asc, b.mtime asc
limit ?
`
const countBlocksQuery = `
select count(*) from blocks b where b.mtime >= ? and b.mtime <= ?
`