mapserver/server/db/sqlite/sqlite.go

171 lines
3.2 KiB
Go
Raw Normal View History

2019-02-05 15:13:34 +03:00
package sqlite
2019-01-08 00:27:24 +03:00
2019-01-08 10:57:50 +03:00
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
2019-01-09 10:44:14 +03:00
"github.com/sirupsen/logrus"
2019-01-09 13:11:45 +03:00
"mapserver/coords"
2019-02-05 15:13:34 +03:00
"mapserver/db"
2019-01-18 11:13:37 +03:00
"time"
2019-01-08 10:57:50 +03:00
)
2019-01-23 15:13:32 +03:00
2019-01-23 10:20:27 +03:00
/*
sqlite extract: https://stackoverflow.com/questions/15448373/how-to-dump-a-file-stored-in-a-sqlite-database-as-a-blob
sqlite3 my.db "SELECT writefile('object0.gz', MyBlob) FROM MyTable WHERE id = 1"
*/
2019-01-08 10:57:50 +03:00
2019-01-08 00:27:24 +03:00
type Sqlite3Accessor struct {
2019-01-13 18:37:03 +03:00
db *sql.DB
2019-01-09 10:44:14 +03:00
filename string
2019-01-08 00:27:24 +03:00
}
func (db *Sqlite3Accessor) Migrate() error {
2019-01-09 10:44:14 +03:00
//RW connection
2019-01-13 18:37:03 +03:00
rwdb, err := sql.Open("sqlite3", db.filename+"?mode=rw")
2019-01-09 10:44:14 +03:00
if err != nil {
return err
}
defer rwdb.Close()
hasMtime := true
_, err = rwdb.Query("select max(mtime) from blocks")
if err != nil {
hasMtime = false
}
if !hasMtime {
2019-01-13 18:37:03 +03:00
log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database")
2019-01-09 11:56:36 +03:00
start := time.Now()
2019-01-09 10:44:14 +03:00
_, err = rwdb.Exec(migrateScript)
if err != nil {
return err
}
2019-01-09 11:56:36 +03:00
t := time.Now()
elapsed := t.Sub(start)
2019-01-13 18:37:03 +03:00
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
2019-01-09 10:44:14 +03:00
}
2019-01-08 00:27:24 +03:00
return nil
}
2019-02-06 10:38:08 +03:00
func convertRows(pos int64, data []byte, mtime int64) *db.Block {
2019-01-09 14:45:54 +03:00
c := coords.PlainToCoord(pos)
2019-02-06 10:38:08 +03:00
return &db.Block{Pos: c, Data: data, Mtime: mtime}
2019-01-08 00:27:24 +03:00
}
2019-02-06 10:38:08 +03:00
func (this *Sqlite3Accessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) {
blocks := make([]*db.Block, 0)
2019-01-24 17:56:37 +03:00
2019-02-05 15:13:34 +03:00
rows, err := this.db.Query(getBlocksByMtimeQuery, gtmtime, limit)
2019-01-24 17:56:37 +03:00
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var pos int64
var data []byte
var mtime int64
err = rows.Scan(&pos, &data, &mtime)
if err != nil {
return nil, err
}
mb := convertRows(pos, data, mtime)
blocks = append(blocks, mb)
}
return blocks, nil
}
2019-02-06 10:38:08 +03:00
func (this *Sqlite3Accessor) FindLegacyBlocksByPos(lastpos *coords.MapBlockCoords, limit int) ([]*db.Block, error) {
blocks := make([]*db.Block, 0)
2019-01-21 11:11:07 +03:00
pc := coords.CoordToPlain(lastpos)
2019-02-05 15:13:34 +03:00
rows, err := this.db.Query(getLastBlockQuery, pc, limit)
2019-01-21 11:11:07 +03:00
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var pos int64
var data []byte
var mtime int64
err = rows.Scan(&pos, &data, &mtime)
if err != nil {
return nil, err
}
mb := convertRows(pos, data, mtime)
blocks = append(blocks, mb)
}
return blocks, nil
}
2019-01-24 09:22:28 +03:00
func (db *Sqlite3Accessor) CountBlocks(frommtime, tomtime int64) (int, error) {
rows, err := db.db.Query(countBlocksQuery, frommtime, tomtime)
2019-01-23 14:46:45 +03:00
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
var count int64
err = rows.Scan(&count)
if err != nil {
return 0, err
}
return int(count), nil
}
return 0, nil
}
2019-02-06 10:38:08 +03:00
func (db *Sqlite3Accessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) {
2019-01-09 14:45:54 +03:00
ppos := coords.CoordToPlain(pos)
2019-01-09 13:11:45 +03:00
2019-01-09 14:45:54 +03:00
rows, err := db.db.Query(getBlockQuery, ppos)
2019-01-09 12:16:22 +03:00
if err != nil {
2019-01-09 14:45:54 +03:00
return nil, err
2019-01-09 12:16:22 +03:00
}
2019-01-17 17:14:13 +03:00
defer rows.Close()
2019-01-09 14:45:54 +03:00
if rows.Next() {
var pos int64
var data []byte
var mtime int64
2019-01-09 12:16:22 +03:00
2019-01-09 14:45:54 +03:00
err = rows.Scan(&pos, &data, &mtime)
if err != nil {
return nil, err
}
mb := convertRows(pos, data, mtime)
2019-02-06 10:38:08 +03:00
return mb, nil
2019-01-09 12:16:22 +03:00
}
2019-01-09 14:45:54 +03:00
return nil, nil
2019-01-08 00:27:24 +03:00
}
2019-02-05 15:13:34 +03:00
func New(filename string) (*Sqlite3Accessor, error) {
db, err := sql.Open("sqlite3", filename+"?mode=ro&_timeout=2000")
2019-01-08 10:57:50 +03:00
if err != nil {
return nil, err
}
2019-01-09 10:44:14 +03:00
sq := &Sqlite3Accessor{db: db, filename: filename}
2019-01-08 10:57:50 +03:00
return sq, nil
2019-01-08 00:27:24 +03:00
}