1 package compbio.cassandra;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
9 import com.datastax.driver.core.ResultSet;
10 import com.datastax.driver.core.Row;
11 import compbio.cassandra.readers.CassandraReader;
14 * The class removes jobs from the cassandra database. 4 different strategies
15 * are possiable: 1. remove 1 job with given job ID 2. remove jobs launched from
16 * an IP 3. remove jobs with particular protein sequence 4. remove jobs launched
17 * within a time range (date1, data2)
19 * @author Alexander Sherstnev
20 * @author Natasha Sherstneva
24 public class CassandraRemover extends CassandraReader {
25 static SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
28 * private method for real deleting one job
37 private int RemoveJob(String jobid, long date) {
40 log.error("CassandraRemover error: job " + jobid + " with date " + date
41 + " can not be deleted in JobDateInfo. Daily statistics is inconsistent");
45 String status = FindStatus(jobid);
46 String com1 = "DELETE FROM ProteinLog WHERE JobID = '" + jobid + "';";
47 System.out.println("Command: " + com1);
50 String com2 = "UPDATE jpredarchive SET finalstatus = 'DELETED' WHERE JobID = '" + jobid + "' ;";
51 System.out.println("Command: " + com2);
54 String com3 = "SELECT * FROM JobDateInfo WHERE jobday = " + date + ";";
55 System.out.println("Command: " + com3);
56 ResultSet results = CassandraQuery(com3);
57 if (results.isExhausted()) {
58 log.error("CassandraRemover error: job " + jobid + " with date " + date
59 + " can not be deleted in JobDateInfo. Daily statistics is inconsistent");
62 Row row = results.one();
63 if (status.equals("OK")) {
64 long njobsOK = row.getLong("TotalOK") - 1;
65 String com4 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
66 System.out.println("Command: " + com4);
69 String com5 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
70 System.out.println("Command: " + com5);
72 UpdateJobDateInfo(date, "TotalOK", njobsOK);
74 String com6 = "DELETE FROM FailLog WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
75 System.out.println("Command: " + com6);
77 if (status.equals("STOPPED")) {
78 long njobsStopped = row.getLong("TotalStopped") - 1;
79 UpdateJobDateInfo(date, "TotalStopped", njobsStopped);
80 } else if (status.equals("ERROR")) {
81 long njobsError = row.getLong("TotalError") - 1;
82 UpdateJobDateInfo(date, "TotalError", njobsError);
83 } else if (status.equals("TIMEDOUT")) {
84 long njobsTimeOut = row.getLong("TotalTimeOut") - 1;
85 UpdateJobDateInfo(date, "TotalTimeOut", njobsTimeOut);
88 System.out.println("Job " + jobid + " removed...");
93 * update a pariticular column in the JobDateInfo table
101 private void UpdateJobDateInfo(long date, String ColumnName, long totalcol) {
102 String com = "UPDATE JobDateInfo SET " + ColumnName + " = " + totalcol + " WHERE jobday = " + date + ";";
107 * external method for deleting job with given job ID (strategy 1)
112 * @return a number of deleted jobs
115 public int RemoveJobById(String jobid) {
118 long date = FindJobDate(jobid);
119 return RemoveJob(jobid, date);
123 * external method for deleting jobs within a time range (strategy 4)
131 * @return a number of deleted jobs
134 public int RemoveJobByDate(String date1, String date2) {
135 if (date1 == null || date2 == null)
139 Long dateBegin = convertDate(date1);
140 Long dateEnd = convertDate(date2);
141 Calendar start = Calendar.getInstance();
142 start.setTime(new Date(dateBegin));
143 Calendar end = Calendar.getInstance();
144 end.setTime(new Date(dateEnd));
146 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
147 String com1 = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
148 System.out.println("Command: " + com1);
149 ResultSet results = CassandraQuery(com1);
150 if (!results.isExhausted()) {
151 List<Row> rows = results.all();
153 String jobid = r.getString("JobID");
155 njobs += RemoveJob(jobid, date.getTime());
160 String com2 = "SELECT JobID FROM FailLog WHERE jobtime = " + date.getTime() + ";";
161 ResultSet resultsfail = CassandraQuery(com2);
162 if (!resultsfail.isExhausted()) {
163 List<Row> rows = resultsfail.all();
165 String jobid = r.getString("JobID");
167 njobs += RemoveJob(jobid, date.getTime());
176 * external method for deleting jobs launched from a particular IP (strategy
182 * @return a number of deleted jobs
185 public int RemoveJobByIp(String ip) {
189 String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
190 ResultSet results = CassandraQuery(com);
191 if (!results.isExhausted()) {
192 List<Row> rows = results.all();
194 Long date = convertDate(r.getString("databegin"));
195 String jobid = r.getString("JobID");
196 if (date != null || jobid != null) {
197 njobs += RemoveJob(jobid, date);
205 * external method for deleting jobs with a protein sequence (strategy 3)
210 * @return a number of deleted jobs
213 public int RemoveJobBySequence(String sequence) {
215 if (sequence == null)
217 String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + sequence + "';";
218 ResultSet results = CassandraQuery(com);
219 if (!results.isExhausted()) {
220 List<Row> rows = results.all();
222 String jobid = r.getString("JobID");
223 long date = FindJobDate(jobid);
224 njobs += RemoveJob(jobid, date);
230 private long FindJobDate(String jobid) {
231 String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
232 ResultSet results = CassandraQuery(com);
233 if (!results.isExhausted()) {
234 return convertDate(results.one().getString("databegin"));
239 private String FindStatus(String jobid) {
240 String com = "SELECT FinalStatus FROM ProteinLog WHERE JobID = '" + jobid + "';";
241 System.out.println("Command: " + com);
242 ResultSet results = CassandraQuery(com);
243 if (!results.isExhausted()) {
244 return results.one().getString("FinalStatus");
249 protected long convertDate(String d) {
252 Date startdate = dateformatter.parse(d);
253 return startdate.getTime();
255 } catch (ParseException e) {