ZEPPELIN-189: Add Max number of OQL result to display

This commit is contained in:
tzolov 2015-08-05 10:32:44 +02:00
parent 437c091b42
commit 0ff81c95b5
2 changed files with 47 additions and 18 deletions

View file

@ -42,6 +42,7 @@ import com.gemstone.gemfire.pdx.PdxInstance;
* <ul>
* <li>{@code geode.locator.host} - The Geode Locator {@code <HOST>} to connect to.</li>
* <li>{@code geode.locator.port} - The Geode Locator {@code <PORT>} to connect to.</li>
* <li>{@code geode.max.result} - Max number of OQL result to display.</li>
* </ul>
* <p>
* Sample usages: <br/>
@ -87,6 +88,7 @@ public class GeodeOqlInterpreter extends Interpreter {
private static final String DEFAULT_PORT = "10334";
private static final String DEFAULT_HOST = "localhost";
private static final String DEFAULT_MAX_RESULT = "1000";
private static final char NEWLINE = '\n';
private static final char TAB = '\t';
@ -96,16 +98,22 @@ public class GeodeOqlInterpreter extends Interpreter {
public static final String LOCATOR_HOST = "geode.locator.host";
public static final String LOCATOR_PORT = "geode.locator.port";
public static final String MAX_RESULT = "geode.max.result";
static {
Interpreter.register("oql", "geode", GeodeOqlInterpreter.class.getName(),
Interpreter.register(
"oql",
"geode",
GeodeOqlInterpreter.class.getName(),
new InterpreterPropertyBuilder().add(LOCATOR_HOST, DEFAULT_HOST, "The Geode Locator Host.")
.add(LOCATOR_PORT, DEFAULT_PORT, "The Geode Locator Port").build());
.add(LOCATOR_PORT, DEFAULT_PORT, "The Geode Locator Port")
.add(MAX_RESULT, DEFAULT_MAX_RESULT, "Max number of OQL result to display.").build());
}
private ClientCache clientCache = null;
private QueryService queryService = null;
private Exception exceptionOnConnect;
private int maxResult;
public GeodeOqlInterpreter(Properties property) {
super(property);
@ -125,9 +133,13 @@ public class GeodeOqlInterpreter extends Interpreter {
@Override
public void open() {
logger.info("Geode open connection called!");
try {
maxResult = Integer.valueOf(getProperty(MAX_RESULT));
clientCache = getClientCache();
queryService = clientCache.getQueryService();
exceptionOnConnect = null;
logger.info("Successfully created Geode connection");
} catch (Exception e) {
@ -171,9 +183,12 @@ public class GeodeOqlInterpreter extends Interpreter {
boolean isTableHeaderSet = false;
Iterator<Object> iterator = results.iterator();
while (iterator.hasNext()) {
int rowDisplayCount = 0;
while (iterator.hasNext() && (rowDisplayCount < getMaxResult())) {
Object entry = iterator.next();
rowDisplayCount++;
if (entry instanceof Number) {
handleNumberEntry(isTableHeaderSet, entry, msg);
@ -286,6 +301,10 @@ public class GeodeOqlInterpreter extends Interpreter {
return null;
}
public int getMaxResult() {
return maxResult;
}
// Test only
QueryService getQueryService() {
return this.queryService;

View file

@ -46,34 +46,42 @@ public class GeodeOqlInterpreterTest {
private static final String OQL_QUERY = "select * from /region";
private static Iterator<Object> asIterator(Object... items) {
return new ArrayList<Object>(Arrays.asList(items)).iterator();
}
@Test
public void oqlNumberResponse() throws Exception {
testOql(new ArrayList<Object>(Arrays.asList(66, 67)).iterator(), "Result\n66\n67\n");
testOql(asIterator(66, 67), "Result\n66\n67\n", 10);
testOql(asIterator(66, 67), "Result\n66\n", 1);
}
@Test
public void oqlStructResponse() throws Exception {
String[] fields = new String[] {"field1", "field2"};
Struct struct = new StructImpl(new StructTypeImpl(fields), new String[] {"val1", "val2"});
Struct s1 = new StructImpl(new StructTypeImpl(fields), new String[] {"val11", "val12"});
Struct s2 = new StructImpl(new StructTypeImpl(fields), new String[] {"val21", "val22"});
testOql(new ArrayList<Object>(Arrays.asList(struct)).iterator(),
"field1\tfield2\t\nval1\tval2\t\n");
testOql(asIterator(s1, s2), "field1\tfield2\t\nval11\tval12\t\nval21\tval22\t\n", 10);
testOql(asIterator(s1, s2), "field1\tfield2\t\nval11\tval12\t\n", 1);
}
@Test
public void oqlStructResponseWithReservedCharacters() throws Exception {
String[] fields = new String[] {"fi\teld1", "f\nield2"};
Struct struct = new StructImpl(new StructTypeImpl(fields), new String[] {"v\nal\t1", "val2"});
Struct s1 = new StructImpl(new StructTypeImpl(fields), new String[] {"v\nal\t1", "val2"});
testOql(new ArrayList<Object>(Arrays.asList(struct)).iterator(),
"fi eld1\tf ield2\t\nv al 1\tval2\t\n");
testOql(asIterator(s1), "fi eld1\tf ield2\t\nv al 1\tval2\t\n", 10);
}
@Test
public void oqlPdxInstanceResponse() throws Exception {
ByteArrayInputStream bais = new ByteArrayInputStream("koza\tboza\n".getBytes());
PdxInstance pdxInstance = new PdxInstanceImpl(new PdxType(), new DataInputStream(bais), 4);
testOql(new ArrayList<Object>(Arrays.asList(pdxInstance)).iterator(), "\n\n");
PdxInstance pdx1 = new PdxInstanceImpl(new PdxType(), new DataInputStream(bais), 4);
PdxInstance pdx2 = new PdxInstanceImpl(new PdxType(), new DataInputStream(bais), 4);
testOql(asIterator(pdx1, pdx2), "\n\n\n", 10);
testOql(asIterator(pdx1, pdx2), "\n\n", 1);
}
private static class DummyUnspportedType {
@ -87,11 +95,12 @@ public class GeodeOqlInterpreterTest {
public void oqlUnsupportedTypeResponse() throws Exception {
DummyUnspportedType unspported1 = new DummyUnspportedType();
DummyUnspportedType unspported2 = new DummyUnspportedType();
testOql(new ArrayList<Object>(Arrays.asList(unspported1, unspported2)).iterator(),
"Unsuppoted Type\n" + unspported1.toString() + "\n" + unspported1.toString() + "\n");
testOql(asIterator(unspported1, unspported2), "Unsuppoted Type\n" + unspported1.toString()
+ "\n" + unspported1.toString() + "\n", 10);
}
private void testOql(Iterator<Object> queryResponseIterator, String expectedOutput)
private void testOql(Iterator<Object> queryResponseIterator, String expectedOutput, int maxResult)
throws Exception {
GeodeOqlInterpreter spyGeodeOqlInterpreter = spy(new GeodeOqlInterpreter(new Properties()));
@ -99,6 +108,7 @@ public class GeodeOqlInterpreterTest {
QueryService mockQueryService = mock(QueryService.class, RETURNS_DEEP_STUBS);
when(spyGeodeOqlInterpreter.getQueryService()).thenReturn(mockQueryService);
when(spyGeodeOqlInterpreter.getMaxResult()).thenReturn(maxResult);
@SuppressWarnings("unchecked")
SelectResults<Object> mockResults = mock(SelectResults.class);
@ -132,8 +142,8 @@ public class GeodeOqlInterpreterTest {
GeodeOqlInterpreter spyGeodeOqlInterpreter = spy(new GeodeOqlInterpreter(new Properties()));
when(spyGeodeOqlInterpreter.getQueryService())
.thenThrow(new RuntimeException("Expected Test Exception!"));
when(spyGeodeOqlInterpreter.getQueryService()).thenThrow(
new RuntimeException("Expected Test Exception!"));
InterpreterResult interpreterResult = spyGeodeOqlInterpreter.interpret(OQL_QUERY, null);