First working "writing" code for Cassandra 2.0
[proteocache.git] / datadb / compbio / cassandra / CassandraNativeConnector.java
index 49224db..1fb01fc 100644 (file)
@@ -36,10 +36,13 @@ public class CassandraNativeConnector {
 
                session = cluster.connect();
                session.execute("CREATE KEYSPACE IF NOT EXISTS ProteinKeyspace WITH replication = {'class':'SimpleStrategy', 'replication_factor':3};");
-               session.execute("CREATE COLUMNFAMILY IF NOT EXISTS ProteinKeyspace.ProteinRow (Protein ascii PRIMARY KEY, Predictions map<ascii,ascii>);");
+               session.execute("CREATE COLUMNFAMILY IF NOT EXISTS ProteinKeyspace.ProteinRow (Protein ascii, JobID ascii, Predictions map<ascii,ascii>, PRIMARY KEY(JobID));");
                session.execute("CREATE COLUMNFAMILY IF NOT EXISTS ProteinKeyspace.ProteinLog "
-                               + "(JobID ascii PRIMARY KEY, DataBegin ascii, DataEnd ascii, ip ascii, FinalStatus ascii, ExecutionStatus ascii, Protein ascii);");
-               session.execute("CREATE COLUMNFAMILY IF NOT EXISTS ProteinKeyspace.ProteinData (jobtime bigint PRIMARY KEY, JobID ascii, Protein ascii);");
+                               + "(JobID ascii, DataBegin ascii, DataEnd ascii, ip ascii, FinalStatus ascii, ExecutionStatus ascii, Protein ascii, PRIMARY KEY(JobID));");
+               session.execute("CREATE COLUMNFAMILY IF NOT EXISTS ProteinKeyspace.ProteinData (jobtime bigint, JobID ascii, Protein ascii, PRIMARY KEY(JobID));");
+
+               session.execute("CREATE INDEX ProteinSeq ON ProteinKeyspace.ProteinRow (protein);");
+               session.execute("CREATE INDEX JobDateStamp ON ProteinKeyspace.ProteinData (jobtime);");
 
                System.out.println("Cassandra connected");
        }
@@ -74,38 +77,25 @@ public class CassandraNativeConnector {
        }
 
        /*
-        * check whether the job id exists in the DB
-        */
-       public boolean CheckID(String jobid) {
-               String com = "SELECT * FROM ProteinKeyspace.ProteinData WHERE jobid = '" + jobid + "';";
-               System.out.println(com);
-               ResultSet results = session.execute(com);
-               if (null != results) {
-                       return true;
-               }
-               return false;
-       }
-
-       /*
         * prepare data for insertion into the db
         */
        public void InsertData(long jobtime, String startdate, String enddate, String ip, String jobid, String statusEx, String statusFinal,
                        String protein, List<FastaSequence> predictions) {
 
-               String check1 = "SELECT count(*) FROM ProteinKeyspace.ProteinLog WHERE JobID = '" + jobid + "';";
+               String check1 = "SELECT * FROM ProteinKeyspace.ProteinLog WHERE JobID = '" + jobid + "';";
                //System.out.println(check1);
                ResultSet results1 = session.execute(check1);
-               if (!results1.isExhausted()) {
+               if (results1.isExhausted()) {
                        String com1 = "INSERT INTO ProteinKeyspace.ProteinLog "
                                        + "(JobID, IP, DataBegin, DataEnd, FinalStatus, ExecutionStatus, Protein)" + " VALUES ('" + jobid + "','" + ip + "','"
                                        + startdate + "','" + enddate + "','" + statusFinal + "','" + statusEx + "','" + protein + "');";
-                       // System.out.println(com1);
+                       //System.out.println(com1);
                        session.execute(com1);
 
                        String com2 = "INSERT INTO ProteinKeyspace.ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + jobtime + ",'" + jobid
                                        + "','" + protein + "');";
-                       // System.out.println(com2);
-                       // session.execute(com2);
+                       //System.out.println(com2);
+                       session.execute(com2);
 
                        String allpredictions = "";
                        for (FastaSequence pred : predictions) {
@@ -118,22 +108,13 @@ public class CassandraNativeConnector {
                                final_prediction = allpredictions.substring(0, allpredictions.length() - 1);
                        }
 
-                       String check2 = "SELECT count(*) FROM ProteinKeyspace.ProteinRow WHERE Protein = '" + protein + "';";
-                       //System.out.println(check1);
+                       String check2 = "SELECT * FROM ProteinKeyspace.ProteinRow WHERE JobID = '" + jobid + "';";
+                       //System.out.println(check2);
                        ResultSet results2 = session.execute(check2);
-
-                       if (results1.isExhausted()) {
-                       String com3 = "INSERT INTO ProteinKeyspace.ProteinRow " + "(Protein, " + jobid + ")" + " VALUES ('" + protein + "'," + "{"
-                                       + final_prediction + "}" + ");";
-                       System.out.println(com3);
-                       session.execute(com3);
-                       } else {
-                               String com4 = "ALTER TABLE ProteinKeyspace.ProteinRow ADD " + jobid + ");";
-                               System.out.println(com4);
-                               session.execute(com4);
-                               String com3 = "INSERT INTO ProteinKeyspace.ProteinRow " + "(" + jobid + ")" + " VALUES ({" + final_prediction + "}" + ")"
-                               + " WHERE Protein = '" + protein + "';";
-                               System.out.println(com3);
+                       if (results2.isExhausted()) {
+                               String com3 = "INSERT INTO ProteinKeyspace.ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" 
+                       + protein + "','" + jobid + "',{" + final_prediction + "});";
+                               //System.out.println(com3);
                                session.execute(com3);
                        }
                }