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