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 {
Migrate() 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
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) {

View File

@ -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)
}
}