Skip to content

Commit

Permalink
Improved performance for HTTP GET requests
Browse files Browse the repository at this point in the history
  • Loading branch information
tebben committed Sep 15, 2017
1 parent 04171e4 commit 085b219
Show file tree
Hide file tree
Showing 23 changed files with 216 additions and 198 deletions.
34 changes: 17 additions & 17 deletions database/postgis/datastream.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ func (gdb *GostDatabase) GetDatastream(id interface{}, qo *odata.QueryOptions) (
}

// GetDatastreams retrieves all datastreams
func (gdb *GostDatabase) GetDatastreams(qo *odata.QueryOptions) ([]*entities.Datastream, int, error) {
func (gdb *GostDatabase) GetDatastreams(qo *odata.QueryOptions) ([]*entities.Datastream, int, bool, error) {
query, qi := gdb.QueryBuilder.CreateQuery(&entities.Datastream{}, nil, nil, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Datastream{}, nil, nil, qo)
return processDatastreams(gdb.Db, query, qi, countSQL)
return processDatastreams(gdb.Db, query, qo, qi, countSQL)
}

// GetDatastreamByObservation retrieves a datastream linked to the given observation
Expand All @@ -128,43 +128,43 @@ func (gdb *GostDatabase) GetDatastreamByObservation(observationID interface{}, q
}

// GetDatastreamsByThing retrieves all datastreams linked to the given thing
func (gdb *GostDatabase) GetDatastreamsByThing(thingID interface{}, qo *odata.QueryOptions) ([]*entities.Datastream, int, error) {
func (gdb *GostDatabase) GetDatastreamsByThing(thingID interface{}, qo *odata.QueryOptions) ([]*entities.Datastream, int, bool, error) {
intID, ok := ToIntID(thingID)
if !ok {
return nil, 0, gostErrors.NewRequestNotFound(errors.New("Datastream does not exist"))
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("Datastream does not exist"))
}

query, qi := gdb.QueryBuilder.CreateQuery(&entities.Datastream{}, &entities.Thing{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Datastream{}, &entities.Thing{}, intID, qo)
return processDatastreams(gdb.Db, query, qi, countSQL)
return processDatastreams(gdb.Db, query, qo, qi, countSQL)
}

// GetDatastreamsBySensor retrieves all datastreams linked to the given sensor
func (gdb *GostDatabase) GetDatastreamsBySensor(sensorID interface{}, qo *odata.QueryOptions) ([]*entities.Datastream, int, error) {
func (gdb *GostDatabase) GetDatastreamsBySensor(sensorID interface{}, qo *odata.QueryOptions) ([]*entities.Datastream, int, bool, error) {
intID, ok := ToIntID(sensorID)
if !ok {
return nil, 0, gostErrors.NewRequestNotFound(errors.New("Datastream does not exist"))
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("Datastream does not exist"))
}

query, qi := gdb.QueryBuilder.CreateQuery(&entities.Datastream{}, &entities.Sensor{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Datastream{}, &entities.Sensor{}, intID, qo)
return processDatastreams(gdb.Db, query, qi, countSQL)
return processDatastreams(gdb.Db, query, qo, qi, countSQL)
}

// GetDatastreamsByObservedProperty retrieves all datastreams linked to the given ObservedProerty
func (gdb *GostDatabase) GetDatastreamsByObservedProperty(oID interface{}, qo *odata.QueryOptions) ([]*entities.Datastream, int, error) {
func (gdb *GostDatabase) GetDatastreamsByObservedProperty(oID interface{}, qo *odata.QueryOptions) ([]*entities.Datastream, int, bool, error) {
intID, ok := ToIntID(oID)
if !ok {
return nil, 0, gostErrors.NewRequestNotFound(errors.New("Datastream does not exist"))
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("Datastream does not exist"))
}

query, qi := gdb.QueryBuilder.CreateQuery(&entities.Datastream{}, &entities.ObservedProperty{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Datastream{}, &entities.ObservedProperty{}, intID, qo)
return processDatastreams(gdb.Db, query, qi, countSQL)
return processDatastreams(gdb.Db, query, qo, qi, countSQL)
}

func processDatastream(db *sql.DB, sql string, qi *QueryParseInfo) (*entities.Datastream, error) {
datastreams, _, err := processDatastreams(db, sql, qi, "")
datastreams, _, _, err := processDatastreams(db, sql, nil, qi, "")
if err != nil {
return nil, err
}
Expand All @@ -176,10 +176,10 @@ func processDatastream(db *sql.DB, sql string, qi *QueryParseInfo) (*entities.Da
return datastreams[0], nil
}

func processDatastreams(db *sql.DB, sql string, qi *QueryParseInfo, countSQL string) ([]*entities.Datastream, int, error) {
data, err := ExecuteSelect(db, qi, sql)
func processDatastreams(db *sql.DB, sql string, qo *odata.QueryOptions, qi *QueryParseInfo, countSQL string) ([]*entities.Datastream, int, bool, error) {
data, hasNext, err := ExecuteSelect(db, qi, sql, qo)
if err != nil {
return nil, 0, fmt.Errorf("Error executing query %v", err)
return nil, 0, false, fmt.Errorf("Error executing query %v", err)
}

datastreams := make([]*entities.Datastream, 0)
Expand All @@ -192,11 +192,11 @@ func processDatastreams(db *sql.DB, sql string, qi *QueryParseInfo, countSQL str
if len(countSQL) > 0 {
count, err = ExecuteSelectCount(db, countSQL)
if err != nil {
return nil, 0, fmt.Errorf("Error executing count %v", err)
return nil, 0, false, fmt.Errorf("Error executing count %v", err)
}
}

return datastreams, count, nil
return datastreams, count, hasNext, nil
}

// CheckDatastreamRelationsExist check if the related entities exist
Expand Down
16 changes: 8 additions & 8 deletions database/postgis/featureofinterest.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ func (gdb *GostDatabase) GetFeatureOfInterestByObservation(id interface{}, qo *o
}

// GetFeatureOfInterests returns all feature of interests
func (gdb *GostDatabase) GetFeatureOfInterests(qo *odata.QueryOptions) ([]*entities.FeatureOfInterest, int, error) {
func (gdb *GostDatabase) GetFeatureOfInterests(qo *odata.QueryOptions) ([]*entities.FeatureOfInterest, int, bool, error) {
query, qi := gdb.QueryBuilder.CreateQuery(&entities.FeatureOfInterest{}, nil, nil, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.FeatureOfInterest{}, nil, nil, qo)
return processFeatureOfInterests(gdb.Db, query, qi, countSQL)
return processFeatureOfInterests(gdb.Db, query, qo, qi, countSQL)
}

// PostFeatureOfInterest inserts a new FeatureOfInterest into the database
Expand All @@ -115,7 +115,7 @@ func (gdb *GostDatabase) PutFeatureOfInterest(id interface{}, f *entities.Featur
}

func processFeatureOfInterest(db *sql.DB, sql string, qi *QueryParseInfo) (*entities.FeatureOfInterest, error) {
locations, _, err := processFeatureOfInterests(db, sql, qi, "")
locations, _, _, err := processFeatureOfInterests(db, sql, nil, qi, "")
if err != nil {
return nil, err
}
Expand All @@ -127,10 +127,10 @@ func processFeatureOfInterest(db *sql.DB, sql string, qi *QueryParseInfo) (*enti
return locations[0], nil
}

func processFeatureOfInterests(db *sql.DB, sql string, qi *QueryParseInfo, countSQL string) ([]*entities.FeatureOfInterest, int, error) {
data, err := ExecuteSelect(db, qi, sql)
func processFeatureOfInterests(db *sql.DB, sql string, qo *odata.QueryOptions, qi *QueryParseInfo, countSQL string) ([]*entities.FeatureOfInterest, int, bool, error) {
data, hasNext, err := ExecuteSelect(db, qi, sql, qo)
if err != nil {
return nil, 0, fmt.Errorf("Error executing query %v", err)
return nil, 0, false, fmt.Errorf("Error executing query %v", err)
}

fois := make([]*entities.FeatureOfInterest, 0)
Expand All @@ -143,11 +143,11 @@ func processFeatureOfInterests(db *sql.DB, sql string, qi *QueryParseInfo, count
if len(countSQL) > 0 {
count, err = ExecuteSelectCount(db, countSQL)
if err != nil {
return nil, 0, fmt.Errorf("Error executing count %v", err)
return nil, 0, false, fmt.Errorf("Error executing count %v", err)
}
}

return fois, count, nil
return fois, count, hasNext, nil
}

// PatchFeatureOfInterest updates a FeatureOfInterest in the database
Expand Down
31 changes: 16 additions & 15 deletions database/postgis/historicallocation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"fmt"
"time"

"strings"

entities "github.com/gost/core"
gostErrors "github.com/gost/server/errors"
"github.com/gost/server/sensorthings/odata"
"strings"
)

func historicalLocationParamFactory(values map[string]interface{}) (entities.Entity, error) {
Expand Down Expand Up @@ -42,36 +43,36 @@ func (gdb *GostDatabase) GetHistoricalLocation(id interface{}, qo *odata.QueryOp
}

// GetHistoricalLocations retrieves all historicallocations
func (gdb *GostDatabase) GetHistoricalLocations(qo *odata.QueryOptions) ([]*entities.HistoricalLocation, int, error) {
func (gdb *GostDatabase) GetHistoricalLocations(qo *odata.QueryOptions) ([]*entities.HistoricalLocation, int, bool, error) {
query, qi := gdb.QueryBuilder.CreateQuery(&entities.HistoricalLocation{}, nil, nil, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.HistoricalLocation{}, nil, nil, qo)
return processHistoricalLocations(gdb.Db, query, qi, countSQL)
return processHistoricalLocations(gdb.Db, query, qo, qi, countSQL)
}

// GetHistoricalLocationsByLocation retrieves all historicallocations linked to the given location
func (gdb *GostDatabase) GetHistoricalLocationsByLocation(locationID interface{}, qo *odata.QueryOptions) ([]*entities.HistoricalLocation, int, error) {
func (gdb *GostDatabase) GetHistoricalLocationsByLocation(locationID interface{}, qo *odata.QueryOptions) ([]*entities.HistoricalLocation, int, bool, error) {
intID, ok := ToIntID(locationID)
if !ok {
return nil, 0, gostErrors.NewRequestNotFound(errors.New("Location does not exist"))
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("Location does not exist"))
}
query, qi := gdb.QueryBuilder.CreateQuery(&entities.HistoricalLocation{}, &entities.Location{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.HistoricalLocation{}, &entities.Location{}, intID, qo)
return processHistoricalLocations(gdb.Db, query, qi, countSQL)
return processHistoricalLocations(gdb.Db, query, qo, qi, countSQL)
}

// GetHistoricalLocationsByThing retrieves all historicallocations linked to the given thing
func (gdb *GostDatabase) GetHistoricalLocationsByThing(thingID interface{}, qo *odata.QueryOptions) ([]*entities.HistoricalLocation, int, error) {
func (gdb *GostDatabase) GetHistoricalLocationsByThing(thingID interface{}, qo *odata.QueryOptions) ([]*entities.HistoricalLocation, int, bool, error) {
intID, ok := ToIntID(thingID)
if !ok {
return nil, 0, gostErrors.NewRequestNotFound(errors.New("Thing does not exist"))
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("Thing does not exist"))
}
query, qi := gdb.QueryBuilder.CreateQuery(&entities.HistoricalLocation{}, &entities.Thing{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.HistoricalLocation{}, &entities.Thing{}, intID, qo)
return processHistoricalLocations(gdb.Db, query, qi, countSQL)
return processHistoricalLocations(gdb.Db, query, qo, qi, countSQL)
}

func processHistoricalLocation(db *sql.DB, sql string, qi *QueryParseInfo) (*entities.HistoricalLocation, error) {
hls, _, err := processHistoricalLocations(db, sql, qi, "")
hls, _, _, err := processHistoricalLocations(db, sql, nil, qi, "")
if err != nil {
return nil, err
}
Expand All @@ -83,10 +84,10 @@ func processHistoricalLocation(db *sql.DB, sql string, qi *QueryParseInfo) (*ent
return hls[0], nil
}

func processHistoricalLocations(db *sql.DB, sql string, qi *QueryParseInfo, countSQL string) ([]*entities.HistoricalLocation, int, error) {
data, err := ExecuteSelect(db, qi, sql)
func processHistoricalLocations(db *sql.DB, sql string, qo *odata.QueryOptions, qi *QueryParseInfo, countSQL string) ([]*entities.HistoricalLocation, int, bool, error) {
data, hasNext, err := ExecuteSelect(db, qi, sql, qo)
if err != nil {
return nil, 0, fmt.Errorf("Error executing query %v", err)
return nil, 0, hasNext, fmt.Errorf("Error executing query %v", err)
}

hls := make([]*entities.HistoricalLocation, 0)
Expand All @@ -99,11 +100,11 @@ func processHistoricalLocations(db *sql.DB, sql string, qi *QueryParseInfo, coun
if len(countSQL) > 0 {
count, err = ExecuteSelectCount(db, countSQL)
if err != nil {
return nil, 0, fmt.Errorf("Error executing count %v", err)
return nil, 0, hasNext, fmt.Errorf("Error executing count %v", err)
}
}

return hls, count, nil
return hls, count, hasNext, nil
}

// PostHistoricalLocation adds a historical location to the database
Expand Down
28 changes: 14 additions & 14 deletions database/postgis/location.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,22 @@ func (gdb *GostDatabase) GetLocation(id interface{}, qo *odata.QueryOptions) (*e
}

// GetLocations retrieves all locations
func (gdb *GostDatabase) GetLocations(qo *odata.QueryOptions) ([]*entities.Location, int, error) {
func (gdb *GostDatabase) GetLocations(qo *odata.QueryOptions) ([]*entities.Location, int, bool, error) {
query, qi := gdb.QueryBuilder.CreateQuery(&entities.Location{}, nil, nil, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Location{}, nil, nil, qo)
return processLocations(gdb.Db, query, qi, countSQL)
return processLocations(gdb.Db, query, qo, qi, countSQL)
}

// GetLocationsByHistoricalLocation retrieves all locations linked to the given HistoricalLocation
func (gdb *GostDatabase) GetLocationsByHistoricalLocation(hlID interface{}, qo *odata.QueryOptions) ([]*entities.Location, int, error) {
func (gdb *GostDatabase) GetLocationsByHistoricalLocation(hlID interface{}, qo *odata.QueryOptions) ([]*entities.Location, int, bool, error) {
intID, ok := ToIntID(hlID)
if !ok {
return nil, 0, gostErrors.NewRequestNotFound(errors.New("HistoricaLocation does not exist"))
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("HistoricaLocation does not exist"))
}

query, qi := gdb.QueryBuilder.CreateQuery(&entities.Location{}, &entities.HistoricalLocation{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Location{}, &entities.HistoricalLocation{}, intID, qo)
return processLocations(gdb.Db, query, qi, countSQL)
return processLocations(gdb.Db, query, qo, qi, countSQL)
}

// GetLocationByDatastreamID returns a location linked to an observation
Expand All @@ -94,19 +94,19 @@ func (gdb *GostDatabase) GetLocationByDatastreamID(datastreamID interface{}, qo
}

// GetLocationsByThing retrieves all locations linked to the given thing
func (gdb *GostDatabase) GetLocationsByThing(thingID interface{}, qo *odata.QueryOptions) ([]*entities.Location, int, error) {
func (gdb *GostDatabase) GetLocationsByThing(thingID interface{}, qo *odata.QueryOptions) ([]*entities.Location, int, bool, error) {
intID, ok := ToIntID(thingID)
if !ok {
return nil, 0, gostErrors.NewRequestNotFound(errors.New("Thing does not exist"))
return nil, 0, false, gostErrors.NewRequestNotFound(errors.New("Thing does not exist"))
}

query, qi := gdb.QueryBuilder.CreateQuery(&entities.Location{}, &entities.Thing{}, intID, qo)
countSQL := gdb.QueryBuilder.CreateCountQuery(&entities.Location{}, &entities.Thing{}, intID, qo)
return processLocations(gdb.Db, query, qi, countSQL)
return processLocations(gdb.Db, query, qo, qi, countSQL)
}

func processLocation(db *sql.DB, sql string, qi *QueryParseInfo) (*entities.Location, error) {
locations, _, err := processLocations(db, sql, qi, "")
locations, _, _, err := processLocations(db, sql, nil, qi, "")
if err != nil {
return nil, err
}
Expand All @@ -118,10 +118,10 @@ func processLocation(db *sql.DB, sql string, qi *QueryParseInfo) (*entities.Loca
return locations[0], nil
}

func processLocations(db *sql.DB, sql string, qi *QueryParseInfo, countSQL string) ([]*entities.Location, int, error) {
data, err := ExecuteSelect(db, qi, sql)
func processLocations(db *sql.DB, sql string, qo *odata.QueryOptions, qi *QueryParseInfo, countSQL string) ([]*entities.Location, int, bool, error) {
data, hasNext, err := ExecuteSelect(db, qi, sql, qo)
if err != nil {
return nil, 0, fmt.Errorf("Error executing query %v", err)
return nil, 0, hasNext, fmt.Errorf("Error executing query %v", err)
}

locations := make([]*entities.Location, 0)
Expand All @@ -134,11 +134,11 @@ func processLocations(db *sql.DB, sql string, qi *QueryParseInfo, countSQL strin
if len(countSQL) > 0 {
count, err = ExecuteSelectCount(db, countSQL)
if err != nil {
return nil, 0, fmt.Errorf("error executing count %v", err)
return nil, 0, hasNext, fmt.Errorf("error executing count %v", err)
}
}

return locations, count, nil
return locations, count, hasNext, nil
}

// PostLocation receives a posted location entity and adds it to the database
Expand Down
Loading

0 comments on commit 085b219

Please sign in to comment.