break out SqliteTable
authoradam <adam@megacz.com>
Sun, 15 Jul 2007 22:01:35 +0000 (22:01 +0000)
committeradam <adam@megacz.com>
Sun, 15 Jul 2007 22:01:35 +0000 (22:01 +0000)
darcs-hash:20070715220135-5007d-864fbcf1bfda69cd7581730afcbf9fee711751f1.gz

src/org/ibex/mail/Graylist.java
src/org/ibex/mail/SqliteDB.java
src/org/ibex/mail/Whitelist.java

index ec1fe12..0a0ad53 100644 (file)
@@ -15,7 +15,7 @@ public class Graylist extends SqliteDB {
                   "create table if not exists 'whitelist' (ip unique)",
                   "create table if not exists 'graylist' (ip,fromaddr,toaddr,date, primary key(ip,fromaddr,toaddr))"
               });
-        reap("graylist", "date");
+        getTable("graylist").reap("date");
     }
 
     public synchronized void addWhitelist(InetAddress ip) {
index 39acded..e5fbc68 100644 (file)
@@ -15,8 +15,18 @@ public class SqliteDB {
 
     protected Connection conn;
     private String filename;
-    private String reapTable;
-    private String reapColumn;
+
+    private HashMap<String,SqliteTable> tables = new HashMap<String,SqliteTable>();
+
+    public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
+
+    public Connection getConnection() { return conn; }
+
+    public synchronized SqliteTable getTable(String name) {
+        SqliteTable ret = tables.get(name);
+        if (ret==null) ret = new SqliteTable(name);
+        return ret;
+    }
 
     // check upstream: PRAGMA encoding = "UTF-8"; 
     // create indices
@@ -47,18 +57,28 @@ public class SqliteDB {
         catch (ClassNotFoundException e) { throw new RuntimeException(e); }
     }
 
-    protected void reap(String reapTable, String reapColumn) {
-        if (this.reapTable != null || this.reapColumn != null)
-            throw new RuntimeException("reapTable/reapColumn already set");
-        this.reapTable = reapTable;
-        this.reapColumn = reapColumn;
-        if (reapTable != null && reapColumn != null)
-            Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, new Reaper());
+    public class SqliteTable {
+        public final String name;
+        private String reapColumn = null;
+        private SqliteTable(String name) {
+            this.name = name;
+            tables.put(name, this);
+        }
+        protected void reap(String reapColumn) {
+            if (this.reapColumn != null) throw new RuntimeException("reapColumn already set");
+            this.reapColumn = reapColumn;
+            Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, new Reaper(name, reapColumn));
+        }
     }
 
-    public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
-
+    // FIXME: desynchronized access to the conn?
     private class Reaper implements Runnable {
+        public Reaper(String reapTable, String reapColumn) {
+            this.reapTable = reapTable;
+            this.reapColumn = reapColumn;
+        }
+        private String reapTable;
+        private String reapColumn;
         public void run() {
             try {
                 Log.warn(Reaper.class, filename + " reaping...");
index e3bc233..f8fc4b1 100644 (file)
@@ -21,7 +21,7 @@ public class Whitelist extends SqliteDB {
                   "create table if not exists 'pending' (spamid,email,message,date)"
               },
               true);
-        reap("pending", "date");
+        getTable("pending").reap("date");
     }
 
     public boolean handleRequest(org.ibex.net.Connection c) {