From 13b395143d6616bf567eec8b297aa54755c9c529 Mon Sep 17 00:00:00 2001 From: BuckarooBanzay Date: Mon, 17 Jan 2022 09:06:07 +0100 Subject: [PATCH] simplify mapobject db (default to max/min positions at world border) --- mapobjectdb/postgres/mapobjects.go | 39 ++++++++++-------------------- mapobjectdb/postgres/sql.go | 14 ----------- mapobjectdb/sqlite/mapobjects.go | 39 ++++++++++-------------------- mapobjectdb/sqlite/sql.go | 15 ------------ web/mapobjects.go | 15 ++++++++++++ 5 files changed, 41 insertions(+), 81 deletions(-) diff --git a/mapobjectdb/postgres/mapobjects.go b/mapobjectdb/postgres/mapobjects.go index ee5cb7a..dc6e4a2 100644 --- a/mapobjectdb/postgres/mapobjects.go +++ b/mapobjectdb/postgres/mapobjects.go @@ -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 diff --git a/mapobjectdb/postgres/sql.go b/mapobjectdb/postgres/sql.go index 579733a..7fca499 100644 --- a/mapobjectdb/postgres/sql.go +++ b/mapobjectdb/postgres/sql.go @@ -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 diff --git a/mapobjectdb/sqlite/mapobjects.go b/mapobjectdb/sqlite/mapobjects.go index 557cc6a..8933fb6 100644 --- a/mapobjectdb/sqlite/mapobjects.go +++ b/mapobjectdb/sqlite/mapobjects.go @@ -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 diff --git a/mapobjectdb/sqlite/sql.go b/mapobjectdb/sqlite/sql.go index 6d5f979..6517bc8 100644 --- a/mapobjectdb/sqlite/sql.go +++ b/mapobjectdb/sqlite/sql.go @@ -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 = ? ` diff --git a/web/mapobjects.go b/web/mapobjects.go index 549932a..ec56b46 100644 --- a/web/mapobjects.go +++ b/web/mapobjects.go @@ -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)