switch getMailExchangerIPs() to DNSUtil
[org.ibex.mail.git] / src / org / ibex / mail / SqliteDB.java
index 900e07c..4b818ce 100644 (file)
@@ -19,6 +19,8 @@ public class SqliteDB {
     private HashMap<String,SqliteTable> tables = new HashMap<String,SqliteTable>();
 
     public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
+    private static final int DAYS = 24 * 60 * 60 * 1000;
+    public static final int REAP_EXPIRATION = 5 * DAYS;
 
     public Connection getConnection() { return conn; }
 
@@ -43,8 +45,10 @@ public class SqliteDB {
             conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
             conn.prepareStatement("PRAGMA auto_vacuum = 1").executeUpdate();
             conn.prepareStatement("VACUUM").executeUpdate();
+
             // until we have better assurances about locking on network filesystems...
-            conn.prepareStatement("PRAGMA locking_mode = EXCLUSIVE").executeUpdate();
+            conn.prepareStatement("PRAGMA locking_mode = EXCLUSIVE").executeQuery();
+
             conn.prepareStatement("PRAGMA temp_store = MEMORY").executeUpdate();
             conn.prepareStatement("PRAGMA page_size=4096").executeUpdate();
             conn.prepareStatement("PRAGMA cache_size=2000").executeUpdate();
@@ -72,15 +76,13 @@ public class SqliteDB {
         private String reapColumn = null;
         private SqliteTable(String name, String schema) throws SQLException {
             this.name = name;
-            PreparedStatement ps = conn.prepareStatement("create table if not exists ?");
-            ps.setString(1, name+" "+schema);
+            PreparedStatement ps = conn.prepareStatement("create table if not exists " + name + " " + schema);
             ps.executeUpdate();
             tables.put(name, this);
         }
-        public void createIndex(String name, String column) throws SQLException {
-            PreparedStatement ps = conn.prepareStatement("create index if not exists ? on ?");
-            ps.setString(1, name);
-            ps.setString(2, column);
+        public void createIndex(String column) throws SQLException { createIndex(column, column+"_index"); }
+        public void createIndex(String indexName, String column) throws SQLException {
+            PreparedStatement ps = conn.prepareStatement("create index if not exists "+column+" on "+name+" ("+indexName+")");
             ps.executeUpdate();
         }
         protected void reap(String reapColumn) {
@@ -102,7 +104,7 @@ public class SqliteDB {
             try {
                 Log.warn(Reaper.class, filename + " reaping...");
                 long when = System.currentTimeMillis();
-                when -= 5 * 24 * 60 * 60 * 1000;
+                when -= REAP_EXPIRATION;
                 synchronized(SqliteDB.this) {
                     PreparedStatement ps =
                         conn.prepareStatement("select count(*) from "+reapTable+" where "+reapColumn+"<?");
@@ -121,4 +123,11 @@ public class SqliteDB {
         }
     }
 
+    static String streamToString(Stream stream) throws Exception {
+        // FIXME
+        StringBuffer b = new StringBuffer();
+        for(String s = stream.readln(); s!=null; s=stream.readln())
+            b.append(s+"\n");
+        return b.toString();
+    }
 }