incubator-blur-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From amccu...@apache.org
Subject [2/2] git commit: More shell improvements.
Date Thu, 08 Aug 2013 14:20:43 GMT
More shell improvements.


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

Branch: refs/heads/master
Commit: e110cd739e94a465ff728dfc25ae6d1dd8c7b5e4
Parents: e7a43e8
Author: Aaron McCurry <amccurry@gmail.com>
Authored: Thu Aug 8 10:20:22 2013 -0400
Committer: Aaron McCurry <amccurry@gmail.com>
Committed: Thu Aug 8 10:20:22 2013 -0400

----------------------------------------------------------------------
 .../org/apache/blur/shell/GetRowCommand.java    |  26 ++++-
 .../main/java/org/apache/blur/shell/Main.java   |   6 +-
 .../apache/blur/shell/PagingPrintWriter.java    | 117 +++++++++++++++++++
 .../org/apache/blur/shell/QueryCommand.java     |  28 +++--
 .../org/apache/blur/shell/SelectorCommand.java  | 100 ++++++++++++++++
 5 files changed, 264 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/e110cd73/blur-shell/src/main/java/org/apache/blur/shell/GetRowCommand.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/GetRowCommand.java b/blur-shell/src/main/java/org/apache/blur/shell/GetRowCommand.java
index da601f8..62fa43a 100644
--- a/blur-shell/src/main/java/org/apache/blur/shell/GetRowCommand.java
+++ b/blur-shell/src/main/java/org/apache/blur/shell/GetRowCommand.java
@@ -24,6 +24,7 @@ import java.util.List;
 import jline.Terminal;
 import jline.console.ConsoleReader;
 
+import org.apache.blur.shell.PagingPrintWriter.FinishedException;
 import org.apache.blur.thirdparty.thrift_0_9_0.TException;
 import org.apache.blur.thrift.generated.Blur;
 import org.apache.blur.thrift.generated.BlurException;
