Mercurial Hosting > luan
annotate src/luan/modules/lucene/PostgresBackup.java @ 1447:851b9a48cc44
Luan.parse
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 18 Feb 2020 14:54:35 -0700 |
parents | 56fb5cd8228d |
children | 928be2a4d565 |
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; |
1447 | 20 import luan.modules.parsers.LuanParser; |
21 import goodjava.parser.ParseException; | |
1402
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1399
diff
changeset
|
22 import goodjava.logging.Logger; |
27efb1fcbcb5
move luan.lib to goodjava
Franklin Schmidt <fschmidt@gmail.com>
parents:
1399
diff
changeset
|
23 import goodjava.logging.LoggerFactory; |
1387 | 24 |
25 | |
26 final class PostgresBackup { | |
1393 | 27 private static final Logger sysLogger = LoggerFactory.getLogger(PostgresBackup.class); |
28 | |
29 private final Logger luanLogger; | |
1387 | 30 |
31 final boolean wasCreated; | |
1391 | 32 private final String url; |
33 private final Properties props = new Properties(); | |
1387 | 34 private final Connection con; |
35 private final PreparedStatement insertStmt; | |
36 private final PreparedStatement updateStmt; | |
37 private final PreparedStatement deleteStmt; | |
38 private int trans = 0; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
39 private final LuanToString luanToString = new LuanToString(); |
1387 | 40 |
1420 | 41 PostgresBackup(Luan luan,LuanTable spec) |
1392 | 42 throws ClassNotFoundException, SQLException, LuanException |
1387 | 43 { |
1420 | 44 spec = new LuanTable(spec); |
1393 | 45 this.luanLogger = luan.getLogger(PostgresBackup.class); |
1392 | 46 /* |
1387 | 47 Class.forName("org.postgresql.Driver"); |
1391 | 48 url = "jdbc:postgresql://localhost:5432/luan"; |
1387 | 49 props.setProperty("user","postgres"); |
50 props.setProperty("password",""); | |
1392 | 51 */ |
52 String cls = "org.postgresql.Driver"; | |
53 if( !Utils.removeRequiredString(spec,"class").equals(cls) ) | |
54 throw new LuanException( "parameter 'class' must be '"+cls+"'" ); | |
55 Class.forName(cls); | |
56 url = Utils.removeRequiredString(spec,"url"); | |
57 props.setProperty( "user", Utils.removeRequiredString(spec,"user") ); | |
58 props.setProperty( "password", Utils.removeRequiredString(spec,"password") ); | |
59 Utils.checkEmpty(spec); | |
1387 | 60 |
1391 | 61 con = newConnection(); |
1387 | 62 |
63 Statement stmt = con.createStatement(); | |
64 boolean hasTable = stmt.executeQuery( | |
65 "select * from information_schema.tables where table_name='lucene'" | |
66 ).next(); | |
67 if( !hasTable ) { | |
68 stmt.executeUpdate( | |
69 "create table lucene (" | |
70 +" id integer not null primary key," | |
71 +" data text not null" | |
72 +")" | |
73 ); | |
74 } | |
75 stmt.close(); | |
76 wasCreated = !hasTable; | |
77 | |
78 insertStmt = con.prepareStatement( | |
79 "insert into lucene (id,data) values (?,?)" | |
80 ); | |
81 updateStmt = con.prepareStatement( | |
82 "update lucene set data=? where id=?" | |
83 ); | |
84 deleteStmt = con.prepareStatement( | |
85 "delete from lucene where id=?" | |
86 ); | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
87 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
88 luanToString.strict = true; |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
89 luanToString.numberTypes = true; |
1391 | 90 } |
91 | |
92 Connection newConnection() throws SQLException { | |
93 return DriverManager.getConnection(url,props); | |
1387 | 94 } |
95 | |
1392 | 96 void close() throws SQLException { |
97 insertStmt.close(); | |
98 updateStmt.close(); | |
99 deleteStmt.close(); | |
100 con.close(); | |
1387 | 101 } |
102 | |
103 protected void finalize() throws Throwable { | |
1398 | 104 close(); |
1387 | 105 super.finalize(); |
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 | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
161 void restoreLucene(LuceneIndex li) |
1447 | 162 throws LuanException, IOException, SQLException, ParseException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
163 { |
1447 | 164 Luan luan = new Luan(); |
1392 | 165 Statement stmt = con.createStatement(); |
166 ResultSet rs = stmt.executeQuery("select data from lucene"); | |
167 while( rs.next() ) { | |
168 String data = rs.getString("data"); | |
1447 | 169 LuanTable doc = (LuanTable)LuanParser.parse(luan,data); |
1392 | 170 li.restore(doc); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
171 } |
1392 | 172 stmt.close(); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
173 } |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
174 |
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
175 long maxId() |
1392 | 176 throws LuanException, IOException, SQLException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
177 { |
1392 | 178 Statement stmt = con.createStatement(); |
179 ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); | |
180 rs.next(); | |
181 long m = rs.getLong("m"); | |
182 stmt.close(); | |
183 return m; | |
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 |
1391 | 186 final class Checker { |
187 private final Connection con; | |
188 private final PreparedStatement pstmt; | |
1447 | 189 private final Luan luan = new Luan(); |
1391 | 190 |
191 Checker() throws SQLException { | |
192 con = newConnection(); | |
1428 | 193 con.setReadOnly(true); |
1391 | 194 pstmt = con.prepareStatement( |
195 "select data from lucene where id=?" | |
196 ); | |
197 } | |
198 | |
199 void close() throws SQLException { | |
200 pstmt.close(); | |
201 con.close(); | |
202 } | |
203 | |
204 List<Long> getIds() throws SQLException { | |
205 List<Long> ids = new ArrayList<Long>(); | |
206 Statement stmt = con.createStatement(); | |
207 ResultSet rs = stmt.executeQuery("select id from lucene order by id"); | |
208 while( rs.next() ) { | |
209 long id = rs.getLong("id"); | |
210 ids.add(id); | |
211 } | |
212 stmt.close(); | |
213 return ids; | |
214 } | |
215 | |
1447 | 216 LuanTable getDoc(long id) throws SQLException, ParseException { |
1391 | 217 pstmt.setLong(1,id); |
218 ResultSet rs = pstmt.executeQuery(); | |
1399 | 219 if( !rs.next() ) |
220 return null; | |
1391 | 221 String data = rs.getString("data"); |
1447 | 222 LuanTable doc = (LuanTable)LuanParser.parse(luan,data); |
1391 | 223 return doc; |
224 } | |
225 } | |
226 | |
227 Checker newChecker() throws SQLException { | |
228 return new Checker(); | |
229 } | |
230 | |
1387 | 231 } |