Skip to content

Commit 2201eda

Browse files
committed
refactor: minor clean-ups
1 parent ed36981 commit 2201eda

File tree

5 files changed

+85
-116
lines changed

5 files changed

+85
-116
lines changed

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/informers/cache/Cache.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@
3131
public interface Cache<T> extends Indexer<T> {
3232

3333
// NAMESPACE_INDEX is the default index function for caching objects
34-
public static final String NAMESPACE_INDEX = "namespace";
34+
String NAMESPACE_INDEX = "namespace";
3535

3636
/**
37-
* It's is a convenient default KeyFunc which know show to make keys for API
37+
* A convenient default KeyFunc which knows how to make keys for API
3838
* objects which implement HasMetadata interface. The key uses the format
3939
* namespace/name unless namespace is empty, then it's just name
4040
*
4141
* @param obj specific object
4242
* @return the key
4343
*/
44-
public static String metaNamespaceKeyFunc(HasMetadata obj) {
44+
static String metaNamespaceKeyFunc(HasMetadata obj) {
4545
if (obj == null) {
4646
return "";
4747
}
@@ -53,7 +53,7 @@ public static String metaNamespaceKeyFunc(HasMetadata obj) {
5353
return namespaceKeyFunc(metadata.getNamespace(), metadata.getName());
5454
}
5555

56-
public static String metaUidKeyFunc(HasMetadata obj) {
56+
static String metaUidKeyFunc(HasMetadata obj) {
5757
if (obj == null || obj.getMetadata() == null) {
5858
return "";
5959
}
@@ -66,21 +66,22 @@ public static String metaUidKeyFunc(HasMetadata obj) {
6666
*
6767
* @see #metaNamespaceKeyFunc
6868
*/
69-
public static String namespaceKeyFunc(String objectNamespace, String objectName) {
69+
static String namespaceKeyFunc(String objectNamespace, String objectName) {
7070
if (Utils.isNullOrEmpty(objectNamespace)) {
7171
return objectName;
7272
}
7373
return objectNamespace + "/" + objectName;
7474
}
7575

7676
/**
77-
* It is a default index function that indexes based on an object's namespace
77+
* Default index function that indexes based on an object's namespace
7878
*
7979
* @param obj the specific object
8080
* @return the indexed value
8181
*/
82-
public static List<String> metaNamespaceIndexFunc(HasMetadata obj) {
83-
return Optional.ofNullable(obj).map(HasMetadata::getMetadata)
84-
.map(metadata -> Collections.singletonList(metadata.getNamespace())).orElse(Collections.emptyList());
82+
static List<String> metaNamespaceIndexFunc(HasMetadata obj) {
83+
return Optional.ofNullable(obj)
84+
.map(hm -> List.of(hm.getMetadata().getNamespace()))
85+
.orElse(Collections.emptyList());
8586
}
8687
}

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/informers/cache/ItemStore.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@
2525
* all entries until they are deleted. At its simplest this is just a map coupled with a key function.
2626
* <p>
2727
* Modifications to this store once the informer is running, by anything other than the informer will alter the event stream. If
28-
* for example an item is not found, an subsequent update from the api version will send notifications to
28+
* for example an item is not found, any subsequent update from the api version will send notifications to
2929
* {@link ResourceEventHandler}s as an add.
3030
* <p>
31-
* Direct modifications to this store by anything other than the informer will not updated indexes nor emit events.
31+
* Direct modifications to this store by anything other than the informer will not update indexes nor emit events.
3232
* <p>
3333
* The implementation should be safe with respect to concurrency. Modifications from the informer
3434
* will be single threaded, but not necessarily the same thread. Reads may be concurrent with writes.
@@ -60,8 +60,6 @@ public interface ItemStore<V> {
6060
* <br>
6161
* If false, then the initial add events must be processed as they
6262
* occur - meaning that the store state may not be complete.
63-
*
64-
* @return
6563
*/
6664
default boolean isFullState() {
6765
return true;

kubernetes-client-api/src/main/java/io/fabric8/kubernetes/client/informers/cache/ReducedStateItemStore.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,12 @@
3737
public class ReducedStateItemStore<V extends HasMetadata> implements ItemStore<V> {
3838

3939
private static final String METADATA = "metadata";
40+
private static final Object[] NOT_FOUND_KEY_VALUE = new Object[1];
4041
private final ConcurrentHashMap<String, Object[]> store = new ConcurrentHashMap<>();
4142
private final List<String[]> fields = new ArrayList<>();
4243
private final Class<V> typeClass;
4344
private final KeyState keyState;
44-
private KubernetesSerialization serialization;
45+
private final KubernetesSerialization serialization;
4546

4647
public static class KeyState {
4748

@@ -64,17 +65,21 @@ public KeyState(Function<HasMetadata, String> keyFunction, Function<String, Stri
6465

6566
}
6667

68+
private static final String NAMESPACE = "namespace";
69+
private static final String NAME = "name";
6770
public static final KeyState NAME_KEY_STATE = new KeyState(Cache::metaNamespaceKeyFunc,
6871
k -> {
69-
int index = k.indexOf("/");
72+
int index = k.indexOf('/');
7073
if (index == -1) {
7174
return new String[] { null, k };
7275
}
7376
return new String[] { k.substring(0, index), k.substring(index + 1) };
74-
}, new String[] { METADATA, "namespace" }, new String[] { METADATA, "name" });
77+
}, new String[] { METADATA, NAMESPACE}, new String[] { METADATA, NAME });
7578

79+
80+
private static final String UID = "uid";
7681
public static final KeyState UID_KEY_STATE = new KeyState(Cache::metaUidKeyFunc,
77-
k -> new String[] { k }, new String[] { METADATA, "uid" });
82+
k -> new String[] { k }, new String[] { METADATA, UID});
7883

7984
/**
8085
* Create a state store with only the fields specified.
@@ -106,14 +111,15 @@ public ReducedStateItemStore(KeyState keyState, Class<V> typeClass, KubernetesSe
106111
this.keyState = keyState;
107112
fields.add(new String[] { METADATA, "resourceVersion" });
108113
if (valueFields != null) {
109-
for (int i = 0; i < valueFields.length; i++) {
110-
fields.add(valueFields[i].split("\\."));
114+
for (String valueField : valueFields) {
115+
fields.add(valueField.split("\\."));
111116
}
112117
}
113118
this.typeClass = typeClass;
114119
this.serialization = serialization;
115120
}
116121

122+
@SuppressWarnings("unchecked")
117123
Object[] store(V value) {
118124
if (value == null) {
119125
return null;
@@ -134,6 +140,7 @@ V restore(String key, Object[] values) {
134140
return serialization.convertValue(raw, typeClass);
135141
}
136142

143+
@SuppressWarnings("unchecked")
137144
private static void applyFields(Object[] values, Map<String, Object> raw, List<String[]> fields) {
138145
for (int i = 0; i < fields.size(); i++) {
139146
Object value = values[i];
@@ -175,7 +182,7 @@ public V get(String key) {
175182
}
176183

177184
public String getResourceVersion(String key) {
178-
return (String) store.getOrDefault(key, new Object[1])[0];
185+
return (String) store.getOrDefault(key, NOT_FOUND_KEY_VALUE)[0];
179186
}
180187

181188
@Override

kubernetes-client-api/src/test/java/io/fabric8/kubernetes/client/informers/cache/ReducedStateItemStoreTest.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,27 @@ void testStoreRestore() {
3434
Pod pod = new PodBuilder().withNewSpec().endSpec().withNewMetadata().withUid("x").withName("y").addToLabels("one", "1")
3535
.addToLabels("two", "2").withResourceVersion("2").endMetadata().withNewStatus().endStatus().build();
3636

37+
final var uid = pod.getMetadata().getUid();
38+
3739
Object[] values = store.store(pod);
3840

3941
assertEquals(3, values.length);
4042
assertEquals("2", values[0]); // always the resourceVersion
4143
assertEquals(pod.getMetadata().getLabels(), values[1]);
4244
assertNull(values[2]);
4345

44-
Pod restored = store.restore("x", values);
46+
Pod restored = store.restore(uid, values);
4547

4648
assertNull(restored.getSpec());
4749
assertNull(restored.getStatus());
48-
assertEquals("x", restored.getMetadata().getUid());
50+
assertEquals(uid, restored.getMetadata().getUid());
4951
assertEquals(pod.getMetadata().getLabels(), restored.getMetadata().getLabels());
5052

51-
assertNull(store.put("x", pod));
52-
assertNotNull(store.get("x"));
53-
assertEquals("2", store.getResourceVersion("x"));
53+
assertNull(store.put(uid, pod));
54+
assertNotNull(store.get(uid));
55+
assertEquals("2", store.getResourceVersion(uid));
5456
assertEquals(1, store.size());
55-
assertNotNull(store.remove("x"));
57+
assertNotNull(store.remove(uid));
5658
}
5759

5860
}

0 commit comments

Comments
 (0)