Skip to content

Commit

Permalink
refactor: Retire multiple definition of weight in oasis
Browse files Browse the repository at this point in the history
issue: #348
  • Loading branch information
CodeBear801 committed May 20, 2020
1 parent 16c80f1 commit 1328354
Show file tree
Hide file tree
Showing 30 changed files with 584 additions and 432 deletions.
47 changes: 11 additions & 36 deletions integration/service/oasis/connectivitymap/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package connectivitymap
import (
"sync"

"github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"
"github.com/Telenav/osrm-backend/integration/service/oasis/spatialindexer"
"github.com/golang/glog"
)
Expand Down Expand Up @@ -73,34 +74,18 @@ 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.PlaceInfo, sink chan<- placeIDWithNearByPlaceIDs) {
func (builder *connectivityMapBuilder) work(workerID int, source <-chan common.PlaceInfo, sink chan<- placeIDWithNearByPlaceIDs) {
defer builder.workerWaitGroup.Done()

counter := 0
for p := range source {
counter += 1
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 {
// skip connectivity to itself
if r.ID == p.ID {
continue
}

ids = append(ids, IDAndWeight{
ID: r.ID,
Weight: Weight{
Distance: r.Distance,
Duration: r.Duration,
},
})
}
nearbyIDs := builder.finder.FindNearByPlaceIDs(*p.Location, builder.distanceLimit, spatialindexer.UnlimitedCount)
rankedResults := builder.ranker.RankPlaceIDsByShortestDistance(*p.Location, nearbyIDs)

sink <- placeIDWithNearByPlaceIDs{
id: p.ID,
ids: ids,
ids: rankedResults,
}
}

Expand Down Expand Up @@ -129,8 +114,8 @@ func (builder *connectivityMapBuilder) wait() {
}

type placeIDWithNearByPlaceIDs struct {
id spatialindexer.PlaceID
ids []IDAndWeight
id common.PlaceID
ids []*common.RankedPlaceInfo
}

func (builder *connectivityMapBuilder) buildInSerial() ID2NearByIDsMap {
Expand All @@ -140,22 +125,12 @@ func (builder *connectivityMapBuilder) buildInSerial() ID2NearByIDsMap {

go func() {
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 {
ids = append(ids, IDAndWeight{
ID: r.ID,
Weight: Weight{
Distance: r.Distance,
Duration: r.Duration,
},
})
}
nearbyIDs := builder.finder.FindNearByPlaceIDs(*p.Location, builder.distanceLimit, spatialindexer.UnlimitedCount)
rankedResults := builder.ranker.RankPlaceIDsByGreatCircleDistance(*p.Location, nearbyIDs)

internalResult <- placeIDWithNearByPlaceIDs{
id: p.ID,
ids: ids,
ids: rankedResults,
}
}
close(internalResult)
Expand Down
147 changes: 104 additions & 43 deletions integration/service/oasis/connectivitymap/builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"runtime"
"testing"

"github.com/Telenav/osrm-backend/integration/api/nav"
"github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"
"github.com/Telenav/osrm-backend/integration/service/oasis/internal/mock"
"github.com/Telenav/osrm-backend/integration/service/oasis/spatialindexer"
"github.com/Telenav/osrm-backend/integration/service/oasis/spatialindexer/ranker"
)

Expand All @@ -32,82 +33,142 @@ func TestBuilderWithMockIteratorAndFinder(t *testing.T) {
// construct expect map
expect := make(ID2NearByIDsMap)

var idAndWeightArray = []IDAndWeight{
var idAndWeightArray = []*common.RankedPlaceInfo{
{
3,
Weight{
345.220003472554,
15.550450606871804,
PlaceInfo: common.PlaceInfo{
ID: 3,
Location: &nav.Location{
Lat: 37.401948,
Lon: -121.977384,
},
},
Weight: &common.Weight{
Distance: 345.220003472554,
Duration: 15.550450606871804,
},
},
{
2,
Weight{
402.8536530341791,
18.146560947485547,
PlaceInfo: common.PlaceInfo{
ID: 2,
Location: &nav.Location{
Lat: 37.399331,
Lon: -121.981193,
},
},
Weight: &common.Weight{
Distance: 402.8536530341791,
Duration: 18.146560947485547,
},
},
{
4,
Weight{
1627.1858848458571,
73.29666147954312,
PlaceInfo: common.PlaceInfo{
ID: 4,
Location: &nav.Location{
Lat: 37.407082,
Lon: -121.991937,
},
},
Weight: &common.Weight{
Distance: 1627.1858848458571,
Duration: 73.29666147954312,
},
},
{
5,
Weight{
4615.586636153461,
207.9093079348406,
PlaceInfo: common.PlaceInfo{
ID: 5,
Location: &nav.Location{
Lat: 37.407277,
Lon: -121.925482,
},
},
Weight: &common.Weight{
Distance: 4615.586636153461,
Duration: 207.9093079348406,
},
},
{
1,
Weight{
5257.70008125706,
236.8333369935613,
PlaceInfo: common.PlaceInfo{
ID: 1,
Location: &nav.Location{
Lat: 37.355204,
Lon: -121.953901,
},
},
Weight: &common.Weight{
Distance: 5257.70008125706,
Duration: 236.8333369935613,
},
},
{
6,
Weight{
6888.7486674247,
310.30399402813964,
PlaceInfo: common.PlaceInfo{
ID: 6,
Location: &nav.Location{
Lat: 37.375024,
Lon: -121.904706,
},
},
Weight: &common.Weight{
Distance: 6888.7486674247,
Duration: 310.30399402813964,
},
},
{
7,
Weight{
7041.893747628621,
317.2024210643523,
PlaceInfo: common.PlaceInfo{
ID: 7,
Location: &nav.Location{
Lat: 37.359592,
Lon: -121.914164,
},
},
Weight: &common.Weight{
Distance: 7041.893747628621,
Duration: 317.2024210643523,
},
},
{
10,
Weight{
8622.213424347745,
388.3879920877363,
PlaceInfo: common.PlaceInfo{
ID: 10,
Location: &nav.Location{
Lat: 37.373546,
Lon: -122.068904,
},
},
Weight: &common.Weight{
Distance: 8622.213424347745,
Duration: 388.3879920877363,
},
},
{
9,
Weight{
9438.804320070916,
425.1713657689602,
PlaceInfo: common.PlaceInfo{
ID: 9,
Location: &nav.Location{
Lat: 37.368453,
Lon: -122.0764,
},
},
Weight: &common.Weight{
Distance: 9438.804320070916,
Duration: 425.1713657689602,
},
},
{
8,
Weight{
9897.44482638937,
445.8308480355572,
PlaceInfo: common.PlaceInfo{
ID: 8,
Location: &nav.Location{
Lat: 37.366023,
Lon: -122.080777,
},
},
Weight: &common.Weight{
Distance: 9897.44482638937,
Duration: 445.8308480355572,
},
},
}

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

if !reflect.DeepEqual(actual, expect) {
Expand Down
17 changes: 3 additions & 14 deletions integration/service/oasis/connectivitymap/connectivity_map.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
package connectivitymap

import (
"github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"
"github.com/Telenav/osrm-backend/integration/service/oasis/spatialindexer"
"github.com/golang/glog"
)

// Weight represent weight information
type Weight struct {
Distance float64
Duration float64
}

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

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

// Connectivity Map used to query connectivity for given placeID
type ConnectivityMap struct {
Expand Down Expand Up @@ -76,7 +65,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.PlaceID) ([]IDAndWeight, bool) {
func (cm *ConnectivityMap) QueryConnectivity(placeID common.PlaceID) ([]*common.RankedPlaceInfo, bool) {
if result, ok := cm.id2nearByIDs[placeID]; ok {
return result, true
}
Expand Down
18 changes: 12 additions & 6 deletions integration/service/oasis/connectivitymap/connectivity_map_mock.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package connectivitymap

import "github.com/Telenav/osrm-backend/integration/service/oasis/internal/common"

var fakeID2NearByIDsMap3 = ID2NearByIDsMap{
1: []IDAndWeight{
1: []*common.RankedPlaceInfo{
{
ID: 2,
Weight: Weight{
PlaceInfo: common.PlaceInfo{
ID: 2,
},
Weight: &common.Weight{
Distance: 1,
Duration: 1,
},
},
},
2: []IDAndWeight{
2: []*common.RankedPlaceInfo{
{
ID: 3,
Weight: Weight{
PlaceInfo: common.PlaceInfo{
ID: 3,
},
Weight: &common.Weight{
Distance: 2,
Duration: 2,
},
Expand Down
Loading

0 comments on commit 1328354

Please sign in to comment.