PROT-4 updated structure ProteinData (change primary key, add column ExecTime), Jpred...
[proteocache.git] / datadb / compbio / cassandra / CassandraRemover.java
index d066d82..8404663 100644 (file)
@@ -1,5 +1,11 @@
 package compbio.cassandra;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
 import org.apache.log4j.Logger;
 
 import com.datastax.driver.core.ResultSet;
@@ -8,6 +14,7 @@ import com.datastax.driver.core.Session;
 
 public class CassandraRemover {
        private Session session;
+       static SimpleDateFormat dateformatter =  new SimpleDateFormat("yyyy/MM/dd");
        private static Logger log = Logger.getLogger(CassandraNativeConnector.class);
 
        public CassandraRemover() {
@@ -21,31 +28,130 @@ public class CassandraRemover {
        }
        
        /*
-        * getting a record from CF for current jobId
+        * delete a record from CF for current jobId
         */
-       public StructureJobLog ReadJobLog(String jobid) {
-               final long startTime = System.currentTimeMillis();
-               String com = "SELECT Protein, StartTime FROM ProteinLog WHERE JobID = '" + jobid + "';";
+       private void RemoveJob(String jobid, long date) {               
+               String com0 = "DELETE FROM ProteinLog WHERE JobID = '" + jobid + "';";
+               System.out.println("Command: " + com0);
+               session.execute(com0);
+               String com1 = "DELETE FROM ProteinRow WHERE JobID = '" + jobid + "';";
+               System.out.println("Command: " + com1);
+               session.execute(com1);
+               String com2 = "DELETE FROM ProteinData WHERE JobID = '" + jobid + "' AND jobtime = " + date + ";";
+               System.out.println("Command: " + com2);
+               session.execute(com2);
+               String com3 = "UPDATE jpredarchive SET finalstatus = 'DELETED'  WHERE JobID = '" + jobid + "' ;";
+               System.out.println("Command: " + com3);
+               session.execute(com3);
+               String com = "SELECT total FROM JobDateInfo WHERE jobday = " + date + ";";
+               System.out.println("Command: " + com);
+               ResultSet results = session.execute(com);
+               long njobs = results.one().getLong("total");
+               System.out.println("njobs: " + njobs);
+               String com4 = "INSERT INTO JobDateInfo " + "(jobday, Total)" + " VALUES (" + date + "," + (njobs -1) + ");";
+               System.out.println("Command: " + com4);
+               session.execute(com4);
+               System.out.println("Remove jobs: " + jobid);
+       }
+       
+       public void RemoveJobById (String jobid) {
+               if (jobid == null) 
+                       return;
+               Long date = FindDate(jobid);
+               if (date == null)
+                       return;
+               RemoveJob(jobid, date);
+       }
+       
+       public void RemoveJobByDate (String date1, String date2) {
+               System.out.println("Start " + date1 + ", " + date2);
+               if (date1 == null || date2 == null) 
+                       return;
+               Long dateBegin = convertDate(date1);
+               Long dateEnd = convertDate(date2);
+               System.out.println("Date to long done!: ");
+               if (dateBegin == null || dateEnd == null) 
+                       return;
+               Calendar start = Calendar.getInstance();
+               start.setTime(new Date(dateBegin));
+               Calendar end = Calendar.getInstance();
+               end.setTime(new Date(dateEnd));
+               System.out.println("Date to cal done!: ");
+               for (Date date = start.getTime(); !start.after(end); start.add(Calendar.DATE, 1), date = start.getTime()) {
+                       String com = "SELECT JobID FROM ProteinData WHERE jobtime = " + date.getTime() + ";";
+                       System.out.println("Command: " + com);
+                       ResultSet results = session.execute(com);
+                       if (!results.isExhausted()) {
+                               List<Row> rows = results.all();
+                               for (Row r : rows) {
+                                       String jobid = r.getString("JobID");
+                                       if (jobid != null)
+                                               RemoveJob(jobid, date.getTime());
+                               }
+                       }
+               }
+       }
+       
+       
+       public void RemoveJobByIp (String ip) {
+               if (ip == null) 
+                       return;
+               String com = "SELECT databegin, JobID FROM ProteinLog WHERE ip = '" + ip + "';";
+               System.out.println("Command: " + com);
+               ResultSet results = session.execute(com);
+               if (!results.isExhausted()) {
+                       List<Row> rows = results.all();
+                       for (Row r : rows) {
+                               Long date = convertDate(r.getString("databegin"));
+                               String jobid = r.getString("JobID");
+                               if (date == null || jobid == null)
+                                       continue;
+                               RemoveJob(jobid, date);
+                       }
+               }
+       }
+       
+       public void RemoveJobBySequence (String seq) {
+               if (seq == null) 
+                       return;
+               String com = "SELECT JobID FROM ProteinRow WHERE Protein = '" + seq + "';";
+               System.out.println("Command: " + com);
+               ResultSet results = session.execute(com);
+               if (!results.isExhausted()) {
+                       List<Row> rows = results.all();
+                       for (Row r : rows) {
+                               String jobid = r.getString("JobID");
+                               if (jobid == null)
+                                       continue;
+                               Long date = FindDate(jobid);
+                               if (date == null)
+                                       continue;
+                               RemoveJob(jobid, date);
+                       }
+               }
+       }
+        
+       private Long FindDate(String jobid) {
+               String com = "SELECT databegin FROM ProteinLog WHERE JobID = '" + jobid + "';";
                System.out.println("Command: " + com);
                ResultSet results = session.execute(com);
                if (results.isExhausted())
                        return null;
-               final long queryTime = System.currentTimeMillis();
-               Row row = results.one();
-               String com1 = "SELECT * FROM ProteinRow WHERE JobID = '" + jobid + "' ALLOW FILTERING;";
-               System.out.println("Command: " + com1);
-               ResultSet results1 = session.execute(com1);
-               if (results1.isExhausted())
-                       return null;
-               Row row1 = results1.one();
-               StructureJobLog res = new StructureJobLog(row.getString("Protein"), row.getString("JobID"), row.getString("DataBegin"),
-                               row.getString("DataEnd"), row.getString("ip"), row1.getMap("Predictions", String.class, String.class));
-               System.out.println("Query time is " + (queryTime - startTime) + " msec");
-               final long endTime = System.currentTimeMillis();
-               System.out.println(" rows analysed, execution time is " + (endTime - startTime) + " msec");
-               return res;
+               Long date = convertDate(results.one().getString("databegin"));
+               return date;
        }
        
        
+       protected long convertDate (String d) {
+               try {
+                       if (null != d) {
+                               Date startdate = dateformatter.parse(d);
+                               return startdate.getTime();
+                       }
+               } catch (ParseException e) {
+                       e.printStackTrace();
+               }
+               return 0L;
+       }
 
 }