Mercurial Hosting > luan
annotate src/luan/modules/lucene/PostgresBackup.java @ 1392:002152af497a
hosted postgres
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 06 Sep 2019 00:19:47 -0600 |
parents | 94f48cc76de8 |
children | cc0dbca576dc |
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; |
1392 | 20 import luan.modules.logging.LuanLogger; |
1387 | 21 import luan.lib.logging.Logger; |
22 import luan.lib.logging.LoggerFactory; | |
23 | |
24 | |
25 final class PostgresBackup { | |
26 private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class); | |
27 | |
28 final boolean wasCreated; | |
1391 | 29 private final String url; |
30 private final Properties props = new Properties(); | |
1387 | 31 private final Connection con; |
32 private final PreparedStatement insertStmt; | |
33 private final PreparedStatement updateStmt; | |
34 private final PreparedStatement deleteStmt; | |
35 private int trans = 0; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
36 private final LuanToString luanToString = new LuanToString(); |
1387 | 37 |
1392 | 38 PostgresBackup(Map spec) |
39 throws ClassNotFoundException, SQLException, LuanException | |
1387 | 40 { |
1392 | 41 /* |
1387 | 42 Class.forName("org.postgresql.Driver"); |
1391 | 43 url = "jdbc:postgresql://localhost:5432/luan"; |
1387 | 44 props.setProperty("user","postgres"); |
45 props.setProperty("password",""); | |
1392 | 46 */ |
47 String cls = "org.postgresql.Driver"; | |
48 if( !Utils.removeRequiredString(spec,"class").equals(cls) ) | |
49 throw new LuanException( "parameter 'class' must be '"+cls+"'" ); | |
50 Class.forName(cls); | |
51 url = Utils.removeRequiredString(spec,"url"); | |
52 props.setProperty( "user", Utils.removeRequiredString(spec,"user") ); | |
53 props.setProperty( "password", Utils.removeRequiredString(spec,"password") ); | |
54 Utils.checkEmpty(spec); | |
1387 | 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 | |
1392 | 91 void close() throws SQLException { |
92 insertStmt.close(); | |
93 updateStmt.close(); | |
94 deleteStmt.close(); | |
95 con.close(); | |
1387 | 96 } |
97 | |
98 protected void finalize() throws Throwable { | |
99 super.finalize(); | |
100 if( !con.isClosed() ) { | |
101 logger.error("con not closed"); | |
102 con.close(); | |
103 } | |
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 ) { | |
121 Logger logger = LuanLogger.getLogger(doc.luan(),PostgresBackup.class); | |
122 logger.error("update not found for id="+id+", trying add"); | |
123 add(doc); | |
124 } else if( n!=1 ) | |
125 throw new RuntimeException(); | |
1387 | 126 } |
127 | |
1392 | 128 void deleteAll() throws SQLException { |
129 Statement stmt = con.createStatement(); | |
130 stmt.executeUpdate("delete from lucene"); | |
131 stmt.close(); | |
1387 | 132 } |
133 | |
1392 | 134 void delete(long id) throws SQLException, LuanException { |
135 deleteStmt.setLong(1,id); | |
136 int n = deleteStmt.executeUpdate(); | |
137 if( n==0 ) | |
138 throw new LuanException("delete not found for id="+id); | |
1387 | 139 } |
140 | |
1392 | 141 void begin() throws SQLException { |
142 if( trans++ == 0 ) | |
143 con.setAutoCommit(false); | |
1387 | 144 } |
145 | |
1392 | 146 void commit() throws SQLException, LuanException { |
147 if( trans <= 0 ) | |
148 throw new LuanException("commit not in transaction"); | |
149 if( --trans == 0 ) | |
1387 | 150 con.setAutoCommit(true); |
1392 | 151 } |
152 | |
153 void rollback() throws SQLException, LuanException { | |
154 if( --trans != 0 ) | |
155 throw new LuanException("rollback failed trans="+trans); | |
156 con.rollback(); | |
157 con.setAutoCommit(true); | |
1387 | 158 } |
159 | |
1391 | 160 private static LuanTable newEnv() { |
161 LuanTable env = new LuanTable(new Luan()); | |
162 LuanToString.addNumberTypes(env); | |
163 return env; | |
164 } | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
165 |
1391 | 166 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
|
167 LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env ); |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
168 return fn.call(); |
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 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
171 void restoreLucene(LuceneIndex li) |
1392 | 172 throws LuanException, IOException, SQLException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
173 { |
1392 | 174 LuanTable env = newEnv(); |
175 Statement stmt = con.createStatement(); | |
176 ResultSet rs = stmt.executeQuery("select data from lucene"); | |
177 while( rs.next() ) { | |
178 String data = rs.getString("data"); | |
179 LuanTable doc = (LuanTable)eval(data,env); | |
180 li.restore(doc); | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
181 } |
1392 | 182 stmt.close(); |
1388
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 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
185 long maxId() |
1392 | 186 throws LuanException, IOException, SQLException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
187 { |
1392 | 188 Statement stmt = con.createStatement(); |
189 ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); | |
190 rs.next(); | |
191 long m = rs.getLong("m"); | |
192 stmt.close(); | |
193 return m; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
194 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
195 |
1391 | 196 final class Checker { |
197 private final Connection con; | |
198 private final PreparedStatement pstmt; | |
199 private final LuanTable env = newEnv(); | |
200 | |
201 Checker() throws SQLException { | |
202 con = newConnection(); | |
203 con.setAutoCommit(false); | |
204 pstmt = con.prepareStatement( | |
205 "select data from lucene where id=?" | |
206 ); | |
207 } | |
208 | |
209 void close() throws SQLException { | |
210 pstmt.close(); | |
211 con.close(); | |
212 } | |
213 | |
214 List<Long> getIds() throws SQLException { | |
215 List<Long> ids = new ArrayList<Long>(); | |
216 Statement stmt = con.createStatement(); | |
217 ResultSet rs = stmt.executeQuery("select id from lucene order by id"); | |
218 while( rs.next() ) { | |
219 long id = rs.getLong("id"); | |
220 ids.add(id); | |
221 } | |
222 stmt.close(); | |
223 return ids; | |
224 } | |
225 | |
226 LuanTable getDoc(long id) throws SQLException, LuanException { | |
227 pstmt.setLong(1,id); | |
228 ResultSet rs = pstmt.executeQuery(); | |
229 rs.next(); | |
230 String data = rs.getString("data"); | |
231 LuanTable doc = (LuanTable)eval(data,env); | |
232 return doc; | |
233 } | |
234 } | |
235 | |
236 Checker newChecker() throws SQLException { | |
237 return new Checker(); | |
238 } | |
239 | |
1387 | 240 } |