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

View File

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

View File

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

View File

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