forked from MTSR/mapserver
simplify mapobject db (default to max/min positions at world border)
This commit is contained in:
parent
25e177fbb9
commit
13b395143d
@ -4,19 +4,15 @@ import (
|
||||
"database/sql"
|
||||
"mapserver/coords"
|
||||
"mapserver/mapobjectdb"
|
||||
"github.com/sirupsen/logrus"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
func (db *PostgresAccessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectdb.MapObject, error) {
|
||||
|
||||
var rows *sql.Rows
|
||||
var err error
|
||||
var limit = 1000
|
||||
|
||||
if q.Limit != nil {
|
||||
limit = *q.Limit
|
||||
}
|
||||
|
||||
if q.AttributeLike == nil {
|
||||
//plain pos search
|
||||
@ -24,27 +20,18 @@ func (db *PostgresAccessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobject
|
||||
q.Type,
|
||||
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
|
||||
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
|
||||
limit,
|
||||
*q.Limit,
|
||||
)
|
||||
|
||||
} else {
|
||||
if (q.Pos1 == nil || q.Pos2 == nil) {
|
||||
//global attribute like search
|
||||
rows, err = db.db.Query(getMapDataWithAttributeLikeGlobalQuery,
|
||||
q.AttributeLike.Key, q.AttributeLike.Value,
|
||||
q.Type,
|
||||
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,
|
||||
)
|
||||
}
|
||||
//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,
|
||||
*q.Limit,
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -110,9 +97,9 @@ func (db *PostgresAccessor) AddMapData(data *mapobjectdb.MapObject) error {
|
||||
if !utf8.Valid([]byte(v)) {
|
||||
// invalid utf8, skip insert into db
|
||||
fields := logrus.Fields{
|
||||
"type": data.Type,
|
||||
"type": data.Type,
|
||||
"value": v,
|
||||
"key": k,
|
||||
"key": k,
|
||||
}
|
||||
log.WithFields(fields).Info("Migration completed")
|
||||
return nil
|
||||
|
@ -29,20 +29,6 @@ and o.posx <= $5 and o.posy <= $6 and o.posz <= $7
|
||||
order by o.id
|
||||
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 = `
|
||||
delete from objects where posx = $1 and posy = $2 and posz = $3
|
||||
|
@ -4,8 +4,9 @@ import (
|
||||
"database/sql"
|
||||
"mapserver/coords"
|
||||
"mapserver/mapobjectdb"
|
||||
"github.com/sirupsen/logrus"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
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 err error
|
||||
|
||||
var limit = 1000
|
||||
if q.Limit != nil {
|
||||
limit = *q.Limit
|
||||
}
|
||||
|
||||
if q.AttributeLike == nil {
|
||||
//plain pos search
|
||||
rows, err = db.db.Query(getMapDataPosQuery,
|
||||
q.Type,
|
||||
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
|
||||
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
|
||||
limit,
|
||||
*q.Limit,
|
||||
)
|
||||
|
||||
} else {
|
||||
if (q.Pos1 == nil || q.Pos2 == nil) {
|
||||
//global attribute like search
|
||||
rows, err = db.db.Query(getMapDataWithAttributeLikeGlobalQuery,
|
||||
q.AttributeLike.Key, q.AttributeLike.Value,
|
||||
q.Type,
|
||||
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,
|
||||
)
|
||||
}
|
||||
//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,
|
||||
*q.Limit,
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
@ -110,9 +97,9 @@ func (db *Sqlite3Accessor) AddMapData(data *mapobjectdb.MapObject) error {
|
||||
if !utf8.Valid([]byte(v)) {
|
||||
// invalid utf8, skip insert into db
|
||||
fields := logrus.Fields{
|
||||
"type": data.Type,
|
||||
"type": data.Type,
|
||||
"value": v,
|
||||
"key": k,
|
||||
"key": k,
|
||||
}
|
||||
log.WithFields(fields).Info("Migration completed")
|
||||
return nil
|
||||
|
@ -31,21 +31,6 @@ order by o.id
|
||||
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 = `
|
||||
delete from objects where posx = ? and posy = ? and posz = ?
|
||||
`
|
||||
|
@ -2,6 +2,7 @@ package web
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"mapserver/coords"
|
||||
"mapserver/mapobjectdb"
|
||||
"net/http"
|
||||
|
||||
@ -23,6 +24,20 @@ func (api *Api) QueryMapobjects(resp http.ResponseWriter, req *http.Request) {
|
||||
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)
|
||||
if err != nil {
|
||||
resp.WriteHeader(500)
|
||||
|
Loading…
Reference in New Issue
Block a user