accumulo-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From e..@apache.org
Subject svn commit: r1477230 - in /accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell: Shell.java commands/HistoryCommand.java
Date Mon, 29 Apr 2013 17:47:08 GMT
Author: ecn
Date: Mon Apr 29 17:47:08 2013
New Revision: 1477230

URL: http://svn.apache.org/r1477230
Log:
ACCUMULO-1193 applying patch from Tim Reardon to paginate history

Modified:
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
    accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java?rev=1477230&r1=1477229&r2=1477230&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java (original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/Shell.java Mon Apr
29 17:47:08 2013
@@ -162,6 +162,8 @@ public class Shell extends ShellOptions 
   
   public static final String CHARSET = "ISO-8859-1";
   public static final int NO_FIXED_ARG_LENGTH_CHECK = -1;
+  public static final String HISTORY_DIR_NAME = ".accumulo";
+  public static final String HISTORY_FILE_NAME = "shell_history.txt";
   private static final String SHELL_DESCRIPTION = "Shell - Apache Accumulo Interactive Shell";
   private static final String DEFAULT_AUTH_TIMEOUT = "60"; // in minutes
   
@@ -426,8 +428,8 @@ public class Shell extends ShellOptions 
     String home = System.getProperty("HOME");
     if (home == null)
       home = System.getenv("HOME");
-    String configDir = home + "/.accumulo";
-    String historyPath = configDir + "/shell_history.txt";
+    String configDir = home + "/" + HISTORY_DIR_NAME;
+    String historyPath = configDir + "/" + HISTORY_FILE_NAME;
     File accumuloDir = new File(configDir);
     if (!accumuloDir.exists() && !accumuloDir.mkdirs())
       log.warn("Unable to make directory for history at " + accumuloDir);
@@ -518,7 +520,6 @@ public class Shell extends ShellOptions 
     return connector.whoami() + "@" + connector.getInstance().getInstanceName() + (getTableName().isEmpty()
? "" : " ") + getTableName() + "> ";
   }
   
-  @SuppressWarnings("deprecation")
   public void execCommand(String input, boolean ignoreAuthTimeout, boolean echoPrompt) throws
IOException {
     audit.log(AuditLevel.AUDIT, getDefaultPrompt() + input);
     if (echoPrompt) {

Modified: accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
URL: http://svn.apache.org/viewvc/accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java?rev=1477230&r1=1477229&r2=1477230&view=diff
==============================================================================
--- accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
(original)
+++ accumulo/trunk/core/src/main/java/org/apache/accumulo/core/util/shell/commands/HistoryCommand.java
Mon Apr 29 17:47:08 2013
@@ -16,68 +16,61 @@
  */
 package org.apache.accumulo.core.util.shell.commands;
 
-import java.io.BufferedReader;
-import java.io.FileNotFoundException;
-import java.io.FileReader;
-import java.io.FileWriter;
+import java.io.File;
+import java.io.FileOutputStream;
 import java.io.IOException;
-import java.io.PrintWriter;
+import java.util.Iterator;
 
 import org.apache.accumulo.core.util.shell.Shell;
 import org.apache.accumulo.core.util.shell.Shell.Command;
 import org.apache.commons.cli.CommandLine;
 import org.apache.commons.cli.Option;
 import org.apache.commons.cli.Options;
+import org.apache.commons.collections.iterators.AbstractIteratorDecorator;
+import org.apache.commons.io.FileUtils;
+import org.apache.commons.io.LineIterator;
 
 public class HistoryCommand extends Command {
-  
   private Option clearHist;
+  private Option disablePaginationOpt;
   
+  @SuppressWarnings("unchecked")
   @Override
-  public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
throws Exception {
-    
+  public int execute(final String fullCommand, final CommandLine cl, final Shell shellState)
throws IOException {
     String home = System.getProperty("HOME");
     if (home == null)
       home = System.getenv("HOME");
-    final String histDir = home + "/.accumulo";
-    int counter = 0;
+    final String historyPath = home + "/" + Shell.HISTORY_DIR_NAME + "/" + Shell.HISTORY_FILE_NAME;
     
     if (cl.hasOption(clearHist.getOpt())) {
-      
+      final FileOutputStream out = new FileOutputStream(historyPath);
+      out.close();
+    } else {
+      final LineIterator it = FileUtils.lineIterator(new File(historyPath));
       try {
-        
-        final FileWriter outFile = new FileWriter(histDir + "/shell_history.txt");
-        final PrintWriter out = new PrintWriter(outFile);
-        out.close();
-        
-      } catch (IOException e) {
-        
-        e.printStackTrace();
+        shellState.printLines(new HistoryLineIterator(it), !cl.hasOption(disablePaginationOpt.getOpt()));
+      } finally {
+        it.close();
       }
     }
     
-    else {
-      try {
-        final BufferedReader in = new BufferedReader(new FileReader(histDir + "/shell_history.txt"));
-        String Line;
-        try {
-          Line = in.readLine();
-          while (Line != null) {
-            shellState.getReader().printString(counter + " " + Line + "\n");
-            counter++;
-            Line = in.readLine();
-          }
-        } catch (IOException e) {
-          
-          e.printStackTrace();
-        }
-      } catch (FileNotFoundException e) {
-        
-        e.printStackTrace();
-      }
+    return 0;
+  }
+  
+  /**
+   * Decorator that prepends a running counter to an Iterator<String>.
+   */
+  private static class HistoryLineIterator extends AbstractIteratorDecorator {
+    int counter = 0;
+    
+    public HistoryLineIterator(Iterator<String> iterator) {
+      super(iterator);
     }
     
-    return 0;
+    @Override
+    public Object next() {
+      return counter++ + " " + super.next();
+    }
   }
   
   @Override
@@ -93,12 +86,10 @@ public class HistoryCommand extends Comm
   @Override
   public Options getOptions() {
     final Options o = new Options();
-    
     clearHist = new Option("c", "clear", false, "clear history file");
-    clearHist.setRequired(false);
-    
     o.addOption(clearHist);
-    
+    disablePaginationOpt = new Option("np", "no-pagination", false, "disable pagination of
output");
+    o.addOption(disablePaginationOpt);
     return o;
   }
 }



Mime
View raw message