Mercurial Hosting > luan
annotate src/luan/modules/lucene/PostgresBackup.java @ 1428:d21a7cf8fa9e
fix postgres check
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 24 Nov 2019 21:02:38 -0700 |
parents | 225808b90cee |
children | 103d0ce70385 |
rev | line source |
---|---|
1387 | 1 package luan.modules.lucene; |
2 | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
3 import java.io.IOException; |
1387 | 4 import java.sql.Connection; |
5 import java.sql.DriverManager; | |
6 import java.sql.Statement; | |
7 import java.sql.PreparedStatement; | |
8 import java.sql.SQLException; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
9 import java.sql.ResultSet; |
1387 | 10 import java.util.Properties; |
1391 | 11 import java.util.List; |
12 import java.util.ArrayList; | |
1392 | 13 import java.util.Map; |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
14 import luan.Luan; |
1387 | 15 import luan.LuanTable; |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
16 import luan.LuanFunction; |
1387 | 17 import luan.LuanException; |
1392 | 18 import luan.modules.Utils; |
1387 | 19 import luan.modules.parsers.LuanToString; |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1399
diff
changeset
|
20 import goodjava.logging.Logger; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1399
diff
changeset
|
21 import goodjava.logging.LoggerFactory; |
1387 | 22 |
23 | |
24 final class PostgresBackup { | |
1393 | 25 private static final Logger sysLogger = LoggerFactory.getLogger(PostgresBackup.class); |
26 | |
27 private final Logger luanLogger; | |
1387 | 28 |
29 final boolean wasCreated; | |
1391 | 30 private final String url; |
31 private final Properties props = new Properties(); | |
1387 | 32 private final Connection con; |
33 private final PreparedStatement insertStmt; | |
34 private final PreparedStatement updateStmt; | |
35 private final PreparedStatement deleteStmt; | |
36 private int trans = 0; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
37 private final LuanToString luanToString = new LuanToString(); |
1387 | 38 |
1420 | 39 PostgresBackup(Luan luan,LuanTable spec) |
1392 | 40 throws ClassNotFoundException, SQLException, LuanException |
1387 | 41 { |
1420 | 42 spec = new LuanTable(spec); |
1393 | 43 this.luanLogger = luan.getLogger(PostgresBackup.class); |
1392 | 44 /* |
1387 | 45 Class.forName("org.postgresql.Driver"); |
1391 | 46 url = "jdbc:postgresql://localhost:5432/luan"; |
1387 | 47 props.setProperty("user","postgres"); |
48 props.setProperty("password",""); | |
1392 | 49 */ |
50 String cls = "org.postgresql.Driver"; | |
51 if( !Utils.removeRequiredString(spec,"class").equals(cls) ) | |
52 throw new LuanException( "parameter 'class' must be '"+cls+"'" ); | |
53 Class.forName(cls); | |
54 url = Utils.removeRequiredString(spec,"url"); | |
55 props.setProperty( "user", Utils.removeRequiredString(spec,"user") ); | |
56 props.setProperty( "password", Utils.removeRequiredString(spec,"password") ); | |
57 Utils.checkEmpty(spec); | |
1387 | 58 |
1391 | 59 con = newConnection(); |
1387 | 60 |
61 Statement stmt = con.createStatement(); | |
62 boolean hasTable = stmt.executeQuery( | |
63 "select * from information_schema.tables where table_name='lucene'" | |
64 ).next(); | |
65 if( !hasTable ) { | |
66 stmt.executeUpdate( | |
67 "create table lucene (" | |
68 +" id integer not null primary key," | |
69 +" data text not null" | |
70 +")" | |
71 ); | |
72 } | |
73 stmt.close(); | |
74 wasCreated = !hasTable; | |
75 | |
76 insertStmt = con.prepareStatement( | |
77 "insert into lucene (id,data) values (?,?)" | |
78 ); | |
79 updateStmt = con.prepareStatement( | |
80 "update lucene set data=? where id=?" | |
81 ); | |
82 deleteStmt = con.prepareStatement( | |
83 "delete from lucene where id=?" | |
84 ); | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
85 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
86 luanToString.strict = true; |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
87 luanToString.numberTypes = true; |
1391 | 88 } |
89 | |
90 Connection newConnection() throws SQLException { | |
91 return DriverManager.getConnection(url,props); | |
1387 | 92 } |
93 | |
1392 | 94 void close() throws SQLException { |
95 insertStmt.close(); | |
96 updateStmt.close(); | |
97 deleteStmt.close(); | |
98 con.close(); | |
1387 | 99 } |
100 | |
101 protected void finalize() throws Throwable { | |
1398 | 102 close(); |
1387 | 103 super.finalize(); |
104 } | |
105 | |
1392 | 106 void add(LuanTable doc) throws LuanException, SQLException { |
107 Long id = (Long)doc.get("id"); | |
108 String data = luanToString.toString(doc); | |
109 insertStmt.setLong(1,id); | |
110 insertStmt.setString(2,data); | |
111 insertStmt.executeUpdate(); | |
1387 | 112 } |
113 | |
1392 | 114 void update(LuanTable doc) throws LuanException, SQLException { |
115 Long id = (Long)doc.get("id"); | |
116 String data = luanToString.toString(doc); | |
117 updateStmt.setString(1,data); | |
118 updateStmt.setLong(2,id); | |
119 int n = updateStmt.executeUpdate(); | |
120 if( n==0 ) { | |
1393 | 121 luanLogger.error("update not found for id="+id+", trying add"); |
1392 | 122 add(doc); |
123 } else if( n!=1 ) | |
124 throw new RuntimeException(); | |
1387 | 125 } |
126 | |
1392 | 127 void deleteAll() throws SQLException { |
128 Statement stmt = con.createStatement(); | |
129 stmt.executeUpdate("delete from lucene"); | |
130 stmt.close(); | |
1387 | 131 } |
132 | |
1392 | 133 void delete(long id) throws SQLException, LuanException { |
134 deleteStmt.setLong(1,id); | |
135 int n = deleteStmt.executeUpdate(); | |
136 if( n==0 ) | |
137 throw new LuanException("delete not found for id="+id); | |
1387 | 138 } |
139 | |
1392 | 140 void begin() throws SQLException { |
141 if( trans++ == 0 ) | |
142 con.setAutoCommit(false); | |
1387 | 143 } |
144 | |
1392 | 145 void commit() throws SQLException, LuanException { |
146 if( trans <= 0 ) | |
147 throw new LuanException("commit not in transaction"); | |
148 if( --trans == 0 ) | |
1387 | 149 con.setAutoCommit(true); |
1392 | 150 } |
151 | |
152 void rollback() throws SQLException, LuanException { | |
153 if( --trans != 0 ) | |
154 throw new LuanException("rollback failed trans="+trans); | |
155 con.rollback(); | |
156 con.setAutoCommit(true); | |
1387 | 157 } |
158 | |
1391 | 159 private static LuanTable newEnv() { |
160 LuanTable env = new LuanTable(new Luan()); | |
161 LuanToString.addNumberTypes(env); | |
162 return env; | |
163 } | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
164 |
1391 | 165 private static Object eval(String s,LuanTable env) throws LuanException { |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
166 LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env ); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
167 return fn.call(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
168 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
169 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
170 void restoreLucene(LuceneIndex li) |
1392 | 171 throws LuanException, IOException, SQLException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
172 { |
1392 | 173 LuanTable env = newEnv(); |
174 Statement stmt = con.createStatement(); | |
175 ResultSet rs = stmt.executeQuery("select data from lucene"); | |
176 while( rs.next() ) { | |
177 String data = rs.getString("data"); | |
178 LuanTable doc = (LuanTable)eval(data,env); | |
179 li.restore(doc); | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
180 } |
1392 | 181 stmt.close(); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
182 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
183 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
184 long maxId() |
1392 | 185 throws LuanException, IOException, SQLException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
186 { |
1392 | 187 Statement stmt = con.createStatement(); |
188 ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); | |
189 rs.next(); | |
190 long m = rs.getLong("m"); | |
191 stmt.close(); | |
192 return m; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
193 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
194 |
1391 | 195 final class Checker { |
196 private final Connection con; | |
197 private final PreparedStatement pstmt; | |
198 private final LuanTable env = newEnv(); | |
199 | |
200 Checker() throws SQLException { | |
201 con = newConnection(); | |
202 con.setAutoCommit(false); | |
1428 | 203 con.setReadOnly(true); |
204 con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE); | |
205 | |
206 // hack to capture data in transaction | |
207 Statement stmt = con.createStatement(); | |
208 ResultSet rs = stmt.executeQuery("select 'x' from lucene"); | |
209 while(rs.next()); | |
210 stmt.close(); | |
211 | |
1391 | 212 pstmt = con.prepareStatement( |
213 "select data from lucene where id=?" | |
214 ); | |
215 } | |
216 | |
217 void close() throws SQLException { | |
218 pstmt.close(); | |
219 con.close(); | |
220 } | |
221 | |
222 List<Long> getIds() throws SQLException { | |
223 List<Long> ids = new ArrayList<Long>(); | |
224 Statement stmt = con.createStatement(); | |
225 ResultSet rs = stmt.executeQuery("select id from lucene order by id"); | |
226 while( rs.next() ) { | |
227 long id = rs.getLong("id"); | |
228 ids.add(id); | |
229 } | |
230 stmt.close(); | |
231 return ids; | |
232 } | |
233 | |
234 LuanTable getDoc(long id) throws SQLException, LuanException { | |
235 pstmt.setLong(1,id); | |
236 ResultSet rs = pstmt.executeQuery(); | |
1399 | 237 if( !rs.next() ) |
238 return null; | |
1391 | 239 String data = rs.getString("data"); |
240 LuanTable doc = (LuanTable)eval(data,env); | |
241 return doc; | |
242 } | |
243 } | |
244 | |
245 Checker newChecker() throws SQLException { | |
246 return new Checker(); | |
247 } | |
248 | |
1387 | 249 } |