2019-01-08 00:27:24 +03:00
|
|
|
package db
|
|
|
|
|
2019-01-08 10:57:50 +03:00
|
|
|
import (
|
|
|
|
"database/sql"
|
2019-01-18 11:13:37 +03:00
|
|
|
"errors"
|
2019-01-08 10:57:50 +03:00
|
|
|
_ "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-17 16:42:02 +03:00
|
|
|
"strings"
|
2019-01-18 11:13:37 +03:00
|
|
|
"time"
|
2019-01-08 10:57:50 +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 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-21 11:11:07 +03:00
|
|
|
const getLegacyBlockQuery = `
|
|
|
|
select pos,data,mtime
|
|
|
|
from blocks b
|
|
|
|
where b.mtime == 0
|
2019-01-21 13:18:27 +03:00
|
|
|
and b.pos > ?
|
2019-01-21 11:11:07 +03:00
|
|
|
order by b.pos asc
|
|
|
|
limit ?
|
|
|
|
`
|
|
|
|
|
|
|
|
func (db *Sqlite3Accessor) FindLegacyBlocks(lastpos coords.MapBlockCoords, limit int) ([]Block, error) {
|
|
|
|
blocks := make([]Block, 0)
|
|
|
|
pc := coords.CoordToPlain(lastpos)
|
|
|
|
|
|
|
|
rows, err := db.db.Query(getLegacyBlockQuery, pc, limit)
|
|
|
|
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-18 15:07:01 +03:00
|
|
|
const getLatestBlockQuery = `
|
|
|
|
select pos,data,mtime
|
|
|
|
from blocks b
|
2019-01-21 15:41:47 +03:00
|
|
|
where b.mtime > ?
|
2019-01-18 15:07:01 +03:00
|
|
|
order by b.mtime asc
|
|
|
|
limit ?
|
|
|
|
`
|
|
|
|
|
2019-01-09 14:45:54 +03:00
|
|
|
func (db *Sqlite3Accessor) FindLatestBlocks(mintime int64, limit int) ([]Block, error) {
|
2019-01-18 15:07:01 +03:00
|
|
|
blocks := make([]Block, 0)
|
|
|
|
|
|
|
|
rows, err := db.db.Query(getLatestBlockQuery, mintime, limit)
|
|
|
|
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-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-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)
|
|
|
|
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-18 11:13:37 +03:00
|
|
|
func sortAsc(a, b int) (int, int) {
|
2019-01-17 17:37:33 +03:00
|
|
|
if a > b {
|
|
|
|
return b, a
|
|
|
|
} else {
|
|
|
|
return a, b
|
|
|
|
}
|
|
|
|
}
|
2019-01-17 16:42:02 +03:00
|
|
|
|
2019-01-18 11:13:37 +03:00
|
|
|
func (db *Sqlite3Accessor) CountBlocks(pos1 coords.MapBlockCoords, pos2 coords.MapBlockCoords) (int, error) {
|
2019-01-17 16:42:02 +03:00
|
|
|
|
|
|
|
poslist := make([]interface{}, 0)
|
|
|
|
|
2019-01-17 17:37:33 +03:00
|
|
|
minX, maxX := sortAsc(pos1.X, pos2.X)
|
|
|
|
minY, maxY := sortAsc(pos1.Y, pos2.Y)
|
|
|
|
minZ, maxZ := sortAsc(pos1.Z, pos2.Z)
|
2019-01-17 16:42:02 +03:00
|
|
|
|
2019-01-17 17:37:33 +03:00
|
|
|
for x := minX; x <= maxX; x++ {
|
|
|
|
for y := minY; y <= maxY; y++ {
|
|
|
|
for z := minZ; z <= maxZ; z++ {
|
2019-01-18 11:13:37 +03:00
|
|
|
poslist = append(poslist, coords.CoordToPlain(coords.NewMapBlockCoords(x, y, z)))
|
2019-01-17 17:37:33 +03:00
|
|
|
}
|
|
|
|
}
|
2019-01-17 16:42:02 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 17:45:14 +03:00
|
|
|
if len(poslist) > 999 {
|
|
|
|
//https://stackoverflow.com/questions/7106016/too-many-sql-variables-error-in-django-witih-sqlite3
|
2019-01-17 17:59:59 +03:00
|
|
|
//TODO: return before nested for loops
|
2019-01-17 17:45:14 +03:00
|
|
|
return -1, nil
|
|
|
|
}
|
|
|
|
|
2019-01-17 17:14:13 +03:00
|
|
|
getBlocksQuery := "select count(*) from blocks b where b.pos in (?" + strings.Repeat(",?", len(poslist)-1) + ")"
|
2019-01-17 16:42:02 +03:00
|
|
|
|
|
|
|
rows, err := db.db.Query(getBlocksQuery, poslist...)
|
|
|
|
if err != nil {
|
2019-01-17 17:14:13 +03:00
|
|
|
return 0, err
|
2019-01-17 16:42:02 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 17:14:13 +03:00
|
|
|
defer rows.Close()
|
2019-01-17 16:42:02 +03:00
|
|
|
|
2019-01-17 17:14:13 +03:00
|
|
|
if rows.Next() {
|
|
|
|
var count int
|
2019-01-17 16:42:02 +03:00
|
|
|
|
2019-01-17 17:14:13 +03:00
|
|
|
err = rows.Scan(&count)
|
2019-01-17 16:42:02 +03:00
|
|
|
if err != nil {
|
2019-01-17 17:14:13 +03:00
|
|
|
return 0, err
|
2019-01-17 16:42:02 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 17:14:13 +03:00
|
|
|
return count, nil
|
2019-01-17 16:42:02 +03:00
|
|
|
}
|
|
|
|
|
2019-01-17 17:14:13 +03:00
|
|
|
return 0, errors.New("No rows returned")
|
2019-01-17 16:42:02 +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
|
|
|
}
|