From 03598d496090c48a9a04e3041524c1c86a2bf4a1 Mon Sep 17 00:00:00 2001 From: NatureFreshMilk Date: Mon, 14 Jan 2019 12:48:46 +0100 Subject: [PATCH] tile.get --- tiledb/accessor.go | 2 +- tiledb/sqlite.go | 42 +++++++++++++++++++++++++++++++++++++++--- tiledb/sqlite_test.go | 9 +++++++++ 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/tiledb/accessor.go b/tiledb/accessor.go index cc96cc7..1cc12ab 100644 --- a/tiledb/accessor.go +++ b/tiledb/accessor.go @@ -13,6 +13,6 @@ type Tile struct { type DBAccessor interface { Migrate() error - GetTile(pos coords.TileCoords) (*Tile, error) + GetTile(layerId int, pos coords.TileCoords) (*Tile, error) SetTile(pos coords.TileCoords, tile *Tile) error } diff --git a/tiledb/sqlite.go b/tiledb/sqlite.go index ab6b3da..bdc41fc 100644 --- a/tiledb/sqlite.go +++ b/tiledb/sqlite.go @@ -12,11 +12,11 @@ const migrateScript = ` create table if not exists tiles( data blob, mtime bigint, - layer int, + layerid int, x int, y int, zoom int, - primary key(x,y,zoom,layer) + primary key(x,y,zoom,layerid) ); ` @@ -39,7 +39,43 @@ func (db *Sqlite3Accessor) Migrate() error { return nil } -func (db *Sqlite3Accessor) GetTile(pos coords.TileCoords) (*Tile, error) { +const getTileQuery = ` +select data,mtime from tiles t +where t.layerid = ? +and t.x = ? +and t.y = ? +and t.zoom = ? +` + +func (db *Sqlite3Accessor) GetTile(layerId int, pos coords.TileCoords) (*Tile, error) { + rows, err := db.db.Query(getTileQuery, layerId, pos.X, pos.Y, pos.Zoom) + if err != nil { + return nil, err + } + + if rows.Next() { + var data []byte + var mtime int64 + + err = rows.Scan(&data, &mtime) + if err != nil { + return nil, err + } + + if data == nil { + return nil, nil + } + + mb := Tile{ + Pos: pos, + LayerId: layerId, + Data: data, + Mtime: mtime, + } + + return &mb, nil + } + return nil, nil } diff --git a/tiledb/sqlite_test.go b/tiledb/sqlite_test.go index b60ef5d..58ac348 100644 --- a/tiledb/sqlite_test.go +++ b/tiledb/sqlite_test.go @@ -4,6 +4,7 @@ import ( "io/ioutil" "os" "testing" + "mapserver/coords" ) func TestMigrate(t *testing.T) { @@ -22,4 +23,12 @@ func TestMigrate(t *testing.T) { if err != nil { panic(err) } + + pos := coords.NewTileCoords(0,0,13) + _, err = db.GetTile(0, pos) + if err != nil { + panic(err) + } + + }