Skip to content

Commit

Permalink
fix fabric8io#3272: addressing npe and index logic in general
Browse files Browse the repository at this point in the history
  • Loading branch information
shawkins committed Jun 25, 2021
1 parent db67557 commit 1e9b2c8
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ public void setIsRunning(BooleanSupplier isRunning) {
* @return registered indexers
*/
@Override
public Map<String, Function<T, List<String>>> getIndexers() {
return indexers;
public synchronized Map<String, Function<T, List<String>>> getIndexers() {
return Collections.unmodifiableMap(indexers);
}

@Override
public void addIndexers(Map<String, Function<T, List<String>>> indexersNew) {
public synchronized void addIndexers(Map<String, Function<T, List<String>>> indexersNew) {
if (isRunning.getAsBoolean()) {
throw new IllegalStateException("Cannot add indexers to a running informer.");
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public synchronized Map<String, T> replace(List<T> list) {
this.items = newItems;

// rebuild any index
this.indices = new HashMap<>();
this.indices.values().stream().forEach(m -> m.clear());
for (Map.Entry<String, T> itemEntry : items.entrySet()) {
this.updateIndices(null, itemEntry.getValue(), itemEntry.getKey());
}
Expand Down Expand Up @@ -294,7 +294,7 @@ public synchronized List<T> byIndex(String indexName, String indexKey) {
* @param newObj new object
* @param key the key
*/
public void updateIndices(T oldObj, T newObj, String key) {
void updateIndices(T oldObj, T newObj, String key) {
if (oldObj != null) {
deleteFromIndices(oldObj, key);
}
Expand All @@ -307,7 +307,7 @@ public void updateIndices(T oldObj, T newObj, String key) {
continue;
}

Map<String, Set<String>> index = this.indices.computeIfAbsent(indexName, k -> new HashMap<>());
Map<String, Set<String>> index = this.indices.get(indexName);
for (String indexValue : indexValues) {
Set<String> indexSet = index.computeIfAbsent(indexValue, k -> new HashSet<>());
indexSet.add(key);
Expand Down Expand Up @@ -350,7 +350,7 @@ private void deleteFromIndices(T oldObj, String key) {
* @param indexName the index name
* @param indexFunc the index func
*/
public void addIndexFunc(String indexName, Function<T, List<String>> indexFunc) {
public synchronized void addIndexFunc(String indexName, Function<T, List<String>> indexFunc) {
this.indices.put(indexName, new HashMap<>());
this.indexers.put(indexName, indexFunc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;

class CacheTest {
private static Cache cache = new Cache("mock", CacheTest::mockIndexFunction, CacheTest::mockKeyFunction);
Expand All @@ -49,6 +51,9 @@ void testCacheIndex() {
List<String> allExistingKeys = cache.listKeys();
assertEquals(1, allExistingKeys.size());
assertEquals(key, allExistingKeys.get(0));

cache.replace(Collections.emptyList());
assertNull(cache.byIndex("mock", "y"));
}

@Test
Expand Down

0 comments on commit 1e9b2c8

Please sign in to comment.