77 lines
1.5 KiB
Go
77 lines
1.5 KiB
Go
package mapobjectdb
|
|
|
|
import (
|
|
"database/sql"
|
|
_ "github.com/mattn/go-sqlite3"
|
|
"github.com/sirupsen/logrus"
|
|
"time"
|
|
)
|
|
|
|
const migrateScript = `
|
|
PRAGMA foreign_keys = ON;
|
|
|
|
create table if not exists objects(
|
|
id integer primary key autoincrement,
|
|
x int,
|
|
y int,
|
|
z int,
|
|
posx int,
|
|
posy int,
|
|
posz int,
|
|
type varchar,
|
|
mtime bigint
|
|
);
|
|
|
|
create index if not exists objects_pos on objects(posx,posy,posz);
|
|
create index if not exists objects_pos_type on objects(posx,posy,posz,type);
|
|
|
|
create table if not exists object_attributes(
|
|
objectid integer not null,
|
|
key varchar not null,
|
|
value varchar not null,
|
|
FOREIGN KEY (objectid) references objects(id) ON DELETE CASCADE
|
|
primary key(objectid, key)
|
|
);
|
|
|
|
create table if not exists tiles(
|
|
data blob,
|
|
mtime bigint,
|
|
layerid int,
|
|
x int,
|
|
y int,
|
|
zoom int,
|
|
primary key(x,y,zoom,layerid)
|
|
);
|
|
`
|
|
|
|
type Sqlite3Accessor struct {
|
|
db *sql.DB
|
|
filename string
|
|
}
|
|
|
|
func (db *Sqlite3Accessor) Migrate() error {
|
|
log.WithFields(logrus.Fields{"filename": db.filename}).Info("Migrating database")
|
|
start := time.Now()
|
|
_, err := db.db.Exec(migrateScript)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
t := time.Now()
|
|
elapsed := t.Sub(start)
|
|
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
|
|
|
|
return nil
|
|
}
|
|
|
|
func (db *Sqlite3Accessor) EnableSpeedSafetyTradeoff(enableSpeed bool) error {
|
|
if enableSpeed {
|
|
_, err := db.db.Exec("PRAGMA journal_mode = MEMORY; PRAGMA synchronous = OFF;")
|
|
return err
|
|
|
|
} else {
|
|
_, err := db.db.Exec("PRAGMA journal_mode = TRUNCATE; PRAGMA synchronous = ON;")
|
|
return err
|
|
|
|
}
|
|
}
|