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"
"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,17 +20,9 @@ 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,
@ -42,10 +30,9 @@ func (db *PostgresAccessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobject
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,
*q.Limit,
)
}
}
if err != nil {
return nil, err

View File

@ -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

View File

@ -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,28 +14,15 @@ 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,
@ -42,10 +30,9 @@ func (db *Sqlite3Accessor) GetMapData(q *mapobjectdb.SearchQuery) ([]*mapobjectd
q.Type,
q.Pos1.X, q.Pos1.Y, q.Pos1.Z,
q.Pos2.X, q.Pos2.Y, q.Pos2.Z,
limit,
*q.Limit,
)
}
}
if err != nil {
return nil, err

View File

@ -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 = ?
`

View File

@ -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)