Skip to content

Commit

Permalink
OAK-11321 : renoved usage of guava Sets.union
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Kumar committed Dec 21, 2024
1 parent 81abf85 commit 0116e9e
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.oak.api.jmx.CheckpointMBean;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.apache.jackrabbit.oak.spi.whiteboard.WhiteboardUtils;
import org.apache.jackrabbit.oak.stats.Clock;
Expand Down Expand Up @@ -59,7 +60,7 @@ public void gcCheckpointHeld() throws Exception {

checkpointMBean.createCheckpoint(100);
Set<String> afterCheckpointBlobs = createBlobs(cluster.blobStore, 2, 100);
Set<String> present = Sets.union(cluster.blobStoreState.blobsPresent, afterCheckpointBlobs);
Set<String> present = CollectionUtils.union(cluster.blobStoreState.blobsPresent, afterCheckpointBlobs);
long maxGcAge = checkpointMBean.getOldestCheckpointCreationTimestamp() - afterSetupTime;

log.info("{} blobs remaining : {}", present.size(), present);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@

import ch.qos.logback.classic.Level;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.jackrabbit.guava.common.collect.Lists;
import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.guava.common.io.Closer;
import org.apache.jackrabbit.oak.api.Blob;
Expand All @@ -61,6 +60,7 @@
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.api.blob.BlobAccessProvider;
import org.apache.jackrabbit.oak.api.blob.BlobUpload;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.concurrent.ExecutorCloser;
import org.apache.jackrabbit.oak.commons.junit.LogCustomizer;
import org.apache.jackrabbit.oak.plugins.blob.MarkSweepGarbageCollector.NotAllRepositoryMarkedException;
Expand Down Expand Up @@ -214,10 +214,8 @@ public void sharedGC() throws Exception {
Cluster secondCluster = new Cluster(folder.newFolder(), cluster.blobStore, secondClusterNodeStore, 100);
closer.register(secondCluster);

Sets.SetView<String> totalPresent =
Sets.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);
Sets.SetView<String> totalAdded =
Sets.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);
Set<String> totalPresent = CollectionUtils.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);
Set<String> totalAdded = CollectionUtils.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);

// Execute mark on the default cluster
executeGarbageCollection(cluster, cluster.getCollector(0), true);
Expand All @@ -238,8 +236,7 @@ public void noSharedGC() throws Exception {
Cluster secondCluster = new Cluster(folder.newFolder(), cluster.blobStore, secondClusterNodeStore, 100);
closer.register(secondCluster);

Sets.SetView<String> totalAdded =
Sets.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);
Set<String> totalAdded = CollectionUtils.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);

Set<String> existingAfterGC = executeGarbageCollection(secondCluster, secondCluster.getCollector(0), false);

Expand All @@ -260,8 +257,7 @@ public void sharedGCRepositoryCloned() throws Exception {
((SharedDataStore) secondCluster.blobStore).deleteMetadataRecord(REPOSITORY.getNameFromId(secondCluster.repoId));
secondCluster.setRepoId(cluster.repoId);

Sets.SetView<String> totalPresent =
Sets.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);
Set<String> totalPresent = CollectionUtils.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);

// Execute mark on the default cluster
executeGarbageCollection(cluster, cluster.getCollector(0), true);
Expand All @@ -279,10 +275,8 @@ public void sharedGCRefsOld() throws Exception {
Cluster secondCluster = new Cluster(folder.newFolder(), cluster.blobStore, secondClusterNodeStore, 100);
closer.register(secondCluster);

Sets.SetView<String> totalPresent =
Sets.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);
Sets.SetView<String> totalAdded =
Sets.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);
Set<String> totalPresent = CollectionUtils.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);
Set<String> totalAdded = CollectionUtils.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);

clock.waitUntil(clock.getTime() + 5);

