You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@accumulo.apache.org by do...@apache.org on 2022/04/20 17:51:10 UTC

[accumulo] branch main updated: Improvements to streams usages (#2638)

This is an automated email from the ASF dual-hosted git repository.

domgarguilo pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/accumulo.git


The following commit(s) were added to refs/heads/main by this push:
     new c130f1cc55 Improvements to streams usages (#2638)
c130f1cc55 is described below

commit c130f1cc5536e959e98cdb5e7da5c92db385a3f0
Author: Dom G <do...@gmail.com>
AuthorDate: Wed Apr 20 13:51:06 2022 -0400

    Improvements to streams usages (#2638)
    
    * Improvements to streams
    * Simplify propertyTypeTest
---
 .../org/apache/accumulo/core/summary/Gatherer.java |  8 +++---
 .../core/classloader/URLClassLoaderFactory.java    |  3 +--
 .../core/conf/ConfigurationTypeHelperTest.java     |  6 ++---
 .../accumulo/core/conf/PropertyTypeTest.java       | 31 +++++++++++-----------
 .../accumulo/manager/TabletGroupWatcher.java       |  3 +--
 .../accumulo/tserver/compactions/Compactable.java  |  9 ++++---
 .../tserver/compactions/PrintableTable.java        |  4 +--
 .../accumulo/tserver/session/ScanSession.java      |  4 +--
 .../tablet/CompactableImplFileManagerTest.java     |  5 ++--
 .../compaction/ExternalCompactionMetricsIT.java    |  2 +-
 .../test/compaction/ExternalCompaction_1_IT.java   |  4 +--
 .../apache/accumulo/test/functional/SummaryIT.java |  6 ++---
 12 files changed, 41 insertions(+), 44 deletions(-)

diff --git a/core/src/main/java/org/apache/accumulo/core/summary/Gatherer.java b/core/src/main/java/org/apache/accumulo/core/summary/Gatherer.java
index 23b80ffcab..9f839cf82d 100644
--- a/core/src/main/java/org/apache/accumulo/core/summary/Gatherer.java
+++ b/core/src/main/java/org/apache/accumulo/core/summary/Gatherer.java
@@ -88,7 +88,6 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.base.Preconditions;
 import com.google.common.cache.Cache;
-import com.google.common.collect.Lists;
 import com.google.common.hash.Hashing;
 
 /**
@@ -220,8 +219,8 @@ public class Gatherer {
       }
 
       // merge contiguous ranges
-      List<Range> merged = Range
-          .mergeOverlapping(Lists.transform(entry.getValue(), tm -> tm.getExtent().toDataRange()));
+      List<Range> merged = Range.mergeOverlapping(entry.getValue().stream()
+          .map(tm -> tm.getExtent().toDataRange()).collect(Collectors.toList()));
       List<TRowRange> ranges =
           merged.stream().map(r -> toClippedExtent(r).toThrift()).collect(Collectors.toList()); // clip
                                                                                                 // ranges
@@ -528,7 +527,8 @@ public class Gatherer {
     List<CompletableFuture<SummaryCollection>> futures = new ArrayList<>();
     for (Entry<String,List<TRowRange>> entry : files.entrySet()) {
       futures.add(CompletableFuture.supplyAsync(() -> {
-        List<RowRange> rrl = Lists.transform(entry.getValue(), RowRange::new);
+        List<RowRange> rrl =
+            entry.getValue().stream().map(RowRange::new).collect(Collectors.toList());
         return getSummaries(volMgr, entry.getKey(), rrl, summaryCache, indexCache, fileLenCache);
       }, srp));
     }
diff --git a/core/src/test/java/org/apache/accumulo/core/classloader/URLClassLoaderFactory.java b/core/src/test/java/org/apache/accumulo/core/classloader/URLClassLoaderFactory.java
index beece5fd5b..576c42f8ec 100644
--- a/core/src/test/java/org/apache/accumulo/core/classloader/URLClassLoaderFactory.java
+++ b/core/src/test/java/org/apache/accumulo/core/classloader/URLClassLoaderFactory.java
@@ -21,7 +21,6 @@ package org.apache.accumulo.core.classloader;
 import java.net.MalformedURLException;
 import java.net.URL;
 import java.net.URLClassLoader;
-import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
 import org.apache.accumulo.core.spi.common.ContextClassLoaderFactory;
@@ -40,7 +39,7 @@ public class URLClassLoaderFactory implements ContextClassLoaderFactory {
       } catch (MalformedURLException e) {
         throw new IllegalArgumentException("Error creating URL from classpath segment: " + p, e);
       }
-    }).collect(Collectors.toList()).toArray(new URL[0]);
+    }).toArray(URL[]::new);
     return URLClassLoader.newInstance(urls);
   }
 
diff --git a/core/src/test/java/org/apache/accumulo/core/conf/ConfigurationTypeHelperTest.java b/core/src/test/java/org/apache/accumulo/core/conf/ConfigurationTypeHelperTest.java
index bbfb5aa4c5..3315b61e83 100644
--- a/core/src/test/java/org/apache/accumulo/core/conf/ConfigurationTypeHelperTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/conf/ConfigurationTypeHelperTest.java
@@ -25,8 +25,8 @@ import static java.util.concurrent.TimeUnit.SECONDS;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
-import java.util.Arrays;
 import java.util.function.Function;
+import java.util.stream.Stream;
 
 import org.junit.jupiter.api.Test;
 
@@ -34,8 +34,8 @@ public class ConfigurationTypeHelperTest {
 
   @Test
   public void testGetMemoryInBytes() {
-    Arrays.<Function<String,Long>>asList(ConfigurationTypeHelper::getFixedMemoryAsBytes,
-        ConfigurationTypeHelper::getMemoryAsBytes).stream().forEach(memFunc -> {
+    Stream.<Function<String,Long>>of(ConfigurationTypeHelper::getFixedMemoryAsBytes,
+        ConfigurationTypeHelper::getMemoryAsBytes).forEach(memFunc -> {
           assertEquals(42L, memFunc.apply("42").longValue());
           assertEquals(42L, memFunc.apply("42b").longValue());
           assertEquals(42L, memFunc.apply("42B").longValue());
diff --git a/core/src/test/java/org/apache/accumulo/core/conf/PropertyTypeTest.java b/core/src/test/java/org/apache/accumulo/core/conf/PropertyTypeTest.java
index cfbde32313..4b53b75c96 100644
--- a/core/src/test/java/org/apache/accumulo/core/conf/PropertyTypeTest.java
+++ b/core/src/test/java/org/apache/accumulo/core/conf/PropertyTypeTest.java
@@ -23,8 +23,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.lang.reflect.Method;
-import java.util.Arrays;
-import java.util.List;
+import java.util.Set;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 
@@ -64,22 +63,22 @@ public class PropertyTypeTest extends WithTestNames {
     assertEquals("string", PropertyType.STRING.toString());
   }
 
+  /**
+   * This test checks the remainder of the methods in this class to ensure each property type has a
+   * corresponding test
+   */
   @Test
   public void testFullCoverage() {
-    // This test checks the remainder of the methods in this class to ensure each property type has
-    // a corresponding test
-    Stream<String> types = Arrays.stream(PropertyType.values()).map(Enum<PropertyType>::name);
-
-    List<String> typesTested = Arrays.stream(this.getClass().getMethods()).map(Method::getName)
-        .filter(m -> m.startsWith("testType")).map(m -> m.substring(8))
-        .collect(Collectors.toList());
-
-    types = types.map(t -> {
-      assertTrue(typesTested.contains(t),
-          PropertyType.class.getSimpleName() + "." + t + " does not have a test.");
-      return t;
-    });
-    assertEquals(types.count(), typesTested.size());
+
+    String typePrefix = "testType";
+    Set<String> typesTested = Stream.of(this.getClass().getMethods()).map(Method::getName)
+        .filter(m -> m.startsWith(typePrefix)).map(m -> m.substring(typePrefix.length()))
+        .collect(Collectors.toSet());
+
+    Set<String> types =
+        Stream.of(PropertyType.values()).map(Enum<PropertyType>::name).collect(Collectors.toSet());
+
+    assertEquals(types, typesTested, "Expected to see a test method for each property type");
   }
 
   private void valid(final String... args) {
diff --git a/server/manager/src/main/java/org/apache/accumulo/manager/TabletGroupWatcher.java b/server/manager/src/main/java/org/apache/accumulo/manager/TabletGroupWatcher.java
index a92d4730ae..fcf421aaf3 100644
--- a/server/manager/src/main/java/org/apache/accumulo/manager/TabletGroupWatcher.java
+++ b/server/manager/src/main/java/org/apache/accumulo/manager/TabletGroupWatcher.java
@@ -762,8 +762,7 @@ abstract class TabletGroupWatcher extends AccumuloDaemonThread {
         ServerColumnFamily.TIME_COLUMN.put(m, new Value(maxLogicalTime.encode()));
 
       // delete any entries for external compactions
-      extCompIds.stream()
-          .forEach(ecid -> m.putDelete(ExternalCompactionColumnFamily.STR_NAME, ecid));
+      extCompIds.forEach(ecid -> m.putDelete(ExternalCompactionColumnFamily.STR_NAME, ecid));
 
       if (!m.getUpdates().isEmpty()) {
         bw.addMutation(m);
diff --git a/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/Compactable.java b/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/Compactable.java
index 76307dd8dd..34c8fb11e8 100644
--- a/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/Compactable.java
+++ b/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/Compactable.java
@@ -62,11 +62,12 @@ public interface Compactable {
         Set<StoredTabletFile> candidates, Collection<CompactionJob> running,
         Map<String,String> executionHints) {
 
-      this.allFiles = Collections.unmodifiableSet(allFiles.entrySet().stream()
+      this.allFiles = allFiles.entrySet().stream()
           .map(entry -> new CompactableFileImpl(entry.getKey(), entry.getValue()))
-          .collect(Collectors.toSet()));
-      this.candidates = Collections.unmodifiableSet(candidates.stream()
-          .map(stf -> new CompactableFileImpl(stf, allFiles.get(stf))).collect(Collectors.toSet()));
+          .collect(Collectors.toUnmodifiableSet());
+      this.candidates =
+          candidates.stream().map(stf -> new CompactableFileImpl(stf, allFiles.get(stf)))
+              .collect(Collectors.toUnmodifiableSet());
 
       this.compacting = Set.copyOf(running);
 
diff --git a/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/PrintableTable.java b/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/PrintableTable.java
index 4b282f01e5..c03e3dbf95 100644
--- a/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/PrintableTable.java
+++ b/server/tserver/src/main/java/org/apache/accumulo/tserver/compactions/PrintableTable.java
@@ -18,7 +18,7 @@
  */
 package org.apache.accumulo.tserver.compactions;
 
-import java.util.Arrays;
+import java.util.stream.Stream;
 
 public class PrintableTable {
   private String[] columns;
@@ -33,7 +33,7 @@ public class PrintableTable {
 
   @Override
   public String toString() {
-    int widestRow = Arrays.asList(rows).stream().mapToInt(String::length).max().getAsInt();
+    int widestRow = Stream.of(rows).mapToInt(String::length).max().getAsInt();
 
     StringBuilder sb = new StringBuilder();
 
diff --git a/server/tserver/src/main/java/org/apache/accumulo/tserver/session/ScanSession.java b/server/tserver/src/main/java/org/apache/accumulo/tserver/session/ScanSession.java
index bb97d74d47..fd4ffa5f7c 100644
--- a/server/tserver/src/main/java/org/apache/accumulo/tserver/session/ScanSession.java
+++ b/server/tserver/src/main/java/org/apache/accumulo/tserver/session/ScanSession.java
@@ -23,6 +23,7 @@ import java.util.Collections;
 import java.util.Map;
 import java.util.OptionalLong;
 import java.util.Set;
+import java.util.stream.Collectors;
 
 import org.apache.accumulo.core.data.Column;
 import org.apache.accumulo.core.dataImpl.thrift.IterInfo;
@@ -34,7 +35,6 @@ import org.apache.accumulo.core.util.Stat;
 import org.apache.accumulo.tserver.scan.ScanParameters;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.Lists;
 
 public abstract class ScanSession extends Session implements ScanInfo {
 
@@ -150,7 +150,7 @@ public abstract class ScanSession extends Session implements ScanInfo {
 
   @Override
   public Collection<IteratorConfiguration> getClientScanIterators() {
-    return Lists.transform(scanParams.getSsiList(), IterConfImpl::new);
+    return scanParams.getSsiList().stream().map(IterConfImpl::new).collect(Collectors.toList());
   }
 
   @Override
diff --git a/server/tserver/src/test/java/org/apache/accumulo/tserver/tablet/CompactableImplFileManagerTest.java b/server/tserver/src/test/java/org/apache/accumulo/tserver/tablet/CompactableImplFileManagerTest.java
index b6825ed4ee..462df02e61 100644
--- a/server/tserver/src/test/java/org/apache/accumulo/tserver/tablet/CompactableImplFileManagerTest.java
+++ b/server/tserver/src/test/java/org/apache/accumulo/tserver/tablet/CompactableImplFileManagerTest.java
@@ -29,11 +29,11 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.time.Duration;
-import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Optional;
 import java.util.Set;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import org.apache.accumulo.core.client.admin.compaction.CompactableFile;
 import org.apache.accumulo.core.data.TableId;
@@ -451,7 +451,8 @@ public class CompactableImplFileManagerTest {
   }
 
   static Set<StoredTabletFile> newFiles(String... strings) {
-    return Arrays.asList(strings).stream().map(s -> newFile(s)).collect(Collectors.toSet());
+    return Stream.of(strings).map(CompactableImplFileManagerTest::newFile)
+        .collect(Collectors.toSet());
   }
 
   private static class TestCompactionJob implements CompactionJob {
diff --git a/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionMetricsIT.java b/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionMetricsIT.java
index 5fc04c8566..d9fe2dca41 100644
--- a/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionMetricsIT.java
+++ b/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompactionMetricsIT.java
@@ -166,7 +166,7 @@ public class ExternalCompactionMetricsIT extends SharedMiniClusterBase {
         UtilWaitThread.sleep(100);
         try (TabletsMetadata tm = getCluster().getServerContext().getAmple().readTablets()
             .forLevel(DataLevel.USER).fetch(ColumnType.ECOMP).build()) {
-          count = tm.stream().flatMap(t -> t.getExternalCompactions().keySet().stream()).count();
+          count = tm.stream().mapToLong(t -> t.getExternalCompactions().keySet().size()).sum();
         }
       } while (count > 0);
 
diff --git a/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompaction_1_IT.java b/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompaction_1_IT.java
index 7a3db449ea..1dae353c72 100644
--- a/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompaction_1_IT.java
+++ b/test/src/main/java/org/apache/accumulo/test/compaction/ExternalCompaction_1_IT.java
@@ -397,7 +397,7 @@ public class ExternalCompaction_1_IT extends SharedMiniClusterBase {
       LOG.info("Waiting for external compaction to complete.");
       TableId tid = getCluster().getServerContext().getTableId(table3);
       Stream<ExternalCompactionFinalState> fs = getFinalStatesForTable(getCluster(), tid);
-      while (fs.count() == 0) {
+      while (fs.findAny().isEmpty()) {
         LOG.info("Waiting for compaction completed marker to appear");
         UtilWaitThread.sleep(250);
         fs = getFinalStatesForTable(getCluster(), tid);
@@ -435,7 +435,7 @@ public class ExternalCompaction_1_IT extends SharedMiniClusterBase {
       // Wait for the compaction to be committed.
       LOG.info("Waiting for compaction completed marker to disappear");
       Stream<ExternalCompactionFinalState> fs2 = getFinalStatesForTable(getCluster(), tid);
-      while (fs2.count() != 0) {
+      while (fs2.findAny().isPresent()) {
         LOG.info("Waiting for compaction completed marker to disappear");
         UtilWaitThread.sleep(500);
         fs2 = getFinalStatesForTable(getCluster(), tid);
diff --git a/test/src/main/java/org/apache/accumulo/test/functional/SummaryIT.java b/test/src/main/java/org/apache/accumulo/test/functional/SummaryIT.java
index ab92eb00e5..53dd239e95 100644
--- a/test/src/main/java/org/apache/accumulo/test/functional/SummaryIT.java
+++ b/test/src/main/java/org/apache/accumulo/test/functional/SummaryIT.java
@@ -35,7 +35,6 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 import static org.junit.jupiter.api.Assertions.fail;
 
-import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
@@ -49,6 +48,7 @@ import java.util.function.IntPredicate;
 import java.util.regex.PatternSyntaxException;
 import java.util.stream.Collectors;
 import java.util.stream.IntStream;
+import java.util.stream.Stream;
 
 import org.apache.accumulo.core.client.Accumulo;
 import org.apache.accumulo.core.client.AccumuloClient;
@@ -89,8 +89,6 @@ import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.api.Test;
 
-import com.google.common.collect.Lists;
-
 public class SummaryIT extends SharedMiniClusterBase {
 
   @BeforeAll
@@ -136,7 +134,7 @@ public class SummaryIT extends SharedMiniClusterBase {
   private void addSplits(final String table, AccumuloClient c, String... splits)
       throws TableNotFoundException, AccumuloException, AccumuloSecurityException {
     c.tableOperations().addSplits(table,
-        new TreeSet<>(Lists.transform(Arrays.asList(splits), Text::new)));
+        Stream.of(splits).map(Text::new).collect(Collectors.toCollection(TreeSet::new)));
   }
 
   @Test