1
0
forked from MTSR/mapserver

simplify mapobject db (default to max/min positions at world border)

This commit is contained in:
BuckarooBanzay 2022-01-17 09:06:07 +01:00
parent 25e177fbb9
commit 13b395143d
5 changed files with 41 additions and 81 deletions

View File

@ -4,19 +4,15 @@ import (
"database/sql" "database/sql"
"mapserver/coords" "mapserver/coords"
"mapserver/mapobjectdb" "mapserver/mapobjectdb"
"github.com/sirupsen/logrus"
"unicode/utf8" "unicode/utf8"
"github.com/sirupsen/logrus"
) )
func (db *PostgresAccessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectdb.MapObject, error) { func (db *PostgresAccessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectdb.MapObject, error) {
var rows *sql.Rows var rows *sql.Rows
var err error var err error
var limit = 1000
if q.Limit != nil {
limit = *q.Limit
}
if q.AttributeLike == nil { if q.AttributeLike == nil {
//plain pos search //plain pos search
@ -24,27 +20,18 @@ func (db *PostgresAccessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobject
q.Type, q.Type,
q.Pos1.X, q.Pos1.Y, q.Pos1.Z, q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Pos2.X, q.Pos2.Y, q.Pos2.Z, q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
limit, *q.Limit,
) )
} else { } else {
if (q.Pos1 == nil || q.Pos2 == nil) { //attribute like search
//global attribute like search rows, err = db.db.Query(getMapDataWithAttributeLikePosQuery,
rows, err = db.db.Query(getMapDataWithAttributeLikeGlobalQuery, q.Type,
q.AttributeLike.Key, q.AttributeLike.Value, q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Type, q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
limit, q.AttributeLike.Key, q.AttributeLike.Value,
) *q.Limit,
} else { )
//attribute like search
rows, err = db.db.Query(getMapDataWithAttributeLikePosQuery,
q.Type,
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
q.AttributeLike.Key, q.AttributeLike.Value,
limit,
)
}
} }
if err != nil { if err != nil {
@ -110,9 +97,9 @@ func (db *PostgresAccessor) AddMapData(data *mapobjectdb.MapObject) error {
if !utf8.Valid([]byte(v)) { if !utf8.Valid([]byte(v)) {
// invalid utf8, skip insert into db // invalid utf8, skip insert into db
fields := logrus.Fields{ fields := logrus.Fields{
"type": data.Type, "type": data.Type,
"value": v, "value": v,
"key": k, "key": k,
} }
log.WithFields(fields).Info("Migration completed") log.WithFields(fields).Info("Migration completed")
return nil return nil

View File

@ -29,20 +29,6 @@ and o.posx <= $5 and o.posy <= $6 and o.posz <= $7
order by o.id order by o.id
limit $10 limit $10
` `
const getMapDataWithAttributeLikeGlobalQuery = `
select o.id, o.type, o.mtime,
o.x, o.y, o.z,
o.posx, o.posy, o.posz,
oa.key, oa.value
from objects o
left join object_attributes oa on o.id = oa.objectid
where o.id in (
select objectid from object_attributes where key = $2 and value ilike $3
)
and o.type = $1
order by o.id
limit $4
`
const removeMapDataQuery = ` const removeMapDataQuery = `
delete from objects where posx = $1 and posy = $2 and posz = $3 delete from objects where posx = $1 and posy = $2 and posz = $3

View File

@ -4,8 +4,9 @@ import (
"database/sql" "database/sql"
"mapserver/coords" "mapserver/coords"
"mapserver/mapobjectdb" "mapserver/mapobjectdb"
"github.com/sirupsen/logrus"
"unicode/utf8" "unicode/utf8"
"github.com/sirupsen/logrus"
) )
func (db *Sqlite3Accessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectdb.MapObject, error) { func (db *Sqlite3Accessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectdb.MapObject, error) {
@ -13,38 +14,24 @@ func (db *Sqlite3Accessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectd
var rows *sql.Rows var rows *sql.Rows
var err error var err error
var limit = 1000
if q.Limit != nil {
limit = *q.Limit
}
if q.AttributeLike == nil { if q.AttributeLike == nil {
//plain pos search //plain pos search
rows, err = db.db.Query(getMapDataPosQuery, rows, err = db.db.Query(getMapDataPosQuery,
q.Type, q.Type,
q.Pos1.X, q.Pos1.Y, q.Pos1.Z, q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Pos2.X, q.Pos2.Y, q.Pos2.Z, q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
limit, *q.Limit,
) )
} else { } else {
if (q.Pos1 == nil || q.Pos2 == nil) { //attribute like search
//global attribute like search rows, err = db.db.Query(getMapDataWithAttributeLikePosQuery,
rows, err = db.db.Query(getMapDataWithAttributeLikeGlobalQuery, q.AttributeLike.Key, q.AttributeLike.Value,
q.AttributeLike.Key, q.AttributeLike.Value, q.Type,
q.Type, q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
limit, q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
) *q.Limit,
} else { )
//attribute like search
rows, err = db.db.Query(getMapDataWithAttributeLikePosQuery,
q.AttributeLike.Key, q.AttributeLike.Value,
q.Type,
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
limit,
)
}
} }
if err != nil { if err != nil {
@ -110,9 +97,9 @@ func (db *Sqlite3Accessor) AddMapData(data *mapobjectdb.MapObject) error {
if !utf8.Valid([]byte(v)) { if !utf8.Valid([]byte(v)) {
// invalid utf8, skip insert into db // invalid utf8, skip insert into db
fields := logrus.Fields{ fields := logrus.Fields{
"type": data.Type, "type": data.Type,
"value": v, "value": v,
"key": k, "key": k,
} }
log.WithFields(fields).Info("Migration completed") log.WithFields(fields).Info("Migration completed")
return nil return nil

View File

@ -31,21 +31,6 @@ order by o.id
limit ? limit ?
` `
const getMapDataWithAttributeLikeGlobalQuery = `
select o.id, o.type, o.mtime,
o.x, o.y, o.z,
o.posx, o.posy, o.posz,
oa.key, oa.value
from objects o
left join object_attributes oa on o.id = oa.objectid
where o.id in (
select objectid from object_attributes where key = ? and value like ?
)
and o.type = ?
order by o.id
limit ?
`
const removeMapDataQuery = ` const removeMapDataQuery = `
delete from objects where posx = ? and posy = ? and posz = ? delete from objects where posx = ? and posy = ? and posz = ?
` `

View File

@ -2,6 +2,7 @@ package web
import ( import (
"encoding/json" "encoding/json"
"mapserver/coords"
"mapserver/mapobjectdb" "mapserver/mapobjectdb"
"net/http" "net/http"
@ -23,6 +24,20 @@ func (api *Api) QueryMapobjects(resp http.ResponseWriter, req *http.Request) {
return return
} }
// apply defaults
limit := 1000
if q.Limit != nil {
q.Limit = &limit
}
if q.Pos1 == nil {
q.Pos1 = &coords.MapBlockCoords{X: -2048, Y: -2048, Z: -2048}
}
if q.Pos2 == nil {
q.Pos2 = &coords.MapBlockCoords{X: 2048, Y: 2048, Z: 2048}
}
objects, err := api.Context.Objectdb.GetMapData(&q) objects, err := api.Context.Objectdb.GetMapData(&q)
if err != nil { if err != nil {
resp.WriteHeader(500) resp.WriteHeader(500)