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