From 5be4319aa66530cacdc3dbffb82a4abd2986c42b Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Thu, 17 Jan 2019 15:14:13 +0100 Subject: [PATCH] check count(*) --- db/accessor.go | 2 +- db/sqlite.go | 29 ++++++++++++++--------------- db/sqlite_test.go | 4 ++-- main.go | 2 +- tilerenderer/renderer.go | 14 ++++++++++++++ tilerenderer/renderer_test.go | 2 +- 6 files changed, 33 insertions(+), 20 deletions(-) diff --git a/db/accessor.go b/db/accessor.go index d1e87a5..b8ae77a 100644 --- a/db/accessor.go +++ b/db/accessor.go @@ -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) } diff --git a/db/sqlite.go b/db/sqlite.go index f352bb3..dfb554f 100644 --- a/db/sqlite.go +++ b/db/sqlite.go @@ -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) { diff --git a/db/sqlite_test.go b/db/sqlite_test.go index a38f32d..53b5cc1 100644 --- a/db/sqlite_test.go +++ b/db/sqlite_test.go @@ -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") } diff --git a/main.go b/main.go index 8e472c6..55fd42b 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/tilerenderer/renderer.go b/tilerenderer/renderer.go index ace418d..14f1660 100644 --- a/tilerenderer/renderer.go +++ b/tilerenderer/renderer.go @@ -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) } diff --git a/tilerenderer/renderer_test.go b/tilerenderer/renderer_test.go index cacdf33..a1c099c 100644 --- a/tilerenderer/renderer_test.go +++ b/tilerenderer/renderer_test.go @@ -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")