check count(*)

This commit is contained in:
NatureFreshMilk 2019-01-17 15:14:13 +01:00
parent b480e0a9b9
commit 5be4319aa6
6 changed files with 33 additions and 20 deletions

View File

@ -13,6 +13,6 @@ type Block struct {
type DBAccessor interface {
Migrate() error
FindLatestBlocks(mintime int64, limit int) ([]Block, error)
GetBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) ([]Block, error)
CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error)
GetBlock(pos coords.MapBlockCoords) (*Block, error)
}

View File

@ -85,6 +85,8 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) {
return nil, err
}
defer rows.Close()
if rows.Next() {
var pos int64
var data []byte
@ -103,16 +105,16 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) {
}
func (db *Sqlite3Accessor) GetBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) ([]Block, error){
func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error){
poslist := make([]interface{}, 0)
if pos1.X != pos2.X {
return nil, errors.New("x does not line up")
return 0, errors.New("x does not line up")
}
if pos1.Z != pos2.Z {
return nil, errors.New("z does not line up")
return 0, errors.New("z does not line up")
}
minY := pos1.Y
@ -126,30 +128,27 @@ func (db *Sqlite3Accessor) GetBlocks(pos1 coords.MapBlockCoords, pos2 coords.Map
poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(pos1.X, y, pos1.Z)))
}
getBlocksQuery := "select pos,data,mtime from blocks b where b.pos in (?" + strings.Repeat(",?", len(poslist)-1) + ")"
getBlocksQuery := "select count(*) from blocks b where b.pos in (?" + strings.Repeat(",?", len(poslist)-1) + ")"
rows, err := db.db.Query(getBlocksQuery, poslist...)
if err != nil {
return nil, err
return 0, err
}
blocklist := make([]Block, 0)
defer rows.Close()
for rows.Next() {
var pos int64
var data []byte
var mtime int64
if rows.Next() {
var count int
err = rows.Scan(&pos, &data, &mtime)
err = rows.Scan(&count)
if err != nil {
return nil, err
return 0, err
}
mb := convertRows(pos, data, mtime)
blocklist = append(blocklist, mb)
return count, nil
}
return blocklist, nil
return 0, errors.New("No rows returned")
}
func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {

View File

@ -94,13 +94,13 @@ func TestMigrateAndQueryStride(t *testing.T) {
panic(err)
}
blocks, err := a.GetBlocks(coords.NewMapBlockCoords(0, -1, 0), coords.NewMapBlockCoords(0, 10, 0))
count, err := a.CountBlocks(coords.NewMapBlockCoords(0, -1, 0), coords.NewMapBlockCoords(0, 10, 0))
if err != nil {
panic(err)
}
if blocks == nil {
if count == 0 {
t.Fatal("no data")
}

View File

@ -75,7 +75,7 @@ func main() {
panic(err)
}
tr := tilerenderer.NewTileRenderer(r, tdb, layerconfig.DefaultLayers)
tr := tilerenderer.NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers)
initialrenderer.Render(tr, layerconfig.DefaultLayers)

View File

@ -7,6 +7,7 @@ import (
"bytes"
"image/png"
"image/draw"
"mapserver/db"
"mapserver/coords"
"mapserver/mapblockrenderer"
"mapserver/layerconfig"
@ -18,16 +19,19 @@ type TileRenderer struct {
mapblockrenderer *mapblockrenderer.MapBlockRenderer
layers []layerconfig.Layer
tdb tiledb.DBAccessor
dba db.DBAccessor
}
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
tdb tiledb.DBAccessor,
dba db.DBAccessor,
layers []layerconfig.Layer) *TileRenderer {
return &TileRenderer{
mapblockrenderer: mapblockrenderer,
layers: layers,
tdb: tdb,
dba: dba,
}
}
@ -97,6 +101,16 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error)
mbr.Pos1.Y = layer.From
mbr.Pos2.Y = layer.To
count, err := tr.dba.CountBlocks(mbr.Pos1, mbr.Pos2)
if err != nil {
return nil, err
}
if count == 0 {
return nil, nil
}
return tr.mapblockrenderer.Render(mbr.Pos1, mbr.Pos2)
}

View File

@ -52,7 +52,7 @@ func TestTileRender(t *testing.T) {
tdb, _ := tiledb.NewSqliteAccessor(tiletmpfile.Name())
tdb.Migrate()
tr := NewTileRenderer(r, tdb, layerconfig.DefaultLayers)
tr := NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers)
if tr == nil {
panic("no renderer")