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 { type DBAccessor interface {
Migrate() error Migrate() error
FindLatestBlocks(mintime int64, limit int) ([]Block, 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) GetBlock(pos coords.MapBlockCoords) (*Block, error)
} }

View File

@ -85,6 +85,8 @@ func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) {
return nil, err return nil, err
} }
defer rows.Close()
if rows.Next() { if rows.Next() {
var pos int64 var pos int64
var data []byte 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) poslist := make([]interface{}, 0)
if pos1.X != pos2.X { 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 { 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 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))) 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...) rows, err := db.db.Query(getBlocksQuery, poslist...)
if err != nil { if err != nil {
return nil, err return 0, err
} }
blocklist := make([]Block, 0) defer rows.Close()
for rows.Next() { if rows.Next() {
var pos int64 var count int
var data []byte
var mtime int64
err = rows.Scan(&pos, &data, &mtime) err = rows.Scan(&count)
if err != nil { if err != nil {
return nil, err return 0, err
} }
mb := convertRows(pos, data, mtime) return count, nil
blocklist = append(blocklist, mb)
} }
return blocklist, nil return 0, errors.New("No rows returned")
} }
func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) { func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {

View File

@ -94,13 +94,13 @@ func TestMigrateAndQueryStride(t *testing.T) {
panic(err) 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 { if err != nil {
panic(err) panic(err)
} }
if blocks == nil { if count == 0 {
t.Fatal("no data") t.Fatal("no data")
} }

View File

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

View File

@ -7,6 +7,7 @@ import (
"bytes" "bytes"
"image/png" "image/png"
"image/draw" "image/draw"
"mapserver/db"
"mapserver/coords" "mapserver/coords"
"mapserver/mapblockrenderer" "mapserver/mapblockrenderer"
"mapserver/layerconfig" "mapserver/layerconfig"
@ -18,16 +19,19 @@ type TileRenderer struct {
mapblockrenderer *mapblockrenderer.MapBlockRenderer mapblockrenderer *mapblockrenderer.MapBlockRenderer
layers []layerconfig.Layer layers []layerconfig.Layer
tdb tiledb.DBAccessor tdb tiledb.DBAccessor
dba db.DBAccessor
} }
func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer, func NewTileRenderer(mapblockrenderer *mapblockrenderer.MapBlockRenderer,
tdb tiledb.DBAccessor, tdb tiledb.DBAccessor,
dba db.DBAccessor,
layers []layerconfig.Layer) *TileRenderer { layers []layerconfig.Layer) *TileRenderer {
return &TileRenderer{ return &TileRenderer{
mapblockrenderer: mapblockrenderer, mapblockrenderer: mapblockrenderer,
layers: layers, layers: layers,
tdb: tdb, tdb: tdb,
dba: dba,
} }
} }
@ -97,6 +101,16 @@ func (tr *TileRenderer) RenderImage(tc coords.TileCoords) (*image.NRGBA, error)
mbr.Pos1.Y = layer.From mbr.Pos1.Y = layer.From
mbr.Pos2.Y = layer.To 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) 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, _ := tiledb.NewSqliteAccessor(tiletmpfile.Name())
tdb.Migrate() tdb.Migrate()
tr := NewTileRenderer(r, tdb, layerconfig.DefaultLayers) tr := NewTileRenderer(r, tdb, a, layerconfig.DefaultLayers)
if tr == nil { if tr == nil {
panic("no renderer") panic("no renderer")