aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/collect
diff options
context:
space:
mode:
authorGravatar ulfjack <ulfjack@google.com>2018-05-15 10:31:19 -0700
committerGravatar Copybara-Service <copybara-piper@google.com>2018-05-15 10:33:36 -0700
commite78867461c594d9afcaae0dd115b86e2d63a29ac (patch)
tree66dfbc1214fda8132cb2cb2d0550e23278a6a1be /src/main/java/com/google/devtools/build/lib/collect
parent374112c3cab43cb37b4e1d2cd9fcef5e402007ea (diff)
Implement a limit for named_set_of_file events
This adds a command-line option which can be used to force Bazel to split up very large events, e.g., events with hundreds of thousands of entries. This is rare, but happens occasionally. It would be better to control the maximum event size directly, but that is significantly more difficult to do here. PiperOrigin-RevId: 196690600
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/collect')
-rw-r--r--src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java30
1 files changed, 28 insertions, 2 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java
index b1f778b3fe..2d81b8ef66 100644
--- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java
+++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/NestedSetView.java
@@ -15,6 +15,7 @@ package com.google.devtools.build.lib.collect.nestedset;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
+import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableSet;
import java.util.Arrays;
import java.util.Set;
@@ -45,9 +46,34 @@ public class NestedSetView<E> {
}
/**
- * Return an object where the {@link equals()} method provides the correct notion of (intensional)
+ * Splits the current view into multiple views if the number of entries in the current set exceeds
+ * the given limit, which has to be at least 2. This method guarantees that the resulting view
+ * contains the same elements (direct and transitive) overall, and that each node's size is less
+ * than or equal to the given limit. It makes no guarantees about the resulting structure of the
+ * graph, and this may affect the traversal order if it is converted back to a nested set.
+ */
+ public NestedSetView<E> splitIfExceedsMaximumSize(int maximumSize) {
+ Preconditions.checkArgument(maximumSize >= 2, "maximumSize must be at least 2");
+ if (!(set instanceof Object[])) {
+ return this;
+ }
+ Object[] arr = (Object[]) set;
+ int size = arr.length;
+ if (size <= maximumSize) {
+ return this;
+ }
+ Object[][] pieces = new Object[((size + maximumSize - 1) / maximumSize)][];
+ for (int i = 0; i < pieces.length; i++) {
+ int max = Math.min((i + 1) * maximumSize, arr.length);
+ pieces[i] = Arrays.copyOfRange(arr, i * maximumSize, max);
+ }
+ return new NestedSetView<E>(pieces).splitIfExceedsMaximumSize(maximumSize);
+ }
+
+ /**
+ * Return an object where the {@link #equals} method provides the correct notion of (intensional)
* equality of the set viewed. Consumers of this method should not assume any properties of the
- * returned object apart from its {@link equals()} method.
+ * returned object apart from its {@link #equals} method.
*
* <p>The identifier is meant as an abstract, but memory efficient way of remembering nested sets
* directly or indirectly seen. Storing the identifier of a nested-set view will not retain more