Mercurial Hosting > luan
annotate src/luan/modules/lucene/PostgresBackup.java @ 1399:38a1c1b4279a
fix
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 11 Sep 2019 16:28:38 -0600 |
parents | 67c0e47b5be3 |
children | 27efb1fcbcb5 |
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 { | |
1398 | 101 close(); |
1387 | 102 super.finalize(); |
103 } | |
104 | |
1392 | 105 void add(LuanTable doc) throws LuanException, SQLException { |
106 Long id = (Long)doc.get("id"); | |
107 String data = luanToString.toString(doc); | |
108 insertStmt.setLong(1,id); | |
109 insertStmt.setString(2,data); | |
110 insertStmt.executeUpdate(); | |
1387 | 111 } |
112 | |
1392 | 113 void update(LuanTable doc) throws LuanException, SQLException { |
114 Long id = (Long)doc.get("id"); | |
115 String data = luanToString.toString(doc); | |
116 updateStmt.setString(1,data); | |
117 updateStmt.setLong(2,id); | |
118 int n = updateStmt.executeUpdate(); | |
119 if( n==0 ) { | |
1393 | 120 luanLogger.error("update not found for id="+id+", trying add"); |
1392 | 121 add(doc); |
122 } else if( n!=1 ) | |
123 throw new RuntimeException(); | |
1387 | 124 } |
125 | |
1392 | 126 void deleteAll() throws SQLException { |
127 Statement stmt = con.createStatement(); | |
128 stmt.executeUpdate("delete from lucene"); | |
129 stmt.close(); | |
1387 | 130 } |
131 | |
1392 | 132 void delete(long id) throws SQLException, LuanException { |
133 deleteStmt.setLong(1,id); | |
134 int n = deleteStmt.executeUpdate(); | |
135 if( n==0 ) | |
136 throw new LuanException("delete not found for id="+id); | |
1387 | 137 } |
138 | |
1392 | 139 void begin() throws SQLException { |
140 if( trans++ == 0 ) | |
141 con.setAutoCommit(false); | |
1387 | 142 } |
143 | |
1392 | 144 void commit() throws SQLException, LuanException { |
145 if( trans <= 0 ) | |
146 throw new LuanException("commit not in transaction"); | |
147 if( --trans == 0 ) | |
1387 | 148 con.setAutoCommit(true); |
1392 | 149 } |
150 | |
151 void rollback() throws SQLException, LuanException { | |
152 if( --trans != 0 ) | |
153 throw new LuanException("rollback failed trans="+trans); | |
154 con.rollback(); | |
155 con.setAutoCommit(true); | |
1387 | 156 } |
157 | |
1391 | 158 private static LuanTable newEnv() { |
159 LuanTable env = new LuanTable(new Luan()); | |
160 LuanToString.addNumberTypes(env); | |
161 return env; | |
162 } | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
163 |
1391 | 164 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
|
165 LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env ); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
166 return fn.call(); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
167 } |
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 void restoreLucene(LuceneIndex li) |
1392 | 170 throws LuanException, IOException, SQLException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
171 { |
1392 | 172 LuanTable env = newEnv(); |
173 Statement stmt = con.createStatement(); | |
174 ResultSet rs = stmt.executeQuery("select data from lucene"); | |
175 while( rs.next() ) { | |
176 String data = rs.getString("data"); | |
177 LuanTable doc = (LuanTable)eval(data,env); | |
178 li.restore(doc); | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
179 } |
1392 | 180 stmt.close(); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
181 } |
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 long maxId() |
1392 | 184 throws LuanException, IOException, SQLException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
185 { |
1392 | 186 Statement stmt = con.createStatement(); |
187 ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); | |
188 rs.next(); | |
189 long m = rs.getLong("m"); | |
190 stmt.close(); | |
191 return m; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
192 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
193 |
1391 | 194 final class Checker { |
195 private final Connection con; | |
196 private final PreparedStatement pstmt; | |
197 private final LuanTable env = newEnv(); | |
198 | |
199 Checker() throws SQLException { | |
200 con = newConnection(); | |
201 con.setAutoCommit(false); | |
202 pstmt = con.prepareStatement( | |
203 "select data from lucene where id=?" | |
204 ); | |
205 } | |
206 | |
207 void close() throws SQLException { | |
208 pstmt.close(); | |
209 con.close(); | |
210 } | |
211 | |
212 List<Long> getIds() throws SQLException { | |
213 List<Long> ids = new ArrayList<Long>(); | |
214 Statement stmt = con.createStatement(); | |
215 ResultSet rs = stmt.executeQuery("select id from lucene order by id"); | |
216 while( rs.next() ) { | |
217 long id = rs.getLong("id"); | |
218 ids.add(id); | |
219 } | |
220 stmt.close(); | |
221 return ids; | |
222 } | |
223 | |
224 LuanTable getDoc(long id) throws SQLException, LuanException { | |
225 pstmt.setLong(1,id); | |
226 ResultSet rs = pstmt.executeQuery(); | |
1399 | 227 if( !rs.next() ) |
228 return null; | |
1391 | 229 String data = rs.getString("data"); |
230 LuanTable doc = (LuanTable)eval(data,env); | |
231 return doc; | |
232 } | |
233 } | |
234 | |
235 Checker newChecker() throws SQLException { | |
236 return new Checker(); | |
237 } | |
238 | |
1387 | 239 } |