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 long njobs = row.getLong("Total") - 1;
64 if (status.equals("OK")) {
65 long njobsOK = row.getLong("TotalOK") - 1;
66 String com4 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
67 System.out.println("Command: " + com4);
70 String com5 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
71 System.out.println("Command: " + com5);
73 UpdateJobDateInfo(date, "TotalOK", njobsOK, njobs);
75 String com6 = "DELETE FROM FailLog WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
76 System.out.println("Command: " + com6);
78 if (status.equals("STOPPED")) {
79 long njobsStopped = row.getLong("TotalStopped") - 1;
80 UpdateJobDateInfo(date, "TotalStopped", njobsStopped, njobs);
81 } else if (status.equals("ERROR")) {
82 long njobsError = row.getLong("TotalError") - 1;
83 UpdateJobDateInfo(date, "TotalError", njobsError, njobs);
84 } else if (status.equals("TIMEDOUT")) {
85 long njobsTimeOut = row.getLong("TotalTimeOut") - 1;
86 UpdateJobDateInfo(date, "TotalTimeOut", njobsTimeOut, njobs);
89 System.out.println("Job " + jobid + " removed...");
94 * update a pariticular column in the JobDateInfo table
102 private void UpdateJobDateInfo(long date, String ColumnName, long totalcol, long total) {
103 String com = "UPDATE JobDateInfo SET " + ColumnName + " = " + totalcol + ", Total = " + total + " WHERE jobday = " + date + ";";
104 System.out.println("Command: " + com);
109 * external method for deleting job with given job ID (strategy 1)
114 * @return a number of deleted jobs
117 public int RemoveJobById(String jobid) {
120 long date = FindJobDate(jobid);
121 return RemoveJob(jobid, date);
125 * external method for deleting jobs within a time range (strategy 4)
133 * @return a number of deleted jobs
136 public int RemoveJobByDate(String date1, String date2) {
137 if (date1 == null || date2 == null)
141 Long dateBegin = convertDate(date1);
142 Long dateEnd = convertDate(date2);
143 Calendar start = Calendar.getInstance();
144 start.setTime(new Date(dateBegin));
145 Calendar end = Calendar.getInstance();
146 end.setTime(new Date(dateEnd));
148 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
149 String com1 = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
150 System.out.println("Command: " + com1);
151 ResultSet results = CassandraQuery(com1);
152 if (!results.isExhausted()) {
153 List<Row> rows = results.all();
155 String jobid = r.getString("JobID");
157 njobs += RemoveJob(jobid, date.getTime());
162 String com2 = "SELECT JobID FROM FailLog WHERE jobtime = " + date.getTime() + ";";
163 ResultSet resultsfail = CassandraQuery(com2);
164 if (!resultsfail.isExhausted()) {
165 List<Row> rows = resultsfail.all();
167 String jobid = r.getString("JobID");
169 njobs += RemoveJob(jobid, date.getTime());
178 * external method for deleting jobs launched from a particular IP (strategy
184 * @return a number of deleted jobs
187 public int RemoveJobByIp(String ip) {
191 String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
192 ResultSet results = CassandraQuery(com);
193 if (!results.isExhausted()) {
194 List<Row> rows = results.all();
196 Long date = convertDate(r.getString("databegin"));
197 String jobid = r.getString("JobID");
198 if (date != null || jobid != null) {
199 njobs += RemoveJob(jobid, date);
207 * external method for deleting jobs with a protein sequence (strategy 3)
212 * @return a number of deleted jobs
215 public int RemoveJobBySequence(String sequence) {
217 if (sequence == null)
219 String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + sequence + "';";
220 ResultSet results = CassandraQuery(com);
221 if (!results.isExhausted()) {
222 List<Row> rows = results.all();
224 String jobid = r.getString("JobID");
225 long date = FindJobDate(jobid);
226 njobs += RemoveJob(jobid, date);
232 private long FindJobDate(String jobid) {
233 String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
234 ResultSet results = CassandraQuery(com);
235 if (!results.isExhausted()) {
236 return convertDate(results.one().getString("databegin"));
241 private String FindStatus(String jobid) {
242 String com = "SELECT FinalStatus FROM ProteinLog WHERE JobID = '" + jobid + "';";
243 System.out.println("Command: " + com);
244 ResultSet results = CassandraQuery(com);
245 if (!results.isExhausted()) {
246 return results.one().getString("FinalStatus");
251 protected long convertDate(String d) {
254 Date startdate = dateformatter.parse(d);
255 return startdate.getTime();
257 } catch (ParseException e) {