Mercurial Hosting > luan
annotate src/luan/modules/lucene/PostgresBackup.java @ 1391:94f48cc76de8
add lucene check
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 05 Sep 2019 01:29:57 -0600 |
parents | 2024d23ddd64 |
children | 002152af497a |
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; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
13 import luan.Luan; |
1387 | 14 import luan.LuanTable; |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
15 import luan.LuanFunction; |
1387 | 16 import luan.LuanException; |
17 import luan.modules.parsers.LuanToString; | |
18 import luan.lib.logging.Logger; | |
19 import luan.lib.logging.LoggerFactory; | |
20 | |
21 | |
22 final class PostgresBackup { | |
23 private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class); | |
24 | |
25 static PostgresBackup newInstance() { | |
26 try { | |
27 return new PostgresBackup(); | |
28 } catch(ClassNotFoundException e) { | |
29 logger.error("creation failed",e); | |
30 return null; | |
31 } catch(SQLException e) { | |
32 logger.error("creation failed",e); | |
33 return null; | |
34 } | |
35 } | |
36 | |
37 final boolean wasCreated; | |
1391 | 38 private final String url; |
39 private final Properties props = new Properties(); | |
1387 | 40 private final Connection con; |
41 private final PreparedStatement insertStmt; | |
42 private final PreparedStatement updateStmt; | |
43 private final PreparedStatement deleteStmt; | |
44 private int trans = 0; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
45 private final LuanToString luanToString = new LuanToString(); |
1387 | 46 |
47 private PostgresBackup() | |
48 throws ClassNotFoundException, SQLException | |
49 { | |
50 Class.forName("org.postgresql.Driver"); | |
51 | |
1391 | 52 url = "jdbc:postgresql://localhost:5432/luan"; |
1387 | 53 props.setProperty("user","postgres"); |
54 props.setProperty("password",""); | |
55 | |
1391 | 56 con = newConnection(); |
1387 | 57 |
58 Statement stmt = con.createStatement(); | |
59 boolean hasTable = stmt.executeQuery( | |
60 "select * from information_schema.tables where table_name='lucene'" | |
61 ).next(); | |
62 if( !hasTable ) { | |
63 stmt.executeUpdate( | |
64 "create table lucene (" | |
65 +" id integer not null primary key," | |
66 +" data text not null" | |
67 +")" | |
68 ); | |
69 } | |
70 stmt.close(); | |
71 wasCreated = !hasTable; | |
72 | |
73 insertStmt = con.prepareStatement( | |
74 "insert into lucene (id,data) values (?,?)" | |
75 ); | |
76 updateStmt = con.prepareStatement( | |
77 "update lucene set data=? where id=?" | |
78 ); | |
79 deleteStmt = con.prepareStatement( | |
80 "delete from lucene where id=?" | |
81 ); | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
82 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
83 luanToString.strict = true; |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
84 luanToString.numberTypes = true; |
1391 | 85 } |
86 | |
87 Connection newConnection() throws SQLException { | |
88 return DriverManager.getConnection(url,props); | |
1387 | 89 } |
90 | |
91 void close() { | |
92 try { | |
93 insertStmt.close(); | |
94 updateStmt.close(); | |
95 deleteStmt.close(); | |
96 con.close(); | |
97 } catch(SQLException e) { | |
98 logger.error("close failed",e); | |
99 } | |
100 } | |
101 | |
102 protected void finalize() throws Throwable { | |
103 super.finalize(); | |
104 if( !con.isClosed() ) { | |
105 logger.error("con not closed"); | |
106 con.close(); | |
107 } | |
108 } | |
109 | |
1391 | 110 void add(LuanTable doc) throws LuanException { |
1387 | 111 try { |
1391 | 112 Long id = (Long)doc.get("id"); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
113 String data = luanToString.toString(doc); |
1387 | 114 insertStmt.setLong(1,id); |
115 insertStmt.setString(2,data); | |
116 insertStmt.executeUpdate(); | |
117 } catch(SQLException e) { | |
118 logger.error("add failed",e); | |
119 } | |
120 } | |
121 | |
1391 | 122 void update(LuanTable doc) throws LuanException { |
1387 | 123 try { |
1391 | 124 Long id = (Long)doc.get("id"); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
125 String data = luanToString.toString(doc); |
1387 | 126 updateStmt.setString(1,data); |
127 updateStmt.setLong(2,id); | |
128 int n = updateStmt.executeUpdate(); | |
129 if( n==0 ) { | |
130 logger.error("update not found for id="+id+", trying add"); | |
1391 | 131 add(doc); |
1387 | 132 } else if( n!=1 ) |
133 throw new RuntimeException(); | |
134 } catch(SQLException e) { | |
135 logger.error("update failed",e); | |
136 } | |
137 } | |
138 | |
139 void deleteAll() { | |
140 try { | |
141 Statement stmt = con.createStatement(); | |
142 stmt.executeUpdate("delete from lucene"); | |
143 stmt.close(); | |
144 } catch(SQLException e) { | |
145 logger.error("update failed",e); | |
146 } | |
147 } | |
148 | |
149 void delete(long id) { | |
150 try { | |
151 deleteStmt.setLong(1,id); | |
152 int n = deleteStmt.executeUpdate(); | |
153 if( n==0 ) { | |
154 logger.error("delete not found for id="+id); | |
155 } | |
156 } catch(SQLException e) { | |
157 logger.error("update failed",e); | |
158 } | |
159 } | |
160 | |
161 void begin() { | |
162 try { | |
163 if( trans++ == 0 ) | |
164 con.setAutoCommit(false); | |
165 } catch(SQLException e) { | |
166 logger.error("begin failed",e); | |
167 } | |
168 } | |
169 | |
170 void commit() { | |
171 try { | |
172 if( trans <= 0 ) { | |
173 logger.error("commit not in transaction"); | |
174 return; | |
175 } | |
176 if( --trans == 0 ) | |
177 con.setAutoCommit(true); | |
178 } catch(SQLException e) { | |
179 logger.error("begin failed",e); | |
180 } | |
181 } | |
182 | |
183 void rollback() { | |
184 try { | |
185 if( --trans != 0 ) { | |
186 logger.error("rollback failed trans="+trans); | |
187 return; | |
188 } | |
189 con.rollback(); | |
190 con.setAutoCommit(true); | |
191 } catch(SQLException e) { | |
192 logger.error("begin failed",e); | |
193 } | |
194 } | |
195 | |
1391 | 196 private static LuanTable newEnv() { |
197 LuanTable env = new LuanTable(new Luan()); | |
198 LuanToString.addNumberTypes(env); | |
199 return env; | |
200 } | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
201 |
1391 | 202 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
|
203 LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env ); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
204 return fn.call(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
205 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
206 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
207 void restoreLucene(LuceneIndex li) |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
208 throws LuanException, IOException |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
209 { |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
210 try { |
1391 | 211 LuanTable env = newEnv(); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
212 Statement stmt = con.createStatement(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
213 ResultSet rs = stmt.executeQuery("select data from lucene"); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
214 while( rs.next() ) { |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
215 String data = rs.getString("data"); |
1391 | 216 LuanTable doc = (LuanTable)eval(data,env); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
217 li.restore(doc); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
218 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
219 stmt.close(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
220 } catch(SQLException e) { |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
221 logger.error("restoreLucene failed",e); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
222 throw new RuntimeException(e); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
223 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
224 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
225 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
226 long maxId() |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
227 throws LuanException, IOException |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
228 { |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
229 try { |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
230 Statement stmt = con.createStatement(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
231 ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
232 rs.next(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
233 long m = rs.getLong("m"); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
234 stmt.close(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
235 return m; |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
236 } catch(SQLException e) { |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
237 logger.error("maxId failed",e); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
238 throw new RuntimeException(e); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
239 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
240 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
241 |
1391 | 242 final class Checker { |
243 private final Connection con; | |
244 private final PreparedStatement pstmt; | |
245 private final LuanTable env = newEnv(); | |
246 | |
247 Checker() throws SQLException { | |
248 con = newConnection(); | |
249 con.setAutoCommit(false); | |
250 pstmt = con.prepareStatement( | |
251 "select data from lucene where id=?" | |
252 ); | |
253 } | |
254 | |
255 void close() throws SQLException { | |
256 pstmt.close(); | |
257 con.close(); | |
258 } | |
259 | |
260 List<Long> getIds() throws SQLException { | |
261 List<Long> ids = new ArrayList<Long>(); | |
262 Statement stmt = con.createStatement(); | |
263 ResultSet rs = stmt.executeQuery("select id from lucene order by id"); | |
264 while( rs.next() ) { | |
265 long id = rs.getLong("id"); | |
266 ids.add(id); | |
267 } | |
268 stmt.close(); | |
269 return ids; | |
270 } | |
271 | |
272 LuanTable getDoc(long id) throws SQLException, LuanException { | |
273 pstmt.setLong(1,id); | |
274 ResultSet rs = pstmt.executeQuery(); | |
275 rs.next(); | |
276 String data = rs.getString("data"); | |
277 LuanTable doc = (LuanTable)eval(data,env); | |
278 return doc; | |
279 } | |
280 } | |
281 | |
282 Checker newChecker() throws SQLException { | |
283 return new Checker(); | |
284 } | |
285 | |
1387 | 286 } |