Summary
When listing objects in a store.Store it is currently not possible to filter the results. The List(context.Context) method always lists all objects. The method should be extended to something like List(ctx context.Context, opts ...ListOption) to support filtering.
See List(context.Context, ObjectList, ...ListOption) in controller-runtime as inspiration.
Basic example
How to filter objects currently:
all, err := store.List(ctx)
if err != nil {
return nil, err
}
filtered []E
sel := labels.SelectorFromSet(labels.Set(map[string]string{"foo": "bar"}))
for _, obj := range all {
if !sel.Matches(labels.Set(obj.Metadata.Labels)) {
continue
}
filtered = append(filtered, obj)
}
return filtered, nil
How to filter with the new List(ctx context.Context, opts ...ListOption) method:
filtered, err := store.List(ctx, store.MatchingLabels{"foo": "bar"})
return filtered, err
Motivation
Filtering the list of objects is a common use-case and always fetching all objects in the store and then filtering in the client is inefficient. Supporting filtering directly in the store simplifies client code using store.Store, and allows to build optimizations into the store (e.g. indexing labels, caching objects).
This should simplify the implementation of ironcore-dev/ceph-provider#857
Summary
When listing objects in a
store.Storeit is currently not possible to filter the results. TheList(context.Context)method always lists all objects. The method should be extended to something likeList(ctx context.Context, opts ...ListOption)to support filtering.See List(context.Context, ObjectList, ...ListOption) in
controller-runtimeas inspiration.Basic example
How to filter objects currently:
How to filter with the new
List(ctx context.Context, opts ...ListOption)method:Motivation
Filtering the list of objects is a common use-case and always fetching all objects in the store and then filtering in the client is inefficient. Supporting filtering directly in the store simplifies client code using
store.Store, and allows to build optimizations into the store (e.g. indexing labels, caching objects).This should simplify the implementation of ironcore-dev/ceph-provider#857