@@ -8,10 +8,12 @@ import (
88 "encoding/json"
99 "errors"
1010 "fmt"
11+ "sort"
1112 "sync"
1213 "time"
1314
1415 "github.com/ceph/go-ceph/rados"
16+ "github.com/go-logr/logr"
1517 utilssync "github.com/ironcore-dev/ceph-provider/internal/sync"
1618 "github.com/ironcore-dev/ceph-provider/internal/utils"
1719 apiutils "github.com/ironcore-dev/provider-utils/apiutils/api"
@@ -31,7 +33,7 @@ type Options[E apiutils.Object] struct {
3133 CreateStrategy CreateStrategy [E ]
3234}
3335
34- func New [E apiutils.Object ](conn * rados.Conn , pool string , opts Options [E ]) (* Store [E ], error ) {
36+ func New [E apiutils.Object ](conn * rados.Conn , pool string , log logr. Logger , opts Options [E ]) (* Store [E ], error ) {
3537 if conn == nil {
3638 return nil , fmt .Errorf ("must specify conn" )
3739 }
@@ -55,10 +57,13 @@ func New[E apiutils.Object](conn *rados.Conn, pool string, opts Options[E]) (*St
5557 pool : pool ,
5658 omapName : opts .OmapName ,
5759
58- watches : sets .New [* watch [E ]](),
60+ // Initialize the label index
61+ labelIndex : make (map [string ]sets.Set [string ]),
62+ watches : sets .New [* watch [E ]](),
5963
6064 newFunc : opts .NewFunc ,
6165 createStrategy : opts .CreateStrategy ,
66+ log : log ,
6267 }, nil
6368}
6469
@@ -68,14 +73,69 @@ type Store[E apiutils.Object] struct {
6873 conn * rados.Conn
6974 pool string
7075 omapName string
76+ log logr.Logger
7177
7278 newFunc func () E
7379 createStrategy CreateStrategy [E ]
80+ labelIndex map [string ]sets.Set [string ] // Add label index field (labelKey=labelValue -> Set[objectID])
7481
7582 watchesMu sync.RWMutex
7683 watches sets.Set [* watch [E ]]
7784}
7885
86+ // --- Internal Label Index Helper ---
87+ func formatLabel (key , value string ) string {
88+ return fmt .Sprintf ("%s=%s" , key , value )
89+ }
90+
91+ // updateLabelIndex updates the index for a single object based on its labels.
92+ func (s * Store [E ]) updateLabelIndex (objID string , oldLabels , newLabels map [string ]string ) {
93+ s .log .V (2 ).Info ("Updating label index" , "id" , objID )
94+ oldLabelSet := sets .New [string ]()
95+ for k , v := range oldLabels {
96+ oldLabelSet .Insert (formatLabel (k , v ))
97+ }
98+
99+ newLabelSet := sets .New [string ]()
100+ for k , v := range newLabels {
101+ newLabelSet .Insert (formatLabel (k , v ))
102+ }
103+
104+ // Labels to remove objID from
105+ for label := range oldLabelSet .Difference (newLabelSet ) {
106+ if ids , ok := s .labelIndex [label ]; ok {
107+ ids .Delete (objID )
108+ if ids .Len () == 0 {
109+ delete (s .labelIndex , label )
110+ }
111+ }
112+ }
113+
114+ // Labels to add objID to
115+ for label := range newLabelSet .Difference (oldLabelSet ) {
116+ if _ , ok := s .labelIndex [label ]; ! ok {
117+ s .labelIndex [label ] = sets .New [string ]()
118+ }
119+ s .labelIndex [label ].Insert (objID )
120+ }
121+ s .log .V (2 ).Info ("Label index updated" , "id" , objID )
122+ }
123+
124+ // removeFromLabelIndex removes an object entirely from the label index.
125+ func (s * Store [E ]) removeFromLabelIndex (objID string , labels map [string ]string ) {
126+ s .log .V (2 ).Info ("Removing object from label index" , "id" , objID )
127+ for k , v := range labels {
128+ label := formatLabel (k , v )
129+ if ids , ok := s .labelIndex [label ]; ok {
130+ ids .Delete (objID )
131+ if ids .Len () == 0 {
132+ delete (s .labelIndex , label )
133+ }
134+ }
135+ }
136+ s .log .V (2 ).Info ("Object removed from label index" , "id" , objID )
137+ }
138+
79139func (s * Store [E ]) enqueue (evt store.WatchEvent [E ]) {
80140 for _ , handler := range s .watchHandlers () {
81141 select {
@@ -108,9 +168,11 @@ func (s *Store[E]) getSingleOmapValue(ioCtx *rados.IOContext, omapName, key stri
108168
109169func (s * Store [E ]) deleteOmapValue (ioCtx * rados.IOContext , omapName , key string ) error {
110170 if err := ioCtx .RmOmapKeys (omapName , []string {key }); err != nil {
171+ if errors .Is (err , rados .ErrNotFound ) {
172+ return err
173+ }
111174 return fmt .Errorf ("unable to delete mapping omap value: %w" , err )
112175 }
113-
114176 return nil
115177}
116178
@@ -155,6 +217,8 @@ func (s *Store[E]) Create(ctx context.Context, obj E) (E, error) {
155217 return utils .Zero [E ](), err
156218 }
157219
220+ s .updateLabelIndex (obj .GetID (), nil , obj .GetLabels ())
221+
158222 s .enqueue (store.WatchEvent [E ]{
159223 Type : store .WatchEventTypeCreated ,
160224 Object : obj ,
@@ -169,7 +233,7 @@ func (s *Store[E]) Delete(ctx context.Context, id string) error {
169233
170234 ioCtx , err := s .conn .OpenIOContext (s .pool )
171235 if err != nil {
172- return fmt .Errorf ("unable to get io context: %w" , err )
236+ return fmt .Errorf ("unable to get io context for delete : %w" , err )
173237 }
174238 defer ioCtx .Destroy ()
175239
@@ -194,6 +258,7 @@ func (s *Store[E]) Delete(ctx context.Context, id string) error {
194258 return fmt .Errorf ("failed to set object metadata: %w" , err )
195259 }
196260
261+ s .removeFromLabelIndex (id , obj .GetLabels ())
197262 s .enqueue (store.WatchEvent [E ]{
198263 Type : store .WatchEventTypeDeleted ,
199264 Object : obj ,
@@ -204,6 +269,10 @@ func (s *Store[E]) Delete(ctx context.Context, id string) error {
204269
205270func (s * Store [E ]) delete (ioCtx * rados.IOContext , id string ) error {
206271 if err := s .deleteOmapValue (ioCtx , s .omapName , id ); err != nil {
272+ if errors .Is (err , rados .ErrNotFound ) {
273+ s .log .V (1 ).Info ("Attempted to delete non-existent key from omap" , "id" , id )
274+ return store .ErrNotFound
275+ }
207276 return fmt .Errorf ("failed to delete object from omap: %w" , err )
208277 }
209278 return nil
@@ -234,25 +303,53 @@ func (s *Store[E]) Update(ctx context.Context, obj E) (E, error) {
234303 return utils .Zero [E ](), err
235304 }
236305
306+ // Begin OMAP Update Logic
307+ deleted := false
308+ oldLabels := oldObj .GetLabels ()
309+ newLabels := obj .GetLabels ()
310+
237311 if obj .GetDeletedAt () != nil && len (obj .GetFinalizers ()) == 0 {
312+ s .log .V (1 ).Info ("Update triggers physical deletion" , "id" , obj .GetID ())
313+ if oldObj .GetResourceVersion () != obj .GetResourceVersion () {
314+ s .log .V (1 ).Info ("ResourceVersion mismatch during update-triggered delete" , "id" , obj .GetID (), "expected" , oldObj .GetResourceVersion (), "got" , obj .GetResourceVersion ())
315+ return utils .Zero [E ](), fmt .Errorf ("failed to delete object during update: %w" , ErrResourceVersionNotLatest )
316+ }
317+
238318 if err := s .delete (ioCtx , obj .GetID ()); err != nil {
239- return utils .Zero [E ](), fmt .Errorf ("failed to delete object metadata: %w" , err )
319+ if ! errors .Is (err , store .ErrNotFound ) {
320+ return utils .Zero [E ](), fmt .Errorf ("failed to delete object from omap during update: %w" , err )
321+ }
322+ s .log .V (1 ).Info ("Object already deleted from OMAP during update" , "id" , obj .GetID ())
240323 }
241- return obj , nil
242- }
324+ deleted = true
325+ } else {
326+ s .log .V (1 ).Info ("Performing standard update" , "id" , obj .GetID ())
327+ if oldObj .GetResourceVersion () != obj .GetResourceVersion () {
328+ s .log .V (1 ).Info ("ResourceVersion mismatch during update" , "id" , obj .GetID (), "expected" , oldObj .GetResourceVersion (), "got" , obj .GetResourceVersion ())
329+ return utils .Zero [E ](), fmt .Errorf ("failed to update object: %w" , ErrResourceVersionNotLatest )
330+ }
331+ obj .IncrementResourceVersion ()
243332
244- if oldObj .GetResourceVersion () != obj .GetResourceVersion () {
245- return utils .Zero [E ](), fmt .Errorf ("failed to update object: %w" , ErrResourceVersionNotLatest )
333+ obj , err = s .set (ioCtx , obj )
334+ if err != nil {
335+ return utils .Zero [E ](), err
336+ }
337+ s .log .V (1 ).Info ("Object updated in OMAP" , "id" , obj .GetID (), "newResourceVersion" , obj .GetResourceVersion ())
246338 }
247- obj .IncrementResourceVersion ()
248339
249- obj , err = s .set (ioCtx , obj )
250- if err != nil {
251- return utils .Zero [E ](), err
340+ var eventType store.WatchEventType
341+ if deleted {
342+ s .removeFromLabelIndex (obj .GetID (), oldLabels )
343+ eventType = store .WatchEventTypeDeleted
344+ s .log .V (1 ).Info ("Object removed from label index" , "id" , obj .GetID ())
345+ } else {
346+ s .updateLabelIndex (obj .GetID (), oldLabels , newLabels )
347+ eventType = store .WatchEventTypeUpdated
348+ s .log .V (1 ).Info ("Object updated in label index" , "id" , obj .GetID ())
252349 }
253350
254351 s .enqueue (store.WatchEvent [E ]{
255- Type : store . WatchEventTypeUpdated ,
352+ Type : eventType ,
256353 Object : obj ,
257354 })
258355
@@ -348,3 +445,78 @@ func (s *Store[E]) get(ioCtx *rados.IOContext, id string) (E, error) {
348445
349446 return obj , nil
350447}
448+
449+ type sizedLabel struct {
450+ ids sets.Set [string ]
451+ size int
452+ }
453+
454+ func (s * Store [E ]) ListByLabels (ctx context.Context , labelSelector map [string ]string ) ([]E , error ) {
455+ if len (labelSelector ) == 0 {
456+ s .log .V (1 ).Info ("Empty label selector provided, returning all items (like List)" )
457+ return s .List (ctx )
458+ }
459+
460+ s .log .V (1 ).Info ("Listing objects by labels" , "selector" , labelSelector )
461+
462+ // pre-allocate the slice to avoid extra memory allocations
463+ labelSelect := make ([]sizedLabel , 0 , len (labelSelector ))
464+ var intersection sets.Set [string ]
465+
466+ // 1 .Gather label set sizes and check for immediate non-matches.
467+ for key , value := range labelSelector {
468+ label := formatLabel (key , value )
469+ ids , found := s .labelIndex [label ]
470+ if ! found {
471+ s .log .V (1 ).Info ("Label not found in index, no objects match the full selector" , "label" , label , "selector" , labelSelector )
472+ return []E {}, nil
473+ }
474+
475+ labelSelect = append (labelSelect , sizedLabel {
476+ ids : ids ,
477+ size : ids .Len (),
478+ })
479+ }
480+ if len (labelSelect ) > 1 {
481+ // 2. Sort the labels by the size of their matching set (smallest first).
482+ sort .Slice (labelSelect , func (i , j int ) bool {
483+ return labelSelect [i ].size < labelSelect [j ].size
484+ })
485+ }
486+
487+ var isFirstLabel = true
488+
489+ // 3. Iterate over the sorted slice (labelsForSort) to compute the intersection.
490+ for _ , info := range labelSelect {
491+ ids := info .ids
492+ if isFirstLabel {
493+ // Use the smallest set to initialize the intersection (copy to avoid modifying the index set).
494+ intersection = ids .Clone ()
495+ s .log .V (1 ).Info ("Initialized intersection set with label" , "initial_count" , intersection .Len ())
496+ isFirstLabel = false
497+ } else {
498+ // Intersect the current result with the next smallest set.
499+ prevCount := intersection .Len ()
500+ intersection = intersection .Intersection (ids )
501+ s .log .V (1 ).Info ("Computed intersection" , "previous_count" , prevCount , "new_count" , intersection .Len ())
502+ }
503+
504+ if intersection .Len () == 0 {
505+ s .log .V (1 ).Info ("Intersection of label matches became empty, returning empty list" , "selector" , labelSelector )
506+ return []E {}, nil
507+ }
508+ }
509+
510+ objs := make ([]E , 0 , intersection .Len ())
511+ for id := range intersection {
512+ obj , err := s .Get (ctx , id )
513+ if err != nil {
514+ s .log .Error (err , "Failed to get object from store during ListByLabels" , "id" , id )
515+ return nil , err
516+ }
517+ objs = append (objs , obj )
518+ }
519+
520+ s .log .V (1 ).Info ("Successfully retrieved objects matching label selector" , "count" , len (objs ), "selector" , labelSelector )
521+ return objs , nil
522+ }
0 commit comments