Skip to content

Commit

Permalink
refactor: Optimize implementation of nearbyquery, remove definition o…
Browse files Browse the repository at this point in the history
…f QueryResult

issue: #348
  • Loading branch information
CodeBear801 committed May 28, 2020
1 parent 94239e3 commit 3219e27
Show file tree
Hide file tree
Showing 21 changed files with 808 additions and 442 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
package connectivitymap

import "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"
import (
"github.com/Telenav/osrm-backend/integration/api/nav"
"github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"
)

var fakeID2NearByIDsMap3 = ID2NearByIDsMap{
1: []*common.RankedPlaceInfo{
{
PlaceInfo: common.PlaceInfo{
ID: 2,
Location: &nav.Location{
Lat: 37.399331,
Lon: -121.981193,
},
},
Weight: &common.Weight{
Distance: 1,
Expand All @@ -18,6 +25,10 @@ var fakeID2NearByIDsMap3 = ID2NearByIDsMap{
{
PlaceInfo: common.PlaceInfo{
ID: 3,
Location: &nav.Location{
Lat: 37.401948,
Lon: -121.977384,
},
},
Weight: &common.Weight{
Distance: 2,
Expand Down
15 changes: 5 additions & 10 deletions integration/service/oasis/connectivitymap/interface.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,16 @@
package connectivitymap

import "github.com/Telenav/osrm-backend/integration/api/nav"

// QueryResult records topological query result
type QueryResult struct {
StationID string
StationLocation *nav.Location
Distance float64
Duration float64
}
import (
"github.com/Telenav/osrm-backend/integration/api/nav"
"github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"
)

// Querier used to return topological information of charge stations
type Querier interface {

// NearByStationQuery finds near by stations by given stationID and return them in recorded sequence
// Returns nil if given stationID is not found or no connectivity
NearByStationQuery(stationID string) []*QueryResult
NearByStationQuery(stationID string) []*common.RankedPlaceInfo

// GetLocation returns location of given station id
// Returns nil if given stationID is not found
Expand Down
4 changes: 4 additions & 0 deletions integration/service/oasis/internal/common/place.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import (
"github.com/Telenav/osrm-backend/integration/api/nav"
)

// todo codebear801 change to a more variable name
// PlaceInfo -> PlaceWithLocation
// RankedPlaceInfo -> TransferInfo

// PlaceInfo records place related information such as ID and location
type PlaceInfo struct {
ID PlaceID
Expand Down
5 changes: 3 additions & 2 deletions integration/service/oasis/internal/mock/osrm_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mock
import (
"github.com/Telenav/osrm-backend/integration/api/osrm"
"github.com/Telenav/osrm-backend/integration/api/osrm/table"
"github.com/Telenav/osrm-backend/integration/service/oasis/stationfinder/stationfindertype"
)

// 1 * 4
Expand All @@ -16,7 +17,7 @@ var Mock1To4TableResponse1 = table.Response{
},
Sources: []*osrm.Waypoint{
{
Name: "orig_location",
Name: stationfindertype.OrigLocationID.String(),
},
},
Destinations: []*osrm.Waypoint{
Expand Down Expand Up @@ -66,7 +67,7 @@ var Mock4To1TableResponse1 = table.Response{
},
Destinations: []*osrm.Waypoint{
{
Name: "dest_location",
Name: stationfindertype.DestLocationID.String(),
},
},
}
Expand Down
12 changes: 6 additions & 6 deletions integration/service/oasis/internal/mock/spatialfinder_type.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var MockChargeStationInfo1 = []*stationfindertype.ChargeStationInfo{

var NeighborInfoArray0 = []stationfindertype.NeighborInfo{
{
FromID: "orig_location",
FromID: stationfindertype.OrigLocationIDStr,
FromLocation: nav.Location{
Lat: 1.1,
Lon: 1.1,
Expand All @@ -109,7 +109,7 @@ var NeighborInfoArray0 = []stationfindertype.NeighborInfo{
},
},
{
FromID: "orig_location",
FromID: stationfindertype.OrigLocationIDStr,
FromLocation: nav.Location{
Lat: 1.1,
Lon: 1.1,
Expand All @@ -125,7 +125,7 @@ var NeighborInfoArray0 = []stationfindertype.NeighborInfo{
},
},
{
FromID: "orig_location",
FromID: stationfindertype.OrigLocationIDStr,
FromLocation: nav.Location{
Lat: 1.1,
Lon: 1.1,
Expand All @@ -141,7 +141,7 @@ var NeighborInfoArray0 = []stationfindertype.NeighborInfo{
},
},
{
FromID: "orig_location",
FromID: stationfindertype.OrigLocationIDStr,
FromLocation: nav.Location{
Lat: 1.1,
Lon: 1.1,
Expand Down Expand Up @@ -296,7 +296,7 @@ var NeighborInfoArray2 = []stationfindertype.NeighborInfo{
Lat: 30.333,
Lon: 122.333,
},
ToID: "dest_location",
ToID: stationfindertype.DestLocationIDStr,
ToLocation: nav.Location{
Lat: 4.4,
Lon: 4.4,
Expand All @@ -312,7 +312,7 @@ var NeighborInfoArray2 = []stationfindertype.NeighborInfo{
Lat: -10.333,
Lon: 122.333,
},
ToID: "dest_location",
ToID: stationfindertype.DestLocationIDStr,
ToLocation: nav.Location{
Lat: 4.4,
Lon: 4.4,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ import (
type StationConnectivityQuerier struct {
stationLocationQuerier spatialindexer.PlaceLocationQuerier
stationConnectivity *connectivitymap.ConnectivityMap
reachableStationsByStart []*connectivitymap.QueryResult
reachableStationToEnd map[string]*connectivitymap.QueryResult
reachableStationsByStart []*common.RankedPlaceInfo
reachableStationToEnd map[string]*common.RankedPlaceInfo
startLocation *nav.Location
endLocation *nav.Location
}
Expand Down Expand Up @@ -48,18 +48,7 @@ func (querier *StationConnectivityQuerier) connectStartIntoStationGraph(stationF
nearByPoints := stationFinder.FindNearByPlaceIDs(center, currEnergyLevel, spatialindexer.UnlimitedCount)
rankedPoints := stationRanker.RankPlaceIDsByShortestDistance(center, nearByPoints)

reachableStationsByStart := make([]*connectivitymap.QueryResult, 0, len(rankedPoints))
for _, rankedPointInfo := range rankedPoints {
tmp := &connectivitymap.QueryResult{
StationID: rankedPointInfo.ID.String(),
StationLocation: &nav.Location{Lat: rankedPointInfo.Location.Lat, Lon: rankedPointInfo.Location.Lon},
Distance: rankedPointInfo.Weight.Distance,
Duration: rankedPointInfo.Weight.Duration,
}
reachableStationsByStart = append(reachableStationsByStart, tmp)
}

querier.reachableStationsByStart = reachableStationsByStart
querier.reachableStationsByStart = rankedPoints
glog.Infof("Add %d stations connects Start.\n", len(querier.reachableStationsByStart))
}

Expand All @@ -69,13 +58,14 @@ func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFin
nearByPoints := stationFinder.FindNearByPlaceIDs(center, maxEnergyLevel, spatialindexer.UnlimitedCount)
rankedPoints := stationRanker.RankPlaceIDsByShortestDistance(center, nearByPoints)

reachableStationToEnd := make(map[string]*connectivitymap.QueryResult)
reachableStationToEnd := make(map[string]*common.RankedPlaceInfo)
for _, rankedPointInfo := range rankedPoints {
reachableStationToEnd[rankedPointInfo.ID.String()] = &connectivitymap.QueryResult{
StationID: stationfindertype.DestLocationID,
StationLocation: end,
Distance: rankedPointInfo.Weight.Distance,
Duration: rankedPointInfo.Weight.Duration,
reachableStationToEnd[rankedPointInfo.ID.String()] = &common.RankedPlaceInfo{
PlaceInfo: common.PlaceInfo{
ID: stationfindertype.DestLocationID,
Location: end,
},
Weight: rankedPointInfo.Weight,
}
}

Expand All @@ -88,13 +78,13 @@ func (querier *StationConnectivityQuerier) connectEndIntoStationGraph(stationFin
// For start point, directly returns reachableStationsByStart which is generated by considering current energy level.
// For end point, return nil, no connectivity expected from end to others
// For charge stations, it retrieves connectivity from pre-build data. If a charge station is reachable to destination/end point, it must connects that into graph.
func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string) []*connectivitymap.QueryResult {
func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string) []*common.RankedPlaceInfo {

if stationID == stationfindertype.OrigLocationID {
if stationID == stationfindertype.OrigLocationIDStr {
return querier.reachableStationsByStart
}

if stationID == stationfindertype.DestLocationID {
if stationID == stationfindertype.DestLocationIDStr {
return nil
}

Expand All @@ -105,27 +95,20 @@ func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string)
}

if connectivityResults, ok := querier.stationConnectivity.QueryConnectivity((common.PlaceID)(placeID)); ok {

size := len(connectivityResults)
if querier.isStationConnectsToEnd(stationID) {
size += 1
}

results := make([]*connectivitymap.QueryResult, 0, size)
for _, idAndWeight := range connectivityResults {
tmp := &connectivitymap.QueryResult{
StationID: idAndWeight.ID.String(),
StationLocation: querier.GetLocation(idAndWeight.ID.String()),
Distance: idAndWeight.Weight.Distance,
Duration: idAndWeight.Weight.Duration,
if !querier.isStationConnectsToEnd(stationID) {
return connectivityResults
} else {
size := len(connectivityResults) + 1
results := make([]*common.RankedPlaceInfo, 0, size)
for _, result := range connectivityResults {
results = append(results, result)
}
results = append(results, tmp)
return querier.connectEndIntoGraph(stationID, results)
}

return querier.connectEndIntoGraph(stationID, results)
} else {
// end position just find one charge station, and this charge station didn't has other conductivities
if querier.isStationConnectsToEnd(stationID) {
results := make([]*connectivitymap.QueryResult, 0, 1)
results := make([]*common.RankedPlaceInfo, 0, 1)
return querier.connectEndIntoGraph(stationID, results)
}
}
Expand All @@ -137,9 +120,9 @@ func (querier *StationConnectivityQuerier) NearByStationQuery(stationID string)
// Returns nil if given stationID is not found
func (querier *StationConnectivityQuerier) GetLocation(stationID string) *nav.Location {
switch stationID {
case stationfindertype.OrigLocationID:
case stationfindertype.OrigLocationIDStr:
return querier.startLocation
case stationfindertype.DestLocationID:
case stationfindertype.DestLocationIDStr:
return querier.endLocation
default:
return querier.stationLocationQuerier.GetLocation(stationID)
Expand All @@ -151,19 +134,23 @@ func (querier *StationConnectivityQuerier) isStationConnectsToEnd(stationID stri
return ok
}

func (querier *StationConnectivityQuerier) connectEndIntoGraph(stationID string, results []*connectivitymap.QueryResult) []*connectivitymap.QueryResult {
func (querier *StationConnectivityQuerier) connectEndIntoGraph(stationID string, results []*common.RankedPlaceInfo) []*common.RankedPlaceInfo {
if queryResult4End, ok := querier.reachableStationToEnd[stationID]; ok {
return appendIntoSortedSlice(queryResult4End, results)
}
return results
}

func appendIntoSortedSlice(item *connectivitymap.QueryResult, results []*connectivitymap.QueryResult) []*connectivitymap.QueryResult {
func appendIntoSortedSlice(item *common.RankedPlaceInfo, results []*common.RankedPlaceInfo) []*common.RankedPlaceInfo {

// todo @codebear801 this code assumes result is sorted by Distance, better hide in array itself or sortStrategy
insertIndex := sort.Search(len(results), func(i int) bool {
return results[i].Distance > item.Distance
return results[i].Weight.Distance > item.Weight.Distance
})

results = append(results, nil)
copy(results[insertIndex+1:], results[insertIndex:])
results[insertIndex] = item

return results
}
Loading

0 comments on commit 3219e27

Please sign in to comment.