1
0
forked from MTSR/mapserver
mapserver/server/db/sqlite.go

179 lines
3.6 KiB
Go
Raw Normal View History

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-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 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-24 09:22:28 +03:00
const getLastBlockQuery = `
2019-01-21 11:11:07 +03:00
select pos,data,mtime
from blocks b
2019-01-24 09:22:28 +03:00
where b.mtime > ?
2019-01-21 13:18:27 +03:00
and b.pos > ?
2019-01-24 09:22:28 +03:00
order by b.pos asc, b.mtime asc
2019-01-21 11:11:07 +03:00
limit ?
`
2019-01-24 09:22:28 +03:00
func (db *Sqlite3Accessor) FindBlocks(lastpos coords.MapBlockCoords, lastmtime int64, limit int) ([]Block, error) {
2019-01-21 11:11:07 +03:00
blocks := make([]Block, 0)
pc := coords.CoordToPlain(lastpos)
2019-01-24 09:22:28 +03:00
rows, err := db.db.Query(getLastBlockQuery, lastmtime, 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
const countBlocksQuery = `
select count(*) from blocks b where b.mtime >= ? and b.mtime <= ?
2019-01-23 14:46:45 +03:00
`
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-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-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
}