cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r1026145 - in /cassandra/trunk: CHANGES.txt src/java/org/apache/cassandra/cli/Cli.g src/java/org/apache/cassandra/cli/CliMain.java src/java/org/apache/cassandra/cli/CliOptions.java src/java/org/apache/cassandra/cli/CliSessionState.java
Date Thu, 21 Oct 2010 21:25:57 GMT
Author: jbellis
Date: Thu Oct 21 21:25:56 2010
New Revision: 1026145

URL: http://svn.apache.org/viewvc?rev=1026145&view=rev
Log:
cli --file option.
patch by Pavel Yaskevich; reviewed by jbellis for CASSANDRA-1616

Modified:
    cassandra/trunk/CHANGES.txt
    cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g
    cassandra/trunk/src/java/org/apache/cassandra/cli/CliMain.java
    cassandra/trunk/src/java/org/apache/cassandra/cli/CliOptions.java
    cassandra/trunk/src/java/org/apache/cassandra/cli/CliSessionState.java

Modified: cassandra/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/cassandra/trunk/CHANGES.txt?rev=1026145&r1=1026144&r2=1026145&view=diff
==============================================================================
--- cassandra/trunk/CHANGES.txt (original)
+++ cassandra/trunk/CHANGES.txt Thu Oct 21 21:25:56 2010
@@ -51,6 +51,7 @@ dev
  * update GC settings in cassandra.bat (CASSANDRA-1636)
  * cli support for index queries (CASSANDRA-1635)
  * cli support for updating schema memtable settings (CASSANDRA-1634)
