2019-01-08 00:27:24 +03:00
|
|
|
package db
|
|
|
|
|
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-01-09 11:56:36 +03:00
|
|
|
"time"
|
2019-01-08 10:57:50 +03:00
|
|
|
)
|
|
|
|
|
2019-01-08 18:33:49 +03:00
|
|
|
const migrateScript = `
|
2019-01-09 18:31:24 +03:00
|
|
|
alter table blocks add mtime integer default 0;
|
2019-01-09 10:44:14 +03:00
|
|
|
create index blocks_mtime on blocks(mtime);
|
|
|
|
|
|
|
|
CREATE TRIGGER update_blocks_mtime_insert after insert on blocks for each row
|
|
|
|
begin
|
2019-01-09 14:45:54 +03:00
|
|
|
update blocks set mtime = strftime('%s', 'now') where pos = new.pos;
|
2019-01-09 10:44:14 +03:00
|
|
|
end;
|
|
|
|
|
|
|
|
CREATE TRIGGER update_blocks_mtime_update after update on blocks for each row
|
|
|
|
begin
|
2019-01-09 14:45:54 +03:00
|
|
|
update blocks set mtime = strftime('%s', 'now') where pos = old.pos;
|
2019-01-09 10:44:14 +03:00
|
|
|
end;
|
2019-01-08 18:33:49 +03:00
|
|
|
`
|
|
|
|
|
2019-01-09 18:31:24 +03:00
|
|
|
//TODO: initial run: https://stackoverflow.com/questions/14468586/efficient-paging-in-sqlite-with-millions-of-records
|
|
|
|
//TODO: postgres test
|
|
|
|
|
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-01-09 14:45:54 +03:00
|
|
|
func convertRows(pos int64, data []byte, mtime int64) Block {
|
|
|
|
c := coords.PlainToCoord(pos)
|
2019-01-13 18:37:03 +03:00
|
|
|
return Block{Pos: c, Data: data, Mtime: mtime}
|
2019-01-08 00:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-09 14:45:54 +03:00
|
|
|
func (db *Sqlite3Accessor) FindLatestBlocks(mintime int64, limit int) ([]Block, error) {
|
2019-01-08 10:57:50 +03:00
|
|
|
return make([]Block, 0), nil
|
2019-01-08 00:27:24 +03:00
|
|
|
}
|
|
|
|
|
2019-01-09 14:45:54 +03:00
|
|
|
const getBlockQuery = `
|
|
|
|
select pos,data,mtime from blocks b where b.pos = ?
|
2019-01-09 13:11:45 +03:00
|
|
|
`
|
|
|
|
|
2019-01-09 14:45:54 +03:00
|
|
|
func (db *Sqlite3Accessor) GetBlock(pos coords.MapBlockCoords) (*Block, error) {
|
|
|
|
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-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)
|
|
|
|
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-01-08 10:46:12 +03:00
|
|
|
func NewSqliteAccessor(filename string) (*Sqlite3Accessor, error) {
|
2019-01-13 18:37:03 +03:00
|
|
|
db, err := sql.Open("sqlite3", filename+"?mode=ro")
|
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
|
|
|
}
|