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;
15 public class CassandraRemover {
16 private Session session;
17 static SimpleDateFormat dateformatter = new SimpleDateFormat("yyyy/MM/dd");
18 private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
20 public CassandraRemover() {
21 Session inis = CassandraNativeConnector.getSession();
25 public void setSession(Session s) {
31 * delete a record from CF for current jobId
33 private void RemoveJob(String jobid, long date) {
34 String status = FindStatus(jobid);
35 String com0 = "DELETE FROM ProteinLog WHERE JobID = '" + jobid + "';";
36 System.out.println("Command: " + com0);
37 session.execute(com0);
38 String com3 = "UPDATE jpredarchive SET finalstatus = 'DELETED' WHERE JobID = '" + jobid + "' ;";
39 System.out.println("Command: " + com3);
40 session.execute(com3);
41 String com = "SELECT * FROM JobDateInfo WHERE jobday = " + date + ";";
42 System.out.println("Command: " + com);
43 ResultSet results = session.execute(com);
44 Row row = results.one();
45 long njobs = row.getLong("Total") -1 ;
46 if (status.equals("OK")) {
47 long njobsOK = row.getLong("TotalOK") - 1;
48 String com1 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
49 System.out.println("Command: " + com1);
50 session.execute(com1);
51 String com2 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
52 System.out.println("Command: " + com2);
53 session.execute(com2);
54 UpdateJobDateInfo(date, "TotalOK", njobsOK, njobs);
56 String com6 = "DELETE FROM FailLog WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
57 System.out.println("Command: " + com6);
58 session.execute(com6);
59 if (status.equals("STOPPED")) {
60 long njobsStopped = row.getLong("TotalStopped") - 1;
61 UpdateJobDateInfo(date, "TotalStopped", njobsStopped, njobs);
62 } else if (status.equals("ERROR")) {
63 long njobsError = row.getLong("TotalError") - 1;
64 UpdateJobDateInfo(date, "TotalError", njobsError, njobs);
65 } else if (status.equals("TIMEDOUT")) {
66 long njobsTimeOut = row.getLong("TotalTimeOut") - 1;
67 UpdateJobDateInfo(date, "TotalTimeOut", njobsTimeOut, njobs);
70 System.out.println("Remove jobs: " + jobid);
73 private void UpdateJobDateInfo(long date, String ColumnName, long totalcol, long total) {
74 String com4 = "UPDATE JobDateInfo SET " + ColumnName + " = " + totalcol + ", Total = " + total + " WHERE jobday = " + date + ";";
75 System.out.println("Command: " + com4);
76 session.execute(com4);
79 public int RemoveJobById (String jobid) {
82 Long date = FindDate(jobid);
83 RemoveJob(jobid, date);
87 public int RemoveJobByDate (String date1, String date2) {
89 if (date1 == null || date2 == null)
91 Long dateBegin = convertDate(date1);
92 Long dateEnd = convertDate(date2);
93 Calendar start = Calendar.getInstance();
94 start.setTime(new Date(dateBegin));
95 Calendar end = Calendar.getInstance();
96 end.setTime(new Date(dateEnd));
97 for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
98 System.out.println("--------------------------------------------------------------------: ");
99 String com = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
100 System.out.println("Command: " + com);
101 ResultSet results = session.execute(com);
102 if (!results.isExhausted()) {
103 List<Row> rows = results.all();
105 String jobid = r.getString("JobID");
107 RemoveJob(jobid, date.getTime());
112 String comm = "SELECT JobID FROM FailLog WHERE jobtime = " + date.getTime() + ";";
113 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^: " + comm);
114 ResultSet resultsfail = session.execute(comm);
115 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^: " + comm);
116 if (!resultsfail.isExhausted()) {
117 List<Row> rows = resultsfail.all();
119 String jobid = r.getString("JobID");
120 System.out.println("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^: " + jobid);
122 RemoveJob(jobid, date.getTime());
132 public int RemoveJobByIp (String ip) {
136 String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
137 ResultSet results = session.execute(com);
138 if (!results.isExhausted()) {
139 List<Row> rows = results.all();
141 Long date = convertDate(r.getString("databegin"));
142 String jobid = r.getString("JobID");
143 if (date == null || jobid == null)
145 RemoveJob(jobid, date);
152 public int RemoveJobBySequence (String seq) {
156 String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + seq + "';";
157 ResultSet results = session.execute(com);
158 if (!results.isExhausted()) {
159 List<Row> rows = results.all();
161 String jobid = r.getString("JobID");
162 Long date = FindDate(jobid);
163 RemoveJob(jobid, date);
170 private Long FindDate(String jobid) {
171 String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
172 ResultSet results = session.execute(com);
173 Long date = convertDate(results.one().getString("databegin"));
177 private String FindStatus(String jobid) {
178 String com = "SELECT FinalStatus FROM ProteinLog WHERE JobID = '" + jobid + "';";
179 System.out.println("Command: " + com);
180 ResultSet results = session.execute(com);
181 String status = results.one().getString("FinalStatus");
182 System.out.println("*****status: " + status);
186 protected long convertDate (String d) {
189 Date startdate = dateformatter.parse(d);
190 return startdate.getTime();
192 } catch (ParseException e) {