Re-engineering of database I/O
[proteocache.git] / datadb / compbio / cassandra / CassandraReader.java
1 package compbio.cassandra;
2
3 import java.util.Calendar;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.ArrayList;
7 import java.util.Map;
8
9 import org.apache.log4j.Logger;
10
11 import com.datastax.driver.core.Row;
12 import com.datastax.driver.core.Session;
13 import com.datastax.driver.core.ResultSet;
14
15 import compbio.engine.ProteoCachePropertyHelperManager;
16 import compbio.util.PropertyHelper;
17
18 public class CassandraReader {
19         private Session session;
20         private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
21
22         public CassandraReader() {
23                 Session inis = CassandraNativeConnector.getSession();
24                 setSession (inis);
25         }
26
27         public void setSession(Session s) {
28                 assert s != null;
29                 session = s;
30         }
31
32         /*
33          * getting data from the db
34          */
35         public List<Pair<String, String>> ReadProteinDataTable() {
36                 final long startTime = System.currentTimeMillis();
37                 String com = "SELECT DataBegin,DataEnd FROM ProteinLog;";
38                 System.out.println("Command: " + com);
39                 ResultSet results = session.execute(com);
40                 final long queryTime = System.currentTimeMillis();
41                 List<Row> rows = results.all();
42                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
43
44                 List<Pair<String, String>> res = new ArrayList<Pair<String, String>>();
45                 int c = 0;
46                 for (Row r : rows) {
47                         Pair<String, String> pair = new Pair<String, String>(r.getString("DataBegin"), r.getString("DataEnd"));
48                         res.add(pair);
49                         ++c;
50                 }
51                 final long endTime = System.currentTimeMillis();
52                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
53                 return res;
54         }
55
56         /*
57          * getting data from the db ProteinData
58          */
59         public Integer ReadDateTable(long queryDate) {
60                 final long startTime = System.currentTimeMillis();
61                 String com = "SELECT jobtime, JobID FROM ProteinData WHERE jobtime = " + queryDate + ";";
62                 System.out.println("Command: " + com);
63                 ResultSet results = session.execute(com);
64                 final long queryTime = System.currentTimeMillis();
65                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
66                 if (results.isExhausted())
67                         return 0;
68                 List<Row> rows = results.all();
69                 final long endTime = System.currentTimeMillis();
70                 System.out.println("Processing time is " + (endTime - queryTime) + " msec");
71                 return rows.size();
72         }
73
74         /*
75          * getting whole protein sequence from the db ProteinRow
76          */
77         public List<StructureProteinPrediction> ReadWholeSequence(String queryProtein) {
78                 final long startTime = System.currentTimeMillis();
79                 String com = "SELECT JobID, Predictions FROM ProteinRow WHERE Protein = '" + queryProtein + "';";
80                 System.out.println("Command: " + com);
81                 ResultSet results = session.execute(com);
82                 if (results.isExhausted())
83                         return null;
84                 final long queryTime = System.currentTimeMillis();
85                 List<Row> rows = results.all();
86                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
87                 System.out.println(" rows analysed,  " + rows.size());
88                 List<StructureProteinPrediction> res = new ArrayList<StructureProteinPrediction>();
89                 int c = 0;
90                 for (Row r : rows) {
91                         StructureProteinPrediction structure = new StructureProteinPrediction(queryProtein, r.getString("JobID"), r.getMap(
92                                         "Predictions", String.class, String.class));
93                         res.add(structure);
94                         ++c;
95                 }
96                 final long endTime = System.currentTimeMillis();
97                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
98                 return res;
99         }
100
101         /*
102          * getting part of protein sequence from the db ProteinRow
103          */
104         public List<StructureProteinPrediction> ReadPartOfSequence(String queryProtein) {
105                 final long startTime = System.currentTimeMillis();
106                 String com = "SELECT * FROM ProteinRow;";
107                 System.out.println("Command: " + com);
108                 ResultSet results = session.execute(com);
109                 if (results.isExhausted())
110                         return null;
111                 final long queryTime = System.currentTimeMillis();
112                 List<Row> rows = results.all();
113                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
114                 System.out.println(" rows analysed,  " + rows.size());
115                 List<StructureProteinPrediction> res = new ArrayList<StructureProteinPrediction>();
116                 int c = 0;
117                 for (Row r : rows) {
118                         String prot = r.getString("Protein");
119                         if (prot.matches("(.*)" + queryProtein + "(.*)")) {
120                                 StructureProteinPrediction structure = new StructureProteinPrediction(prot, r.getString("JobID"), r.getMap("Predictions",
121                                                 String.class, String.class));
122                                 res.add(structure);
123                                 ++c;
124                         }
125                 }
126                 final long endTime = System.currentTimeMillis();
127                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
128                 return res;
129         }
130
131         /*
132          * getting protein sequences by counter
133          */
134         public Map<String, Integer> ReadProteinDataByCounter() {
135                 final long startTime = System.currentTimeMillis();
136                 String com = "SELECT Protein FROM ProteinRow;";
137                 System.out.println("Command: " + com);
138                 ResultSet results = session.execute(com);
139                 if (results.isExhausted())
140                         return null;
141                 final long queryTime = System.currentTimeMillis();
142                 List<Row> rows = results.all();
143                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
144                 System.out.println(" rows analysed,  " + rows.size());
145                 Map<String, Integer> res = new HashMap<String, Integer>();
146                 int c = 0;
147                 for (Row r : rows) {
148                         String protein = r.getString("Protein");
149                         if (res.containsKey(protein))
150                                 res.put(protein, res.get(protein) + 1);
151                         else
152                                 res.put(protein, 1);
153                 }
154                 final long endTime = System.currentTimeMillis();
155                 System.out.println(c + " rows analysed, execution time is " + (endTime - startTime) + " msec");
156                 return res;
157         }
158
159         /*
160          * getting protein sequences by counter
161          */
162         public StructureJobLog ReadJobLog(String jobid) {
163                 final long startTime = System.currentTimeMillis();
164                 String com = "SELECT * FROM ProteinLog WHERE JobID = '" + jobid + "';";
165                 System.out.println("Command: " + com);
166                 ResultSet results = session.execute(com);
167                 if (results.isExhausted())
168                         return null;
169                 final long queryTime = System.currentTimeMillis();
170                 Row row = results.one();
171                 String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
172                 System.out.println("Command: " + com1);
173                 ResultSet results1 = session.execute(com1);
174                 if (results1.isExhausted())
175                         return null;
176                 Row row1 = results1.one();
177                 StructureJobLog res = new StructureJobLog(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"),
178                                 row.getString("DataEnd"), row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
179                 System.out.println("Query time is " + (queryTime - startTime) + " msec");
180                 final long endTime = System.currentTimeMillis();
181                 System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");
182                 return res;
183         }
184 }