diff --git a/tiledb/accessor.go b/tiledb/accessor.go index 1cc12ab..367a1e6 100644 --- a/tiledb/accessor.go +++ b/tiledb/accessor.go @@ -14,5 +14,5 @@ type Tile struct { type DBAccessor interface { Migrate() error GetTile(layerId int, pos coords.TileCoords) (*Tile, error) - SetTile(pos coords.TileCoords, tile *Tile) error + SetTile(tile *Tile) error } diff --git a/tiledb/sqlite.go b/tiledb/sqlite.go index bdc41fc..d9e6c73 100644 --- a/tiledb/sqlite.go +++ b/tiledb/sqlite.go @@ -1,11 +1,11 @@ package tiledb import ( - "database/sql" - _ "github.com/mattn/go-sqlite3" - "github.com/sirupsen/logrus" - "mapserver/coords" - "time" + "database/sql" + _ "github.com/mattn/go-sqlite3" + "github.com/sirupsen/logrus" + "mapserver/coords" + "time" ) const migrateScript = ` @@ -26,17 +26,17 @@ type Sqlite3Accessor struct { } func (db *Sqlite3Accessor) Migrate() error { - log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database") - start := time.Now() - _, err := db.db.Exec(migrateScript) - if err != nil { - return err - } - t := time.Now() - elapsed := t.Sub(start) - log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed") + log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database") + start := time.Now() + _, err := db.db.Exec(migrateScript) + if err != nil { + return err + } + t := time.Now() + elapsed := t.Sub(start) + log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed") - return nil + return nil } const getTileQuery = ` @@ -48,39 +48,46 @@ 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 - } + 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 + if rows.Next() { + var data []byte + var mtime int64 - err = rows.Scan(&data, &mtime) - if err != nil { - return nil, err - } + err = rows.Scan(&data, &mtime) + if err != nil { + return nil, err + } - if data == nil { - return nil, nil - } + if data == nil { + return nil, nil + } - mb := Tile{ - Pos: pos, - LayerId: layerId, - Data: data, - Mtime: mtime, - } + mb := Tile{ + Pos: pos, + LayerId: layerId, + Data: data, + Mtime: mtime, + } - return &mb, nil - } + return &mb, nil + } - return nil, nil + return nil, nil } -func (db *Sqlite3Accessor) SetTile(pos coords.TileCoords, tile *Tile) error { - return nil +const setTileQuery = ` +insert into tiles(x,y,zoom,layerid,data,mtime) +values(?, ?, ?, ?, ?, ?) +on conflict replace +` + +func (db *Sqlite3Accessor) SetTile(tile *Tile) error { + _, err := db.db.Query(setTileQuery, tile.Pos.X, tile.Pos.Y, tile.Pos.Zoom, tile.LayerId, tile.Data, tile.Mtime) + return err } func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) { diff --git a/tiledb/sqlite_test.go b/tiledb/sqlite_test.go index 58ac348..fea1aef 100644 --- a/tiledb/sqlite_test.go +++ b/tiledb/sqlite_test.go @@ -2,9 +2,9 @@ package tiledb import ( "io/ioutil" + "mapserver/coords" "os" "testing" - "mapserver/coords" ) func TestMigrate(t *testing.T) { @@ -24,11 +24,18 @@ func TestMigrate(t *testing.T) { panic(err) } - pos := coords.NewTileCoords(0,0,13) + pos := coords.NewTileCoords(0, 0, 13) _, err = db.GetTile(0, pos) if err != nil { panic(err) } + data := []byte{} + tile := Tile{LayerId: 0, Pos: pos, Data: data} + err = db.SetTile(&tile) + + if err != nil { + panic(err) + } }