Skip to content

Commit a8a14e9

Browse files
rishabhdaimRishabh Kumar
andauthored
OAK-11350 : removed usage of Guava's Maps.filterKeys (#1956)
* OAK-11350 : removed usage of Guava's Maps.filterKeys * OAK-11350 : fixed test failures due to insertion order mismatch --------- Co-authored-by: Rishabh Kumar <diam@adobe.com>
1 parent 5c0cbfc commit a8a14e9

File tree

7 files changed

+106
-11
lines changed

7 files changed

+106
-11
lines changed

oak-blob-cloud/src/main/java/org/apache/jackrabbit/oak/blob/cloud/s3/S3Backend.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@
9898
import org.apache.jackrabbit.guava.common.cache.Cache;
9999
import org.apache.jackrabbit.guava.common.cache.CacheBuilder;
100100
import org.apache.jackrabbit.guava.common.collect.AbstractIterator;
101-
import org.apache.jackrabbit.guava.common.collect.Maps;
102101

103102
import static org.apache.jackrabbit.oak.commons.conditions.Validate.checkArgument;
104103
import static org.apache.jackrabbit.guava.common.collect.Iterables.filter;
@@ -251,7 +250,7 @@ public void init() throws DataStoreException {
251250
LOG.error("Error ", e);
252251
Map<String, Object> filteredMap = new HashMap<>();
253252
if (properties != null) {
254-
filteredMap = Maps.filterKeys(Utils.asMap(properties),
253+
filteredMap = CollectionUtils.filterKeys(Utils.asMap(properties),
255254
input -> !input.equals(S3Constants.ACCESS_KEY) &&!input.equals(S3Constants.SECRET_KEY));
256255
}
257256
throw new DataStoreException("Could not initialize S3 from " + filteredMap, e);

oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.HashSet;
2727
import java.util.IdentityHashMap;
2828
import java.util.Iterator;
29+
import java.util.LinkedHashMap;
2930
import java.util.LinkedHashSet;
3031
import java.util.LinkedList;
3132
import java.util.List;
@@ -416,6 +417,27 @@ public static Map<String, String> fromProperties(final @NotNull Properties prope
416417
e -> String.valueOf(e.getValue())));
417418
}
418419

