cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r1005912 - in /cassandra/trunk: CHANGES.txt src/java/org/apache/cassandra/cli/Cli.g src/java/org/apache/cassandra/cli/CliClient.java src/java/org/apache/cassandra/cli/CliCompiler.java
Date Fri, 08 Oct 2010 17:16:42 GMT
Author: jbellis
Date: Fri Oct  8 17:16:42 2010
New Revision: 1005912

URL: http://svn.apache.org/viewvc?rev=1005912&view=rev
Log:
add ColumnDef support to cli.
patch by Pavel Yaskevich; reviewed by jbellis for CASSANDRA-1583

Modified:
    cassandra/trunk/CHANGES.txt
    cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g
    cassandra/trunk/src/java/org/apache/cassandra/cli/CliClient.java
    cassandra/trunk/src/java/org/apache/cassandra/cli/CliCompiler.java

Modified: cassandra/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/cassandra/trunk/CHANGES.txt?rev=1005912&r1=1005911&r2=1005912&view=diff
==============================================================================
--- cassandra/trunk/CHANGES.txt (original)
+++ cassandra/trunk/CHANGES.txt Fri Oct  8 17:16:42 2010
@@ -22,6 +22,7 @@ dev
  * Disallow bootstrap to an in-use token (CASSANDRA-1561)
  * Allow dynamic secondary index creation and destruction (CASSANDRA-1532)
  * log auto-guessed memtable thresholds (CASSANDRA-1595)
+ * add ColumnDef support to cli (CASSANDRA-1583)
 
 
 0.7-beta2

