From 4e6c0f6c6bc51ab0e35dbdd0b07017085682bc95 Mon Sep 17 00:00:00 2001 From: Sylvain Lebresne Date: Thu, 19 May 2011 13:53:06 +0000 Subject: [PATCH] Allow limiting row slices in the cli patch by slebresne; reviewed by xedin for CASSANDRA-2646 git-svn-id: https://svn.apache.org/repos/asf/cassandra/branches/cassandra-0.7@1124780 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/cassandra/cli/Cli.g | 8 ++-- .../org/apache/cassandra/cli/CliClient.java | 45 +++++++++++++++---- 2 files changed, 40 insertions(+), 13 deletions(-) diff --git a/src/java/org/apache/cassandra/cli/Cli.g b/src/java/org/apache/cassandra/cli/Cli.g index 44b6212b4d..e954838c69 100644 --- a/src/java/org/apache/cassandra/cli/Cli.g +++ b/src/java/org/apache/cassandra/cli/Cli.g @@ -228,10 +228,10 @@ exitStatement ; getStatement - : GET columnFamilyExpr ('AS' typeIdentifier)? - -> ^(NODE_THRIFT_GET columnFamilyExpr ( ^(CONVERT_TO_TYPE typeIdentifier) )? ) - | GET columnFamily 'WHERE' getCondition ('AND' getCondition)* ('LIMIT' limit=IntegerLiteral)* - -> ^(NODE_THRIFT_GET_WITH_CONDITIONS columnFamily ^(CONDITIONS getCondition+) ^(NODE_LIMIT $limit)*) + : GET columnFamilyExpr ('AS' typeIdentifier)? ('LIMIT' limit=IntegerLiteral)? + -> ^(NODE_THRIFT_GET columnFamilyExpr ( ^(CONVERT_TO_TYPE typeIdentifier) )? ^(NODE_LIMIT $limit)?) + | GET columnFamily 'WHERE' getCondition ('AND' getCondition)* ('LIMIT' limit=IntegerLiteral)? + -> ^(NODE_THRIFT_GET_WITH_CONDITIONS columnFamily ^(CONDITIONS getCondition+) ^(NODE_LIMIT $limit)?) ; getCondition diff --git a/src/java/org/apache/cassandra/cli/CliClient.java b/src/java/org/apache/cassandra/cli/CliClient.java index dbaff6da3d..2611e08e50 100644 --- a/src/java/org/apache/cassandra/cli/CliClient.java +++ b/src/java/org/apache/cassandra/cli/CliClient.java @@ -306,7 +306,7 @@ public class CliClient extends CliUserHelp sessionState.out.println(String.format("%s removed.", (columnSpecCnt == 0) ? "row" : "column")); } - private void doSlice(String keyspace, ByteBuffer key, String columnFamily, byte[] superColumnName) + private void doSlice(String keyspace, ByteBuffer key, String columnFamily, byte[] superColumnName, int limit) throws InvalidRequestException, UnavailableException, TimedOutException, TException, IllegalAccessException, NotFoundException, InstantiationException, NoSuchFieldException { @@ -314,7 +314,7 @@ public class CliClient extends CliUserHelp if(superColumnName != null) parent.setSuper_column(superColumnName); - SliceRange range = new SliceRange(ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, 1000000); + SliceRange range = new SliceRange(ByteBufferUtil.EMPTY_BYTE_BUFFER, ByteBufferUtil.EMPTY_BYTE_BUFFER, false, limit); List columns = thriftClient.get_slice(key, parent, new SlicePredicate().setColumn_names(null).setSlice_range(range), consistencyLevel); AbstractType validator; @@ -401,10 +401,39 @@ public class CliClient extends CliUserHelp byte[] superColumnName = null; ByteBuffer columnName; + Tree typeTree = null; + Tree limitTree = null; + + int limit = 1000000; + + if (statement.getChildCount() >= 2) + { + if (statement.getChild(1).getType() == CliParser.CONVERT_TO_TYPE) + { + typeTree = statement.getChild(1).getChild(0); + if (statement.getChildCount() == 3) + limitTree = statement.getChild(2).getChild(0); + } + else + { + limitTree = statement.getChild(1).getChild(0); + } + } + + if (limitTree != null) + { + limit = Integer.parseInt(limitTree.getText()); + + if (limit == 0) + { + throw new IllegalArgumentException("LIMIT should be greater than zero."); + } + } + // table.cf['key'] -- row slice if (columnSpecCnt == 0) { - doSlice(keySpace, key, columnFamily, superColumnName); + doSlice(keySpace, key, columnFamily, superColumnName, limit); return; } // table.cf['key']['column'] -- slice of a super, or get of a standard @@ -415,7 +444,7 @@ public class CliClient extends CliUserHelp if (isSuper) { superColumnName = columnName.array(); - doSlice(keySpace, key, columnFamily, superColumnName); + doSlice(keySpace, key, columnFamily, superColumnName, limit); return; } } @@ -433,7 +462,7 @@ public class CliClient extends CliUserHelp } AbstractType validator = getValidatorForValue(cfDef, TBaseHelper.byteBufferToByteArray(columnName)); - + // Perform a get() ColumnPath path = new ColumnPath(columnFamily); if(superColumnName != null) path.setSuper_column(superColumnName); @@ -451,14 +480,12 @@ public class CliClient extends CliUserHelp byte[] columnValue = column.getValue(); String valueAsString; - + // we have ^(CONVERT_TO_TYPE ) inside of GET statement // which means that we should try to represent byte[] value according // to specified type - if (statement.getChildCount() == 2) + if (typeTree != null) { - // getting ^(CONVERT_TO_TYPE ) tree - Tree typeTree = statement.getChild(1).getChild(0); // .getText() will give us String typeName = CliUtils.unescapeSQLString(typeTree.getText()); // building AbstractType from