4 import org.ibex.mail.protocol.*;
5 import org.ibex.util.*;
11 import java.sql.Timestamp;
12 import java.sql.Connection;
14 public class SqliteTable {
16 protected Connection conn;
17 private String filename;
18 private String reapTable;
19 private String reapColumn;
21 public SqliteTable(String filename, String[] tables, String reapTable, String reapColumn) {
22 this.filename = filename;
24 Class.forName("org.sqlite.JDBC");
25 conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
26 for(String s : tables)
27 conn.prepareStatement(s).executeUpdate();
29 catch (SQLException e) { throw new RuntimeException(e); }
30 catch (ClassNotFoundException e) { throw new RuntimeException(e); }
31 this.reapTable = reapTable;
32 this.reapColumn = reapColumn;
33 if (reapTable != null && reapColumn != null)
37 public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
39 private class Reaper extends Thread {
44 try { Thread.sleep(1000 * REAPER_INTERVAL_SECONDS); } catch (Exception e) { };
45 Log.warn(Reaper.class, filename + " reaping...");
46 long when = System.currentTimeMillis();
47 when -= 5 * 24 * 60 * 60 * 1000;
48 synchronized(SqliteTable.this) {
49 PreparedStatement ps = conn.prepareStatement("select count(*) from "+reapTable+" where "+reapColumn+"<?");
50 ps.setTimestamp(1, new Timestamp(when));
51 ResultSet rs = ps.executeQuery();
53 Log.warn(Reaper.class, filename + " reaping " + rs.getInt(1) + " entries");
54 Log.warn(Reaper.class, filename + ": " + "delete from "+reapTable+" where "+reapColumn+"<"+when);
55 ps = conn.prepareStatement("delete from "+reapTable+" where "+reapColumn+"<?");
56 ps.setTimestamp(1, new Timestamp(when));
57 int rows = ps.executeUpdate();
58 Log.warn(Reaper.class, filename + " done reaping; removed " + rows + " rows");
60 } catch (Exception e) { Log.error(Reaper.class, e); }