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

206 lines
3.7 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"
"embed"
2022-12-16 10:30:21 +03:00
"errors"
2019-01-09 13:11:45 +03:00
"mapserver/coords"
2019-02-05 15:13:34 +03:00
"mapserver/db"
2023-12-29 18:00:11 +03:00
"mapserver/types"
2019-01-18 11:13:37 +03:00
"time"
2019-02-15 19:53:32 +03:00
"github.com/sirupsen/logrus"
_ "modernc.org/sqlite"
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
}
//go:embed migrations/*.sql
var migrations embed.FS
2019-01-08 00:27:24 +03:00
func (db *Sqlite3Accessor) Migrate() error {
2019-01-09 10:44:14 +03:00
//RW connection
rwdb, err := sql.Open("sqlite", 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 {
2021-04-12 14:03:10 +03:00
log.WithFields(logrus.Fields{
"filename": db.filename,
}).Info("Migrating database, this might take a while depending on the mapblock-count")
2019-01-09 11:56:36 +03:00
start := time.Now()
2021-04-12 14:03:10 +03:00
sql, err := migrations.ReadFile("migrations/sqlite_mapdb_migrate.sql")
2021-04-12 14:03:10 +03:00
if err != nil {
return err
}
_, err = rwdb.Exec(string(sql))
2019-01-09 10:44:14 +03:00
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
}
2021-04-12 14:03:10 +03:00
func (a *Sqlite3Accessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) {
2019-02-06 10:38:08 +03:00
blocks := make([]*db.Block, 0)
2019-01-24 17:56:37 +03:00
2021-04-12 14:03:10 +03:00
rows, err := a.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-15 19:53:32 +03:00
func (db *Sqlite3Accessor) CountBlocks() (int, error) {
rows, err := db.db.Query(countBlocksQuery)
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-04-03 09:15:32 +03:00
func (db *Sqlite3Accessor) GetTimestamp() (int64, error) {
rows, err := db.db.Query(getTimestampQuery)
if err != nil {
return 0, err
}
defer rows.Close()
if rows.Next() {
var ts int64
err = rows.Scan(&ts)
if err != nil {
return 0, err
}
return ts, nil
}
return 0, nil
}
2023-12-29 18:00:11 +03:00
func (db *Sqlite3Accessor) GetBlock(pos *types.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) {
2022-03-06 13:52:40 +03:00
db, err := sql.Open("sqlite", filename+"?mode=ro")
if err != nil {
return nil, err
}
2022-12-16 10:30:21 +03:00
// sqlite connection limit
2022-03-06 13:52:40 +03:00
db.SetMaxOpenConns(1)
2022-12-16 10:30:21 +03:00
err = EnableWAL(db)
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
}
2022-12-16 10:30:21 +03:00
func EnableWAL(db *sql.DB) error {
result := db.QueryRow("pragma journal_mode;")
var mode string
err := result.Scan(&mode)
if err != nil {
return err
}
if mode != "wal" {
_, err = db.Exec("pragma journal_mode = wal;")
if err != nil {
return errors.New("couldn't switch the db-journal to wal-mode, please stop the minetest-engine to allow doing this or do it manually: " + err.Error())
}
}
return nil
}