0ee1401ed0432d00f1b7a696310356b7446ec9cc
[org.ibex.mail.git] / src / org / ibex / mail / SqliteDB.java
1 package org.ibex.mail;
2
3 import org.ibex.io.*;
4 import org.ibex.mail.protocol.*;
5 import org.ibex.util.*;
6 import org.ibex.net.*;
7 import java.sql.*;
8 import java.net.*;
9 import java.io.*;
10 import java.util.*;
11 import java.sql.Timestamp;
12 import java.sql.Connection;
13
14 public class SqliteDB {
15
16     protected Connection conn;
17     private String filename;
18
19     private HashMap<String,SqliteTable> tables = new HashMap<String,SqliteTable>();
20
21     public static final int REAPER_INTERVAL_SECONDS = 60 * 60;
22
23     public Connection getConnection() { return conn; }
24
25     public synchronized SqliteTable getTable(String name, String schema) throws SQLException {
26         SqliteTable ret = tables.get(name);
27         if (ret==null) ret = new SqliteTable(name, schema);
28         return ret;
29     }
30
31     // check upstream: PRAGMA encoding = "UTF-8"; 
32     // create indices
33     // periodically run "analyze"?
34
35     public void setCacheSize(int kilobytes) throws SQLException {
36         conn.prepareStatement("PRAGMA cache_size="+Math.ceil(kilobytes/1.5)+";").executeUpdate();
37     }
38
39     public SqliteDB(String filename) {
40         this.filename = filename;
41         try {
42             Class.forName("org.sqlite.JDBC");
43             conn = DriverManager.getConnection("jdbc:sqlite:"+filename);
44             conn.prepareStatement("PRAGMA auto_vacuum = 1").executeUpdate();
45             conn.prepareStatement("VACUUM").executeUpdate();
46
47             // until we have better assurances about locking on network filesystems...
48             conn.prepareStatement("PRAGMA locking_mode = EXCLUSIVE").executeQuery();
49
50             conn.prepareStatement("PRAGMA temp_store = MEMORY").executeUpdate();
51             conn.prepareStatement("PRAGMA page_size=4096").executeUpdate();
52             conn.prepareStatement("PRAGMA cache_size=2000").executeUpdate();
53             ResultSet rs = conn.prepareStatement("PRAGMA integrity_check").executeQuery();
54             rs.next();
55             String result = rs.getString(1);
56             if (!result.equals("ok"))
57                 throw new RuntimeException("PRAGMA integrity_check returned \""+result+"\"");
58         }
59         catch (SQLException e) { throw new RuntimeException(e); }
60         catch (ClassNotFoundException e) { throw new RuntimeException(e); }
61     }
62
63     public void setFastButDangerous(boolean on) throws SQLException {
64         conn.prepareStatement("PRAGMA synchronous = "+(on?"OFF":"ON")).executeUpdate();
65     }
66
67     /*
68     public void dump(OutputStream os) {
69     }
70     */
71
72     public class SqliteTable {
73         public final String name;
74         private String reapColumn = null;
75         private SqliteTable(String name, String schema) throws SQLException {
76             this.name = name;
77             PreparedStatement ps = conn.prepareStatement("create table if not exists " + name + " " + schema);
78             ps.executeUpdate();
79             tables.put(name, this);
80         }
81         public void createIndex(String name, String column) throws SQLException {
82             PreparedStatement ps = conn.prepareStatement("create index if not exists "+name+" on "+column);
83             ps.executeUpdate();
84         }
85         protected void reap(String reapColumn) {
86             if (this.reapColumn != null) throw new RuntimeException("reapColumn already set");
87             this.reapColumn = reapColumn;
88             Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, new Reaper(name, reapColumn));
89         }
90     }
91
92     // FIXME: desynchronized access to the conn?
93     private class Reaper implements Runnable {
94         public Reaper(String reapTable, String reapColumn) {
95             this.reapTable = reapTable;
96             this.reapColumn = reapColumn;
97         }
98         private String reapTable;
99         private String reapColumn;
100         public void run() {
101             try {
102                 Log.warn(Reaper.class, filename + " reaping...");
103                 long when = System.currentTimeMillis();
104                 when -= 5 * 24 * 60 * 60 * 1000;
105                 synchronized(SqliteDB.this) {
106                     PreparedStatement ps =
107                         conn.prepareStatement("select count(*) from "+reapTable+" where "+reapColumn+"<?");
108                     ps.setTimestamp(1, new Timestamp(when));
109                     ResultSet rs = ps.executeQuery();
110                     if (rs.next())
111                         Log.warn(Reaper.class, filename + " reaping " + rs.getInt(1) + " entries");
112                     Log.warn(Reaper.class, filename + ": " + "delete from "+reapTable+" where "+reapColumn+"<"+when);
113                     ps = conn.prepareStatement("delete from "+reapTable+" where "+reapColumn+"<?");
114                     ps.setTimestamp(1, new Timestamp(when));
115                     int rows = ps.executeUpdate();
116                     Log.warn(Reaper.class, filename + " done reaping; removed " + rows + " rows");
117                 }
118             } catch (Exception e) { Log.error(Reaper.class, e); }
119             Main.cron.executeLater(1000 * REAPER_INTERVAL_SECONDS, this);
120         }
121     }
122
123     static String streamToString(Stream stream) throws Exception {
124         // FIXME
125         StringBuffer b = new StringBuffer();
126         for(String s = stream.readln(); s!=null; s=stream.readln())
127             b.append(s+"\n");
128         return b.toString();
129     }
130 }