1616package io .fabric8 .kubernetes .client .informers .impl .cache ;
1717
1818import io .fabric8 .kubernetes .api .model .HasMetadata ;
19- import io .fabric8 .kubernetes .api .model .ObjectMeta ;
2019import io .fabric8 .kubernetes .client .informers .cache .BasicItemStore ;
2120import io .fabric8 .kubernetes .client .informers .cache .Cache ;
2221import io .fabric8 .kubernetes .client .informers .cache .ItemStore ;
23- import io .fabric8 .kubernetes .client .utils .Utils ;
2422
2523import java .util .ArrayList ;
2624import java .util .Collections ;
4543 */
4644public class CacheImpl <T extends HasMetadata > implements Cache <T > {
4745
48- private static class Index {
49- private Map <Object , Set <String >> values = new ConcurrentHashMap <Object , Set <String >>();
50-
51- public void update (String indexKey , String key , boolean remove ) {
52- if (remove ) {
53- values .computeIfPresent (indexKey == null ? this : indexKey , (k , v ) -> {
54- v .remove (key );
55- return v .isEmpty () ? null : v ;
56- });
57- } else {
58- values .computeIfAbsent (indexKey == null ? this : indexKey , k -> ConcurrentHashMap .newKeySet ()).add (key );
59- }
60- }
61-
62- public Set <String > get (String indexKey ) {
63- return values .getOrDefault (indexKey == null ? this : indexKey , Collections .emptySet ());
64- }
65- }
66-
6746 // NAMESPACE_INDEX is the default index function for caching objects
6847 public static final String NAMESPACE_INDEX = "namespace" ;
69-
7048 // indexers stores index functions by their names
7149 private final Map <String , Function <T , List <String >>> indexers = Collections .synchronizedMap (new HashMap <>());
72-
73- // items stores object instances
74- private ItemStore <T > items ;
75-
7650 // indices stores objects' key by their indices
7751 private final ConcurrentMap <String , Index > indices = new ConcurrentHashMap <>();
52+ // items stores object instances
53+ private ItemStore <T > items ;
7854
7955 public CacheImpl () {
8056 this (NAMESPACE_INDEX , Cache ::metaNamespaceIndexFunc , Cache ::metaNamespaceKeyFunc );
@@ -85,6 +61,30 @@ public CacheImpl(String indexName, Function<T, List<String>> indexFunc, Function
8561 addIndexFunc (indexName , indexFunc );
8662 }
8763
64+ /**
65+ * @deprecated use {@link Cache#metaNamespaceKeyFunc(HasMetadata)} instead
66+ */
67+ public static String metaNamespaceKeyFunc (Object obj ) {
68+ if (obj == null ) {
69+ return "" ;
70+ }
71+ return Cache .metaNamespaceKeyFunc ((HasMetadata ) obj );
72+ }
73+
74+ /**
75+ * @deprecated Use {@link Cache#namespaceKeyFunc(String, String)} instead
76+ */
77+ public static String namespaceKeyFunc (String objectNamespace , String objectName ) {
78+ return Cache .namespaceKeyFunc (objectNamespace , objectName );
79+ }
80+
81+ /**
82+ * @deprecated Use {@link Cache#metaNamespaceKeyFunc(HasMetadata)} instead
83+ */
84+ public static List <String > metaNamespaceIndexFunc (Object obj ) {
85+ return Cache .metaNamespaceIndexFunc ((HasMetadata ) obj );
86+ }
87+
8888 public void setItemStore (ItemStore <T > items ) {
8989 this .items = items ;
9090 }
@@ -263,7 +263,6 @@ public List<T> byIndex(String indexName, String indexKey) {
263263 * UpdateIndices modifies the objects location in the managed indexes, if there is
264264 * an update, you must provide an oldObj
265265 *
266- *
267266 * @param oldObj old object
268267 * @param newObj new object
269268 * @param key the key
@@ -312,63 +311,6 @@ public synchronized CacheImpl<T> addIndexFunc(String indexName, Function<T, List
312311 return this ;
313312 }
314313
315- /**
316- * It's is a convenient default KeyFunc which know show to make keys for API
317- * objects which implement HasMetadata interface. The key uses the format
318- * namespace/name unless namespace is empty, then it's just name
319- *
320- * @param obj specific object
321- * @return the key
322- */
323- public static String metaNamespaceKeyFunc (Object obj ) {
324- if (obj == null ) {
325- return "" ;
326- }
327- ObjectMeta metadata = null ;
328- if (obj instanceof String ) {
329- return (String ) obj ;
330- } else if (obj instanceof ObjectMeta ) {
331- metadata = (ObjectMeta ) obj ;
332- } else if (obj instanceof HasMetadata ) {
333- metadata = ((HasMetadata ) obj ).getMetadata ();
334- }
335- if (metadata == null ) {
336- throw new RuntimeException ("Object is bad :" + obj );
337- }
338-
339- return namespaceKeyFunc (metadata .getNamespace (), metadata .getName ());
340- }
341-
342- /**
343- * Default index function that indexes based on an object's namespace and name.
344- *
345- * @see #metaNamespaceKeyFunc
346- */
347- public static String namespaceKeyFunc (String objectNamespace , String objectName ) {
348- if (Utils .isNullOrEmpty (objectNamespace )) {
349- return objectName ;
350- }
351- return objectNamespace + "/" + objectName ;
352- }
353-
354- /**
355- * It is a default index function that indexes based on an object's namespace
356- *
357- * @param obj the specific object
358- * @return the indexed value
359- */
360- public static List <String > metaNamespaceIndexFunc (Object obj ) {
361- final ObjectMeta metadata ;
362- if (obj instanceof HasMetadata ) {
363- metadata = ((HasMetadata ) obj ).getMetadata ();
364- } else if (obj instanceof ObjectMeta ) {
365- metadata = (ObjectMeta ) obj ;
366- } else {
367- metadata = null ;
368- }
369- return metadata == null ? Collections .emptyList () : Collections .singletonList (metadata .getNamespace ());
370- }
371-
372314 @ Override
373315 public synchronized void removeIndexer (String name ) {
374316 this .indices .remove (name );
@@ -383,4 +325,23 @@ public Object getLockObject() {
383325 return this ;
384326 }
385327
328+ private static class Index {
329+ private final Map <Object , Set <String >> values = new ConcurrentHashMap <>();
330+
331+ public void update (String indexKey , String key , boolean remove ) {
332+ if (remove ) {
333+ values .computeIfPresent (indexKey == null ? this : indexKey , (k , v ) -> {
334+ v .remove (key );
335+ return v .isEmpty () ? null : v ;
336+ });
337+ } else {
338+ values .computeIfAbsent (indexKey == null ? this : indexKey , k -> ConcurrentHashMap .newKeySet ()).add (key );
339+ }
340+ }
341+
342+ public Set <String > get (String indexKey ) {
343+ return values .getOrDefault (indexKey == null ? this : indexKey , Collections .emptySet ());
344+ }
345+ }
346+
386347}
0 commit comments