+ * cli --file option (CASSANDRA-1616)
  * reduce automatically chosen memtable sizes by 50% (CASSANDRA-1641)
  * move endpoint cache from snitch to strategy (CASSANDRA-1643)
 

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=1026145&r1=1026144&r2=1026145&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cli/Cli.g Thu Oct 21 21:25:56 2010
@@ -86,7 +86,7 @@ package org.apache.cassandra.cli;
 {
     public void reportError(RecognitionException e) 
     {
-        throw new RuntimeException("Syntax error at " + e.line + "-" + e.charPositionInLine
+ ": " + this.getErrorMessage(e, this.getTokenNames()));
+        throw new RuntimeException("Syntax error at position " + e.charPositionInLine + ":
" + this.getErrorMessage(e, this.getTokenNames()));
     }
 }
 
@@ -94,7 +94,7 @@ package org.apache.cassandra.cli;
 {
     public void reportError(RecognitionException e) 
     {
-        throw new RuntimeException("Syntax error at " + e.line + "-" + e.charPositionInLine
+ ": " + this.getErrorMessage(e, this.getTokenNames()));
+        throw new RuntimeException("Syntax error at position " + e.charPositionInLine + ":
" + this.getErrorMessage(e, this.getTokenNames()));
     }
 }
 

Modified: cassandra/trunk/src/java/org/apache/cassandra/cli/CliMain.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cli/CliMain.java?rev=1026145&r1=1026144&r2=1026145&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cli/CliMain.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cli/CliMain.java Thu Oct 21 21:25:56 2010
@@ -47,6 +47,7 @@ public class CliMain
     public  static CliSessionState sessionState = new CliSessionState();
     private static CliClient cliClient;
     private static CliCompleter completer = new CliCompleter();
+    private static int lineNumber = 1;
 
     /**
      * Establish a thrift connection to cassandra instance
@@ -220,30 +221,38 @@ public class CliMain
 
     public static void processStatement(String query)
     {
+
         try
         {
             cliClient.executeCLIStmt(query);
         }
-        catch (InvalidRequestException ire)
+        catch (Exception e)
         {
-            sessionState.err.println(ire.why);
-            if (sessionState.debug)
-                ire.printStackTrace(sessionState.err);
+            String errorTemplate = sessionState.inFileMode() ? "Line " + lineNumber + " =>
" : "";
             
-            // Abort a batch run when errors are encountered
-            if (sessionState.batch)
-                System.exit(4);
-        }
-        catch (Throwable e)
-        {
-            sessionState.err.println((e.getCause() == null) ? e.getMessage() : e.getCause().getMessage());
+            if (e instanceof InvalidRequestException)
+            {
+                sessionState.err.println(errorTemplate + ((InvalidRequestException) e).getWhy());
+            }
+            else
+            {
+                String message = (e.getCause() == null) ? e.getMessage() : e.getCause().getMessage();
+                sessionState.err.println(errorTemplate + message);
+            }
             
             if (sessionState.debug)
+            {
                 e.printStackTrace(sessionState.err);
-            
-            // Abort a batch run when errors are encountered
-            if (sessionState.batch)
-                System.exit(8);
+            }
+
+            if (sessionState.batch || sessionState.inFileMode())
+            {
+                System.exit(4);
+            }
+        }
+        finally
+        {
+            lineNumber++;
         }
     }
 
@@ -274,6 +283,33 @@ public class CliMain
             cliClient = new CliClient(sessionState, null);
         }
 
+        // load statements from file and process them
+        if (sessionState.inFileMode())
+        {
+            FileReader fileReader;
+
+            try
+            {
+                fileReader = new FileReader(sessionState.filename);
+            }
+            catch (IOException e)
+            {
+                sessionState.err.println(e.getMessage());
+                return;
+            }
+
+            BufferedReader reader = new BufferedReader(fileReader);
+
+            String statement;
+
+            while ((statement = reader.readLine()) != null)
+            {
+                processStatement(statement);
+            }
+
+            return;
+        }
+
         ConsoleReader reader = new ConsoleReader();
         
         if (!sessionState.batch)

Modified: cassandra/trunk/src/java/org/apache/cassandra/cli/CliOptions.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cli/CliOptions.java?rev=1026145&r1=1026144&r2=1026145&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cli/CliOptions.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cli/CliOptions.java Thu Oct 21 21:25:56
2010
@@ -39,6 +39,7 @@ public class CliOptions {
     private static final String KEYSPACE_OPTION = "keyspace";
     private static final String BATCH_OPTION = "batch";
     private static final String HELP_OPTION = "help";
+    private static final String FILE_OPTION = "file";
     
     // Default values for optional command line arguments
     private static final int    DEFAULT_THRIFT_PORT = 9160;
@@ -56,12 +57,13 @@ public class CliOptions {
         options.addOption(PASSWORD_OPTION, true, "password for cassandra authentication");
         options.addOption(KEYSPACE_OPTION, true, "cassandra keyspace user is authenticated
against");
         options.addOption(BATCH_OPTION, false, "enabled batch mode (supress output; errors
are fatal)");
+        options.addOption(FILE_OPTION, true, "load statements from the specific file.");
         options.addOption(HELP_OPTION, false, "usage help.");
     }
 
     private static void printUsage()
     {
-        System.err.println("Usage: cassandra-cli --host hostname [--port <portname>]
[--unframed] [--debug]");
+        System.err.println("Usage: cassandra-cli --host hostname [--port <portname>]
[--file <filename>] [--unframed] [--debug]");
         System.err.println("\t[--username username] [--password password] [--keyspace keyspace]
[--batch] [--help]");
     }
 
@@ -139,10 +141,16 @@ public class CliOptions {
             css.batch = true;
         }
 
+        if (cmd.hasOption(FILE_OPTION))
+        {
+            css.filename = cmd.getOptionValue(FILE_OPTION);
+        }
+
         if (cmd.hasOption(HELP_OPTION))
         {
             printUsage();
             System.exit(1);
         }
+
     }
 }

Modified: cassandra/trunk/src/java/org/apache/cassandra/cli/CliSessionState.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/cli/CliSessionState.java?rev=1026145&r1=1026144&r2=1026145&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/cli/CliSessionState.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/cli/CliSessionState.java Thu Oct 21 21:25:56
2010
@@ -35,6 +35,8 @@ public class CliSessionState
     public String  password;      // cassandra login password (if SimpleAuthenticator is
used)
     public String  keyspace;      // cassandra keyspace user is authenticating
     public boolean batch = false; // enable/disable batch processing mode
+    public String  filename = ""; // file to read commands from
+
     /*
      * Streams to read/write from
      */
@@ -58,4 +60,9 @@ public class CliSessionState
     {
         this.err = newErr;
     }
+
+    public boolean inFileMode()
+    {
+        return !this.filename.isEmpty();
+    }
 }



Mime
View raw message