Modified: cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g?rev=1005912&r1=1005911&r2=1005912&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g Fri Oct  8 17:16:42 2010
@@ -59,6 +59,10 @@ tokens {
     NODE_ID_LIST;
     NODE_NEW_CF_ACCESS;
     NODE_NEW_KEYSPACE_ACCESS;
+    
+    ARRAY;
+    HASH;
+    PAIR;
 }
 
 @parser::header {
@@ -113,32 +117,55 @@ statement
     ;
 
 connectStatement
-    : K_CONNECT host SLASH port -> ^(NODE_CONNECT host port)
-    | K_CONNECT ipaddr SLASH port -> ^(NODE_CONNECT ipaddr port)
+    : K_CONNECT host SLASH port 
+        -> ^(NODE_CONNECT host port)
+    | K_CONNECT ipaddr SLASH port 
+        -> ^(NODE_CONNECT ipaddr port)
     ;
 
 helpStatement
-    : K_HELP K_HELP -> ^(NODE_HELP NODE_HELP)
-    | K_HELP K_CONNECT -> ^(NODE_HELP NODE_CONNECT)
-    | K_HELP K_USE -> ^(NODE_HELP NODE_USE_TABLE)
-    | K_HELP K_DESCRIBE K_TABLE -> ^(NODE_HELP NODE_DESCRIBE_TABLE)
-    | K_HELP K_EXIT -> ^(NODE_HELP NODE_EXIT)
-    | K_HELP K_QUIT -> ^(NODE_HELP NODE_EXIT)
-    | K_HELP K_SHOW K_CLUSTER K_NAME -> ^(NODE_HELP NODE_SHOW_CLUSTER_NAME)
-    | K_HELP K_SHOW K_TABLES -> ^(NODE_HELP NODE_SHOW_TABLES)
-    | K_HELP K_SHOW K_VERSION -> ^(NODE_HELP NODE_SHOW_VERSION)
-    | K_HELP K_CREATE K_TABLE -> ^(NODE_HELP NODE_ADD_KEYSPACE)
-    | K_HELP K_CREATE K_COLUMN K_FAMILY -> ^(NODE_HELP NODE_ADD_COLUMN_FAMILY)
-    | K_HELP K_DROP K_TABLE -> ^(NODE_HELP NODE_DEL_KEYSPACE)
-    | K_HELP K_DROP K_COLUMN K_FAMILY -> ^(NODE_HELP NODE_DEL_COLUMN_FAMILY)
-    | K_HELP K_RENAME K_TABLE -> ^(NODE_HELP NODE_RENAME_KEYSPACE)
-    | K_HELP K_RENAME K_COLUMN K_FAMILY -> ^(NODE_HELP NODE_RENAME_COLUMN_FAMILY)
-    | K_HELP K_GET -> ^(NODE_HELP NODE_THRIFT_GET)
-    | K_HELP K_SET -> ^(NODE_HELP NODE_THRIFT_SET)
-    | K_HELP K_DEL -> ^(NODE_HELP NODE_THRIFT_DEL)
-    | K_HELP K_COUNT -> ^(NODE_HELP NODE_THRIFT_COUNT)
-    | K_HELP -> ^(NODE_HELP)
-    | '?'    -> ^(NODE_HELP)
+    : K_HELP K_HELP 
+        -> ^(NODE_HELP NODE_HELP)
+    | K_HELP K_CONNECT 
+        -> ^(NODE_HELP NODE_CONNECT)
+    | K_HELP K_USE 
+        -> ^(NODE_HELP NODE_USE_TABLE)
+    | K_HELP K_DESCRIBE K_TABLE 
+        -> ^(NODE_HELP NODE_DESCRIBE_TABLE)
+    | K_HELP K_EXIT 
+        -> ^(NODE_HELP NODE_EXIT)
+    | K_HELP K_QUIT 
+        -> ^(NODE_HELP NODE_EXIT)
+    | K_HELP K_SHOW K_CLUSTER K_NAME 
+        -> ^(NODE_HELP NODE_SHOW_CLUSTER_NAME)
+    | K_HELP K_SHOW K_TABLES 
+        -> ^(NODE_HELP NODE_SHOW_TABLES)
+    | K_HELP K_SHOW K_VERSION 
+        -> ^(NODE_HELP NODE_SHOW_VERSION)
+    | K_HELP K_CREATE K_TABLE 
+        -> ^(NODE_HELP NODE_ADD_KEYSPACE)
+    | K_HELP K_CREATE K_COLUMN K_FAMILY 
+        -> ^(NODE_HELP NODE_ADD_COLUMN_FAMILY)
+    | K_HELP K_DROP K_TABLE 
+        -> ^(NODE_HELP NODE_DEL_KEYSPACE)
+    | K_HELP K_DROP K_COLUMN K_FAMILY 
+        -> ^(NODE_HELP NODE_DEL_COLUMN_FAMILY)
+    | K_HELP K_RENAME K_TABLE 
+        -> ^(NODE_HELP NODE_RENAME_KEYSPACE)
+    | K_HELP K_RENAME K_COLUMN K_FAMILY 
+        -> ^(NODE_HELP NODE_RENAME_COLUMN_FAMILY)
+    | K_HELP K_GET 
+        -> ^(NODE_HELP NODE_THRIFT_GET)
+    | K_HELP K_SET 
+        -> ^(NODE_HELP NODE_THRIFT_SET)
+    | K_HELP K_DEL 
+        -> ^(NODE_HELP NODE_THRIFT_DEL)
+    | K_HELP K_COUNT 
+        -> ^(NODE_HELP NODE_THRIFT_COUNT)
+    | K_HELP 
+        -> ^(NODE_HELP)
+    | '?'    
+        -> ^(NODE_HELP)
     ;
 
 exitStatement
@@ -147,19 +174,23 @@ exitStatement
     ;
 
 getStatement
-    : K_GET columnFamilyExpr -> ^(NODE_THRIFT_GET columnFamilyExpr)
+    : K_GET columnFamilyExpr 
+        -> ^(NODE_THRIFT_GET columnFamilyExpr)
     ;
 
 setStatement
-    : K_SET columnFamilyExpr '=' value -> ^(NODE_THRIFT_SET columnFamilyExpr value)
+    : K_SET columnFamilyExpr '=' value 
+        -> ^(NODE_THRIFT_SET columnFamilyExpr value)
     ;
 
 countStatement
-    : K_COUNT columnFamilyExpr -> ^(NODE_THRIFT_COUNT columnFamilyExpr)
+    : K_COUNT columnFamilyExpr 
+        -> ^(NODE_THRIFT_COUNT columnFamilyExpr)
     ;
 
 delStatement
-    : K_DEL columnFamilyExpr -> ^(NODE_THRIFT_DEL columnFamilyExpr)
+    : K_DEL columnFamilyExpr 
+        -> ^(NODE_THRIFT_DEL columnFamilyExpr)
     ;
 
 showStatement
@@ -169,55 +200,95 @@ showStatement
     ;
 
 showClusterName
-    : K_SHOW K_CLUSTER K_NAME -> ^(NODE_SHOW_CLUSTER_NAME)
+    : K_SHOW K_CLUSTER K_NAME 
+        -> ^(NODE_SHOW_CLUSTER_NAME)
     ;
 
 addKeyspace
-    : K_CREATE K_TABLE keyValuePairExpr -> ^(NODE_ADD_KEYSPACE keyValuePairExpr)
+    : K_CREATE K_TABLE keyValuePairExpr 
+        -> ^(NODE_ADD_KEYSPACE keyValuePairExpr)
     ;
 
 addColumnFamily
-    : K_CREATE K_COLUMN K_FAMILY keyValuePairExpr -> ^(NODE_ADD_COLUMN_FAMILY keyValuePairExpr)
+    : K_CREATE K_COLUMN K_FAMILY keyValuePairExpr 
+        -> ^(NODE_ADD_COLUMN_FAMILY keyValuePairExpr)
     ;
 
 delKeyspace
-    : K_DROP K_TABLE keyspace -> ^(NODE_DEL_KEYSPACE keyspace)
+    : K_DROP K_TABLE keyspace 
+        -> ^(NODE_DEL_KEYSPACE keyspace)
     ;
 
 delColumnFamily
-    : K_DROP K_COLUMN K_FAMILY columnFamily -> ^(NODE_DEL_COLUMN_FAMILY columnFamily)
+    : K_DROP K_COLUMN K_FAMILY columnFamily 
+        -> ^(NODE_DEL_COLUMN_FAMILY columnFamily)
     ;
 
 renameKeyspace
-    : K_RENAME K_TABLE keyspace keyspaceNewName -> ^(NODE_RENAME_KEYSPACE keyspace keyspaceNewName)
+    : K_RENAME K_TABLE keyspace keyspaceNewName 
+        -> ^(NODE_RENAME_KEYSPACE keyspace keyspaceNewName)
     ;
 
 renameColumnFamily
-    : K_RENAME K_COLUMN K_FAMILY columnFamily newColumnFamily -> ^(NODE_RENAME_COLUMN_FAMILY
columnFamily newColumnFamily)
+    : K_RENAME K_COLUMN K_FAMILY columnFamily newColumnFamily 
+        -> ^(NODE_RENAME_COLUMN_FAMILY columnFamily newColumnFamily)
     ;
 
 
 showVersion
-    : K_SHOW K_VERSION -> ^(NODE_SHOW_VERSION)
+    : K_SHOW K_VERSION 
+        -> ^(NODE_SHOW_VERSION)
     ;
 
 showTables
-    : K_SHOW K_TABLES -> ^(NODE_SHOW_TABLES)
+    : K_SHOW K_TABLES 
+        -> ^(NODE_SHOW_TABLES)
     ;
 
 describeTable
-    : K_DESCRIBE K_TABLE table -> ^(NODE_DESCRIBE_TABLE table);
+    : K_DESCRIBE K_TABLE table 
+        -> ^(NODE_DESCRIBE_TABLE table)
+    ;
     
 useTable
-    : K_USE table ( username )? ( password )? -> ^(NODE_USE_TABLE table ( username )?
( password )?);
+    : K_USE table ( username )? ( password )? 
+        -> ^(NODE_USE_TABLE table ( username )? ( password )?)
+    ;
 
 
 keyValuePairExpr
-    : objectName 
-            ( (K_AND|K_WITH) a+=attname '=' b+=attvaluestring)*
-            ( (K_AND|K_WITH) c+=attname '=' d+=attvalueint)*
-            -> ^(NODE_NEW_KEYSPACE_ACCESS objectName ($a $b)* ($c $d)*);
+    : objectName ( (K_AND | K_WITH) keyValuePair )* 
+        -> ^(NODE_NEW_KEYSPACE_ACCESS objectName ( keyValuePair )* )
+    ;
             
+keyValuePair 
+    : attr_name '=' attrValue -> attr_name attrValue
+    ;
+
+attrValue
+    : arrayConstruct
+    | attrValueString
+    | attrValueInt
+    ;
+
+
+arrayConstruct 
+    : '[' (hashConstruct ','?)+ ']'
+        -> ^(ARRAY (hashConstruct)+)
+    ; 
+
+hashConstruct 
+    : '{' hashElementPair (',' hashElementPair)* '}'
+        -> ^(HASH (hashElementPair)+)
+    ;
+
+hashElementPair
+    : rowKey ':' value
+        -> ^(PAIR rowKey value)
+    ;
+
+
+
 columnFamilyExpr
     : columnFamily '[' rowKey ']' 
         ( '[' a+=columnOrSuperColumn ']' 
@@ -230,11 +301,11 @@ table: Identifier;
 
 columnName: Identifier;
 
-attname: Identifier;
+attr_name: Identifier;
 
-attvaluestring: (Identifier | StringLiteral);
+attrValueString: (Identifier | StringLiteral);
       
-attvalueint: IntegerLiteral;
+attrValueInt: IntegerLiteral;
   
 objectName: Identifier;
 
@@ -246,7 +317,7 @@ replication_factor: IntegerLiteral;
 
 keyspaceNewName: Identifier;
 
-comparator:  StringLiteral;
+comparator: StringLiteral;
       
 command: Identifier;
 

Modified: cassandra/trunk/src/java/org/apache/cassandra/cli/CliClient.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cli/CliClient.java?rev=1005912&r1=1005911&r2=1005912&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cli/CliClient.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cli/CliClient.java Fri Oct  8 17:16:42 2010
@@ -48,7 +48,8 @@ public class CliClient 
         PRELOAD_ROW_CACHE,
         KEY_CACHE_SIZE,
         READ_REPAIR_CHANCE,
-        GC_GRACE_SECONDS
+        GC_GRACE_SECONDS,
+        COLUMN_METADATA
     }
 
     /*
@@ -243,10 +244,17 @@ public class CliClient 
                 css_.out.println("    - key_cache_size: Number of keys to cache");
                 css_.out.println("    - read_repair_chance: Valid values for this attribute
are any number");
                 css_.out.println("                          between 0.0 and 1.0\n");
+                css_.out.println("    - column_metadata: Metadata which describes columns
of column family.");
+                css_.out.println("        Supported format is [{ k:v, k:v, ... }, { ... },
...]");
+                css_.out.println("        Valid attributes: column_name, validation_class
(see comparator),");
+                css_.out.println("                          index_type (integer), index_name.");
                 css_.out.println("example:\n");
                 css_.out.println("create column family bar with column_type = 'Super' and
comparator = 'AsciiType'");
                 css_.out.println("      and rows_cached = 10000");
                 css_.out.println("create column family baz with comparator = 'LongType' and
rows_cached = 10000");
+                css_.out.print("create column family foo with comparator=LongType and column_metadata=");
+                css_.out.print("[{ column_name:Test, validation_class:IntegerType, index_type:0,
index_name:IdxName");
+                css_.out.println("}, { column_name:'other name', validation_class:LongType
}]");
                 break;
                 
             case CliParser.NODE_RENAME_KEYSPACE:
@@ -724,18 +732,18 @@ public class CliClient 
         {
             return;
         }
+
         CommonTree newCFTree = (CommonTree)ast.getChild(0);
         //parser send the keyspace, plus a series of key value pairs, ie should always be
an odd number...
         assert(newCFTree.getChildCount() > 0);
         assert(newCFTree.getChildCount() % 2 == 1);
 
-
         /*
          * first value is the keyspace name, after that it is all key=value
          */
-        String columnName = newCFTree.getChild(0).getText();
-        int argumentLength = newCFTree.getChildCount();
-        CfDef cfDef = new CfDef(keySpace, columnName);        
+        final String columnName = newCFTree.getChild(0).getText();
+        final int argumentLength = newCFTree.getChildCount();
+        final CfDef cfDef = new CfDef(keySpace, columnName);        
 
         for(int i = 1; i < argumentLength; i = i + 2)
         {
@@ -781,6 +789,17 @@ public class CliClient 
                 cfDef.setGc_grace_seconds(Integer.parseInt(mValue));
                 break;
 
+            case COLUMN_METADATA:
+                final Tree arrayOfMetaAttributes = newCFTree.getChild(i + 1);
+                
+                if (!arrayOfMetaAttributes.getText().equals("ARRAY"))
+                {
+                    throw new RuntimeException("'column_metadata' format - [{ k:v, k:v, ..},
{ ... }, ...]");
+                }
+                
+                cfDef.setColumn_metadata(getCFColumnMetaFromTree(arrayOfMetaAttributes));
+                break;
+            
             default:
                 //must match one of the above or we'd throw an exception at the valueOf statement
above.
                 assert(false);
@@ -1095,6 +1114,95 @@ public class CliClient 
         return ksd.cf_defs.get(idx);
     }
 
+    /**
+     * Used to parse meta tree and compile meta attributes into List<ColumnDef>
+     * @param meta (Tree representing Array of the hashes with metadata attributes)
+     * @return List<ColumnDef> List of the ColumnDef's
+     * 
+     * meta is in following format - ^(ARRAY ^(HASH ^(PAIR .. ..) ^(PAIR .. ..)) ^(HASH ...))
+     */
+    private List<ColumnDef> getCFColumnMetaFromTree(final Tree meta) {
+        // this list will be returned
+        final List<ColumnDef> columnDefinitions = new ArrayList<ColumnDef>();
+        
+        // each child node is a ^(HASH ...)
+        for (int i = 0; i < meta.getChildCount(); i++)
+        {
+            Tree metaHash = meta.getChild(i);
+
+            final ColumnDef columnDefinition = new ColumnDef();
+            
+            // each child node is ^(PAIR $key $value)
+            for (int j = 0; j < metaHash.getChildCount(); j++)
+            {
+                Tree metaPair = metaHash.getChild(j);
+
+                // current $key
+                String metaKey = CliUtils.unescapeSQLString(metaPair.getChild(0).getText());
+                // current $value
+                String metaVal = CliUtils.unescapeSQLString(metaPair.getChild(1).getText());
+
+                if (metaKey.equals("column_name"))
+                {
+                    try
+                    {
+                        columnDefinition.setName(metaVal.getBytes("UTF-8"));
+                    }
+                    catch (UnsupportedEncodingException e)
+                    {
+                        throw new RuntimeException(e.getMessage(), e);
+                    }
+                }
+                else if (metaKey.equals("validation_class"))
+                {
+                    columnDefinition.setValidation_class(metaVal);
+                }
+                else if (metaKey.equals("index_type"))
+                {
+                    columnDefinition.setIndex_type(getIndexTypeFromString(metaVal));
+                }
+                else if (metaKey.equals("index_name"))
+                {
+                    columnDefinition.setIndex_name(metaVal);    
+                }
+            }
+
+            // validating columnDef structure, 'name' and 'validation_class' must be set

+            try
+            {
+                columnDefinition.validate();
+            }
+            catch (TException e)
+            {
+                throw new RuntimeException(e.getMessage(), e);
+            }
+
+            columnDefinitions.add(columnDefinition);
+        }
+
+        return columnDefinitions;
+    }
+
+    private IndexType getIndexTypeFromString(final String indexTypeAsString) {
+        final Integer indexTypeId;
+        final IndexType indexType;
+
+        try {
+            indexTypeId = new Integer(indexTypeAsString);
+        }
+        catch (NumberFormatException e) {
+            throw new RuntimeException("Could not convert " + indexTypeAsString + " into
Integer.");
+        }
+
+        indexType = IndexType.findByValue(indexTypeId);
+
+        if (indexType == null) {
+            throw new RuntimeException(indexTypeAsString + " is unsupported.");
+        }
+
+        return indexType;
+    }
+
     private byte[] columnNameAsByteArray(final String column, final String columnFamily)
throws NoSuchFieldException, InstantiationException, IllegalAccessException, UnsupportedEncodingException
     {
         List<String> cfnames = new ArrayList<String>();

Modified: cassandra/trunk/src/java/org/apache/cassandra/cli/CliCompiler.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cli/CliCompiler.java?rev=1005912&r1=1005911&r2=1005912&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cli/CliCompiler.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cli/CliCompiler.java Fri Oct  8 17:16:42
2010
@@ -58,7 +58,8 @@ public class CliCompiler
 
     public static CommonTree compileQuery(String query)
     {
-        CommonTree queryTree = null;
+        CommonTree queryTree;
+        
         try
         {
             ANTLRStringStream input = new ANTLRNoCaseStringStream(query);



Mime
View raw message