move filtering to store

This commit is contained in:
Annika Hannig 2024-01-16 16:33:22 +01:00
parent 852d2d7a6d
commit b21c47c8de
4 changed files with 33 additions and 17 deletions

View File

@ -51,7 +51,7 @@ func (s *Server) apiLookupPrefixGlobal(
if err != nil { if err != nil {
return nil, err return nil, err
} }
routes, err = s.routesStore.LookupPrefix(ctx, q) routes, err = s.routesStore.LookupPrefix(ctx, q, filtersApplied)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -66,7 +66,7 @@ func (s *Server) apiLookupPrefixGlobal(
if err != nil { if err != nil {
return nil, err return nil, err
} }
routes, err = s.routesStore.LookupPrefixForNeighbors(ctx, neighbors) routes, err = s.routesStore.LookupPrefixForNeighbors(ctx, neighbors, filtersApplied)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -94,15 +94,11 @@ func (s *Server) apiLookupPrefixGlobal(
filtersNotAvailable, api.SearchKeyCommunities) filtersNotAvailable, api.SearchKeyCommunities)
} }
// Now, as we have allocated even more space process routes by, splitting, // Now, as we have allocated even more space split routes,
// filtering and updating the available filters... // and update the available filters...
filtersAvailable := api.NewSearchFilters() filtersAvailable := api.NewSearchFilters()
for _, r := range routes { for _, r := range routes {
if !filtersApplied.MatchRoute(r) {
continue // Exclude route from results set
}
switch r.State { switch r.State {
case api.RouteStateFiltered: case api.RouteStateFiltered:
filtered = append(filtered, r) filtered = append(filtered, r)

View File

@ -67,15 +67,21 @@ func (r *RoutesBackend) CountRoutesAt(
func (r *RoutesBackend) FindByNeighbors( func (r *RoutesBackend) FindByNeighbors(
ctx context.Context, ctx context.Context,
query []*api.NeighborQuery, query []*api.NeighborQuery,
filters *api.SearchFilters,
) (api.LookupRoutes, error) { ) (api.LookupRoutes, error) {
result := api.LookupRoutes{} result := api.LookupRoutes{}
r.routes.Range(func(k, rs interface{}) bool { r.routes.Range(func(k, rs interface{}) bool {
for _, route := range rs.(api.LookupRoutes) { for _, route := range rs.(api.LookupRoutes) {
for _, q := range query { for _, q := range query {
if route.MatchNeighborQuery(q) { if !route.MatchNeighborQuery(q) {
result = append(result, route) continue
} }
if !filters.MatchRoute(route) {
continue
}
result = append(result, route)
} }
} }
return true return true
@ -88,6 +94,7 @@ func (r *RoutesBackend) FindByNeighbors(
func (r *RoutesBackend) FindByPrefix( func (r *RoutesBackend) FindByPrefix(
ctx context.Context, ctx context.Context,
prefix string, prefix string,
filters *api.SearchFilters,
) (api.LookupRoutes, error) { ) (api.LookupRoutes, error) {
// We make our compare case insensitive // We make our compare case insensitive
prefix = strings.ToLower(prefix) prefix = strings.ToLower(prefix)
@ -95,9 +102,13 @@ func (r *RoutesBackend) FindByPrefix(
r.routes.Range(func(k, rs interface{}) bool { r.routes.Range(func(k, rs interface{}) bool {
for _, route := range rs.(api.LookupRoutes) { for _, route := range rs.(api.LookupRoutes) {
// Naiive string filtering: // Naiive string filtering:
if strings.HasPrefix(strings.ToLower(route.Network), prefix) { if !strings.HasPrefix(strings.ToLower(route.Network), prefix) {
result = append(result, route) continue
} }
if !filters.MatchRoute(route) {
continue
}
result = append(result, route)
} }
return true return true
}) })

View File

@ -214,6 +214,7 @@ func (b *RoutesBackend) CountRoutesAt(
func (b *RoutesBackend) FindByNeighbors( func (b *RoutesBackend) FindByNeighbors(
ctx context.Context, ctx context.Context,
neighbors []*api.NeighborQuery, neighbors []*api.NeighborQuery,
filters *api.SearchFilters,
) (api.LookupRoutes, error) { ) (api.LookupRoutes, error) {
tx, err := b.pool.BeginTx(ctx, pgx.TxOptions{ tx, err := b.pool.BeginTx(ctx, pgx.TxOptions{
IsoLevel: pgx.ReadCommitted, IsoLevel: pgx.ReadCommitted,
@ -248,13 +249,14 @@ func (b *RoutesBackend) FindByNeighbors(
return nil, err return nil, err
} }
return fetchRoutes(rows) return fetchRoutes(rows, filters)
} }
// FindByPrefix will return the prefixes matching a pattern // FindByPrefix will return the prefixes matching a pattern
func (b *RoutesBackend) FindByPrefix( func (b *RoutesBackend) FindByPrefix(
ctx context.Context, ctx context.Context,
prefix string, prefix string,
filters *api.SearchFilters,
) (api.LookupRoutes, error) { ) (api.LookupRoutes, error) {
tx, err := b.pool.BeginTx(ctx, pgx.TxOptions{ tx, err := b.pool.BeginTx(ctx, pgx.TxOptions{
IsoLevel: pgx.ReadCommitted, IsoLevel: pgx.ReadCommitted,
@ -278,11 +280,11 @@ func (b *RoutesBackend) FindByPrefix(
if err != nil { if err != nil {
return nil, err return nil, err
} }
return fetchRoutes(rows) return fetchRoutes(rows, filters)
} }
// Private fetchRoutes will load the queried result set // Private fetchRoutes will load the queried result set
func fetchRoutes(rows pgx.Rows) (api.LookupRoutes, error) { func fetchRoutes(rows pgx.Rows, filters *api.SearchFilters) (api.LookupRoutes, error) {
cmd := rows.CommandTag() cmd := rows.CommandTag()
results := make(api.LookupRoutes, 0, cmd.RowsAffected()) results := make(api.LookupRoutes, 0, cmd.RowsAffected())
for rows.Next() { for rows.Next() {
@ -290,6 +292,9 @@ func fetchRoutes(rows pgx.Rows) (api.LookupRoutes, error) {
if err := rows.Scan(&route); err != nil { if err := rows.Scan(&route); err != nil {
return nil, err return nil, err
} }
if !filters.MatchRoute(route) {
continue
}
results = append(results, route) results = append(results, route)
} }
return results, nil return results, nil

View File

@ -52,12 +52,14 @@ type RoutesStoreBackend interface {
FindByNeighbors( FindByNeighbors(
ctx context.Context, ctx context.Context,
neighbors []*api.NeighborQuery, neighbors []*api.NeighborQuery,
filters *api.SearchFilters,
) (api.LookupRoutes, error) ) (api.LookupRoutes, error)
// FindByPrefix // FindByPrefix
FindByPrefix( FindByPrefix(
ctx context.Context, ctx context.Context,
prefix string, prefix string,
filters *api.SearchFilters,
) (api.LookupRoutes, error) ) (api.LookupRoutes, error)
} }
@ -328,8 +330,9 @@ func (s *RoutesStore) CacheTTL(
func (s *RoutesStore) LookupPrefix( func (s *RoutesStore) LookupPrefix(
ctx context.Context, ctx context.Context,
prefix string, prefix string,
filters *api.SearchFilters,
) (api.LookupRoutes, error) { ) (api.LookupRoutes, error) {
return s.backend.FindByPrefix(ctx, prefix) return s.backend.FindByPrefix(ctx, prefix, filters)
} }
// LookupPrefixForNeighbors returns all routes for // LookupPrefixForNeighbors returns all routes for
@ -337,6 +340,7 @@ func (s *RoutesStore) LookupPrefix(
func (s *RoutesStore) LookupPrefixForNeighbors( func (s *RoutesStore) LookupPrefixForNeighbors(
ctx context.Context, ctx context.Context,
neighbors api.NeighborsLookupResults, neighbors api.NeighborsLookupResults,
filters *api.SearchFilters,
) (api.LookupRoutes, error) { ) (api.LookupRoutes, error) {
query := make([]*api.NeighborQuery, 0, len(neighbors)) query := make([]*api.NeighborQuery, 0, len(neighbors))
@ -349,5 +353,5 @@ func (s *RoutesStore) LookupPrefixForNeighbors(
query = append(query, q) query = append(query, q)
} }
} }
return s.backend.FindByNeighbors(ctx, query) return s.backend.FindByNeighbors(ctx, query, filters)
} }