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

150 lines
2.7 KiB
Go
Raw Normal View History

2019-02-05 15:13:34 +03:00
package postgres
import (
"database/sql"
"mapserver/coords"
"mapserver/db"
2019-02-14 11:02:46 +03:00
"mapserver/vfs"
2019-02-09 20:54:26 +03:00
"time"
_ "github.com/lib/pq"
"github.com/sirupsen/logrus"
2019-02-05 15:13:34 +03:00
)
type PostgresAccessor struct {
2019-02-07 10:02:13 +03:00
db *sql.DB
2019-02-05 15:13:34 +03:00
}
func (db *PostgresAccessor) Migrate() error {
2019-02-09 20:54:26 +03:00
hasMtime := true
_, err := db.db.Query("select max(mtime) from blocks")
if err != nil {
hasMtime = false
}
if !hasMtime {
log.Info("Migrating database")
start := time.Now()
2019-02-14 11:02:46 +03:00
_, err = db.db.Exec(vfs.FSMustString(false, "/sql/postgres_mapdb_migrate.sql"))
2019-02-09 20:54:26 +03:00
if err != nil {
return err
}
t := time.Now()
elapsed := t.Sub(start)
log.WithFields(logrus.Fields{"elapsed": elapsed}).Info("Migration completed")
}
2019-02-05 15:13:34 +03:00
return nil
}
2019-02-14 11:09:04 +03:00
func convertRows(posx, posy, posz int, data []byte, mtime int64) *db.Block {
c := coords.NewMapBlockCoords(posx, posy, posz)
2019-02-06 10:38:08 +03:00
return &db.Block{Pos: c, Data: data, Mtime: mtime}
2019-02-05 15:13:34 +03:00
}
2019-02-06 10:38:08 +03:00
func (this *PostgresAccessor) FindBlocksByMtime(gtmtime int64, limit int) ([]*db.Block, error) {
blocks := make([]*db.Block, 0)
2019-02-05 15:13:34 +03:00
rows, err := this.db.Query(getBlocksByMtimeQuery, gtmtime, limit)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
2019-02-14 11:09:04 +03:00
var posx, posy, posz int
2019-02-05 15:13:34 +03:00
var data []byte
var mtime int64
2019-02-14 11:09:04 +03:00
err = rows.Scan(&posx, &posy, &posz, &data, &mtime)
2019-02-05 15:13:34 +03:00
if err != nil {
return nil, err
}
2019-02-14 11:09:04 +03:00
mb := convertRows(posx, posy, posz, data, mtime)
2019-02-05 15:13:34 +03:00
blocks = append(blocks, mb)
}
return blocks, nil
}
func (this *PostgresAccessor) CountBlocks(frommtime, tomtime int64) (int, error) {
rows, err := this.db.Query(countBlocksQuery, frommtime, tomtime)
if err != nil {
2019-02-14 21:59:35 +03:00
panic(err)
2019-02-05 15:13:34 +03:00
}
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 *PostgresAccessor) 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
}
2019-02-06 10:38:08 +03:00
func (this *PostgresAccessor) GetBlock(pos *coords.MapBlockCoords) (*db.Block, error) {
2019-02-06 18:10:18 +03:00
rows, err := this.db.Query(getBlockQuery, pos.X, pos.Y, pos.Z)
2019-02-05 15:13:34 +03:00
if err != nil {
return nil, err
}
defer rows.Close()
if rows.Next() {
2019-02-14 11:09:04 +03:00
var posx, posy, posz int
2019-02-05 15:13:34 +03:00
var data []byte
var mtime int64
2019-02-14 11:09:04 +03:00
err = rows.Scan(&posx, &posy, &posz, &data, &mtime)
2019-02-05 15:13:34 +03:00
if err != nil {
return nil, err
}
2019-02-14 11:09:04 +03:00
mb := convertRows(posx, posy, posz, data, mtime)
2019-02-06 10:38:08 +03:00
return mb, nil
2019-02-05 15:13:34 +03:00
}
return nil, nil
}
2019-02-09 20:54:26 +03:00
func New(connStr string) (*PostgresAccessor, error) {
2019-02-14 21:59:35 +03:00
db, err := sql.Open("postgres", connStr+" sslmode=disable")
2019-02-09 20:54:26 +03:00
if err != nil {
return nil, err
}
sq := &PostgresAccessor{db: db}
return sq, nil
2019-02-05 15:13:34 +03:00
}