Skip to content

Commit

Permalink
OAK-11323 : removed usage of Guava's Sets.difference
Browse files Browse the repository at this point in the history
  • Loading branch information
Rishabh Kumar committed Dec 30, 2024
1 parent 2d0834d commit ca264ac
Show file tree
Hide file tree
Showing 18 changed files with 138 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
import org.apache.jackrabbit.guava.common.base.Splitter;
import org.apache.jackrabbit.guava.common.collect.FluentIterable;
import org.apache.jackrabbit.guava.common.collect.Ordering;
import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.core.data.DataRecord;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.plugins.blob.SharedDataStore;
import org.apache.jackrabbit.oak.spi.blob.BlobStore;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -72,7 +72,7 @@ public Long apply(@NotNull DataRecord input) {
*/
public static Set<String> refsNotAvailableFromRepos(List<DataRecord> repos,
List<DataRecord> refs) {
return Sets.difference(FluentIterable.from(repos)
return CollectionUtils.difference(FluentIterable.from(repos)
.uniqueIndex(input -> SharedStoreRecordType.REPOSITORY.getIdFromName(input.getIdentifier().toString())).keySet(),
FluentIterable.from(refs)
.index(input -> SharedStoreRecordType.REFERENCES.getIdFromName(input.getIdentifier().toString())).keySet());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,23 @@ public static <T> Set<T> symmetricDifference(final Set<T> s1, final Set<T> s2) {
return result;
}

/**
* Returns a new set containing the difference of the two specified sets.
* The difference of two sets is a set containing elements that are in the first set
* but not in the second set.
*
* @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 difference of the two specified sets
* @throws NullPointerException if either of the sets is null
*/
public static <T> Set<T> difference(final Set<T> s1, final Set<T> s2) {
Objects.requireNonNull(s1);
Objects.requireNonNull(s2);
return s1.stream().filter(e -> !s2.contains(e)).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 @@ -370,6 +370,79 @@ public void testSymmetricDifferenceWithNoCommonElements() {
Assert.assertEquals(expected, result);
}

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

final Set<String> result = CollectionUtils.difference(set1, set2);

final Set<String> expected = Set.of("a");
Assert.assertEquals(expected, result);
}

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

final Set<String> result = CollectionUtils.difference(set1, set2);

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

@Test
public void testDifferenceWithBothEmptySets() {
final Set<String> set1 = Set.of();
final Set<String> set2 = Set.of();

final Set<String> result = CollectionUtils.difference(set1, set2);

final Set<String> expected = Set.of();
Assert.assertEquals(expected, result);
}

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

CollectionUtils.difference(set1, set2);
fail("Shouldn't reach here");
}

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

CollectionUtils.difference(set1, set2);
fail("Shouldn't reach here");
}

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

final Set<String> result = CollectionUtils.difference(set1, set2);

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

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

final Set<String> result = CollectionUtils.difference(set1, set2);

final Set<String> expected = Set.of();
Assert.assertEquals(expected, result);
}

@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 @@ -39,10 +39,9 @@
import java.util.Set;
import java.util.function.Supplier;

import org.apache.jackrabbit.guava.common.collect.Sets;

import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.api.PropertyState;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.plugins.index.IndexEditor;
import org.apache.jackrabbit.oak.plugins.index.property.Multiplexers;
import org.apache.jackrabbit.oak.plugins.index.property.strategy.IndexStoreStrategy;
Expand Down Expand Up @@ -187,7 +186,7 @@ public void leave(NodeState before, NodeState after)
}

checkReferentialIntegrity(refStores, root, definition.getNodeState(),
Sets.difference(rmIds, newIds));
CollectionUtils.difference(rmIds, newIds));

