Restore one servlet
[proteocache.git] / datadb / compbio / cassandra / CassandraNativeConnector.java
index 1fb01fc..be90b05 100644 (file)
@@ -1,25 +1,21 @@
 package compbio.cassandra;
 
 import java.io.IOException;
+import java.util.Calendar;
+import java.util.HashMap;
 import java.util.List;
+import java.util.ArrayList;
 
 import com.datastax.driver.core.Cluster;
 import com.datastax.driver.core.Host;
 import com.datastax.driver.core.Metadata;
+import com.datastax.driver.core.Row;
 import com.datastax.driver.core.Session;
 import com.datastax.driver.core.ResultSet;
 
 public class CassandraNativeConnector {
        private static Cluster cluster;
        private static Session session;
-
-       /*
-        * private static Keyspace ksp; private static Mutator<Long> mutatorLong;
-        * private static Mutator<String> mutatorString; private static
-        * Mutator<String> mutatorLog; StringSerializer ss = StringSerializer.get();
-        * LongSerializer ls = LongSerializer.get();
-        */
-
        /*
         * connect to the cluster and look weather the dababase has any data inside
         */
@@ -41,8 +37,8 @@ public class CassandraNativeConnector {
                                + "(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);");
+               session.execute("CREATE INDEX IF NOT EXISTS ProteinSeq ON ProteinKeyspace.ProteinRow (protein);");
+               session.execute("CREATE INDEX IF NOT EXISTS JobDateStamp ON ProteinKeyspace.ProteinData (jobtime);");
 
                System.out.println("Cassandra connected");
        }
@@ -51,7 +47,7 @@ public class CassandraNativeConnector {
         * parsing data source and filling the database
         */
        public void Parsing() throws IOException {
-               if (false) {
+               if (true) {
                        // if (source.equals("http")) {
                        // get data from real Jpred production server
                        System.out.println("Parsing web data source......");
@@ -60,7 +56,7 @@ public class CassandraNativeConnector {
                        JpredParserHTTP parser = new JpredParserHTTP(prefix);
                        parser.Parsing(datasrc, 4);
                }
-               if (true) {
+               if (false) {
                        // if (source.equals("file")) {
                        // get irtifical data generated for the DB stress tests
                        System.out.println("Parsing local file data source......");
@@ -72,29 +68,27 @@ public class CassandraNativeConnector {
        }
 
        public void Closing() {
+               session.shutdown();
                cluster.shutdown();
                System.out.println("Cassandra has been shut down");
        }
 
        /*
-        * prepare data for insertion into the db
+        * inserting data 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 * FROM ProteinKeyspace.ProteinLog WHERE JobID = '" + jobid + "';";
-               //System.out.println(check1);
                ResultSet results1 = session.execute(check1);
                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);
                        session.execute(com1);
 
                        String com2 = "INSERT INTO ProteinKeyspace.ProteinData " + "(jobtime, JobID, Protein)" + " VALUES (" + jobtime + ",'" + jobid
                                        + "','" + protein + "');";
-                       //System.out.println(com2);
                        session.execute(com2);
 
                        String allpredictions = "";
@@ -109,15 +103,64 @@ public class CassandraNativeConnector {
                        }
 
                        String check2 = "SELECT * FROM ProteinKeyspace.ProteinRow WHERE JobID = '" + jobid + "';";
-                       //System.out.println(check2);
                        ResultSet results2 = session.execute(check2);
                        if (results2.isExhausted()) {
                                String com3 = "INSERT INTO ProteinKeyspace.ProteinRow " + "(Protein, JobID, Predictions)" + " VALUES ('" 
                        + protein + "','" + jobid + "',{" + final_prediction + "});";
-                               //System.out.println(com3);
                                session.execute(com3);
                        }
                }
        }
 
+       /*
+        * getting data from the db
+        */
+       public List<Pair<String, String>> ReadProteinDataTable() {
+               final long startTime = System.currentTimeMillis();
+               String com = "SELECT DataBegin,DataEnd FROM ProteinKeyspace.ProteinLog;";
+               System.out.println("Command: " + com);
+               ResultSet results = session.execute(com);
+               final long queryTime = System.currentTimeMillis();
+               List<Row> rows = results.all();
+               System.out.println ("Query time is " + (queryTime - startTime) + " msec");
+
+               List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
+               int c = 0;
+               for (Row r : rows) {
+                       Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"),r.getString("DataEnd"));
+                       res.add(pair);
+                       ++c;
+               }
+               final long endTime = System.currentTimeMillis();
+               System.out.println (c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
+               return res;
+       }
+
+       /*
+        * getting earlest date of jobs from the db
+        */
+       public long getEarliestDateInDB() {
+               final long startTime = System.currentTimeMillis();
+               String com = "SELECT jobtime FROM ProteinKeyspace.ProteinData;";
+               System.out.println("Command: " + com);
+               ResultSet results = session.execute(com);
+               final long queryTime = System.currentTimeMillis();
+               System.out.println ("Query time is " + (queryTime - startTime) + " msec");
+
+               Calendar cal = Calendar.getInstance();
+               long res = cal.getTimeInMillis();
+               int c = 0;
+               while (!results.isExhausted()) {
+                       Row r = results.one();
+                       long d1 = r.getLong("jobtime");
+                       if (res > d1) {
+                               res = d1;
+                       }
+                       ++c;
+               }
+               final long endTime = System.currentTimeMillis();
+               System.out.println (c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
+               return res;
+       }
+       
 }