github.com/cilium/cilium@v1.16.2/pkg/kvstore/watcher_cache.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package kvstore
     5  
     6  type watchState struct {
     7  	deletionMark bool
     8  }
     9  
    10  type watcherCache map[string]watchState
    11  
    12  func (wc watcherCache) Exists(key []byte) bool {
    13  	if _, ok := wc[string(key)]; ok {
    14  		return true
    15  	}
    16  
    17  	return false
    18  }
    19  
    20  // RemoveDeleted removes keys marked for deletion from the local cache exiting
    21  // early if the given function returns false.
    22  func (wc watcherCache) RemoveDeleted(f func(string) bool) bool {
    23  	for k, localKey := range wc {
    24  		if localKey.deletionMark {
    25  			if !f(k) {
    26  				return false
    27  			}
    28  			delete(wc, k)
    29  		}
    30  	}
    31  	return true
    32  }
    33  
    34  func (wc watcherCache) MarkAllForDeletion() {
    35  	for k := range wc {
    36  		wc[k] = watchState{deletionMark: true}
    37  	}
    38  }
    39  
    40  func (wc watcherCache) MarkInUse(key []byte) {
    41  	wc[string(key)] = watchState{deletionMark: false}
    42  }
    43  
    44  func (wc watcherCache) RemoveKey(key []byte) {
    45  	delete(wc, string(key))
    46  }