Mercurial Hosting > luan
annotate src/luan/modules/lucene/PostgresBackup.java @ 1539:c27dc6af87ca
remove nextId
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 06 Sep 2020 11:35:30 -0600 |
parents | 3bd4d7963456 |
children | 26c51acf00f3 |
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; | |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
37 private final LuanToString luanToString = new 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 |
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 ) { | |
1454 | 120 logger.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 | |
1528
3bd4d7963456
use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents:
1454
diff
changeset
|
158 void restoreLucene(LuceneIndex li) |
1447 | 159 throws LuanException, IOException, SQLException, ParseException |
1388
2024d23ddd64
add restore_from_postgres
Franklin Schmidt <fschmidt@gmail.com>
parents:
1387
diff
changeset
|
160 { |
1447 | 161 Luan luan = new Luan(); |
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"); | |
1447 | 166 LuanTable doc = (LuanTable)LuanParser.parse(luan,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; | |
1447 | 175 private final Luan luan = new Luan(); |
1391 | 176 |
177 Checker() throws SQLException { | |
178 con = newConnection(); | |
1428 | 179 con.setReadOnly(true); |
1391 | 180 pstmt = con.prepareStatement( |
181 "select data from lucene where id=?" | |
182 ); | |
183 } | |
184 | |
185 void close() throws SQLException { | |
186 pstmt.close(); | |
187 con.close(); | |
188 } | |
189 | |
190 List<Long> getIds() throws SQLException { | |
191 List<Long> ids = new ArrayList<Long>(); | |
192 Statement stmt = con.createStatement(); | |
193 ResultSet rs = stmt.executeQuery("select id from lucene order by id"); | |
194 while( rs.next() ) { | |
195 long id = rs.getLong("id"); | |
196 ids.add(id); | |
197 } | |
198 stmt.close(); | |
199 return ids; | |
200 } | |
201 | |
1447 | 202 LuanTable getDoc(long id) throws SQLException, ParseException { |
1391 | 203 pstmt.setLong(1,id); |
204 ResultSet rs = pstmt.executeQuery(); | |
1399 | 205 if( !rs.next() ) |
206 return null; | |
1391 | 207 String data = rs.getString("data"); |
1447 | 208 LuanTable doc = (LuanTable)LuanParser.parse(luan,data); |
1391 | 209 return doc; |
210 } | |
211 } | |
212 | |
213 Checker newChecker() throws SQLException { | |
214 return new Checker(); | |
215 } | |
216 | |
1387 | 217 } |