// update weak references
for (Entry<String, Set<String>> ref : rmWeakRefs.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.jetbrains.annotations.NotNull;

import org.apache.jackrabbit.guava.common.collect.Sets;

/**
* The {@code RepMembersConflictHandler} takes care of merging the {@code rep:members} property
* during parallel updates.
Expand Down Expand Up @@ -162,12 +160,12 @@ private static void mergeChange(@NotNull NodeBuilder parent, @NotNull PropertySt

// merge ours and theirs to a de-duplicated set
Set<String> combined = new LinkedHashSet<>(CollectionUtils.intersection(ourMembers, theirMembers));
for (String m : Sets.difference(ourMembers, theirMembers)) {
for (String m : CollectionUtils.difference(ourMembers, theirMembers)) {
if (!base.contains(m)) {
combined.add(m);
}
}
for (String m : Sets.difference(theirMembers, ourMembers)) {
for (String m : CollectionUtils.difference(theirMembers, ourMembers)) {
if (!base.contains(m)) {
combined.add(m);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private boolean expected(Set<String> check, Set<String> supported1, Set<String>
}

if (type == CompositionType.OR) {
return Sets.difference(Sets.difference(check, granted1), granted2).isEmpty();
return CollectionUtils.difference(CollectionUtils.difference(check, granted1), granted2).isEmpty();
} else {
Set<String> f1 = CollectionUtils.intersection(supported1, check);
boolean hasf1 = granted1.containsAll(f1);
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.Sets;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.oak.api.Blob;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
Expand Down Expand Up @@ -385,7 +384,7 @@ public void gcForcedRetrieve() throws Exception {
state.blobsPresent.addAll(newBlobs);

// The new blobs should not be found now as new snapshot not done
assertEquals(Sets.difference(state.blobsAdded, retrieveTracked(tracker)), newBlobs);
assertEquals(CollectionUtils.difference(state.blobsAdded, retrieveTracked(tracker)), newBlobs);

//force gc to retrieve blob ids from datastore
cluster.gc.collectGarbage(false, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
import java.util.Set;

import org.apache.jackrabbit.guava.common.base.Splitter;
import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.guava.common.io.Files;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.osgi.framework.BundleContext;
Expand Down Expand Up @@ -93,7 +93,7 @@ private void synchronizeConfigs(ConfigInstaller configInstaller) throws Exceptio
//current config files. Such configs must be remove. Note it does not lead to
//removal of configs added by using ConfigAdmin directly, say using WebConsole
//ui
Set<String> pidsToBeRemoved = Sets.difference(existingPids, configs.keySet());
Set<String> pidsToBeRemoved = CollectionUtils.difference(existingPids, configs.keySet());
configInstaller.removeConfigs(pidsToBeRemoved);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@

import org.apache.jackrabbit.guava.common.base.Stopwatch;

import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.guava.common.io.Closer;
import joptsimple.OptionParser;
import org.apache.commons.io.FileUtils;
import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.commons.conditions.Validate;
import org.apache.jackrabbit.oak.index.async.AsyncIndexerElastic;
import org.apache.jackrabbit.oak.plugins.commit.AnnotatingConflictHandler;
Expand Down Expand Up @@ -161,7 +161,7 @@ private List<String> computeIndexPaths(ElasticIndexOptions indexOpts) throws IOE
if (definitions != null) {
IndexDefinitionUpdater updater = new IndexDefinitionUpdater(definitions);
Set<String> indexPathsFromJson = updater.getIndexPaths();
Set<String> diff = Sets.difference(indexPathsFromJson, indexPaths);
Set<String> diff = CollectionUtils.difference(indexPathsFromJson, indexPaths);
if (!diff.isEmpty()) {
log.info("Augmenting the indexPaths with {} which are present in {}", diff, definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
package org.apache.jackrabbit.oak.index;

import org.apache.jackrabbit.guava.common.base.Stopwatch;
import org.apache.jackrabbit.guava.common.collect.Sets;
import org.apache.jackrabbit.guava.common.io.Closer;
import joptsimple.OptionParser;
import org.apache.commons.io.FileUtils;
import org.apache.felix.inventory.Format;
import org.apache.jackrabbit.oak.api.CommitFailedException;
import org.apache.jackrabbit.oak.commons.collections.CollectionUtils;
import org.apache.jackrabbit.oak.index.async.AsyncIndexerLucene;
import org.apache.jackrabbit.oak.index.indexer.document.DocumentStoreIndexer;
import org.apache.jackrabbit.oak.index.indexer.document.indexstore.IndexStore;
Expand Down Expand Up @@ -205,7 +205,7 @@ private List<String> computeIndexPaths(IndexOptions indexOpts) throws IOExceptio
if (definitions != null) {
IndexDefinitionUpdater updater = new IndexDefinitionUpdater(definitions);
Set<String> indexPathsFromJson = updater.getIndexPaths();
Set<String> diff = Sets.difference(indexPathsFromJson, indexPaths);
Set<String> diff = CollectionUtils.difference(indexPathsFromJson, indexPaths);
if (!diff.isEmpty()) {
log.info("Augmenting the indexPaths with {} which are present in {}", diff, definitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
import org.apache.jackrabbit.guava.common.base.Splitter;
import org.apache.jackrabbit.guava.common.base.Strings;
import org.apache.jackrabbit.guava.common.collect.Iterators;
import org.apache.jackrabbit.guava.common.collect.Sets;
import joptsimple.OptionException;
import org.apache.commons.io.FileUtils;
import org.apache.felix.cm.file.ConfigurationHandler;
Expand Down Expand Up @@ -567,7 +566,7 @@ public void gcWithConsistency() throws Exception {
additionalParams += " --check-consistency-gc true";
testGc(dump, data, 0, false, false);

assertFileEquals(dump, "avail-", Sets.difference(data.added, data.missingDataStore));
assertFileEquals(dump, "avail-", CollectionUtils.difference(data.added, data.missingDataStore));

// Verbose would have paths as well as ids changed but normally only DocumentNS would have paths suffixed
assertFileEquals(dump, "consistencyCandidatesAfterGC", data.missingDataStore);
Expand Down Expand Up @@ -711,7 +710,7 @@ public void testConsistencyFakeDS() throws Exception {

cmd.execute(argsList.toArray(new String[0]));
assertFileEquals(dump, "avail-", new HashSet<>());
assertFileEquals(dump, "marked-", Sets.difference(data.added, data.deleted));
assertFileEquals(dump, "marked-", CollectionUtils.difference(data.added, data.deleted));
}


Expand All @@ -734,18 +733,18 @@ private void testConsistency(File dump, Data data, boolean verbose, boolean verb
cmd.execute(argsList.toArray(new String[0]));

if (!markOnly) {
assertFileEquals(dump, "avail-", Sets.difference(data.added, data.missingDataStore));
assertFileEquals(dump, "avail-", CollectionUtils.difference(data.added, data.missingDataStore));
} else {
assertFileNull(dump, "avail-");
}

// Verbose would have paths as well as ids changed but normally only DocumentNS would have paths suffixed
assertFileEquals(dump, "marked-", verbose ?
encodedIdsAndPath(Sets.difference(verboseRootPath ? data.addedSubset :
encodedIdsAndPath(CollectionUtils.difference(verboseRootPath ? data.addedSubset :
data.added, data.deleted), blobFixture.getType(), data.idToPath, true) :
(storeFixture instanceof StoreFixture.MongoStoreFixture) ?
encodedIdsAndPath(Sets.difference(data.added, data.deleted), blobFixture.getType(), data.idToPath, false) :
Sets.difference(data.added, data.deleted));
encodedIdsAndPath(CollectionUtils.difference(data.added, data.deleted), blobFixture.getType(), data.idToPath, false) :
CollectionUtils.difference(data.added, data.deleted));

if (!markOnly) {
// Verbose would have paths as well as ids changed but normally only DocumentNS would have paths suffixed
Expand Down Expand Up @@ -776,9 +775,9 @@ private void testDumpRef(File dump, Data data, boolean verbose, boolean verboseR

// Verbose would have paths as well as ids changed, otherwise only ids would be listed as it is.
assertFileEquals(dump, "dump-ref-", verbose ?
encodedIdsAndPath(Sets.difference(verboseRootPath ? data.addedSubset :
encodedIdsAndPath(CollectionUtils.difference(verboseRootPath ? data.addedSubset :
data.added, data.deleted), blobFixture.getType(), data.idToPath, true) :
Sets.difference(data.added, data.deleted), "dump");
CollectionUtils.difference(data.added, data.deleted), "dump");

}

Expand All @@ -798,8 +797,8 @@ private void testDumpIds(File dump, Data data, boolean verbose) throws Exception

// Verbose would have backend friendly encoded ids
assertFileEquals(dump, "dump-id-", verbose ?
encodeIds(Sets.difference(data.added, data.missingDataStore), blobFixture.getType()) :
Sets.difference(data.added, data.missingDataStore), "dump");
encodeIds(CollectionUtils.difference(data.added, data.missingDataStore), blobFixture.getType()) :
CollectionUtils.difference(data.added, data.missingDataStore), "dump");

}

Expand All @@ -816,21 +815,21 @@ private void testGc(File dump, Data data, long maxAge, boolean markOnly, boolean
cmd.execute(argsList.toArray(new String[0]));

if (!markOnly && !refsOld) {
assertFileEquals(dump, "avail-", Sets.difference(data.added, data.missingDataStore));
assertFileEquals(dump, "avail-", CollectionUtils.difference(data.added, data.missingDataStore));
} else {
assertFileNull(dump, "avail-");
}

assertFileEquals(dump, "marked-", Sets.difference(data.added, data.deleted));
assertFileEquals(dump, "marked-", CollectionUtils.difference(data.added, data.deleted));
if (!markOnly && !refsOld) {
assertFileEquals(dump, "gccand-", data.deleted);
} else {
assertFileNull(dump, "gccand-");
}

Sets.SetView<String> blobsBeforeGc = Sets.difference(data.added, data.missingDataStore);
Set<String> blobsBeforeGc = CollectionUtils.difference(data.added, data.missingDataStore);
if (maxAge <= 0) {
assertEquals(Sets.difference(blobsBeforeGc, data.deleted), blobs(setupDataStore));
assertEquals(CollectionUtils.difference(blobsBeforeGc, data.deleted), blobs(setupDataStore));
} else {
assertEquals(blobsBeforeGc, blobs(setupDataStore));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import static org.apache.jackrabbit.oak.commons.conditions.Validate.checkArgument;
import static java.util.Objects.requireNonNull;
import static org.apache.jackrabbit.guava.common.collect.Sets.difference;
import static java.util.Collections.emptySet;
import static org.apache.commons.io.FileUtils.sizeOfDirectory;
import static org.apache.jackrabbit.oak.commons.IOUtils.humanReadableByteCount;
Expand Down Expand Up @@ -359,8 +358,8 @@ public int run() {
Set<File> afterFiles = listFiles(path);
printFiles(System.out, afterFiles);
System.out.printf(" size %s\n", printableSize(sizeOfDirectory(path)));
System.out.printf(" removed files %s\n", fileNames(difference(beforeFiles, afterFiles)));
System.out.printf(" added files %s\n", fileNames(difference(afterFiles, beforeFiles)));
System.out.printf(" removed files %s\n", fileNames(CollectionUtils.difference(beforeFiles, afterFiles)));
System.out.printf(" added files %s\n", fileNames(CollectionUtils.difference(afterFiles, beforeFiles)));
System.out.printf("Compaction succeeded in %s.\n", printableStopwatch(watch));
return 0;
}
Expand Down
Loading

0 comments on commit ca264ac

Please sign in to comment.