Mercurial Hosting > luan
annotate src/luan/modules/lucene/PostgresBackup.java @ 1562:b89212fd04b5
remove table.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 08 Nov 2020 16:50:59 -0700 |
parents | 26c51acf00f3 |
children | 8fbcc4747091 |
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 { | |
1454 | 27 private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class); |
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; | |
1562 | 37 private final LuanToString luanToString; |
1387 | 38 |
1420 | 39 PostgresBackup(Luan luan,LuanTable spec) |
1392 | 40 throws ClassNotFoundException, SQLException, LuanException |
1387 | 41 { |
1420 | 42 spec = new LuanTable(spec); |
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 |
1562 | 85 luanToString = new LuanToString(luan,null,null); |
1553 | 86 luanToString.settingsInit.strict = true; |
87 luanToString.settingsInit.numberTypes = true; | |
1391 | 88 } |
89 | |
90 Connection newConnection() throws SQLException { | |
91 return DriverManager.getConnection(url,props); | |
1387 | 92 } |
93 | |
1392 | 94 void close() throws SQLException { |
95 insertStmt.close(); | |
96 updateStmt.close(); | |
97 deleteStmt.close(); | |
98 con.close(); | |
1387 | 99 } |
100 | |
101 protected void finalize() throws Throwable { | |
1398 | 102 close(); |
1387 | 103 super.finalize(); |
104 } | |
105 | |
1562 | 106 void add(Luan luan,LuanTable doc) throws LuanException, SQLException { |
107 Long id = (Long)doc.get(luan,"id"); | |
1392 | 108 String data = luanToString.toString(doc); |
109 insertStmt.setLong(1,id); | |
110 insertStmt.setString(2,data); | |
111 insertStmt.executeUpdate(); | |
1387 | 112 } |
113 | |
1562 | 114 void update(Luan luan,LuanTable doc) throws LuanException, SQLException { |
115 Long id = (Long)doc.get(luan,"id"); | |
1392 | 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 ) { | |
1454 | 121 logger.error("update not found for id="+id+", trying add"); |
1562 | 122 add(luan,doc); |
1392 | 123 } else if( n!=1 ) |
124 throw new RuntimeException(); | |
1387 | 125 } |
126 | |
1392 | 127 void deleteAll() throws SQLException { |
128 Statement stmt = con.createStatement(); | |
129 stmt.executeUpdate("delete from lucene"); | |
130 stmt.close(); | |
1387 | 131 } |
132 | |
1392 | 133 void delete(long id) throws SQLException, LuanException { |
134 deleteStmt.setLong(1,id); | |
135 int n = deleteStmt.executeUpdate(); | |
136 if( n==0 ) | |
137 throw new LuanException("delete not found for id="+id); | |
1387 | 138 } |
139 | |
1392 | 140 void begin() throws SQLException { |
141 if( trans++ == 0 ) | |
142 con.setAutoCommit(false); | |
1387 | 143 } |
144 | |
1392 | 145 void commit() throws SQLException, LuanException { |
146 if( trans <= 0 ) | |
147 throw new LuanException("commit not in transaction"); | |
148 if( --trans == 0 ) | |
1387 | 149 con.setAutoCommit(true); |
1392 | 150 } |
151 | |
152 void rollback() throws SQLException, LuanException { | |
153 if( --trans != 0 ) | |
154 throw new LuanException("rollback failed trans="+trans); | |
155 con.rollback(); | |
156 con.setAutoCommit(true); | |
1387 | 157 } |
158 | |
1528
3bd4d7963456
use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents:
1454
diff
changeset
|
159 void restoreLucene(LuceneIndex li) |
1447 | 160 throws LuanException, IOException, SQLException, ParseException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
161 { |
1392 | 162 Statement stmt = con.createStatement(); |
163 ResultSet rs = stmt.executeQuery("select data from lucene"); | |
164 while( rs.next() ) { | |
165 String data = rs.getString("data"); | |
1562 | 166 LuanTable doc = (LuanTable)LuanParser.parse(data); |
1528
3bd4d7963456
use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents:
1454
diff
changeset
|
167 li.restore(doc); |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
168 } |
1392 | 169 stmt.close(); |
1388
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 |
1391 | 172 final class Checker { |
173 private final Connection con; | |
174 private final PreparedStatement pstmt; | |
175 | |
176 Checker() throws SQLException { | |
177 con = newConnection(); | |
1428 | 178 con.setReadOnly(true); |
1391 | 179 pstmt = con.prepareStatement( |
180 "select data from lucene where id=?" | |
181 ); | |
182 } | |
183 | |
184 void close() throws SQLException { | |
185 pstmt.close(); | |
186 con.close(); | |
187 } | |
188 | |
189 List<Long> getIds() throws SQLException { | |
190 List<Long> ids = new ArrayList<Long>(); | |
191 Statement stmt = con.createStatement(); | |
192 ResultSet rs = stmt.executeQuery("select id from lucene order by id"); | |
193 while( rs.next() ) { | |
194 long id = rs.getLong("id"); | |
195 ids.add(id); | |
196 } | |
197 stmt.close(); | |
198 return ids; | |
199 } | |
200 | |
1447 | 201 LuanTable getDoc(long id) throws SQLException, ParseException { |
1391 | 202 pstmt.setLong(1,id); |
203 ResultSet rs = pstmt.executeQuery(); | |
1399 | 204 if( !rs.next() ) |
205 return null; | |
1391 | 206 String data = rs.getString("data"); |
1562 | 207 LuanTable doc = (LuanTable)LuanParser.parse(data); |
1391 | 208 return doc; |
209 } | |
210 } | |
211 | |
212 Checker newChecker() throws SQLException { | |
213 return new Checker(); | |
214 } | |
215 | |
1387 | 216 } |