1 package compbio.cassandra;
3 import java.text.ParseException;
4 import java.text.SimpleDateFormat;
5 import java.util.Calendar;
9 import org.apache.log4j.Logger;
11 import com.datastax.driver.core.ResultSet;
12 import com.datastax.driver.core.Row;
13 import com.datastax.driver.core.Session;
14 import com.datastax.driver.core.exceptions.QueryExecutionException;
15 import com.datastax.driver.core.exceptions.QueryValidationException;
17 public class CassandraRemover {
18 private Session session;
19 static SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
20 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
22 public CassandraRemover() {
23 Session inis = CassandraNativeConnector.getSession();
27 public void setSession(Session s) {
33 * delete a record from CF for current jobId
35 private void RemoveJob(String jobid, long date) {
36 String status = FindStatus(jobid);
37 String com0 = "DELETE FROM ProteinLog WHERE JobID = '" + jobid + "';";
38 System.out.println("Command: " + com0);
39 session.execute(com0);
40 String com3 = "UPDATE jpredarchive SET finalstatus = 'DELETED' WHERE JobID = '" + jobid + "' ;";
41 System.out.println("Command: " + com3);
42 session.execute(com3);
43 String com = "SELECT * FROM JobDateInfo WHERE jobday = " + date + ";";
44 System.out.println("Command: " + com);
45 ResultSet results = session.execute(com);
46 Row row = results.one();
47 long njobs = row.getLong("Total") - 1;
48 if (status.equals("OK")) {
49 long njobsOK = row.getLong("TotalOK") - 1;
50 String com1 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
51 System.out.println("Command: " + com1);
52 session.execute(com1);
53 String com2 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
54 System.out.println("Command: " + com2);
55 session.execute(com2);
56 UpdateJobDateInfo(date, "TotalOK", njobsOK, njobs);
58 String com6 = "DELETE FROM FailLog WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
59 System.out.println("Command: " + com6);
60 session.execute(com6);
61 if (status.equals("STOPPED")) {
62 long njobsStopped = row.getLong("TotalStopped") - 1;
63 UpdateJobDateInfo(date, "TotalStopped", njobsStopped, njobs);
64 } else if (status.equals("ERROR")) {
65 long njobsError = row.getLong("TotalError") - 1;
66 UpdateJobDateInfo(date, "TotalError", njobsError, njobs);
67 } else if (status.equals("TIMEDOUT")) {
68 long njobsTimeOut = row.getLong("TotalTimeOut") - 1;
69 UpdateJobDateInfo(date, "TotalTimeOut", njobsTimeOut, njobs);
72 System.out.println("Remove jobs: " + jobid);
75 private void UpdateJobDateInfo(long date, String ColumnName, long totalcol, long total) {
76 String com4 = "UPDATE JobDateInfo SET " + ColumnName + " = " + totalcol + ", Total = " + total + " WHERE jobday = " + date + ";";
77 System.out.println("Command: " + com4);
78 session.execute(com4);
81 public int RemoveJobById(String jobid) {
84 Long date = FindDate(jobid);
85 RemoveJob(jobid, date);
89 public int RemoveJobByDate(String date1, String date2) {
91 if (date1 == null || date2 == null)
93 Long dateBegin = convertDate(date1);
94 Long dateEnd = convertDate(date2);
95 Calendar start = Calendar.getInstance();
96 start.setTime(new Date(dateBegin));
97 Calendar end = Calendar.getInstance();
98 end.setTime(new Date(dateEnd));
99 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
100 System.out.println("--------------------------------------------------------------------: ");
101 String com = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
102 System.out.println("Command: " + com);
103 ResultSet results = session.execute(com);
104 if (!results.isExhausted()) {
105 List<Row> rows = results.all();
107 String jobid = r.getString("JobID");
109 RemoveJob(jobid, date.getTime());
114 String comm = "SELECT JobID FROM FailLog WHERE jobtime = " + date.getTime() + ";";
115 ResultSet resultsfail = session.execute(comm);
116 if (!resultsfail.isExhausted()) {
117 List<Row> rows = resultsfail.all();
119 String jobid = r.getString("JobID");
121 RemoveJob(jobid, date.getTime());
130 public int RemoveJobByIp(String ip) {
134 String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
135 ResultSet results = session.execute(com);
136 if (!results.isExhausted()) {
137 List<Row> rows = results.all();
139 Long date = convertDate(r.getString("databegin"));
140 String jobid = r.getString("JobID");
141 if (date == null || jobid == null)
143 RemoveJob(jobid, date);
150 public int RemoveJobBySequence(String seq) {
154 String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + seq + "';";
155 ResultSet results = session.execute(com);
156 if (!results.isExhausted()) {
157 List<Row> rows = results.all();
159 String jobid = r.getString("JobID");
160 Long date = FindDate(jobid);
161 RemoveJob(jobid, date);
168 private Long FindDate(String jobid) {
169 String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
170 ResultSet results = session.execute(com);
171 Long date = convertDate(results.one().getString("databegin"));
175 private String FindStatus(String jobid) {
176 String status = "UNKNOWN";
177 String command = "SELECT FinalStatus FROM ProteinLog WHERE JobID = '" + jobid + "';";
179 System.out.println("Command: " + command);
180 ResultSet results = session.execute(command);
181 Row raw = results.one();
183 status = raw.getString("FinalStatus");
185 } catch (QueryExecutionException e) {
186 String mess = "CassandraRemover: query execution exception...";
187 System.out.println(mess);
189 log.error(e.getLocalizedMessage(), e.getCause());
190 } catch (QueryValidationException e) {
191 String mess = "CassandraRemover: query validation exception... Command: " + command;
192 System.out.println(mess);
194 log.error(e.getLocalizedMessage(), e.getCause());
196 System.out.println("*****status: " + status);
200 protected long convertDate(String d) {
203 Date startdate = dateformatter.parse(d);
204 return startdate.getTime();
206 } catch (ParseException e) {