From 73cafe3540d0c6e2ede9bbfe3a7a14e285b5170b Mon Sep 17 00:00:00 2001 From: Rishabh Kumar Date: Sun, 22 Dec 2024 15:01:18 +0530 Subject: [PATCH] OAK-11322 : removed usage of guava Sets.intersection --- .../cug/impl/CugAccessControlManagerTest.java | 3 +- .../commons/collections/CollectionUtils.java | 17 +++++++ .../collections/CollectionUtilsTest.java | 51 +++++++++++++++++++ .../user/RepMembersConflictHandler.java | 2 +- .../CompositeProviderCustomMixTest.java | 4 +- .../oak/explorer/NodeStoreTree.java | 3 +- .../oak/run/DataStoreCommandTest.java | 4 +- .../segment/SegmentBufferWriterPoolTest.java | 6 +-- .../document/DocumentNodeStoreBranch.java | 5 +- .../document/rdb/RDBDocumentStore.java | 2 +- 10 files changed, 80 insertions(+), 17 deletions(-) diff --git a/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java b/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java index cdc445031c7..628d0a56a9f 100644 --- a/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java +++ b/oak-authorization-cug/src/test/java/org/apache/jackrabbit/oak/spi/security/authorization/cug/impl/CugAccessControlManagerTest.java @@ -32,7 +32,6 @@ import org.apache.jackrabbit.guava.common.collect.ImmutableList; import org.apache.jackrabbit.guava.common.collect.Iterators; -import org.apache.jackrabbit.guava.common.collect.Sets; import org.apache.jackrabbit.JcrConstants; import org.apache.jackrabbit.api.security.JackrabbitAccessControlPolicy; import org.apache.jackrabbit.oak.api.ContentSession; @@ -585,7 +584,7 @@ public void testGetEffectivePrincipalDistributed() throws Exception { AccessControlPolicy[] testgroupEffective = cugAccessControlManager.getEffectivePolicies(Set.of(getTestGroupPrincipal())); assertEquals(2, testgroupEffective.length); - assertTrue(Sets.intersection(Set.of(everyoneEffective), Set.of(testgroupEffective)).isEmpty()); + assertTrue(CollectionUtils.intersection(Set.of(everyoneEffective), Set.of(testgroupEffective)).isEmpty()); } @Test diff --git a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java index 323ee6be8d6..82bcbc201da 100644 --- a/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java +++ b/oak-commons/src/main/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtils.java @@ -223,6 +223,23 @@ public static Set union(@NotNull final Set s1, @NotNull final Set s return Stream.concat(s1.stream(), s2.stream()).collect(Collectors.toSet()); } + /** + * Returns a new set containing the intersection of the two specified sets. + * The intersection of two sets is a set containing only the elements that are present in both sets. + * + * @param the type of elements in the sets + * @param s1 the first set, must not be null + * @param s2 the second set, must not be null + * @return a new set containing the intersection of the two specified sets + * @throws NullPointerException if either of the sets is null + */ + @NotNull + public static Set intersection(@NotNull final Set s1, @NotNull final Set s2) { + Objects.requireNonNull(s1); + Objects.requireNonNull(s2); + return s1.stream().filter(s2::contains).collect(Collectors.toSet()); + } + /** * Convert an iterable to a {@link java.util.ArrayDeque}. * The returning array deque is mutable and supports all optional operations. diff --git a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java index 9508e4e4321..5ae5266b408 100644 --- a/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java +++ b/oak-commons/src/test/java/org/apache/jackrabbit/oak/commons/collections/CollectionUtilsTest.java @@ -257,6 +257,57 @@ public void testUnionWithOverlappingSets() { Assert.assertEquals(expected, CollectionUtils.union(set1, set2)); } + @Test + public void testIntersectionWithNonEmptySets() { + final Set set1 = Set.of("a", "b", "c"); + final Set set2 = Set.of("b", "c", "d"); + + final Set result = CollectionUtils.intersection(set1, set2); + + final Set expected = Set.of("b", "c"); + Assert.assertEquals(expected, result); + } + + @Test + public void testIntersectionWithEmptySet() { + final Set set1 = Set.of("a", "b", "c"); + final Set set2 = Set.of(); + + Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2)); + } + + @Test + public void testIntersectionWithBothEmptySets() { + final Set set1 = new HashSet<>(); + final Set set2 = new HashSet<>(); + + Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2)); + } + + @Test(expected = NullPointerException.class) + public void testIntersectionWithNullFirstSet() { + final Set set1 = null; + final Set set2 = Set.of("a", "b", "c"); + + CollectionUtils.intersection(set1, set2); + } + + @Test(expected = NullPointerException.class) + public void testIntersectionWithNullSecondSet() { + final Set set1 = Set.of("a", "b", "c"); + final Set set2 = null; + + CollectionUtils.intersection(set1, set2); + } + + @Test + public void testIntersectionWithNoCommonElements() { + final Set set1 = Set.of("a", "b", "c"); + final Set set2 = Set.of("d", "e", "f"); + + Assert.assertEquals(Collections.EMPTY_SET, CollectionUtils.intersection(set1, set2)); + } + @Test public void iteratorToIIteratable() { Iterator iterator = List.of("a", "b", "c").iterator(); diff --git a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java index 97fa82bb058..fd3bb2dd87d 100644 --- a/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java +++ b/oak-core/src/main/java/org/apache/jackrabbit/oak/security/user/RepMembersConflictHandler.java @@ -161,7 +161,7 @@ private static void mergeChange(@NotNull NodeBuilder parent, @NotNull PropertySt Set ourMembers = CollectionUtils.toSet(ours.getValue(Type.STRINGS)); // merge ours and theirs to a de-duplicated set - Set combined = new LinkedHashSet<>(Sets.intersection(ourMembers, theirMembers)); + Set combined = new LinkedHashSet<>(CollectionUtils.intersection(ourMembers, theirMembers)); for (String m : Sets.difference(ourMembers, theirMembers)) { if (!base.contains(m)) { combined.add(m); diff --git a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java index 6407a516777..cbd93a9d2ff 100644 --- a/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java +++ b/oak-core/src/test/java/org/apache/jackrabbit/oak/security/authorization/composite/CompositeProviderCustomMixTest.java @@ -218,9 +218,9 @@ private boolean expected(Set check, Set supported1, Set if (type == CompositionType.OR) { return Sets.difference(Sets.difference(check, granted1), granted2).isEmpty(); } else { - Set f1 = Sets.intersection(supported1, check); + Set f1 = CollectionUtils.intersection(supported1, check); boolean hasf1 = granted1.containsAll(f1); - Set f2 = Sets.intersection(supported2, check); + Set f2 = CollectionUtils.intersection(supported2, check); boolean hasf2 = granted2.containsAll(f2); return hasf1 && hasf2; } diff --git a/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java b/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java index 6ee544075c0..9ed539fbe2b 100644 --- a/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java +++ b/oak-run/src/main/java/org/apache/jackrabbit/oak/explorer/NodeStoreTree.java @@ -18,7 +18,6 @@ */ package org.apache.jackrabbit.oak.explorer; -import static org.apache.jackrabbit.guava.common.collect.Sets.intersection; import static org.apache.jackrabbit.guava.common.escape.Escapers.builder; import static java.util.Collections.sort; import static javax.jcr.PropertyType.BINARY; @@ -413,7 +412,7 @@ void printTarInfo(String file) { } } - Set inMem = intersection(backend.getReferencedSegmentIds(), uuids); + Set inMem = CollectionUtils.intersection(backend.getReferencedSegmentIds(), uuids); if (!inMem.isEmpty()) { sb.append("In Memory segment references: "); sb.append(newline); diff --git a/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java b/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java index f0cc1d8d1fd..0ac5bdd757b 100644 --- a/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java +++ b/oak-run/src/test/java/org/apache/jackrabbit/oak/run/DataStoreCommandTest.java @@ -44,8 +44,6 @@ import org.apache.jackrabbit.guava.common.base.Strings; import org.apache.jackrabbit.guava.common.collect.ImmutableList; import org.apache.jackrabbit.guava.common.collect.Iterators; -import org.apache.jackrabbit.guava.common.collect.Lists; -import org.apache.jackrabbit.guava.common.collect.Maps; import org.apache.jackrabbit.guava.common.collect.Sets; import joptsimple.OptionException; import org.apache.commons.io.FileUtils; @@ -753,7 +751,7 @@ private void testConsistency(File dump, Data data, boolean verbose, boolean verb if (!markOnly) { // Verbose would have paths as well as ids changed but normally only DocumentNS would have paths suffixed assertFileEquals(dump, "gccand-", verbose ? - encodedIdsAndPath(verboseRootPath ? Sets.intersection(data.addedSubset, data.missingDataStore) : + encodedIdsAndPath(verboseRootPath ? CollectionUtils.intersection(data.addedSubset, data.missingDataStore) : data.missingDataStore, blobFixture.getType(), data.idToPath, true) : (storeFixture instanceof StoreFixture.MongoStoreFixture) ? encodedIdsAndPath(data.missingDataStore, blobFixture.getType(), data.idToPath, false) : diff --git a/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java index a894cbf9669..7bc95cdac7b 100644 --- a/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java +++ b/oak-segment-tar/src/test/java/org/apache/jackrabbit/oak/segment/SegmentBufferWriterPoolTest.java @@ -19,7 +19,6 @@ package org.apache.jackrabbit.oak.segment; -import static org.apache.jackrabbit.guava.common.collect.Sets.intersection; import static org.apache.jackrabbit.guava.common.util.concurrent.Uninterruptibles.sleepUninterruptibly; import static java.util.concurrent.Executors.newSingleThreadExecutor; import static java.util.concurrent.TimeUnit.MILLISECONDS; @@ -38,6 +37,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeoutException; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.apache.jackrabbit.oak.segment.WriteOperationHandler.WriteOperation; import org.apache.jackrabbit.oak.segment.file.tar.GCGeneration; import org.apache.jackrabbit.oak.segment.memory.MemoryStore; @@ -159,7 +159,7 @@ public void testFlush() throws ExecutionException, InterruptedException, IOExcep assertEquals(rootId, res5.get()); assertEquals(rootId, res6.get()); assertEquals(3, map2.size()); - assertTrue(intersection(new HashSet<>(map1.values()), new HashSet<>(map2.values())).isEmpty()); + assertTrue(CollectionUtils.intersection(new HashSet<>(map1.values()), new HashSet<>(map2.values())).isEmpty()); } @Test @@ -209,7 +209,7 @@ public void testCompaction() throws ExecutionException, InterruptedException, IO assertEquals(rootId, res8.get()); assertEquals(rootId, res9.get()); assertEquals(3, map3.size()); - assertTrue(intersection(new HashSet<>(map1.values()), new HashSet<>(map3.values())).isEmpty()); + assertTrue(CollectionUtils.intersection(new HashSet<>(map1.values()), new HashSet<>(map3.values())).isEmpty()); } @Test diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java index ce43bb6f1e3..0afb88244b0 100644 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/DocumentNodeStoreBranch.java @@ -32,9 +32,8 @@ import java.util.concurrent.locks.ReadWriteLock; import org.apache.jackrabbit.guava.common.collect.Iterables; -import org.apache.jackrabbit.guava.common.collect.Sets; - import org.apache.jackrabbit.oak.api.CommitFailedException; +import org.apache.jackrabbit.oak.commons.collections.CollectionUtils; import org.apache.jackrabbit.oak.plugins.document.util.Utils; import org.apache.jackrabbit.oak.plugins.memory.MemoryNodeBuilder; import org.apache.jackrabbit.oak.spi.commit.CommitHook; @@ -752,7 +751,7 @@ private void checkForConflicts() throws CommitFailedException { Set collisions = new HashSet<>(doc.getLocalMap(COLLISIONS).keySet()); Set commits = new HashSet<>(); Iterables.transform(b.getCommits(), Revision::asTrunkRevision).forEach(commits::add); - Set conflicts = Sets.intersection(collisions, commits); + Set conflicts = CollectionUtils.intersection(collisions, commits); if (!conflicts.isEmpty()) { throw new CommitFailedException(STATE, 2, "Conflicting concurrent change on branch commits " + conflicts); diff --git a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java index 186295ad51b..d08345d5562 100755 --- a/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java +++ b/oak-store-document/src/main/java/org/apache/jackrabbit/oak/plugins/document/rdb/RDBDocumentStore.java @@ -1831,7 +1831,7 @@ protected Iterable queryAsIterable(final Collection c final List excludeKeyPatterns, final List conditions, final int limit, final String sortBy) { final RDBTableMetaData tmd = getTable(collection); - Set allowedProps = Sets.intersection(INDEXEDPROPERTIES, tmd.getColumnProperties()); + Set allowedProps = CollectionUtils.intersection(INDEXEDPROPERTIES, tmd.getColumnProperties()); for (QueryCondition cond : conditions) { if (!allowedProps.contains(cond.getPropertyName())) { String message = "indexed property " + cond.getPropertyName() + " not supported, query was '" + cond