Skip to content
Open
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
19 changes: 10 additions & 9 deletions gtfsdb/helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gtfsdb

import (
"cmp"
"context"
"crypto/sha256"
"database/sql"
Expand All @@ -11,7 +12,7 @@ import (
"log/slog"
"os"
"runtime"
"sort"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -890,8 +891,8 @@ func (c *Client) bulkInsertStopTimes(ctx context.Context, stopTimes []CreateStop
}

// Sort batches by index to maintain insertion order
sort.Slice(preparedBatches, func(i, j int) bool {
return preparedBatches[i].index < preparedBatches[j].index
slices.SortFunc(preparedBatches, func(a, b preparedStopTimeBatch) int {
return cmp.Compare(a.index, b.index)
})

logging.LogOperation(
Expand Down Expand Up @@ -1062,8 +1063,8 @@ func (c *Client) bulkInsertShapes(ctx context.Context, shapes []CreateShapeParam
}

// Sort batches by index to maintain insertion order
sort.Slice(preparedBatches, func(i, j int) bool {
return preparedBatches[i].index < preparedBatches[j].index
slices.SortFunc(preparedBatches, func(a, b preparedShapeBatch) int {
return cmp.Compare(a.index, b.index)
})

// ===== PHASE 3: SEQUENTIAL DATABASE EXECUTION =====
Expand Down Expand Up @@ -1293,11 +1294,11 @@ func (c *Client) buildBlockTripIndex(ctx context.Context, staticData *gtfs.Stati
}

// Sort trips within the group by block_id and then trip_id for deterministic ordering
sort.Slice(trips, func(i, j int) bool {
if trips[i].blockID != trips[j].blockID {
return trips[i].blockID < trips[j].blockID
slices.SortFunc(trips, func(a, b *tripInfo) int {
if c := cmp.Compare(a.blockID, b.blockID); c != 0 {
return c
}
return trips[i].tripID < trips[j].tripID
return cmp.Compare(a.tripID, b.tripID)
})

// Insert block_trip_entry records for each trip in this index
Expand Down
4 changes: 2 additions & 2 deletions gtfsdb/query_latency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"math"
"os"
"path/filepath"
"sort"
"slices"
"strings"
"sync"
"testing"
Expand Down Expand Up @@ -171,7 +171,7 @@ func (s *queryLatencyStat) report(t *testing.T) {
}
sorted := make([]time.Duration, len(s.samples))
copy(sorted, s.samples)
sort.Slice(sorted, func(i, j int) bool { return sorted[i] < sorted[j] })
slices.Sort(sorted)

idx := func(pct float64) int {
i := int(math.Round(float64(len(sorted))*pct)) - 1
Expand Down
4 changes: 2 additions & 2 deletions internal/gtfs/advanced_direction_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"errors"
"log/slog"
"math"
"sort"
"slices"
"strconv"
"strings"
"sync"
Expand Down Expand Up @@ -270,7 +270,7 @@ func (adc *AdvancedDirectionCalculator) computeFromShapes(ctx context.Context, s
normalizedThetas = append(normalizedThetas, thetaMu+delta)
}

sort.Float64s(normalizedThetas)
slices.Sort(normalizedThetas)
thetaMedian := median(normalizedThetas)

return adc.getAngleAsDirection(thetaMedian)
Expand Down
7 changes: 4 additions & 3 deletions internal/gtfs/block_layover_index.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package gtfs

import (
"sort"
"cmp"
"slices"

"github.com/OneBusAway/go-gtfs"
)
Expand Down Expand Up @@ -63,8 +64,8 @@ func buildBlockLayoverIndices(staticData *gtfs.Static) map[string][]*BlockLayove
}

// Sort trips by their start time (first stop departure)
sort.Slice(trips, func(i, j int) bool {
return trips[i].StopTimes[0].DepartureTime < trips[j].StopTimes[0].DepartureTime
slices.SortFunc(trips, func(a, b *gtfs.ScheduledTrip) int {
return cmp.Compare(a.StopTimes[0].DepartureTime, b.StopTimes[0].DepartureTime)
})

// Find layovers between consecutive trips
Expand Down
9 changes: 5 additions & 4 deletions internal/gtfs/gtfs_manager.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package gtfs

import (
"cmp"
"context"
"fmt"
"log/slog"
"os"
"sort"
"slices"
"strings"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -299,7 +300,7 @@ func InitGTFSManager(ctx context.Context, config Config) (*Manager, error) {
for validID := range manager.agenciesMap {
validAgencies = append(validAgencies, validID)
}
sort.Strings(validAgencies)
slices.Sort(validAgencies)
}

logger.Warn("configured agency-id not found in static GTFS data",
Expand Down Expand Up @@ -613,8 +614,8 @@ func (manager *Manager) GetStopsForLocation(
}
}

sort.Slice(candidates, func(i, j int) bool {
return candidates[i].distance < candidates[j].distance
slices.SortFunc(candidates, func(a, b stopWithDistance) int {
return cmp.Compare(a.distance, b.distance)
})

// When isForRoutes is true, return all matching stops without applying maxCount limit.
Expand Down
8 changes: 4 additions & 4 deletions internal/gtfs/realtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"log/slog"
"math/rand"
"net/http"
"sort"
"slices"
"sync"
"time"

Expand Down Expand Up @@ -597,7 +597,7 @@ func (manager *Manager) rebuildMergedRealtimeLocked() {
feedIDs = append(feedIDs, id)
totalTrips += len(trips)
}
sort.Strings(feedIDs)
slices.Sort(feedIDs)

allTrips := make([]gtfs.Trip, 0, totalTrips)
for _, id := range feedIDs {
Expand All @@ -610,7 +610,7 @@ func (manager *Manager) rebuildMergedRealtimeLocked() {
vehicleFeedIDs = append(vehicleFeedIDs, id)
totalVehicles += len(vehicles)
}
sort.Strings(vehicleFeedIDs)
slices.Sort(vehicleFeedIDs)

allVehicles := make([]gtfs.Vehicle, 0, totalVehicles)
for _, id := range vehicleFeedIDs {
Expand All @@ -621,7 +621,7 @@ func (manager *Manager) rebuildMergedRealtimeLocked() {
for id := range manager.feedAlerts {
alertFeedIDs = append(alertFeedIDs, id)
}
sort.Strings(alertFeedIDs)
slices.Sort(alertFeedIDs)

tripLookup := make(map[string]int, len(allTrips))
for i, trip := range allTrips {
Expand Down
7 changes: 4 additions & 3 deletions internal/restapi/block_distance_helper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package restapi

import (
"cmp"
"context"
"math"
"sort"
"slices"
"time"

"github.com/OneBusAway/go-gtfs"
Expand Down Expand Up @@ -70,8 +71,8 @@ func (api *RestAPI) getBlockDistanceToStop(ctx context.Context, targetTripID, ta
})
}

sort.Slice(activeTrips, func(i, j int) bool {
return activeTrips[i].StartTime < activeTrips[j].StartTime
slices.SortFunc(activeTrips, func(a, b TripInfo) int {
return cmp.Compare(a.StartTime, b.StartTime)
})

cumulativeDist := 0.0
Expand Down
11 changes: 6 additions & 5 deletions internal/restapi/block_handler.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package restapi

import (
"cmp"
"context"
"database/sql"
"net/http"
"sort"
"slices"
"strconv"

"maglev.onebusaway.org/gtfsdb"
Expand Down Expand Up @@ -75,7 +76,7 @@ func transformBlockToEntry(block []gtfsdb.GetBlockDetailsRow, blockID, agencyID
for serviceID := range serviceGroups {
serviceIDs = append(serviceIDs, serviceID)
}
sort.Strings(serviceIDs)
slices.Sort(serviceIDs)

configurations := make([]models.BlockConfiguration, 0, len(serviceGroups))

Expand All @@ -99,13 +100,13 @@ func transformBlockToEntry(block []gtfsdb.GetBlockDetailsRow, blockID, agencyID
for tripID := range tripStops {
tripIDs = append(tripIDs, tripID)
}
sort.Strings(tripIDs)
slices.Sort(tripIDs)

for _, tripID := range tripIDs {
stops := tripStops[tripID]

sort.Slice(stops, func(i, j int) bool {
return stops[i].StopSequence < stops[j].StopSequence
slices.SortFunc(stops, func(a, b gtfsdb.GetBlockDetailsRow) int {
return cmp.Compare(a.StopSequence, b.StopSequence)
})

var blockStopTimes []models.BlockStopTime
Expand Down
7 changes: 4 additions & 3 deletions internal/restapi/block_sequence_helper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package restapi

import (
"cmp"
"context"
"math"
"sort"
"slices"
"time"
)

Expand Down Expand Up @@ -53,8 +54,8 @@ func (api *RestAPI) getBlockSequenceForStopSequence(ctx context.Context, tripID
}
}

sort.Slice(activeTrips, func(i, j int) bool {
return activeTrips[i].StartTime < activeTrips[j].StartTime
slices.SortFunc(activeTrips, func(a, b TripWithDetails) int {
return cmp.Compare(a.StartTime, b.StartTime)
})

blockSequence := 0
Expand Down
7 changes: 4 additions & 3 deletions internal/restapi/calculate_block_trip_sequence_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package restapi

import (
"cmp"
"context"
"math"
"sort"
"slices"
"testing"
"time"

Expand Down Expand Up @@ -109,8 +110,8 @@ func TestCalculateBlockTripSequence(t *testing.T) {
}
results = append(results, tripSeq{sequence: seq, earliestDepart: minDepart})
}
sort.Slice(results, func(i, j int) bool {
return results[i].sequence < results[j].sequence
slices.SortFunc(results, func(a, b tripSeq) int {
return cmp.Compare(a.sequence, b.sequence)
})
for i := 1; i < len(results); i++ {
assert.LessOrEqual(t, results[i-1].earliestDepart, results[i].earliestDepart)
Expand Down
7 changes: 4 additions & 3 deletions internal/restapi/stops_for_location_handler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package restapi

import (
"cmp"
"fmt"
"net/http"
"sort"
"slices"
"strings"
"time"

Expand Down Expand Up @@ -98,8 +99,8 @@ func (api *RestAPI) stopsForLocationHandler(w http.ResponseWriter, r *http.Reque
stops := api.GtfsManager.GetStopsForLocation(ctx, loc.Lat, loc.Lon, loc.Radius, loc.LatSpan, loc.LonSpan, query, maxCount, false, routeTypes, queryTime)

// Referenced Java code: "here we sort by distance for possible truncation, but later it will be re-sorted by stopId"
sort.SliceStable(stops, func(i, j int) bool {
return stops[i].ID < stops[j].ID
slices.SortStableFunc(stops, func(a, b gtfsdb.Stop) int {
return cmp.Compare(a.ID, b.ID)
})

results := []models.Stop{}
Expand Down
19 changes: 11 additions & 8 deletions internal/restapi/stops_for_route_handler.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package restapi

import (
"cmp"
"context"
"net/http"
"sort"
"slices"
"strconv"
"time"

Expand Down Expand Up @@ -256,8 +257,8 @@ func processTripGroups(
// produces normalized group IDs "0", "1", … that match the Java OBA server's
// convention where outbound (direction_id=1) is group "0" and inbound
// (direction_id=0) is group "1".
sort.Slice(directionIDs, func(i, j int) bool {
return directionIDs[i] > directionIDs[j]
slices.SortFunc(directionIDs, func(a, b int64) int {
return cmp.Compare(b, a)
})

for i, dirID := range directionIDs {
Expand All @@ -268,8 +269,8 @@ func processTripGroups(
tripsInGroup := tripGroups[dirID]

// Sort trips by ID to ensure we always pick the same representative trip
sort.Slice(tripsInGroup, func(i, j int) bool {
return tripsInGroup[i].ID < tripsInGroup[j].ID
slices.SortFunc(tripsInGroup, func(a, b gtfsdb.Trip) int {
return cmp.Compare(a.ID, b.ID)
})

headsignCounts := make(map[string]int)
Expand Down Expand Up @@ -363,9 +364,10 @@ func processTripGroups(
}

if len(allStopGroups) > 0 {
sort.Slice(allStopGroups, func(i, j int) bool {
return allStopGroups[i].ID < allStopGroups[j].ID
slices.SortFunc(*stopGroupings, func(a, b models.StopGrouping) int {
return cmp.Compare(a.StopGroups[0].ID, b.StopGroups[0].ID)
})

*stopGroupings = append(*stopGroupings, models.StopGrouping{
Ordered: true,
StopGroups: allStopGroups,
Expand Down Expand Up @@ -395,6 +397,7 @@ func formatStopIDs(agencyID string, stops map[string]bool) []string {
for key := range stops {
stopIDs = append(stopIDs, utils.FormCombinedID(agencyID, key))
}
sort.Strings(stopIDs)
slices.Sort(stopIDs)

return stopIDs
}
4 changes: 2 additions & 2 deletions internal/utils/maps_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package utils

import (
"sort"
"slices"
"testing"
)

Expand Down Expand Up @@ -42,7 +42,7 @@ func TestMapValues(t *testing.T) {
if len(result) != 3 {
t.Errorf("expected 3 elements, got %d", len(result))
}
sort.Ints(result)
slices.Sort(result)
expected := []int{1, 2, 3}
for i, v := range result {
if v != expected[i] {
Expand Down
Loading