Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use nav.Location to replace Location, change point to place #341

Merged
merged 2 commits into from
May 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-FORK.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Changes from v10.2.0
- CHANGED for mock objects, hide them in internal/mock package for OASIS service [#334](https://github.com/Telenav/osrm-backend/issues/334)
- ADDED package `oasis/selectionstrategy`, move logic related with how to select optimum charge stations into this package [#339](https://github.com/Telenav/osrm-backend/pull/339)
- CHANGED for integration of pre-generated connectivity data with OASIS service [#339](https://github.com/Telenav/osrm-backend/pull/339)
- CHANGED for internal refactoring, replace `Location` in `spatialindexer` to nav.Location, replace all name of `point` to `place` [#341](https://github.com/Telenav/osrm-backend/pull/341)

- Bugfix:
- CHANGED `osrm-ranking` parsing of OSRM route response to compatible with `string` array `annotation/nodes` [#296](https://github.com/Telenav/osrm-backend/pull/296)
Expand Down
20 changes: 10 additions & 10 deletions integration/service/oasis/connectivitymap/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
)

type connectivityMapBuilder struct {
iterator spatialindexer.PointsIterator
iterator spatialindexer.PlacesIterator
finder spatialindexer.Finder
ranker spatialindexer.Ranker
distanceLimit float64
Expand All @@ -20,7 +20,7 @@ type connectivityMapBuilder struct {
aggregatorC chan placeIDWithNearByPlaceIDs
}

func newConnectivityMapBuilder(iterator spatialindexer.PointsIterator, finder spatialindexer.Finder,
func newConnectivityMapBuilder(iterator spatialindexer.PlacesIterator, finder spatialindexer.Finder,
ranker spatialindexer.Ranker, distanceLimit float64, numOfWorker int) *connectivityMapBuilder {
builder := &connectivityMapBuilder{
iterator: iterator,
Expand Down Expand Up @@ -63,7 +63,7 @@ func (builder *connectivityMapBuilder) build() ID2NearByIDsMap {
}

func (builder *connectivityMapBuilder) process() {
inputC := builder.iterator.IteratePoints()
inputC := builder.iterator.IteratePlaces()

for i := 0; i < builder.numOfWorker; i++ {
builder.workerWaitGroup.Add(1)
Expand All @@ -73,14 +73,14 @@ func (builder *connectivityMapBuilder) process() {
glog.Infof("builder's process is finished, start number of %d workers.\n", builder.numOfWorker)
}

func (builder *connectivityMapBuilder) work(workerID int, source <-chan spatialindexer.PointInfo, sink chan<- placeIDWithNearByPlaceIDs) {
func (builder *connectivityMapBuilder) work(workerID int, source <-chan spatialindexer.PlaceInfo, sink chan<- placeIDWithNearByPlaceIDs) {
defer builder.workerWaitGroup.Done()

counter := 0
for p := range source {
counter += 1
nearbyIDs := builder.finder.FindNearByPointIDs(p.Location, builder.distanceLimit, spatialindexer.UnlimitedCount)
rankedResults := builder.ranker.RankPointIDsByShortestDistance(p.Location, nearbyIDs)
nearbyIDs := builder.finder.FindNearByPlaceIDs(p.Location, builder.distanceLimit, spatialindexer.UnlimitedCount)
rankedResults := builder.ranker.RankPlaceIDsByShortestDistance(p.Location, nearbyIDs)

ids := make([]IDAndWeight, 0, len(rankedResults))
for _, r := range rankedResults {
Expand Down Expand Up @@ -129,7 +129,7 @@ func (builder *connectivityMapBuilder) wait() {
}

type placeIDWithNearByPlaceIDs struct {
id spatialindexer.PointID
id spatialindexer.PlaceID
ids []IDAndWeight
}

Expand All @@ -139,9 +139,9 @@ func (builder *connectivityMapBuilder) buildInSerial() ID2NearByIDsMap {
m := make(ID2NearByIDsMap)

go func() {
for p := range builder.iterator.IteratePoints() {
nearbyIDs := builder.finder.FindNearByPointIDs(p.Location, builder.distanceLimit, spatialindexer.UnlimitedCount)
rankedResults := builder.ranker.RankPointIDsByGreatCircleDistance(p.Location, nearbyIDs)
for p := range builder.iterator.IteratePlaces() {
nearbyIDs := builder.finder.FindNearByPlaceIDs(p.Location, builder.distanceLimit, spatialindexer.UnlimitedCount)
rankedResults := builder.ranker.RankPlaceIDsByGreatCircleDistance(p.Location, nearbyIDs)

ids := make([]IDAndWeight, 0, len(rankedResults))
for _, r := range rankedResults {
Expand Down
16 changes: 8 additions & 8 deletions integration/service/oasis/connectivitymap/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ import (
)

// Mock env:
// Iterator returns 100 of fixed location point(IDs are 1000, 1001, 1002, ...)
// Finder returns fixed array result(10 points, IDs are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
// Iterator returns 100 of fixed location places(IDs are 1000, 1001, 1002, ...)
// Finder returns fixed array result(10 places, IDs are 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 )
// Ranker use great circle distance to calculate
// Expect result:
// build() generate same result as pre-calculated map
// map[1000] = {results of 10 points}
// map[1001] = {results of 10 points}
// map[1000] = {results of 10 places}
// map[1001] = {results of 10 places}
// ...
// map[1099] = {results of 10 points}
// map[1099] = {results of 10 places}
func TestBuilderWithMockIteratorAndFinder(t *testing.T) {
builder := newConnectivityMapBuilder(&mock.MockOneHundredPointsIterator{},
builder := newConnectivityMapBuilder(&mock.MockOneHundredPlacesIterator{},
&mock.MockFinder{},
ranker.CreateRanker(ranker.SimpleRanker, nil),
100,
Expand Down Expand Up @@ -107,7 +107,7 @@ func TestBuilderWithMockIteratorAndFinder(t *testing.T) {

for i := 0; i < 100; i++ {
index := i + 1000
expect[(spatialindexer.PointID(index))] = idAndWeightArray
expect[(spatialindexer.PlaceID(index))] = idAndWeightArray
}

if !reflect.DeepEqual(actual, expect) {
Expand All @@ -116,7 +116,7 @@ func TestBuilderWithMockIteratorAndFinder(t *testing.T) {
}

func TestBuilderWithSingleWorker(t *testing.T) {
builder := newConnectivityMapBuilder(&mock.MockOneHundredPointsIterator{},
builder := newConnectivityMapBuilder(&mock.MockOneHundredPlacesIterator{},
&mock.MockFinder{},
ranker.CreateRanker(ranker.SimpleRanker, nil),
100,
Expand Down
8 changes: 4 additions & 4 deletions integration/service/oasis/connectivitymap/connectivity_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ type Weight struct {

// IDAndWeight wraps ID and weight information
type IDAndWeight struct {
ID spatialindexer.PointID
ID spatialindexer.PlaceID
Weight Weight
}

// ID2NearByIDsMap is a mapping between ID and its nearby IDs
type ID2NearByIDsMap map[spatialindexer.PointID][]IDAndWeight
type ID2NearByIDsMap map[spatialindexer.PlaceID][]IDAndWeight

// Connectivity Map used to query connectivity for given placeID
type ConnectivityMap struct {
Expand All @@ -36,7 +36,7 @@ func New(maxRange float64) *ConnectivityMap {
}

// Build creates ConnectivityMap
func (cm *ConnectivityMap) Build(iterator spatialindexer.PointsIterator, finder spatialindexer.Finder,
func (cm *ConnectivityMap) Build(iterator spatialindexer.PlacesIterator, finder spatialindexer.Finder,
ranker spatialindexer.Ranker, numOfWorkers int) *ConnectivityMap {
glog.Info("Start ConnectivityMap's Build().\n")

Expand Down Expand Up @@ -76,7 +76,7 @@ func (cm *ConnectivityMap) Load(folderPath string) *ConnectivityMap {

// QueryConnectivity answers connectivity query for given placeID
// Return true and IDAndWeight array for given placeID, otherwise false and nil
func (cm *ConnectivityMap) QueryConnectivity(placeID spatialindexer.PointID) ([]IDAndWeight, bool) {
func (cm *ConnectivityMap) QueryConnectivity(placeID spatialindexer.PlaceID) ([]IDAndWeight, bool) {
if result, ok := cm.id2nearByIDs[placeID]; ok {
return result, true
}
Expand Down
33 changes: 18 additions & 15 deletions integration/service/oasis/internal/mock/spatialindexer_finder.go
Original file line number Diff line number Diff line change
@@ -1,85 +1,88 @@
package mock

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

// MockFinder implements Finder's interface
type MockFinder struct {
}

// FindNearByPointIDs returns mock result
// FindNearByPlaceIDs returns mock result
// It returns 10 places defined in MockPlaceInfo1
func (finder *MockFinder) FindNearByPointIDs(center spatialindexer.Location, radius float64, limitCount int) []*spatialindexer.PointInfo {
func (finder *MockFinder) FindNearByPlaceIDs(center nav.Location, radius float64, limitCount int) []*spatialindexer.PlaceInfo {
return MockPlaceInfo1
}

// MockPlaceInfo1 contains 10 PointInfo items
var MockPlaceInfo1 = []*spatialindexer.PointInfo{
// MockPlaceInfo1 contains 10 PlaceInfo items
var MockPlaceInfo1 = []*spatialindexer.PlaceInfo{
{
ID: 1,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.355204,
Lon: -121.953901,
},
},
{
ID: 2,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.399331,
Lon: -121.981193,
},
},
{
ID: 3,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.401948,
Lon: -121.977384,
},
},
{
ID: 4,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.407082,
Lon: -121.991937,
},
},
{
ID: 5,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.407277,
Lon: -121.925482,
},
},
{
ID: 6,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.375024,
Lon: -121.904706,
},
},
{
ID: 7,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.359592,
Lon: -121.914164,
},
},
{
ID: 8,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.366023,
Lon: -122.080777,
},
},
{
ID: 9,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.368453,
Lon: -122.076400,
},
},
{
ID: 10,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.373546,
Lon: -122.068904,
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package mock

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

// MockPointsIterator implements PointsIterator's interface
type MockPointsIterator struct {
// MockPlacesIterator implements PlacesIterator's interface
type MockPlacesIterator struct {
}

// IteratePoints() iterate places with mock data
func (iterator *MockPointsIterator) IteratePoints() <-chan spatialindexer.PointInfo {
pointInfoC := make(chan spatialindexer.PointInfo, len(MockPlaceInfo1))
// IteratePlaces() iterate places with mock data
func (iterator *MockPlacesIterator) IteratePlaces() <-chan spatialindexer.PlaceInfo {
pointInfoC := make(chan spatialindexer.PlaceInfo, len(MockPlaceInfo1))

go func() {
for _, item := range MockPlaceInfo1 {
Expand All @@ -21,21 +24,21 @@ func (iterator *MockPointsIterator) IteratePoints() <-chan spatialindexer.PointI
return pointInfoC
}

// MockOneHundredPointsIterator implements PointsIterator's interface
type MockOneHundredPointsIterator struct {
// MockOneHundredPlacesIterator implements PlacesIterator's interface
type MockOneHundredPlacesIterator struct {
}

// IteratePoints() iterate places with mock data.
// IteratePlaces() iterate places with mock data.
// It returns {ID:1000, fixed location}, {ID:1001, fixed location}, ... {ID:1099, fixed location}
func (iterator *MockOneHundredPointsIterator) IteratePoints() <-chan spatialindexer.PointInfo {
pointInfoC := make(chan spatialindexer.PointInfo, 100)
func (iterator *MockOneHundredPlacesIterator) IteratePlaces() <-chan spatialindexer.PlaceInfo {
pointInfoC := make(chan spatialindexer.PlaceInfo, 100)

go func() {
for i := 0; i < 100; i++ {
id := (spatialindexer.PointID)(i + 1000)
pointInfoC <- spatialindexer.PointInfo{
id := (spatialindexer.PlaceID)(i + 1000)
pointInfoC <- spatialindexer.PlaceInfo{
ID: id,
Location: spatialindexer.Location{
Location: nav.Location{
Lat: 37.398896,
Lon: -121.976665,
},
Expand Down
2 changes: 1 addition & 1 deletion integration/service/oasis/spatialindexer/doc.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Package spatialindexer answers query of nearest points(place, point of interest) for conditions
// Package spatialindexer answers query of nearest places(place, point of interest) for conditions
// such as center location, radius, etc
//
// Sample Scenario 1: Build connectivity for charge stations during pro-processing
Expand Down
53 changes: 23 additions & 30 deletions integration/service/oasis/spatialindexer/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,25 @@ import (
"github.com/Telenav/osrm-backend/integration/api/nav"
)

// Location for poi point
// todo codebear801 will be replaced by the one in nav
type Location struct {
Lat float64
Lon float64
// PlaceInfo records place related information such as ID and location
type PlaceInfo struct {
ID PlaceID
Location nav.Location
}

// PointInfo records point related information such as ID and location
type PointInfo struct {
ID PointID
Location Location
}

// RankedPointInfo used to record ranking result, distance to specific point could be used for ranking
type RankedPointInfo struct {
PointInfo
// RankedPlaceInfo used to record ranking result, e.g. distance to specific place could be used for ranking
type RankedPlaceInfo struct {
PlaceInfo
Distance float64
Duration float64
}

// PointID defines ID for given point(location, point of interest)
// Only the data used for pre-processing contains valid PointID
type PointID int64
// PlaceID defines ID for given place(location, point of interest)
// Only the data used for pre-processing contains valid PlaceID
type PlaceID int64

// String converts PointID to string
func (p PointID) String() string {
// String converts PlaceID to string
func (p PlaceID) String() string {
return strconv.FormatInt((int64)(p), 10)
}

Expand All @@ -42,18 +35,18 @@ const UnlimitedCount = math.MaxInt32
// Finder answers special query
type Finder interface {

// FindNearByPointIDs returns a group of points near to given center location
FindNearByPointIDs(center Location, radius float64, limitCount int) []*PointInfo
// FindNearByPlaceIDs returns a group of places near to given center location
FindNearByPlaceIDs(center nav.Location, radius float64, limitCount int) []*PlaceInfo
}

// Ranker used to ranking a group of points
// Ranker used to ranking a group of places
type Ranker interface {

// RankPointIDsByGreatCircleDistance ranks a group of points based on great circle distance to given location
RankPointIDsByGreatCircleDistance(center Location, targets []*PointInfo) []*RankedPointInfo
// RankPlaceIDsByGreatCircleDistance ranks a group of places based on great circle distance to given location
RankPlaceIDsByGreatCircleDistance(center nav.Location, targets []*PlaceInfo) []*RankedPlaceInfo

// RankPointIDsByShortestDistance ranks a group of points based on shortest path distance to given location
RankPointIDsByShortestDistance(center Location, targets []*PointInfo) []*RankedPointInfo
// RankPlaceIDsByShortestDistance ranks a group of places based on shortest path distance to given location
RankPlaceIDsByShortestDistance(center nav.Location, targets []*PlaceInfo) []*RankedPlaceInfo
}

// PlaceLocationQuerier returns *nav.location for given location
Expand All @@ -64,9 +57,9 @@ type PlaceLocationQuerier interface {
GetLocation(placeID string) *nav.Location
}

// PointsIterator provides iterateability for PointInfo
type PointsIterator interface {
// PlacesIterator provides iterateability for PlaceInfo
type PlacesIterator interface {

// IteratePoints returns channel for PointInfo
IteratePoints() <-chan PointInfo
// IteratePlaces returns channel for PlaceInfo
IteratePlaces() <-chan PlaceInfo
}
Loading