Repository: incubator-blur Updated Branches: refs/heads/apache-blur-0.2 56bf3611d -> ce74aa204
Adding new query command as default, old query command has been renamed to 'query-old'. Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/3ec0b122 Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/3ec0b122 Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/3ec0b122 Branch: refs/heads/apache-blur-0.2 Commit: 3ec0b122342380f7557f6c287681a2617598438c Parents: 56bf361 Author: Aaron McCurry <[email protected]> Authored: Mon Jun 23 09:15:31 2014 -0400 Committer: Aaron McCurry <[email protected]> Committed: Mon Jun 23 09:15:31 2014 -0400 ---------------------------------------------------------------------- .../main/java/org/apache/blur/shell/Main.java | 2 +- .../org/apache/blur/shell/QueryCommand.java | 157 ------------- .../org/apache/blur/shell/QueryCommand2.java | 225 ++++++++++++++----- .../apache/blur/shell/QueryCommandHelper.java | 39 +++- .../org/apache/blur/shell/QueryCommandOld.java | 157 +++++++++++++ .../org/apache/blur/shell/TableDisplay.java | 9 +- 6 files changed, 360 insertions(+), 229 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/3ec0b122/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 5c94ba1..8ea0a11 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 @@ -664,7 +664,7 @@ public class Main { register(builder, new DescribeTableCommand()); register(builder, new TableStatsCommand()); register(builder, new SchemaTableCommand()); - register(builder, new QueryCommand()); + register(builder, new QueryCommandOld()); register(builder, new GetRowCommand()); register(builder, new DeleteRowCommand()); register(builder, new MutateRowCommand()); http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/3ec0b122/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 deleted file mode 100644 index b15657a..0000000 --- a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand.java +++ /dev/null @@ -1,157 +0,0 @@ -/** - * 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 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; -import org.apache.blur.thrift.generated.BlurQuery; -import org.apache.blur.thrift.generated.BlurResult; -import org.apache.blur.thrift.generated.BlurResults; -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.Query; -import org.apache.blur.thrift.generated.Selector; - -public class QueryCommand extends Command implements TableFirstArgCommand { - @Override - 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 queryStr = ""; - for (int i = 2; i < args.length; i++) { - queryStr += args[i] + " "; - } - - BlurQuery blurQuery = new BlurQuery(); - Query query = new Query(); - query.setQuery(queryStr); - blurQuery.setQuery(query); - blurQuery.setSelector(new Selector(Main.selector)); - blurQuery.setCacheResult(false); - blurQuery.setUseCacheIfPresent(false); - - if (Main.highlight) { - blurQuery.getSelector().setHighlightOptions(new HighlightOptions()); - } - - if (Main.debug) { - out.println(blurQuery); - } - - int maxWidth = 100; - ConsoleReader reader = getConsoleReader(); - 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); - long e = System.nanoTime(); - long timeInNanos = e - s; - - printSummary(out, blurResults, maxWidth, timeInNanos); - lineBreak(out, maxWidth); - - int hit = 0; - for (BlurResult result : blurResults.getResults()) { - double score = result.getScore(); - out.println(" hit : " + hit); - out.println(" score : " + score); - if (Main.debug) { - String locationId = result.getLocationId(); - out.println("locationId : " + locationId); - } - FetchResult fetchResult = result.getFetchResult(); - if (Main.debug) { - out.println("deleted : " + fetchResult.isDeleted()); - out.println("exists : " + fetchResult.isExists()); - out.println("table : " + fetchResult.getTable()); - } - FetchRowResult rowResult = fetchResult.getRowResult(); - if (rowResult != null) { - Row row = rowResult.getRow(); - if (row != null) { - GetRowCommand.format(out, rowResult, maxWidth); - } - } - lineBreak(out, maxWidth); - hit++; - } - printSummary(out, blurResults, maxWidth, timeInNanos); - } - - private void lineBreak(PagingPrintWriter out, int maxWidth) throws FinishedException { - for (int i = 0; i < maxWidth; i++) { - out.print('-'); - } - out.println(); - } - - 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); - out.println(" time : " + (timeInNanos / 1000000.0) + " ms"); - if (Main.debug) { - out.println("shardinfo: " + blurResults.getShardInfo()); - } - } - - @Override - public String description() { - return "Query the named table."; - } - - @Override - public String usage() { - return "<tablename> <query>"; - } - - @Override - public String name() { - return "query"; - } -} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/3ec0b122/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand2.java ---------------------------------------------------------------------- diff --git a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand2.java b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand2.java index 86d7438..dfadab7 100644 --- a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand2.java +++ b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand2.java @@ -24,11 +24,13 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.TreeSet; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicInteger; import jline.console.ConsoleReader; @@ -41,6 +43,7 @@ import org.apache.blur.thrift.generated.BlurResult; import org.apache.blur.thrift.generated.BlurResults; import org.apache.blur.thrift.generated.Column; import org.apache.blur.thrift.generated.ErrorType; +import org.apache.blur.thrift.generated.FetchRecordResult; import org.apache.blur.thrift.generated.FetchResult; import org.apache.blur.thrift.generated.FetchRowResult; import org.apache.blur.thrift.generated.Record; @@ -48,6 +51,13 @@ import org.apache.blur.thrift.generated.Row; import org.apache.commons.cli.CommandLine; public class QueryCommand2 extends Command implements TableFirstArgCommand { + + static enum RenderType { + ROW_MULTI_FAMILY, ROW_SINGLE_FAMILY + } + + private int _width; + @Override public void doit(PrintWriter outPw, Blur.Iface client, String[] args) throws CommandException, TException, BlurException { @@ -68,25 +78,39 @@ public class QueryCommand2 extends Command implements TableFirstArgCommand { TException { String tablename = args[1]; CommandLine commandLine = QueryCommandHelper.parse(args, out); + if (commandLine == null) { + return; + } BlurQuery blurQuery = QueryCommandHelper.getBlurQuery(commandLine); if (Main.debug) { out.println(blurQuery); } + _width = 100; + if (commandLine.hasOption(QueryCommandHelper.WIDTH)) { + _width = Integer.parseInt(commandLine.getOptionValue(QueryCommandHelper.WIDTH)); + } ConsoleReader reader = getConsoleReader(); if (reader == null) { throw new BlurException("This command can only be run with a proper jline environment.", null, ErrorType.UNKNOWN); } + + long s = System.nanoTime(); + BlurResults blurResults = client.query(tablename, blurQuery); + long e = System.nanoTime(); + long timeInNanos = e - s; + if (blurResults.getTotalResults() == 0) { + out.println("No results found in [" + timeInNanos / 1000000.0 + " ms]."); + return; + } + String prompt = reader.getPrompt(); reader.setPrompt(""); final TableDisplay tableDisplay = new TableDisplay(reader); + tableDisplay.setDescription(blurResults.getTotalResults() + " results found in [" + timeInNanos / 1000000.0 + + " ms]."); tableDisplay.setSeperator(" "); try { - long s = System.nanoTime(); - BlurResults blurResults = client.query(tablename, blurQuery); - long e = System.nanoTime(); - long timeInNanos = e - s; - final AtomicBoolean viewing = new AtomicBoolean(true); tableDisplay.addKeyHook(new Runnable() { @@ -100,7 +124,17 @@ public class QueryCommand2 extends Command implements TableFirstArgCommand { } }, 'q'); - renderRowMultiFamily(tableDisplay, blurResults); + RenderType type = getRenderRype(blurResults); + switch (type) { + case ROW_MULTI_FAMILY: + renderRowMultiFamily(tableDisplay, blurResults); + break; + case ROW_SINGLE_FAMILY: + renderRowSingleFamily(tableDisplay, blurResults); + break; + default: + break; + } while (viewing.get()) { synchronized (viewing) { @@ -114,24 +148,24 @@ public class QueryCommand2 extends Command implements TableFirstArgCommand { } finally { try { tableDisplay.close(); - } catch (IOException e) { + } catch (IOException ex) { if (Main.debug) { - e.printStackTrace(); + ex.printStackTrace(); } } try { Thread.sleep(100); - } catch (InterruptedException e) { + } catch (InterruptedException ex) { if (Main.debug) { - e.printStackTrace(); + ex.printStackTrace(); } } try { reader.setPrompt(""); reader.clearScreen(); - } catch (IOException e) { + } catch (IOException ex) { if (Main.debug) { - e.printStackTrace(); + ex.printStackTrace(); } } out.write("\u001B[0m"); @@ -140,48 +174,141 @@ public class QueryCommand2 extends Command implements TableFirstArgCommand { } } - private void renderRowMultiFamily(TableDisplay tableDisplay, BlurResults blurResults) { + private RenderType getRenderRype(BlurResults blurResults) { + Set<String> families = new HashSet<String>(); + for (BlurResult blurResult : blurResults.getResults()) { + families.addAll(getFamily(blurResult.getFetchResult())); + } + if (families.size() > 1) { + return RenderType.ROW_MULTI_FAMILY; + } + return RenderType.ROW_SINGLE_FAMILY; + } + + private Set<String> getFamily(FetchResult fetchResult) { + Set<String> result = new HashSet<String>(); + FetchRowResult rowResult = fetchResult.getRowResult(); + if (rowResult == null) { + FetchRecordResult recordResult = fetchResult.getRecordResult(); + Record record = recordResult.getRecord(); + result.add(record.getFamily()); + } else { + Row row = rowResult.getRow(); + List<Record> records = row.getRecords(); + if (records != null) { + for (Record record : records) { + result.add(record.getFamily()); + } + } + } + return result; + } + + private void renderRowSingleFamily(TableDisplay tableDisplay, BlurResults blurResults) { int line = 0; + tableDisplay.setHeader(0, highlight(getTruncatedVersion("rowid"))); + tableDisplay.setHeader(1, highlight(getTruncatedVersion("recordid"))); + List<String> columnsLabels = new ArrayList<String>(); + for (BlurResult blurResult : blurResults.getResults()) { + FetchResult fetchResult = blurResult.getFetchResult(); + FetchRowResult rowResult = fetchResult.getRowResult(); + if (rowResult == null) { + FetchRecordResult recordResult = fetchResult.getRecordResult(); + String rowid = recordResult.getRowid(); + Record record = recordResult.getRecord(); + String family = record.getFamily(); + if (record.getColumns() != null) { + for (Column column : record.getColumns()) { + addToTableDisplay(columnsLabels, tableDisplay, line, rowid, family, record.getRecordId(), column); + } + } + line++; + } else { + Row row = rowResult.getRow(); + List<Record> records = row.getRecords(); + if (records != null) { + for (Record record : records) { + if (record.getColumns() != null) { + for (Column column : record.getColumns()) { + addToTableDisplay(columnsLabels, tableDisplay, line, row.getId(), record.getFamily(), + record.getRecordId(), column); + } + } + line++; + } + } + } + } + } - tableDisplay.setHeader(0, white("rowid")); - tableDisplay.setHeader(1, white("recordid")); + private void addToTableDisplay(List<String> columnsLabels, TableDisplay tableDisplay, int line, String rowId, + String family, String recordId, Column column) { + String name = family + "." + column.getName(); + int indexOf = columnsLabels.indexOf(name); + if (indexOf < 0) { + indexOf = columnsLabels.size(); + columnsLabels.add(name); + tableDisplay.setHeader(indexOf + 2, highlight(getTruncatedVersion(name))); + } + tableDisplay.set(0, line, white(getTruncatedVersion(rowId))); + tableDisplay.set(1, line, white(getTruncatedVersion(recordId))); + tableDisplay.set(indexOf + 2, line, white(getTruncatedVersion(column.getValue()))); + } - Map<String, List<String>> columnOrder = new HashMap<String, List<String>>(); + private String getTruncatedVersion(String s) { + if (s.length() > _width) { + return s.substring(0, _width - 3) + "..."; + } + return s; + } + private void renderRowMultiFamily(TableDisplay tableDisplay, BlurResults blurResults) { + AtomicInteger line = new AtomicInteger(); + tableDisplay.setHeader(0, highlight(getTruncatedVersion("rowid"))); + tableDisplay.setHeader(1, highlight(getTruncatedVersion("recordid"))); + Map<String, List<String>> columnOrder = new HashMap<String, List<String>>(); for (BlurResult result : blurResults.getResults()) { FetchResult fetchResult = result.getFetchResult(); FetchRowResult rowResult = fetchResult.getRowResult(); if (rowResult != null) { Row row = rowResult.getRow(); String id = row.getId(); - tableDisplay.set(0, line, white(id)); + tableDisplay.set(0, line.get(), white(getTruncatedVersion(id))); List<Record> records = order(row.getRecords()); String currentFamily = "#"; for (Record record : records) { - int c = 2; - List<String> orderedColumns = getOrderColumnValues(record, columnOrder); - String family = record.getFamily(); - tableDisplay.set(1, line, white(record.getRecordId())); - if (!family.equals(currentFamily)) { - List<String> list = columnOrder.get(family); - for (int i = 0; i < list.size(); i++) { - tableDisplay.set(i + c, line, highlight(family + "." + list.get(i))); - } - currentFamily = family; - line++; - } - for (String oc : orderedColumns) { - if (oc != null) { - tableDisplay.set(c, line, white(oc)); - } - c++; - } - line++; + currentFamily = displayRecordInRowMultiFamilyView(tableDisplay, line, columnOrder, currentFamily, record); } } else { - throw new RuntimeException("impl"); + String currentFamily = "#"; + FetchRecordResult recordResult = fetchResult.getRecordResult(); + Record record = recordResult.getRecord(); + currentFamily = displayRecordInRowMultiFamilyView(tableDisplay, line, columnOrder, currentFamily, record); + } + } + } + + private String displayRecordInRowMultiFamilyView(final TableDisplay tableDisplay, final AtomicInteger line, + final Map<String, List<String>> columnOrder, final String currentFamily, final Record record) { + int c = 2; + List<String> orderedColumns = getOrderColumnValues(record, columnOrder); + String family = record.getFamily(); + tableDisplay.set(1, line.get(), white(getTruncatedVersion(record.getRecordId()))); + if (!family.equals(currentFamily)) { + List<String> list = columnOrder.get(family); + for (int i = 0; i < list.size(); i++) { + tableDisplay.set(i + c, line.get(), highlight(getTruncatedVersion(family + "." + list.get(i)))); + } + line.incrementAndGet(); + } + for (String oc : orderedColumns) { + if (oc != null) { + tableDisplay.set(c, line.get(), white(getTruncatedVersion(oc))); } + c++; } + line.incrementAndGet(); + return family; } private String white(String s) { @@ -273,36 +400,18 @@ public class QueryCommand2 extends Command implements TableFirstArgCommand { return map; } - private void lineBreak(PagingPrintWriter out, int maxWidth) throws FinishedException { - for (int i = 0; i < maxWidth; i++) { - out.print('-'); - } - out.println(); - } - - 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); - out.println(" time : " + (timeInNanos / 1000000.0) + " ms"); - if (Main.debug) { - out.println("shardinfo: " + blurResults.getShardInfo()); - } - } - @Override public String description() { - return "Query the named table."; + return "Query the named table. Run -h for full argument list."; } @Override public String usage() { - return "<tablename> <query>"; + return "<tablename> <query> [<options>]"; } @Override public String name() { - return "query2"; + return "query"; } } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/3ec0b122/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandHelper.java ---------------------------------------------------------------------- diff --git a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandHelper.java b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandHelper.java index 576b162..8bb7309 100644 --- a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandHelper.java +++ b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandHelper.java @@ -41,17 +41,18 @@ import org.apache.commons.cli.PosixParser; public class QueryCommandHelper { - private static final String SORT = "sort"; - private static final String FACET = "facet"; - private static final String ROW_ID = "rowId"; - private static final String MINIMUM_NUMBER_OF_RESULTS = "minimumNumberOfResults"; - private static final String MAX_QUERY_TIME = "maxQueryTime"; - private static final String FETCH = "fetch"; - private static final String START = "start"; - private static final String DISABLE_ROW_QUERY = "disableRowQuery"; - private static final String SCORE_TYPE = "scoreType"; - private static final String RECORD_FILTER = "recordFilter"; - private static final String ROW_FILTER = "rowFilter"; + public static final String WIDTH = "width"; + public static final String SORT = "sort"; + public static final String FACET = "facet"; + public static final String ROW_ID = "rowId"; + public static final String MINIMUM_NUMBER_OF_RESULTS = "min"; + public static final String MAX_QUERY_TIME = "max"; + public static final String FETCH = "fetch"; + public static final String START = "start"; + public static final String DISABLE_ROW_QUERY = "disableRowQuery"; + public static final String SCORE_TYPE = "scoreType"; + public static final String RECORD_FILTER = "recordFilter"; + public static final String ROW_FILTER = "rowFilter"; @SuppressWarnings("unchecked") public static BlurQuery getBlurQuery(CommandLine commandLine) { @@ -90,7 +91,11 @@ public class QueryCommandHelper { BlurQuery blurQuery = new BlurQuery(); blurQuery.setQuery(query); - blurQuery.setSelector(new Selector(Main.selector)); + Selector selector = new Selector(Main.selector); + if (!query.isRowQuery()) { + selector.setRecordOnly(true); + } + blurQuery.setSelector(selector); blurQuery.setCacheResult(false); blurQuery.setUseCacheIfPresent(false); @@ -229,6 +234,16 @@ public class QueryCommandHelper { .hasArgs() .withDescription("Specify a sort to be applied to this query <family> <column> [<reverse>].") .create(SORT)); + options.addOption( + OptionBuilder + .withDescription("Displays help for this command.") + .create("h")); + options.addOption( + OptionBuilder + .withArgName(WIDTH) + .hasArgs() + .withDescription("Specify max column width for display.") + .create(WIDTH)); CommandLineParser parser = new PosixParser(); CommandLine cmd = null; http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/3ec0b122/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandOld.java ---------------------------------------------------------------------- diff --git a/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandOld.java b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandOld.java new file mode 100644 index 0000000..1e91501 --- /dev/null +++ b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommandOld.java @@ -0,0 +1,157 @@ +/** + * 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 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; +import org.apache.blur.thrift.generated.BlurQuery; +import org.apache.blur.thrift.generated.BlurResult; +import org.apache.blur.thrift.generated.BlurResults; +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.Query; +import org.apache.blur.thrift.generated.Selector; + +public class QueryCommandOld extends Command implements TableFirstArgCommand { + @Override + 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 queryStr = ""; + for (int i = 2; i < args.length; i++) { + queryStr += args[i] + " "; + } + + BlurQuery blurQuery = new BlurQuery(); + Query query = new Query(); + query.setQuery(queryStr); + blurQuery.setQuery(query); + blurQuery.setSelector(new Selector(Main.selector)); + blurQuery.setCacheResult(false); + blurQuery.setUseCacheIfPresent(false); + + if (Main.highlight) { + blurQuery.getSelector().setHighlightOptions(new HighlightOptions()); + } + + if (Main.debug) { + out.println(blurQuery); + } + + int maxWidth = 100; + ConsoleReader reader = getConsoleReader(); + 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); + long e = System.nanoTime(); + long timeInNanos = e - s; + + printSummary(out, blurResults, maxWidth, timeInNanos); + lineBreak(out, maxWidth); + + int hit = 0; + for (BlurResult result : blurResults.getResults()) { + double score = result.getScore(); + out.println(" hit : " + hit); + out.println(" score : " + score); + if (Main.debug) { + String locationId = result.getLocationId(); + out.println("locationId : " + locationId); + } + FetchResult fetchResult = result.getFetchResult(); + if (Main.debug) { + out.println("deleted : " + fetchResult.isDeleted()); + out.println("exists : " + fetchResult.isExists()); + out.println("table : " + fetchResult.getTable()); + } + FetchRowResult rowResult = fetchResult.getRowResult(); + if (rowResult != null) { + Row row = rowResult.getRow(); + if (row != null) { + GetRowCommand.format(out, rowResult, maxWidth); + } + } + lineBreak(out, maxWidth); + hit++; + } + printSummary(out, blurResults, maxWidth, timeInNanos); + } + + private void lineBreak(PagingPrintWriter out, int maxWidth) throws FinishedException { + for (int i = 0; i < maxWidth; i++) { + out.print('-'); + } + out.println(); + } + + 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); + out.println(" time : " + (timeInNanos / 1000000.0) + " ms"); + if (Main.debug) { + out.println("shardinfo: " + blurResults.getShardInfo()); + } + } + + @Override + public String description() { + return "Query the named table."; + } + + @Override + public String usage() { + return "<tablename> <query>"; + } + + @Override + public String name() { + return "query-old"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/3ec0b122/blur-shell/src/main/java/org/apache/blur/shell/TableDisplay.java ---------------------------------------------------------------------- diff --git a/blur-shell/src/main/java/org/apache/blur/shell/TableDisplay.java b/blur-shell/src/main/java/org/apache/blur/shell/TableDisplay.java index 0cf31cf..22b30eb 100644 --- a/blur-shell/src/main/java/org/apache/blur/shell/TableDisplay.java +++ b/blur-shell/src/main/java/org/apache/blur/shell/TableDisplay.java @@ -126,6 +126,11 @@ public class TableDisplay implements Closeable { private final Thread _inputReaderThread; private final Map<Integer, Runnable> _keyHookMap = new ConcurrentHashMap<Integer, Runnable>(); private final AtomicBoolean _stopReadingInput = new AtomicBoolean(false); + private String _description = ""; + + public void setDescription(String description) { + _description = description; + } public void setSeperator(String seperator) { _seperator = seperator; @@ -207,6 +212,8 @@ public class TableDisplay implements Closeable { private void render(Canvas canvas) throws IOException { canvas.reset(); + canvas.append(_description); + canvas.endLine(); buildHeaderOutput(canvas); buildTableOutput(canvas); canvas.write(); @@ -530,7 +537,7 @@ public class TableDisplay implements Closeable { _line = 0; _screenBuilder.setLength(0); Terminal terminal = _reader.getTerminal(); - _height = terminal.getHeight() - 2; + _height = terminal.getHeight() - 3; _width = terminal.getWidth() - 2; _leftRightMoveSize = _width / 4; }
