Repository: incubator-blur Updated Branches: refs/heads/apache-blur-0.2 79803a133 -> 198cca0e7
Starting a new query display command for the shell. Project: http://git-wip-us.apache.org/repos/asf/incubator-blur/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-blur/commit/a088c1c1 Tree: http://git-wip-us.apache.org/repos/asf/incubator-blur/tree/a088c1c1 Diff: http://git-wip-us.apache.org/repos/asf/incubator-blur/diff/a088c1c1 Branch: refs/heads/apache-blur-0.2 Commit: a088c1c1ea2f15925a3ac196c333c5dbe9408255 Parents: 79803a1 Author: Aaron McCurry <[email protected]> Authored: Thu Jun 19 18:50:09 2014 -0400 Committer: Aaron McCurry <[email protected]> Committed: Thu Jun 19 18:50:09 2014 -0400 ---------------------------------------------------------------------- .../main/java/org/apache/blur/shell/Main.java | 1 + .../org/apache/blur/shell/QueryCommand2.java | 194 +++++++++++++++++++ .../org/apache/blur/shell/TableDisplay.java | 68 +++++-- 3 files changed, 246 insertions(+), 17 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a088c1c1/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 7c912c1..5c94ba1 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 @@ -696,6 +696,7 @@ public class Main { register(builder, new LogResetCommand()); register(builder, new RemoveShardServerCommand()); register(builder, new OptimizeTableCommand()); + register(builder, new QueryCommand2()); commands = builder.build(); } http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a088c1c1/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 new file mode 100644 index 0000000..bce3a16 --- /dev/null +++ b/blur-shell/src/main/java/org/apache/blur/shell/QueryCommand2.java @@ -0,0 +1,194 @@ +/** + * 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.IOException; +import java.io.PrintWriter; +import java.util.List; +import java.util.concurrent.atomic.AtomicBoolean; + +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.Column; +import org.apache.blur.thrift.generated.ErrorType; +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.Record; +import org.apache.blur.thrift.generated.Row; +import org.apache.blur.thrift.generated.Query; +import org.apache.blur.thrift.generated.Selector; + +public class QueryCommand2 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); + } + + ConsoleReader reader = getConsoleReader(); + if (reader == null) { + throw new BlurException("This command can only be run with a proper jline environment.", null, ErrorType.UNKNOWN); + } + String prompt = reader.getPrompt(); + reader.setPrompt(""); + TableDisplay tableDisplay = new TableDisplay(reader); + 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() { + @Override + public void run() { + synchronized (viewing) { + viewing.set(false); + viewing.notify(); + } + } + }, 'q'); + + int line = 0; + + tableDisplay.setHeader(0, "rowid"); + tableDisplay.setHeader(1, "family"); + tableDisplay.setHeader(2, "recordid"); + + for (BlurResult result : blurResults.getResults()) { + FetchResult fetchResult = result.getFetchResult(); + FetchRowResult rowResult = fetchResult.getRowResult(); + if (rowResult != null) { + Row row = rowResult.getRow(); + String id = row.getId(); + List<Record> records = row.getRecords(); + for (Record record : records) { + tableDisplay.set(0, line, id); + tableDisplay.set(1, line, record.getFamily()); + tableDisplay.set(2, line, record.getRecordId()); + int c = 3; + for (Column column : record.getColumns()) { + tableDisplay.set(c, line, column.getName() + " " + column.getValue()); + c++; + } + line++; + } + } else { + throw new RuntimeException("impl"); + } + } + while (viewing.get()) { + synchronized (viewing) { + try { + viewing.wait(); + } catch (InterruptedException ex) { + throw new RuntimeException(ex); + } + } + } + } finally { + reader.setPrompt(prompt); + try { + tableDisplay.close(); + } catch (IOException ex) { + ex.printStackTrace(); + } + } + } + + 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 "query2"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-blur/blob/a088c1c1/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 7a0240c..77013a6 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 @@ -20,7 +20,7 @@ import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.Writer; -import java.util.Random; +import java.util.Map; import java.util.Timer; import java.util.TimerTask; import java.util.concurrent.ConcurrentHashMap; @@ -42,8 +42,8 @@ public class TableDisplay implements Closeable { ConsoleReader reader = new ConsoleReader(); TableDisplay tableDisplay = new TableDisplay(reader); - tableDisplay.setSeperator(" "); - Random random = new Random(); + tableDisplay.setSeperator("|"); +// Random random = new Random(); int maxX = 20; int maxY = 100; tableDisplay.setHeader(0, ""); @@ -55,33 +55,61 @@ public class TableDisplay implements Closeable { tableDisplay.set(0, i, i); } + final AtomicBoolean running = new AtomicBoolean(true); + + tableDisplay.addKeyHook(new Runnable() { + @Override + public void run() { + synchronized (running) { + running.set(false); + running.notifyAll(); + } + } + }, 'q'); + try { - int i = 0; - while (true) { - if (i >= 100000) { - i = 0; + // int i = 0; + // while (true) { + // if (i >= 100000) { + // i = 0; + // } + // tableDisplay.set(random.nextInt(maxX) + 1, random.nextInt(maxY), + // random.nextLong()); + // Thread.sleep(3000); + // i++; + // } + for (int x = 0; x < maxX; x++) { + for (int y = 0; y < maxY; y++) { + tableDisplay.set(x, y, x + "," + y); + } + } + while (running.get()) { + synchronized (running) { + running.wait(1000); } - tableDisplay.set(random.nextInt(maxX) + 1, random.nextInt(maxY), random.nextLong()); - Thread.sleep(1); - i++; } } finally { tableDisplay.close(); } } + public void addKeyHook(Runnable runnable, int c) { + _keyHookMap.put(c, runnable); + } + private static final String SEP = "|"; private final ConsoleReader _reader; private final ConcurrentMap<Key, Object> _values = new ConcurrentHashMap<TableDisplay.Key, Object>(); private final ConcurrentMap<Integer, String> _header = new ConcurrentHashMap<Integer, String>(); private final ConcurrentMap<Integer, Integer> _maxWidth = new ConcurrentHashMap<Integer, Integer>(); - private final AtomicBoolean _running = new AtomicBoolean(); + private final AtomicBoolean _running = new AtomicBoolean(true); private final Timer _timer; private final Canvas _canvas; private int _maxY; private int _maxX; private String _seperator = SEP; private final Thread _inputReaderThread; + private final Map<Integer, Runnable> _keyHookMap = new ConcurrentHashMap<Integer, Runnable>(); public void setSeperator(String seperator) { _seperator = seperator; @@ -107,7 +135,7 @@ public class TableDisplay implements Closeable { try { InputStream input = _reader.getInput(); int read; - while ((read = input.read()) != -1) { + while ((read = input.read()) != -1 && _running.get()) { if (read == 27) { if (input.read() == 91) { read = input.read(); @@ -132,6 +160,11 @@ public class TableDisplay implements Closeable { break; } } + } else { + Runnable runnable = _keyHookMap.get(read); + if (runnable != null) { + runnable.run(); + } } } } catch (IOException e) { @@ -244,14 +277,14 @@ public class TableDisplay implements Closeable { } private void setMaxY(int y) { - if (_maxY < y) { - _maxY = y; + if (_maxY < y + 1) { + _maxY = y + 1; } } private void setMaxX(int x) { - if (_maxX < x) { - _maxX = x; + if (_maxX < x + 1) { + _maxX = x + 1; } } @@ -347,7 +380,8 @@ public class TableDisplay implements Closeable { } public void endLine() { - if (_line + _posY < _height && _line >= _posY) { + int pos = _line - _posY; + if (pos >= 0 && pos < _height) { int end = _posX + _width; _builder.append(_lineBuilder.substring(_posX, Math.min(_lineBuilder.length(), end))); _builder.append('\n');
