Skip to content

Commit

Permalink
Move a few internal modules to where they are used
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Nov 10, 2023
1 parent 223339b commit 579c5c3
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package admincache
package dbfinder

import (
"context"
Expand All @@ -15,27 +15,27 @@ import (
"github.com/tidwall/rtree"
)

type AdminItem struct {
type adminCacheItem struct {
Adm0Name string
Adm1Name string
Adm0Iso string
Adm1Iso string
Geometry *geom.Polygon
}

type AdminCache struct {
type adminCache struct {
lock sync.Mutex
index rtree.Generic[*AdminItem]
cache map[xy.Point]*AdminItem
index rtree.Generic[*adminCacheItem]
cache map[xy.Point]*adminCacheItem
}

func NewAdminCache() *AdminCache {
return &AdminCache{
cache: map[xy.Point]*AdminItem{},
func newAdminCache() *adminCache {
return &adminCache{
cache: map[xy.Point]*adminCacheItem{},
}
}

func (c *AdminCache) LoadAdmins(ctx context.Context, dbx sqlx.Ext) error {
func (c *adminCache) LoadAdmins(ctx context.Context, dbx sqlx.Ext) error {
c.lock.Lock()
defer c.lock.Unlock()
var ents []struct {
Expand All @@ -62,7 +62,7 @@ func (c *AdminCache) LoadAdmins(ctx context.Context, dbx sqlx.Ext) error {
continue
}
for i := 0; i < g.NumPolygons(); i++ {
item := AdminItem{
item := adminCacheItem{
Adm0Name: ent.Adm0Name.Val,
Adm1Name: ent.Adm1Name.Val,
Adm0Iso: ent.Adm0Iso.Val,
Expand All @@ -78,7 +78,7 @@ func (c *AdminCache) LoadAdmins(ctx context.Context, dbx sqlx.Ext) error {
return nil
}

func (c *AdminCache) Check(pt xy.Point) (AdminItem, bool) {
func (c *adminCache) Check(pt xy.Point) (adminCacheItem, bool) {
ret, count := c.CheckPolygon(pt)
if count >= 1 {
return ret, count == 1
Expand All @@ -89,18 +89,18 @@ func (c *AdminCache) Check(pt xy.Point) (AdminItem, bool) {
return nearestAdmin, count >= 1
}

func (c *AdminCache) CheckPolygon(p xy.Point) (AdminItem, int) {
func (c *adminCache) CheckPolygon(p xy.Point) (adminCacheItem, int) {
// Checking just the index can be much faster, but can be invalid in open water, e.g. 0,0 = Kiribati
// However, in practice, most land area on Earth falls into more than 1 admin bbox
// No, we are not being fancy with projections.
// That could be improved.
ret := AdminItem{}
ret := adminCacheItem{}
gp := geom.NewPointFlat(geom.XY, []float64{p.Lon, p.Lat})
count := 0
c.index.Search(
[2]float64{p.Lon, p.Lat},
[2]float64{p.Lon, p.Lat},
func(min, max [2]float64, s *AdminItem) bool {
func(min, max [2]float64, s *adminCacheItem) bool {
if pointInPolygon(s.Geometry, gp) {
ret.Adm0Name = s.Adm0Name
ret.Adm1Name = s.Adm1Name
Expand All @@ -114,15 +114,15 @@ func (c *AdminCache) CheckPolygon(p xy.Point) (AdminItem, int) {
return ret, count
}

func (c *AdminCache) NearestPolygon(p xy.Point, tolerance float64) (AdminItem, float64, int) {
ret := AdminItem{}
func (c *adminCache) NearestPolygon(p xy.Point, tolerance float64) (adminCacheItem, float64, int) {
ret := adminCacheItem{}
minDist := -1.0
gp := geom.NewPointFlat(geom.XY, []float64{p.Lon, p.Lat})
count := 0
c.index.Search(
[2]float64{p.Lon - tolerance, p.Lat - tolerance},
[2]float64{p.Lon + tolerance, p.Lat + tolerance},
func(min, max [2]float64, s *AdminItem) bool {
func(min, max [2]float64, s *adminCacheItem) bool {
d := pointPolygonDistance(s.Geometry, gp)
if d < tolerance && (d < minDist || minDist < 0) {
ret.Adm0Name = s.Adm0Name
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package admincache
package dbfinder

import (
"context"
Expand Down Expand Up @@ -48,7 +48,7 @@ func getTestCases() []testCase {

func TestAdminCache(t *testing.T) {
dbx := testutil.MustOpenTestDB()
c := NewAdminCache()
c := newAdminCache()
c.LoadAdmins(context.Background(), dbx)
tcs := getTestCases()
for _, tc := range tcs {
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestAdminCache(t *testing.T) {

func BenchmarkTestAdminCache(b *testing.B) {
dbx := testutil.MustOpenTestDB()
c := NewAdminCache()
c := newAdminCache()
c.LoadAdmins(context.Background(), dbx)
b.ResetTimer()
tcs := getTestCases()
Expand All @@ -101,7 +101,7 @@ func BenchmarkTestAdminCache(b *testing.B) {

func BenchmarkTestAdminCache_LoadAdmins(b *testing.B) {
dbx := testutil.MustOpenTestDB()
c := NewAdminCache()
c := newAdminCache()
for n := 0; n < b.N; n++ {
if err := c.LoadAdmins(context.Background(), dbx); err != nil {
b.Fatal(err)
Expand Down
7 changes: 3 additions & 4 deletions finders/dbfinder/finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/interline-io/transitland-lib/tl"
"github.com/interline-io/transitland-lib/tl/tt"
"github.com/interline-io/transitland-server/auth/authz"
"github.com/interline-io/transitland-server/internal/admincache"
"github.com/interline-io/transitland-server/internal/clock"
"github.com/interline-io/transitland-server/internal/dbutil"
"github.com/interline-io/transitland-server/internal/xy"
Expand All @@ -24,7 +23,7 @@ import (
type Finder struct {
Clock clock.Clock
db sqlx.Ext
adminCache *admincache.AdminCache
adminCache *adminCache
authzChecker model.Checker
}

Expand All @@ -38,7 +37,7 @@ func (f *Finder) DBX() sqlx.Ext {

func (f *Finder) LoadAdmins() error {
log.Trace().Msg("loading admins")
adminCache := admincache.NewAdminCache()
adminCache := newAdminCache()
if err := adminCache.LoadAdmins(context.Background(), f.db); err != nil {
return err
}
Expand Down Expand Up @@ -1668,7 +1667,7 @@ func (f *Finder) StopPlacesByStopID(ctx context.Context, params []model.StopPlac
}
}

// Lookup stop places using AdminCache
// Lookup stop places using adminCache
a := map[int]*model.StopPlace{}
for _, param := range params {
if admin, ok := f.adminCache.Check(param.Point); ok {
Expand Down
24 changes: 0 additions & 24 deletions internal/fvsl/fvsl_cache_test.go

This file was deleted.

22 changes: 11 additions & 11 deletions internal/fvsl/fvsl_cache.go → server/gql/fvsl_cache.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package fvsl
package gql

import (
"context"
Expand All @@ -9,28 +9,28 @@ import (
"github.com/interline-io/transitland-server/model"
)

type FVSLWindow struct {
type fvslWindow struct {
FetchedAt time.Time
StartDate time.Time
EndDate time.Time
BestWeek time.Time
Valid bool
}

type FVSLCache struct {
type fvslCache struct {
Finder model.Finder
lock sync.Mutex
fvWindows map[int]FVSLWindow
fvWindows map[int]fvslWindow
}

func NewFVSLCache(f model.Finder) *FVSLCache {
return &FVSLCache{
func newFvslCache(f model.Finder) *fvslCache {
return &fvslCache{
Finder: f,
fvWindows: map[int]FVSLWindow{},
fvWindows: map[int]fvslWindow{},
}
}

func (f *FVSLCache) Get(fvid int) (FVSLWindow, bool) {
func (f *fvslCache) Get(fvid int) (fvslWindow, bool) {
f.lock.Lock()
a, ok := f.fvWindows[fvid]
f.lock.Unlock()
Expand All @@ -45,15 +45,15 @@ func (f *FVSLCache) Get(fvid int) (FVSLWindow, bool) {
return a, a.Valid
}

func (f *FVSLCache) Set(fvid int, w FVSLWindow) {
func (f *fvslCache) Set(fvid int, w fvslWindow) {
f.lock.Lock()
defer f.lock.Unlock()
f.fvWindows[fvid] = w
}

func (f *FVSLCache) query(fvid int) (FVSLWindow, error) {
func (f *fvslCache) query(fvid int) (fvslWindow, error) {
var err error
w := FVSLWindow{}
w := fvslWindow{}
w.StartDate, w.EndDate, w.BestWeek, err = f.Finder.FindFeedVersionServiceWindow(context.TODO(), fvid)
log.Trace().
Str("start_date", w.StartDate.Format("2006-01-02")).
Expand Down
13 changes: 13 additions & 0 deletions server/gql/fvsl_cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package gql

import (
"testing"

"github.com/interline-io/transitland-server/internal/testfinder"
)

func TestFvslCache(t *testing.T) {
te := testfinder.Finders(t, nil, nil)
c := newFvslCache(te.Finder)
c.Get(1)
}
3 changes: 1 addition & 2 deletions server/gql/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"strconv"

"github.com/interline-io/transitland-server/config"
"github.com/interline-io/transitland-server/internal/fvsl"
"github.com/interline-io/transitland-server/internal/generated/gqlout"
"github.com/interline-io/transitland-server/internal/xy"
"github.com/interline-io/transitland-server/model"
Expand Down Expand Up @@ -60,7 +59,7 @@ type Resolver struct {
finder model.Finder
gbfsFinder model.GbfsFinder
authzChecker model.Checker
fvslCache *fvsl.FVSLCache
fvslCache *fvslCache
}

// Query .
Expand Down
3 changes: 1 addition & 2 deletions server/gql/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/99designs/gqlgen/graphql/handler"
"github.com/interline-io/transitland-server/auth/authn"
"github.com/interline-io/transitland-server/config"
"github.com/interline-io/transitland-server/internal/fvsl"
"github.com/interline-io/transitland-server/internal/generated/gqlout"
"github.com/interline-io/transitland-server/model"
)
Expand All @@ -20,7 +19,7 @@ func NewServer(cfg config.Config, dbfinder model.Finder, rtfinder model.RTFinder
finder: dbfinder,
rtfinder: rtfinder,
gbfsFinder: gbfsFinder,
fvslCache: fvsl.NewFVSLCache(dbfinder),
fvslCache: newFvslCache(dbfinder),
authzChecker: checker,
}}
c.Directives.HasRole = func(ctx context.Context, obj interface{}, next graphql.Resolver, role model.Role) (interface{}, error) {
Expand Down

0 comments on commit 579c5c3

Please sign in to comment.