sqlite tile db

This commit is contained in:
NatureFreshMilk 2019-01-16 16:36:28 +01:00
parent e0da8308e2
commit fa66f682e4
3 changed files with 57 additions and 43 deletions

View File

@ -14,5 +14,5 @@ type Tile struct {
type DBAccessor interface { type DBAccessor interface {
Migrate() error Migrate() error
GetTile(layerId int, pos coords.TileCoords) (*Tile, error) GetTile(layerId int, pos coords.TileCoords) (*Tile, error)
SetTile(pos coords.TileCoords, tile *Tile) error SetTile(tile *Tile) error
} }

View File

@ -1,11 +1,11 @@
package tiledb package tiledb
import ( import (
"database/sql" "database/sql"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"mapserver/coords" "mapserver/coords"
"time" "time"
) )
const migrateScript = ` const migrateScript = `
@ -26,17 +26,17 @@ type Sqlite3Accessor struct {
} }
func (db *Sqlite3Accessor) Migrate() error { func (db *Sqlite3Accessor) Migrate() error {
log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database") log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database")
start := time.Now() start := time.Now()
_, err := db.db.Exec(migrateScript) _, err := db.db.Exec(migrateScript)
if err != nil { if err != nil {
return err return err
} }
t := time.Now() t := time.Now()
elapsed := t.Sub(start) elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed") log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
return nil return nil
} }
const getTileQuery = ` const getTileQuery = `
@ -48,39 +48,46 @@ and t.zoom = ?
` `
func (db *Sqlite3Accessor) GetTile(layerId int, pos coords.TileCoords) (*Tile, error) { func (db *Sqlite3Accessor) GetTile(layerId int, pos coords.TileCoords) (*Tile, error) {
rows, err := db.db.Query(getTileQuery, layerId, pos.X, pos.Y, pos.Zoom) rows, err := db.db.Query(getTileQuery, layerId, pos.X, pos.Y, pos.Zoom)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if rows.Next() { if rows.Next() {
var data []byte var data []byte
var mtime int64 var mtime int64
err = rows.Scan(&data, &mtime) err = rows.Scan(&data, &mtime)
if err != nil { if err != nil {
return nil, err return nil, err
} }
if data == nil { if data == nil {
return nil, nil return nil, nil
} }
mb := Tile{ mb := Tile{
Pos: pos, Pos: pos,
LayerId: layerId, LayerId: layerId,
Data: data, Data: data,
Mtime: mtime, Mtime: mtime,
} }
return &mb, nil return &mb, nil
} }
return nil, nil return nil, nil
} }
func (db *Sqlite3Accessor) SetTile(pos coords.TileCoords, tile *Tile) error { const setTileQuery = `
return nil 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) { func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {

View File

@ -2,9 +2,9 @@ package tiledb
import ( import (
"io/ioutil" "io/ioutil"
"mapserver/coords"
"os" "os"
"testing" "testing"
"mapserver/coords"
) )
func TestMigrate(t *testing.T) { func TestMigrate(t *testing.T) {
@ -24,11 +24,18 @@ func TestMigrate(t *testing.T) {
panic(err) panic(err)
} }
pos := coords.NewTileCoords(0,0,13) pos := coords.NewTileCoords(0, 0, 13)
_, err = db.GetTile(0, pos) _, err = db.GetTile(0, pos)
if err != nil { if err != nil {
panic(err) panic(err)
} }
data := []byte{}
tile := Tile{LayerId: 0, Pos: pos, Data: data}
err = db.SetTile(&tile)
if err != nil {
panic(err)
}
} }