420+
/**
421+
* Create a new {@link Map} after filtering the entries of the given map
422+
* based on the specified predicate applied to the keys.
423+
*
424+
* @param <K> the type of keys in the map
425+
* @param <V> the type of values in the map
426+
* @param map the map to filter, must not be null
427+
* @param predicate the predicate to apply to the keys, must not be null
428+
* @return a new map containing only the entries whose keys match the predicate
429+
* @throws NullPointerException if the map or predicate is null
430+
*/
431+
@NotNull
432+
public static <K,V> Map<K, V> filterKeys(final @NotNull Map<K, V> map, final @NotNull Predicate<? super K> predicate) {
433+
Objects.requireNonNull(map);
434+
Objects.requireNonNull(predicate);
435+
return map.entrySet()
436+
.stream()
437+
.filter(e -> predicate.test(e.getKey())) // using LinkedHashMap to maintain the order of previous map
438+
.collect(LinkedHashMap::new, (m, v)->m.put(v.getKey(), v.getValue()), LinkedHashMap::putAll);
439+
}
440+
419441
/**
420442
* Convert an {@code Iterator} to an {@code Iterable}.
421443
* <p>

oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.ArrayDeque;
2525
import java.util.Arrays;
2626
import java.util.Collections;
27+
import java.util.HashMap;
2728
import java.util.HashSet;
2829
import java.util.Iterator;
2930
import java.util.LinkedHashSet;
@@ -33,6 +34,7 @@
3334
import java.util.Properties;
3435
import java.util.Set;
3536
import java.util.TreeSet;
37+
import java.util.function.Predicate;
3638
import java.util.stream.Collectors;
3739
import java.util.stream.Stream;
3840

@@ -558,6 +560,80 @@ public void testFromPropertiesNull() {
558560
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.fromProperties(null));
559561
}
560562

563+
@Test
564+
public void testFilterKeys() {
565+
final Map<String, Integer> map = new HashMap<>();
566+
map.put("one", 1);
567+
map.put("two", 2);
568+
map.put("three", 3);
569+
570+
final Predicate<String> predicate = key -> key.startsWith("t");
571+
572+
final Map<String, Integer> result = CollectionUtils.filterKeys(map, predicate);
573+
574+
Assert.assertEquals(2, result.size());
575+
Assert.assertTrue(result.containsKey("two"));
576+
Assert.assertTrue(result.containsKey("three"));
577+
Assert.assertFalse(result.containsKey("one"));
578+
}
579+
580+
@Test
581+
public void testFilterKeysEmptyMap() {
582+
final Map<String, Integer> map = new HashMap<>();
583+
final Predicate<String> predicate = key -> key.startsWith("t");
584+
585+
final Map<String, Integer> result = CollectionUtils.filterKeys(map, predicate);
586+
587+
Assert.assertTrue(result.isEmpty());
588+
}
589+
590+
@Test
591+
public void testFilterNullKeys() {
592+
final Map<String, Integer> map = new HashMap<>();
593+
map.put("one", 1);
594+
map.put("two", 2);
595+
map.put("three", 3);
596+
map.put(null, 4);
597+
598+
final Predicate<String> predicate = Objects::isNull;
599+
600+
final Map<String, Integer> result = CollectionUtils.filterKeys(map, predicate);
601+
602+
Assert.assertEquals(1, result.size());
603+
}
604+
605+
@Test
606+
public void testFilterNonNullKeys() {
607+
final Map<String, Integer> map = new HashMap<>();
608+
map.put("one", 1);
609+
map.put("two", 2);
610+
map.put("three", 3);
611+
map.put(null, 4);
612+
613+
final Predicate<String> predicate = Objects::nonNull;
614+
615+
final Map<String, Integer> result = CollectionUtils.filterKeys(map, predicate);
616+
617+
Assert.assertEquals(3, result.size());
618+
}
619+
620+
@Test
621+
public void testFilterKeysNullMap() {
622+
final Predicate<String> predicate = key -> key.startsWith("t");
623+
624+
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterKeys(null, predicate));
625+
}
626+
627+
@Test
628+
public void testFilterKeysNullPredicate() {
629+
final Map<String, Integer> map = new HashMap<>();
630+
map.put("one", 1);
631+
map.put("two", 2);
632+
map.put("three", 3);
633+
634+
Assert.assertThrows(NullPointerException.class, () -> CollectionUtils.filterKeys(map, null));
635+
}
636+
561637
@Test
562638
public void ensureCapacity() {
563639
int capacity = CollectionUtils.ensureCapacity(8);

oak-lucene/src/main/java/org/apache/jackrabbit/oak/plugins/index/lucene/IndexTracker.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.apache.jackrabbit.guava.common.collect.Maps;
3030
import org.apache.jackrabbit.oak.commons.PathUtils;
3131
import org.apache.jackrabbit.oak.commons.PerfLogger;
32+
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
3233
import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService;
3334
import org.apache.jackrabbit.oak.plugins.index.lucene.hybrid.NRTIndexFactory;
3435
import org.apache.jackrabbit.oak.plugins.index.lucene.reader.DefaultIndexReaderFactory;
@@ -49,8 +50,6 @@
4950
import org.slf4j.LoggerFactory;
5051

5152
import static java.util.Objects.requireNonNull;
52-
53-
5453
import static java.util.Collections.emptyMap;
5554
import static org.apache.jackrabbit.oak.plugins.index.lucene.LuceneIndexConstants.TYPE_LUCENE;
5655
import static org.apache.jackrabbit.oak.plugins.index.lucene.util.LuceneIndexHelper.isLuceneIndexNode;
@@ -186,7 +185,7 @@ public void leave(NodeState before, NodeState after) {
186185

187186
if (!updates.isEmpty()) {
188187
Map<String, LuceneIndexNodeManager> builder = new HashMap<>();
189-
builder.putAll(Maps.filterKeys(original, x -> !updates.keySet().contains(x)));
188+
builder.putAll(CollectionUtils.filterKeys(original, x -> !updates.containsKey(x)));
190189
builder.putAll(Maps.filterValues(updates, x -> x != null));
191190
indices = Collections.unmodifiableMap(builder);
192191

oak-search/src/main/java/org/apache/jackrabbit/oak/plugins/index/search/spi/query/FulltextIndexTracker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.jackrabbit.guava.common.collect.Iterables;
2828
import org.apache.jackrabbit.oak.commons.PathUtils;
2929
import org.apache.jackrabbit.oak.commons.PerfLogger;
30+
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
3031
import org.apache.jackrabbit.oak.plugins.index.AsyncIndexInfoService;
3132
import org.apache.jackrabbit.oak.plugins.index.search.BadIndexTracker;
3233
import org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition;
@@ -45,7 +46,6 @@
4546

4647
import static java.util.Objects.requireNonNull;
4748

48-
import static org.apache.jackrabbit.guava.common.collect.Maps.filterKeys;
4949
import static org.apache.jackrabbit.guava.common.collect.Maps.filterValues;
5050
import static java.util.Collections.emptyMap;
5151
import static org.apache.jackrabbit.oak.plugins.index.search.IndexDefinition.INDEX_DEFINITION_NODE;
@@ -148,7 +148,7 @@ public void leave(NodeState before, NodeState after) {
148148

149149
if (!updates.isEmpty()) {
150150
Map<String, I> builder = new HashMap<>();
151-
builder.putAll(filterKeys(original, x -> !updates.keySet().contains(x)));
151+
builder.putAll(CollectionUtils.filterKeys(original, x -> !updates.containsKey(x)));
152152
builder.putAll(filterValues(updates, x -> x != null));
153153
indices = Collections.unmodifiableMap(builder);
154154

oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/LastRevRecoveryAgent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import static org.apache.jackrabbit.guava.common.collect.Iterables.filter;
2222
import static org.apache.jackrabbit.guava.common.collect.Iterables.transform;
23-
import static org.apache.jackrabbit.guava.common.collect.Maps.filterKeys;
2423
import static java.util.Collections.singletonList;
2524
import static org.apache.jackrabbit.oak.plugins.document.util.Utils.asISO8601;
2625
import static org.apache.jackrabbit.oak.plugins.document.Collection.JOURNAL;
@@ -46,6 +45,7 @@
4645

4746
import org.apache.jackrabbit.guava.common.collect.Iterables;
4847
import org.apache.jackrabbit.oak.commons.TimeDurationFormatter;
48+
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
4949
import org.apache.jackrabbit.oak.commons.properties.SystemPropertySupplier;
5050
import org.apache.jackrabbit.oak.plugins.document.bundlor.DocumentBundlor;
5151
import org.apache.jackrabbit.oak.plugins.document.cache.CacheInvalidationStats;
@@ -726,7 +726,7 @@ private Revision determineLastModification(NodeDocument doc, int clusterId) {
726726
for (String property : doc.keySet().stream().filter(PROPERTY_OR_DELETED).collect(Collectors.toSet())) {
727727
Map<Revision, String> valueMap = doc.getLocalMap(property);
728728
// collect committed changes of this cluster node
729-
for (Map.Entry<Revision, String> entry : filterKeys(valueMap, cp::test).entrySet()) {
729+
for (Map.Entry<Revision, String> entry : CollectionUtils.filterKeys(valueMap, cp).entrySet()) {
730730
Revision rev = entry.getKey();
731731
String cv = revisionContext.getCommitValue(rev, doc);
732732
if (isCommitted(cv)) {

oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/mongo/MongoDocumentStore.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,6 @@
119119
import static java.util.concurrent.TimeUnit.NANOSECONDS;
120120
import static java.util.stream.Collectors.toList;
121121
import static org.apache.jackrabbit.guava.common.collect.Iterables.filter;
122-
import static org.apache.jackrabbit.guava.common.collect.Maps.filterKeys;
123122
import static com.mongodb.client.model.Projections.include;
124123
import static java.lang.Integer.MAX_VALUE;
125124
import static java.util.Collections.emptyList;
@@ -1487,7 +1486,7 @@ private <T extends Document> Map<UpdateOp, T> bulkUpdate(Collection<T> collectio
14871486

14881487
if (collection == Collection.NODES) {
14891488
List<NodeDocument> docsToCache = new ArrayList<>();
1490-
for (UpdateOp op : filterKeys(bulkOperations, x -> bulkResult.upserts.contains(x)).values()) {
1489+
for (UpdateOp op : CollectionUtils.filterKeys(bulkOperations, bulkResult.upserts::contains).values()) {
14911490
NodeDocument doc = Collection.NODES.newDocument(this);
14921491
UpdateUtils.applyChanges(doc, op);
14931492
docsToCache.add(doc);

0 commit comments

Comments
 (0)