134a139d45606937d365be4183fbe9f71cc91690
[proteocache.git] / datadb / compbio / cassandra / readers / CassandraReaderExecutionTime.java
1 package compbio.cassandra.readers;
2
3 import java.util.Calendar;
4 import java.util.List;
5 import java.util.ArrayList;
6
7 import com.datastax.driver.core.Row;
8 import com.datastax.driver.core.Session;
9 import com.datastax.driver.core.ResultSet;
10
11 import compbio.cassandra.Pair;
12
13 public class CassandraReaderExecutionTime {
14         private Session session;
15
16         public static String CASSANDRA_HOSTNAME = "localhost";
17         public static boolean READ_WEB_JPRED = false;
18         public static boolean READ_LOCALFILE_JPRED = false;
19
20         public void setSession(Session s) {
21                 assert s != null;
22                 session = s;
23         }
24
25         private void setConditions() {
26
27         }
28
29         public boolean JobisNotInsterted(String jobid) {
30                 ResultSet results1 = session.execute("SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';");
31                 if (results1.isExhausted()) {
32                         return true;
33                 }
34                 return false;
35         }
36
37         public boolean JobisNotArchived(String jobid) {
38                 ResultSet results1 = session.execute("SELECT * FROM JpredArchive WHERE JobID = '" + jobid + "';");
39                 if (results1.isExhausted()) {
40                         return true;
41                 }
42                 return false;
43         }
44
45         /*
46          * getting data from the db
47          */
48         public List<Pair<String, String>> ReadProteinDataTable() {
49                 final long startTime = System.currentTimeMillis();
50                 String com = "SELECT DataBegin,DataEnd FROM ProteinKeyspace.ProteinLog;";
51                 System.out.println("Command: " + com);
52                 ResultSet results = session.execute(com);
53                 final long queryTime = System.currentTimeMillis();
54                 List<Row> rows = results.all();
55                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
56
57                 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
58                 int c = 0;
59                 for (Row r : rows) {
60                         Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
61                         res.add(pair);
62                         ++c;
63                 }
64                 final long endTime = System.currentTimeMillis();
65                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
66                 return res;
67         }
68
69         /*
70          * getting earlest date of jobs from the db
71          */
72         public long getEarliestDateInDB() {
73                 final long startTime = System.currentTimeMillis();
74                 String com = "SELECT jobtime FROM ProteinKeyspace.ProteinData;";
75                 System.out.println("Command: " + com);
76                 ResultSet results = session.execute(com);
77                 final long queryTime = System.currentTimeMillis();
78                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
79
80                 Calendar cal = Calendar.getInstance();
81                 long res = cal.getTimeInMillis();
82                 int c = 0;
83                 while (!results.isExhausted()) {
84                         Row r = results.one();
85                         long d1 = r.getLong("jobtime");
86                         if (res > d1) {
87                                 res = d1;
88                         }
89                         ++c;
90                 }
91                 final long endTime = System.currentTimeMillis();
92                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
93                 return res;
94         }
95
96 }