Expand All @@ -309,10 +303,8 @@ public void sharedGCRefsNotOld() throws Exception {
Cluster secondCluster = new Cluster(folder.newFolder(), cluster.blobStore, secondClusterNodeStore, 100);
closer.register(secondCluster);

Sets.SetView<String> totalPresent =
Sets.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);
Sets.SetView<String> totalAdded =
Sets.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);
Set<String> totalPresent = CollectionUtils.union(cluster.blobStoreState.blobsPresent, secondCluster.blobStoreState.blobsPresent);
Set<String> totalAdded = CollectionUtils.union(cluster.blobStoreState.blobsAdded, secondCluster.blobStoreState.blobsAdded);

// Execute mark on the default cluster
executeGarbageCollection(cluster, cluster.getCollector(5), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,23 @@ public static <T> Set<T> toLinkedSet(@NotNull final T... elements) {
return result;
}

/**
* Returns a new set containing the union of the two specified sets.
* The union of two sets is a set containing all the elements of both sets.
*
* @param <T> 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 union of the two specified sets
* @throws NullPointerException if either of the sets is null
*/
@NotNull
public static <T> Set<T> union(@NotNull final Set<T> s1, @NotNull final Set<T> s2) {
Objects.requireNonNull(s1);
Objects.requireNonNull(s2);
return Stream.concat(s1.stream(), s2.stream()).collect(Collectors.toSet());
}

/**
* Convert an iterable to a {@link java.util.ArrayDeque}.
* The returning array deque is mutable and supports all optional operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
* Utilities for Java collections and streams.
*/
@Internal(since = "1.0.0")
@Version("1.3.0")
@Version("1.4.0")
package org.apache.jackrabbit.oak.commons.collections;
import org.apache.jackrabbit.oak.commons.annotations.Internal;
import org.osgi.annotation.versioning.Version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.jackrabbit.oak.commons;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.apache.jackrabbit.guava.common.collect.Sets.union;
import static org.apache.jackrabbit.oak.commons.FileIOUtils.append;
import static org.apache.jackrabbit.oak.commons.FileIOUtils.copy;
import static org.apache.jackrabbit.oak.commons.FileIOUtils.lexComparator;
Expand Down Expand Up @@ -61,7 +60,6 @@

import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.guava.common.base.Splitter;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.sort.EscapeUtils;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -238,7 +236,7 @@ public void appendTest() throws IOException {
File f3 = assertWrite(added3.iterator(), false, added3.size());

append(List.of(f2, f3), f1, true);
assertEquals(union(union(added1, added2), added3),
assertEquals(CollectionUtils.union(CollectionUtils.union(added1, added2), added3),
readStringsAsSet(new FileInputStream(f1), false));
assertTrue(!f2.exists());
assertTrue(!f3.exists());
Expand All @@ -258,7 +256,7 @@ public void appendTestNoDelete() throws IOException {

append(List.of(f2, f3), f1, false);

assertEquals(union(union(added1, added2), added3),
assertEquals(CollectionUtils.union(CollectionUtils.union(added1, added2), added3),
readStringsAsSet(new FileInputStream(f1), false));
assertTrue(f2.exists());
assertTrue(f3.exists());
Expand Down Expand Up @@ -294,7 +292,7 @@ public void appendRandomizedTest() throws Exception {

append(List.of(f2), f1, true);

assertEquals(union(added1, added2),
assertEquals(CollectionUtils.union(added1, added2),
readStringsAsSet(new FileInputStream(f1), true));
}

Expand All @@ -308,7 +306,7 @@ public void appendWithLineBreaksTest() throws IOException {

append(List.of(f1), f2, true);

assertEquals(union(added1, added2), readStringsAsSet(new FileInputStream(f2), true));
assertEquals(CollectionUtils.union(added1, added2), readStringsAsSet(new FileInputStream(f2), true));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,57 @@ public void nullArrayToLinkedSet() {
CollectionUtils.toLinkedSet((String[])null);
}

@Test
public void testUnionWithNonEmptySets() {
final Set<String> set1 = Set.of("a", "b", "c");
final Set<String> set2 = Set.of("d", "e", "f");

final Set<String> expected = Set.of("a", "b", "c", "d", "e", "f");
Assert.assertEquals(expected, CollectionUtils.union(set1, set2));
}

@Test
public void testUnionWithEmptySet() {
final Set<String> set1 = Set.of("a", "b", "c");
final Set<String> set2 = new HashSet<>();

final Set<String> expected = Set.of("a", "b", "c");
Assert.assertEquals(expected, CollectionUtils.union(set1, set2));
}

@Test
public void testUnionWithBothEmptySets() {
final Set<String> set1 = new HashSet<>();
final Set<String> set2 = new HashSet<>();

Assert.assertEquals(new HashSet<>(), CollectionUtils.union(set1, set2));
}

@Test(expected = NullPointerException.class)
public void testUnionWithNullFirstSet() {
Set<String> set1 = null;
Set<String> set2 = Set.of("a", "b", "c");

CollectionUtils.union(set1, set2);
}

@Test(expected = NullPointerException.class)
public void testUnionWithNullSecondSet() {
Set<String> set1 = Set.of("a", "b", "c");
Set<String> set2 = null;

CollectionUtils.union(set1, set2);
}

@Test
public void testUnionWithOverlappingSets() {
final Set<String> set1 = Set.of("a", "b", "c");
final Set<String> set2 = Set.of("b", "c", "d");

final Set<String> expected = Set.of("a", "b", "c", "d");
Assert.assertEquals(expected, CollectionUtils.union(set1, set2));
}

@Test
public void iteratorToIIteratable() {
Iterator<String> iterator = List.of("a", "b", "c").iterator();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.StreamSupport;
import java.util.stream.Stream;

import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.json.JsopDiff;
Expand Down Expand Up @@ -135,8 +134,8 @@ private void resolveConflict(ConflictType conflictType, NodeState conflictInfo)
NodeState oursNS = conflictInfo.getChildNode(OURS);
NodeState baseNS = conflictInfo.getChildNode(BASE);

Set<String> candidates = Sets.union(CollectionUtils.toSet(oursNS.getChildNodeNames()),
CollectionUtils.toSet(baseNS.getChildNodeNames()));
Set<String> candidates = Stream.concat(CollectionUtils.toStream(oursNS.getChildNodeNames()),
CollectionUtils.toStream(baseNS.getChildNodeNames())).collect(toSet());
for (String name : candidates) {
NodeState ours = oursNS.getChildNode(name);
NodeState base = baseNS.getChildNode(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static org.apache.jackrabbit.guava.common.collect.Iterables.addAll;
import static org.apache.jackrabbit.guava.common.collect.Iterables.contains;
import static org.apache.jackrabbit.guava.common.collect.Iterables.isEmpty;
import static org.apache.jackrabbit.guava.common.collect.Sets.union;
import static java.util.Collections.emptyList;
import static org.apache.jackrabbit.JcrConstants.JCR_CHILDNODEDEFINITION;
import static org.apache.jackrabbit.JcrConstants.JCR_ISMIXIN;
Expand Down Expand Up @@ -124,7 +123,7 @@ boolean isModified() {
*/
Set<String> getModifiedTypes(NodeState beforeTypes) {
Set<String> types = new HashSet<>();
for (String name : union(changedTypes, removedTypes)) {
for (String name : CollectionUtils.union(changedTypes, removedTypes)) {
types.add(name);
NodeState type = beforeTypes.getChildNode(name);
addAll(types, type.getNames(REP_PRIMARY_SUBTYPES));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
*/
package org.apache.jackrabbit.oak.query.ast;

import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.oak.api.PropertyValue;
import org.apache.jackrabbit.oak.api.Type;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.query.index.FilterImpl;
import org.apache.jackrabbit.oak.spi.query.QueryConstants;
import org.apache.jackrabbit.oak.spi.query.QueryIndex.OrderEntry;
Expand Down Expand Up @@ -68,7 +68,7 @@ public PropertyExistenceImpl getPropertyExistence() {

@Override
public Set<SelectorImpl> getSelectors() {
return Sets.union(
return CollectionUtils.union(
operand1.getSelectors(),
operand2.getSelectors()
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public class CompositeProviderCustomMixTest extends AbstractSecurityTest {
public void hasPrivilegesTest() throws Exception {
Set<String> supp1 = Set.of(JCR_READ, JCR_NAMESPACE_MANAGEMENT);
Set<String> supp2 = Set.of(JCR_READ, JCR_WRITE);
Set<String> all = Sets.union(supp1, supp2);
Set<String> all = CollectionUtils.union(supp1, supp2);

// tests all possible 256 shuffles
for (CompositionType type : CompositionType.values()) {
Expand All @@ -84,7 +84,7 @@ public void hasPrivilegesTest() throws Exception {
public void isGrantedTest() throws Exception {
Set<String> supp1 = Set.of(JCR_READ, JCR_NODE_TYPE_MANAGEMENT);
Set<String> supp2 = Set.of(JCR_READ, JCR_WRITE);
Set<String> all = Sets.union(supp1, supp2);
Set<String> all = CollectionUtils.union(supp1, supp2);

Map<String, Long> grantMap = new HashMap<>();
grantMap.put(JCR_READ, Permissions.READ);
Expand Down Expand Up @@ -131,7 +131,7 @@ public void isGrantedTest() throws Exception {
public void getRepositoryPermissionTest() throws Exception {
Set<String> supp1 = Set.of(JCR_READ, JCR_NODE_TYPE_MANAGEMENT);
Set<String> supp2 = Set.of(JCR_READ, JCR_WRITE);
Set<String> all = Sets.union(supp1, supp2);
Set<String> all = CollectionUtils.union(supp1, supp2);

Map<String, Long> grantMap = new HashMap<>();
grantMap.put(JCR_READ, Permissions.READ);
Expand Down Expand Up @@ -161,7 +161,7 @@ public void getRepositoryPermissionTest() throws Exception {
public void getTreePermissionTest() throws Exception {
Set<String> supp1 = Set.of(JCR_READ, JCR_NODE_TYPE_MANAGEMENT);
Set<String> supp2 = Set.of(JCR_READ, JCR_WRITE);
Set<String> all = Sets.union(supp1, supp2);
Set<String> all = CollectionUtils.union(supp1, supp2);

Map<String, Long> grantMap = new HashMap<>();
grantMap.put(JCR_READ, Permissions.READ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.concurrent.ScheduledFuture;

import ch.qos.logback.classic.Level;
import org.apache.jackrabbit.guava.common.collect.Lists;
import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.oak.api.Blob;
Expand Down Expand Up @@ -62,7 +61,6 @@
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import static org.apache.jackrabbit.guava.common.collect.Sets.union;
import static java.lang.String.valueOf;
import static java.util.concurrent.Executors.newSingleThreadExecutor;
import static java.util.concurrent.Executors.newSingleThreadScheduledExecutor;
Expand Down Expand Up @@ -470,12 +468,9 @@ private void clusterGCInternal(Cluster cluster1, Cluster cluster2, boolean same)
Set<String> existingAfterGC = iterate(s1);

// Check the state of the blob store after gc
assertEquals(
union(state1.blobsPresent, state2.blobsPresent), existingAfterGC);
assertEquals(CollectionUtils.union(state1.blobsPresent, state2.blobsPresent), existingAfterGC);
// Tracked blobs should reflect deletions after gc
assertEquals(
union(state1.blobsPresent, state2.blobsPresent),
retrieveTracked(tracker1));
assertEquals(CollectionUtils.union(state1.blobsPresent, state2.blobsPresent), retrieveTracked(tracker1));

// Again create snapshots at both cluster nodes to synchronize the latest state of
// local references with datastore at each node
Expand Down Expand Up @@ -506,8 +501,7 @@ private void clusterGCInternal(Cluster cluster1, Cluster cluster2, boolean same)

customLogs.finished();
// Check the state of the blob store after gc
assertEquals(
union(state1.blobsPresent, state2.blobsPresent), existingAfterGC);
assertEquals(CollectionUtils.union(state1.blobsPresent, state2.blobsPresent), existingAfterGC);
}

/**
Expand Down
Loading

0 comments on commit 0116e9e

Please sign in to comment.