accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ctubb...@apache.org
Subject [1/6] accumulo git commit: ACCUMULO-4541 Ensure grep output gets flushed
Date Mon, 09 Jan 2017 23:40:03 GMT
Repository: accumulo
Updated Branches:
  refs/heads/1.7 62c31b586 -> c3d870329
  refs/heads/1.8 da47fb210 -> 4cc53bb12
  refs/heads/master 0de942d71 -> f6993056d


ACCUMULO-4541 Ensure grep output gets flushed

Ensure output of grep command in shell (and egrep) gets flushed to the
output file when the '-o' option is used. Also ensure the '-o' option is
not shown as an option in ScanCommand subclasses which do not support
its use.


Project: http://git-wip-us.apache.org/repos/asf/accumulo/repo
Commit: http://git-wip-us.apache.org/repos/asf/accumulo/commit/c3d87032
Tree: http://git-wip-us.apache.org/repos/asf/accumulo/tree/c3d87032
Diff: http://git-wip-us.apache.org/repos/asf/accumulo/diff/c3d87032

Branch: refs/heads/1.7
Commit: c3d8703296718141e86d5d63a27f235d6f98f1ce
Parents: 62c31b5
Author: Christopher Tubbs <ctubbsii@apache.org>
Authored: Mon Jan 9 17:52:16 2017 -0500
Committer: Christopher Tubbs <ctubbsii@apache.org>
Committed: Mon Jan 9 17:52:16 2017 -0500

----------------------------------------------------------------------
 .../java/org/apache/accumulo/shell/Shell.java   |  2 +-
 .../accumulo/shell/commands/GrepCommand.java    | 67 ++++++++-------
 .../accumulo/shell/commands/ScanCommand.java    | 90 ++++++++++----------
 3 files changed, 82 insertions(+), 77 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/accumulo/blob/c3d87032/shell/src/main/java/org/apache/accumulo/shell/Shell.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/accumulo/shell/Shell.java b/shell/src/main/java/org/apache/accumulo/shell/Shell.java
index 776c4f4..9dc69c5 100644
--- a/shell/src/main/java/org/apache/accumulo/shell/Shell.java
+++ b/shell/src/main/java/org/apache/accumulo/shell/Shell.java
@@ -995,7 +995,7 @@ public class Shell extends ShellOptions implements KeywordExecutable {
     }
   }
 