@@ -37,15 +38,29 @@ import org.apache.blur.thrift.generated.Selector;
 public class GetRowCommand extends Command {
 
   @Override
-  public void doit(PrintWriter out, Blur.Iface client, String[] args) throws CommandException,
TException,
+  public void doit(PrintWriter outPw, Blur.Iface client, String[] args) throws CommandException,
TException,
       BlurException {
     if (args.length != 3) {
       throw new CommandException("Invalid args: " + help());
     }
+    
+    PagingPrintWriter out = new PagingPrintWriter(outPw);
+    
+    try {
+      doItInternal(client, args, out);
+    } catch (FinishedException e) {
+      if (Main.debug) {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  private void doItInternal(Blur.Iface client, String[] args, PagingPrintWriter out) throws
BlurException, TException,
+      FinishedException {
     String tablename = args[1];
     String rowId = args[2];
 
-    Selector selector = new Selector();
+    Selector selector = new Selector(Main.selector);
     selector.setRowId(rowId);
     FetchResult fetchRow = client.fetchRow(tablename, selector);
     FetchRowResult rowResult = fetchRow.getRowResult();
@@ -63,11 +78,12 @@ public class GetRowCommand extends Command {
     if (reader != null) {
       Terminal terminal = reader.getTerminal();
       maxWidth = terminal.getWidth() - 15;
+      out.setLineLimit(terminal.getHeight() - 2);
     }
     format(out, row, maxWidth);
   }
 
-  public static void format(PrintWriter out, Row row, int maxWidth) {
+  public static void format(PagingPrintWriter out, Row row, int maxWidth) throws FinishedException
{
     String id = row.getId();
     int recordCount = row.getRecordCount();
     out.println("       id : " + id);
@@ -80,7 +96,7 @@ public class GetRowCommand extends Command {
     }
   }
 
-  public static void format(PrintWriter out, Record record, int maxWidth) {
+  public static void format(PagingPrintWriter out, Record record, int maxWidth) throws FinishedException
{
     String recordId = record.getRecordId();
     String family = record.getFamily();
     List<Column> columns = record.getColumns();
@@ -91,7 +107,7 @@ public class GetRowCommand extends Command {
     }
   }
 
-  private static void format(PrintWriter out, Column column, int maxWidth) {
+  private static void format(PagingPrintWriter out, Column column, int maxWidth) throws FinishedException
{
     String lead = "     " + column.getName() + " : ";
     String value = column.getValue();
     int length = value.length();

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/e110cd73/blur-shell/src/main/java/org/apache/blur/shell/Main.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/Main.java b/blur-shell/src/main/java/org/apache/blur/shell/Main.java
index acddd3c..f18f98d 100644
--- a/blur-shell/src/main/java/org/apache/blur/shell/Main.java
+++ b/blur-shell/src/main/java/org/apache/blur/shell/Main.java
@@ -45,6 +45,7 @@ import org.apache.blur.thrift.Connection;
 import org.apache.blur.thrift.generated.Blur;
 import org.apache.blur.thrift.generated.Blur.Iface;
 import org.apache.blur.thrift.generated.BlurException;
+import org.apache.blur.thrift.generated.Selector;
 
 import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.ImmutableMap.Builder;
@@ -57,6 +58,8 @@ public class Main {
   static boolean timed = false;
   /** is highlight enabled - off by default */
   static boolean highlight = false;
+  /** default selector */
+  static Selector selector = new Selector();
 
   private static Map<String, Command> commands;
   static String cluster;
@@ -235,7 +238,7 @@ public class Main {
 
       out.println();
       out.println(" - Data commands - ");
-      String[] dataCommands = { "query", "get", "mutate", "delete", "highlight" };
+      String[] dataCommands = { "query", "get", "mutate", "delete", "highlight", "selector"
};
       printCommandAndHelp(out, cmds, dataCommands, bufferLength);
 
       out.println();
@@ -347,6 +350,7 @@ public class Main {
     builder.put("top", new TopCommand());
     builder.put("parse", new ParseCommand());
     builder.put("loadtestdata", new LoadTestDataCommand());
+    builder.put("selector", new SelectorCommand());
     commands = builder.build();
 
     CliShellOptions cliShellOptions = getCliShellOptions(args);

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/e110cd73/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java b/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java
new file mode 100644
index 0000000..03d24a0
--- /dev/null
+++ b/blur-shell/src/main/java/org/apache/blur/shell/PagingPrintWriter.java
@@ -0,0 +1,117 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.blur.shell;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.io.PrintWriter;
+
+public class PagingPrintWriter {
+
+  @SuppressWarnings("serial")
+  public static class FinishedException extends Exception {
+  }
+
+  private static final String HIT_ANY_KEY_TO_CONTINUE = "< Hit any key to continue or
q to quit>";
+  private final PrintWriter _printWriter;
+  private int _line = 0;
+  private int _lineLimit = Integer.MAX_VALUE;
+  private boolean _finished;
+
+  public PagingPrintWriter(PrintWriter printWriter) {
+    _printWriter = printWriter;
+  }
+
+  public void println(String s) throws FinishedException {
+    _printWriter.println(s);
+    _line++;
+    flush();
+  }
+
+  public void println(Object o) throws FinishedException {
+    _printWriter.println(o.toString());
+    _line++;
+    flush();
+  }
+
+  public void print(char c) throws FinishedException {
+    _printWriter.print(c);
+    flush();
+  }
+
+  public void println() throws FinishedException {
+    _printWriter.println();
+    _line++;
+    flush();
+  }
+
+  public void flush() throws FinishedException {
+    _printWriter.flush();
+    checkForLineBreak();
+  }
+
+  private void checkForLineBreak() throws FinishedException {
+    if (_line >= _lineLimit) {
+      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
+      try {
+        _printWriter.print(HIT_ANY_KEY_TO_CONTINUE);
+        _printWriter.flush();
+        do {
+          int read = reader.read();
+          if (read != 'q') {
+            break;
+          } else {
+            _finished = true;
+            break;
+          }
+        } while (true);
+        _printWriter.print('\r');
+        for (int i = 0; i < HIT_ANY_KEY_TO_CONTINUE.length(); i++) {
+          _printWriter.print(' ');
+        }
+        _printWriter.print('\r');
+        _printWriter.flush();
+        if (_finished) {
+          throw new FinishedException();
+        }
+        _line = 0;
+      } catch (IOException e) {
+        if (Main.debug) {
+          e.printStackTrace();
+        }
+      }
+    }
+  }
+
+  public int getLineLimit() {
+    return _lineLimit;
+  }
+
+  public void setLineLimit(int lineLimit) {
+    _lineLimit = lineLimit;
+  }
+
+  public boolean isFinished() {
+    return _finished;
+  }
+
+  public void setFinished(boolean finished) {
+    _finished = finished;
+  }
+}

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/e110cd73/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand.java b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand.java
index 9637e85..2c28d1b 100644
--- a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand.java
+++ b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand.java
@@ -23,6 +23,7 @@ import java.io.PrintWriter;
 import jline.Terminal;
 import jline.console.ConsoleReader;
 
+import org.apache.blur.shell.PagingPrintWriter.FinishedException;
 import org.apache.blur.thirdparty.thrift_0_9_0.TException;
 import org.apache.blur.thrift.generated.Blur;
 import org.apache.blur.thrift.generated.BlurException;
@@ -33,16 +34,28 @@ import org.apache.blur.thrift.generated.FetchResult;
 import org.apache.blur.thrift.generated.FetchRowResult;
 import org.apache.blur.thrift.generated.HighlightOptions;
 import org.apache.blur.thrift.generated.Row;
-import org.apache.blur.thrift.generated.Selector;
 import org.apache.blur.thrift.generated.SimpleQuery;
 
 public class QueryCommand extends Command {
   @Override
-  public void doit(PrintWriter out, Blur.Iface client, String[] args) throws CommandException,
TException,
+  public void doit(PrintWriter outPw, Blur.Iface client, String[] args) throws CommandException,
TException,
       BlurException {
     if (args.length < 3) {
       throw new CommandException("Invalid args: " + help());
     }
+    PagingPrintWriter out = new PagingPrintWriter(outPw);
+
+    try {
+      doItInternal(client, args, out);
+    } catch (FinishedException e) {
+      if (Main.debug) {
+        e.printStackTrace();
+      }
+    }
+  }
+
+  private void doItInternal(Blur.Iface client, String[] args, PagingPrintWriter out) throws
FinishedException,
+      BlurException, TException {
     String tablename = args[1];
     String query = "";
     for (int i = 2; i < args.length; i++) {
@@ -53,8 +66,8 @@ public class QueryCommand extends Command {
     SimpleQuery simpleQuery = new SimpleQuery();
     simpleQuery.setQueryStr(query);
     blurQuery.setSimpleQuery(simpleQuery);
-    blurQuery.setSelector(new Selector());
-    
+    blurQuery.setSelector(Main.selector);
+
     if (Main.highlight) {
       blurQuery.getSelector().setHighlightOptions(new HighlightOptions());
     }
@@ -68,6 +81,7 @@ public class QueryCommand extends Command {
     if (reader != null) {
       Terminal terminal = reader.getTerminal();
       maxWidth = terminal.getWidth() - 15;
+      out.setLineLimit(terminal.getHeight() - 2);
     }
     long s = System.nanoTime();
     BlurResults blurResults = client.query(tablename, blurQuery);
@@ -76,7 +90,7 @@ public class QueryCommand extends Command {
 
     printSummary(out, blurResults, maxWidth, timeInNanos);
     lineBreak(out, maxWidth);
-    
+
     int hit = 0;
     for (BlurResult result : blurResults.getResults()) {
       double score = result.getScore();
@@ -105,14 +119,14 @@ public class QueryCommand extends Command {
     printSummary(out, blurResults, maxWidth, timeInNanos);
   }
 
-  private void lineBreak(PrintWriter out, int maxWidth) {
+  private void lineBreak(PagingPrintWriter out, int maxWidth) throws FinishedException {
     for (int i = 0; i < maxWidth; i++) {
       out.print('-');
     }
     out.println();
   }
 
-  private void printSummary(PrintWriter out, BlurResults blurResults, int maxWidth, long
timeInNanos) {
+  private void printSummary(PagingPrintWriter out, BlurResults blurResults, int maxWidth,
long timeInNanos) throws FinishedException {
     long totalResults = blurResults.getTotalResults();
     out.println(" - Results Summary -");
     out.println("    total : " + totalResults);

http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/e110cd73/blur-shell/src/main/java/org/apache/blur/shell/SelectorCommand.java
----------------------------------------------------------------------
diff --git a/blur-shell/src/main/java/org/apache/blur/shell/SelectorCommand.java b/blur-shell/src/main/java/org/apache/blur/shell/SelectorCommand.java
new file mode 100644
index 0000000..b5e0aab
--- /dev/null
+++ b/blur-shell/src/main/java/org/apache/blur/shell/SelectorCommand.java
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.blur.shell;
+
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.apache.blur.thirdparty.thrift_0_9_0.TException;
+import org.apache.blur.thrift.generated.Blur;
+import org.apache.blur.thrift.generated.BlurException;
+import org.apache.blur.thrift.generated.Selector;
+
+public class SelectorCommand extends Command {
+  @Override
+  public void doit(PrintWriter out, Blur.Iface client, String[] args) throws CommandException,
TException,
+      BlurException {
+    if (args.length < 1) {
+      throw new CommandException("Invalid args: " + help());
+    }
+    String command = null;
+    if (args.length > 1) {
+      command = args[1];
+    }
+    if (command == null) {
+      Set<String> columnFamiliesToFetch = Main.selector.getColumnFamiliesToFetch();
+      Map<String, Set<String>> columnsToFetch = Main.selector.getColumnsToFetch();
+      TreeSet<String> names = new TreeSet<String>();
+      if (columnFamiliesToFetch != null) {
+        names.addAll(columnFamiliesToFetch);
+      } else {
+        columnFamiliesToFetch = new TreeSet<String>();
+      }
+      if (columnsToFetch != null) {
+        names.addAll(columnsToFetch.keySet());
+      } else {
+        columnsToFetch = new HashMap<String, Set<String>>();
+      }
+      for (String family : names) {
+        if (columnFamiliesToFetch.contains(family)) {
+          out.println(family + " - All Columns");
+        } else {
+          out.println(family + " - " + new TreeSet<String>(columnsToFetch.get(family)));
+        }
+      }
+    } else if (command.equals("reset")) {
+      Main.selector = new Selector();
+    } else if (command.equals("add")) {
+      if (args.length < 3) {
+        throw new CommandException("Invalid args: " + help());
+      }
+      String family = args[2];
+      if (args.length > 3) {
+        if (Main.selector.columnsToFetch == null) {
+          Main.selector.columnsToFetch = new HashMap<String, Set<String>>();

+        }
+        Set<String> columns = Main.selector.columnsToFetch.get(family);
+        if (columns == null) {
+          columns = new TreeSet<String>();
+          Main.selector.columnsToFetch.put(family, columns);
+        }
+        for (int i = 3; i < args.length; i++) {
+          columns.add(args[i]);
+        }
+        if (!columns.isEmpty()) {
+          Main.selector.putToColumnsToFetch(family, columns);
+        } else {
+          Main.selector.addToColumnFamiliesToFetch(family);
+        }
+      } else {
+        Main.selector.addToColumnFamiliesToFetch(family);
+      }
+    } else {
+      throw new CommandException("Invalid args: " + help());
+    }
+  }
+
+  @Override
+  public String help() {
+    return "manage the default selector, args; [reset] | [add family [columnName*]]";
+  }
+}


Mime
View raw message