-  public interface PrintLine {
+  public interface PrintLine extends AutoCloseable {
     void print(String s);
 
     void close();

http://git-wip-us.apache.org/repos/asf/accumulo/blob/c3d87032/shell/src/main/java/org/apache/accumulo/shell/commands/GrepCommand.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/accumulo/shell/commands/GrepCommand.java b/shell/src/main/java/org/apache/accumulo/shell/commands/GrepCommand.java
index 97bddc9..6d20790 100644
--- a/shell/src/main/java/org/apache/accumulo/shell/commands/GrepCommand.java
+++ b/shell/src/main/java/org/apache/accumulo/shell/commands/GrepCommand.java
@@ -39,39 +39,40 @@ public class GrepCommand extends ScanCommand {
 
   @Override
   public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
throws Exception {
-    final PrintFile printFile = getOutputFile(cl);
-
-    final String tableName = OptUtil.getTableOpt(cl, shellState);
-
-    if (cl.getArgList().isEmpty()) {
-      throw new MissingArgumentException("No terms specified");
-    }
-    final Class<? extends Formatter> formatter = getFormatter(cl, tableName, shellState);
-    final ScanInterpreter interpeter = getInterpreter(cl, tableName, shellState);
-
-    // handle first argument, if present, the authorizations list to
-    // scan with
-    int numThreads = 20;
-    if (cl.hasOption(numThreadsOpt.getOpt())) {
-      numThreads = Integer.parseInt(cl.getOptionValue(numThreadsOpt.getOpt()));
-    }
-    final Authorizations auths = getAuths(cl, shellState);
-    final BatchScanner scanner = shellState.getConnector().createBatchScanner(tableName,
auths, numThreads);
-    scanner.setRanges(Collections.singletonList(getRange(cl, interpeter)));
-
-    scanner.setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
-
-    for (int i = 0; i < cl.getArgs().length; i++) {
-      setUpIterator(Integer.MAX_VALUE - cl.getArgs().length + i, "grep" + i, cl.getArgs()[i],
scanner, cl);
-    }
-    try {
-      // handle columns
-      fetchColumns(cl, scanner, interpeter);
-
-      // output the records
-      printRecords(cl, shellState, scanner, formatter, printFile);
-    } finally {
-      scanner.close();
+    try (final PrintFile printFile = getOutputFile(cl)) {
+
+      final String tableName = OptUtil.getTableOpt(cl, shellState);
+
+      if (cl.getArgList().isEmpty()) {
+        throw new MissingArgumentException("No terms specified");
+      }
+      final Class<? extends Formatter> formatter = getFormatter(cl, tableName, shellState);
+      final ScanInterpreter interpeter = getInterpreter(cl, tableName, shellState);
+
+      // handle first argument, if present, the authorizations list to
+      // scan with
+      int numThreads = 20;
+      if (cl.hasOption(numThreadsOpt.getOpt())) {
+        numThreads = Integer.parseInt(cl.getOptionValue(numThreadsOpt.getOpt()));
+      }
+      final Authorizations auths = getAuths(cl, shellState);
+      final BatchScanner scanner = shellState.getConnector().createBatchScanner(tableName,
auths, numThreads);
+      scanner.setRanges(Collections.singletonList(getRange(cl, interpeter)));
+
+      scanner.setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
+
+      for (int i = 0; i < cl.getArgs().length; i++) {
+        setUpIterator(Integer.MAX_VALUE - cl.getArgs().length + i, "grep" + i, cl.getArgs()[i],
scanner, cl);
+      }
+      try {
+        // handle columns
+        fetchColumns(cl, scanner, interpeter);
+
+        // output the records
+        printRecords(cl, shellState, scanner, formatter, printFile);
+      } finally {
+        scanner.close();
+      }
     }
 
     return 0;

http://git-wip-us.apache.org/repos/asf/accumulo/blob/c3d87032/shell/src/main/java/org/apache/accumulo/shell/commands/ScanCommand.java
----------------------------------------------------------------------
diff --git a/shell/src/main/java/org/apache/accumulo/shell/commands/ScanCommand.java b/shell/src/main/java/org/apache/accumulo/shell/commands/ScanCommand.java
index 6f1ddd3..b6eb8d6 100644
--- a/shell/src/main/java/org/apache/accumulo/shell/commands/ScanCommand.java
+++ b/shell/src/main/java/org/apache/accumulo/shell/commands/ScanCommand.java
@@ -19,6 +19,7 @@ package org.apache.accumulo.shell.commands;
 import java.io.FileNotFoundException;
 import java.io.IOException;
 import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map.Entry;
 import java.util.concurrent.TimeUnit;
@@ -64,50 +65,48 @@ public class ScanCommand extends Command {
 
   @Override
   public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
throws Exception {
-    final PrintFile printFile = getOutputFile(cl);
-    final String tableName = OptUtil.getTableOpt(cl, shellState);
-
-    final Class<? extends Formatter> formatter = getFormatter(cl, tableName, shellState);
-    final ScanInterpreter interpeter = getInterpreter(cl, tableName, shellState);
-
-    // handle first argument, if present, the authorizations list to
-    // scan with
-    final Authorizations auths = getAuths(cl, shellState);
-    final Scanner scanner = shellState.getConnector().createScanner(tableName, auths);
-
-    // handle session-specific scan iterators
-    addScanIterators(shellState, cl, scanner, tableName);
-
-    // handle remaining optional arguments
-    scanner.setRange(getRange(cl, interpeter));
-
-    // handle columns
-    fetchColumns(cl, scanner, interpeter);
-
-    // set timeout
-    scanner.setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
-
-    // output the records
-    if (cl.hasOption(showFewOpt.getOpt())) {
-      final String showLength = cl.getOptionValue(showFewOpt.getOpt());
-      try {
-        final int length = Integer.parseInt(showLength);
-        if (length < 1) {
-          throw new IllegalArgumentException();
+    try (final PrintFile printFile = getOutputFile(cl)) {
+      final String tableName = OptUtil.getTableOpt(cl, shellState);
+
+      final Class<? extends Formatter> formatter = getFormatter(cl, tableName, shellState);
+      final ScanInterpreter interpeter = getInterpreter(cl, tableName, shellState);
+
+      // handle first argument, if present, the authorizations list to
+      // scan with
+      final Authorizations auths = getAuths(cl, shellState);
+      final Scanner scanner = shellState.getConnector().createScanner(tableName, auths);
+
+      // handle session-specific scan iterators
+      addScanIterators(shellState, cl, scanner, tableName);
+
+      // handle remaining optional arguments
+      scanner.setRange(getRange(cl, interpeter));
+
+      // handle columns
+      fetchColumns(cl, scanner, interpeter);
+
+      // set timeout
+      scanner.setTimeout(getTimeout(cl), TimeUnit.MILLISECONDS);
+
+      // output the records
+      if (cl.hasOption(showFewOpt.getOpt())) {
+        final String showLength = cl.getOptionValue(showFewOpt.getOpt());
+        try {
+          final int length = Integer.parseInt(showLength);
+          if (length < 1) {
+            throw new IllegalArgumentException();
+          }
+          BinaryFormatter.getlength(length);
+          printBinaryRecords(cl, shellState, scanner, printFile);
+        } catch (NumberFormatException nfe) {
+          shellState.getReader().println("Arg must be an integer.");
+        } catch (IllegalArgumentException iae) {
+          shellState.getReader().println("Arg must be greater than one.");
         }
-        BinaryFormatter.getlength(length);
-        printBinaryRecords(cl, shellState, scanner, printFile);
-      } catch (NumberFormatException nfe) {
-        shellState.getReader().println("Arg must be an integer.");
-      } catch (IllegalArgumentException iae) {
-        shellState.getReader().println("Arg must be greater than one.");
-      }
 
-    } else {
-      printRecords(cl, shellState, scanner, formatter, printFile);
-    }
-    if (printFile != null) {
-      printFile.close();
+      } else {
+        printRecords(cl, shellState, scanner, formatter, printFile);
+      }
     }
 
     return 0;
@@ -325,7 +324,12 @@ public class ScanCommand extends Command {
     o.addOption(interpreterOpt);
     o.addOption(formatterInterpeterOpt);
     o.addOption(timeoutOption);
-    o.addOption(outputFileOpt);
+    if (Arrays.asList(new String[] {ScanCommand.class.getName(), GrepCommand.class.getName(),
EGrepCommand.class.getName()})
+        .contains(this.getClass().getName())) {
+      // supported subclasses must handle the output file option properly
+      // only add this option to commands which handle it correctly
+      o.addOption(outputFileOpt);
+    }
     o.addOption(profileOpt);
 
     return o;


Mime
View raw message