Mercurial Hosting > luan
comparison 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 |
comparison
equal
deleted
inserted
replaced
1391:94f48cc76de8 | 1392:002152af497a |
---|---|
8 import java.sql.SQLException; | 8 import java.sql.SQLException; |
9 import java.sql.ResultSet; | 9 import java.sql.ResultSet; |
10 import java.util.Properties; | 10 import java.util.Properties; |
11 import java.util.List; | 11 import java.util.List; |
12 import java.util.ArrayList; | 12 import java.util.ArrayList; |
13 import java.util.Map; | |
13 import luan.Luan; | 14 import luan.Luan; |
14 import luan.LuanTable; | 15 import luan.LuanTable; |
15 import luan.LuanFunction; | 16 import luan.LuanFunction; |
16 import luan.LuanException; | 17 import luan.LuanException; |
18 import luan.modules.Utils; | |
17 import luan.modules.parsers.LuanToString; | 19 import luan.modules.parsers.LuanToString; |
20 import luan.modules.logging.LuanLogger; | |
18 import luan.lib.logging.Logger; | 21 import luan.lib.logging.Logger; |
19 import luan.lib.logging.LoggerFactory; | 22 import luan.lib.logging.LoggerFactory; |
20 | 23 |
21 | 24 |
22 final class PostgresBackup { | 25 final class PostgresBackup { |
23 private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class); | 26 private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class); |
24 | |
25 static PostgresBackup newInstance() { | |
26 try { | |
27 return new PostgresBackup(); | |
28 } catch(ClassNotFoundException e) { | |
29 logger.error("creation failed",e); | |
30 return null; | |
31 } catch(SQLException e) { | |
32 logger.error("creation failed",e); | |
33 return null; | |
34 } | |
35 } | |
36 | 27 |
37 final boolean wasCreated; | 28 final boolean wasCreated; |
38 private final String url; | 29 private final String url; |
39 private final Properties props = new Properties(); | 30 private final Properties props = new Properties(); |
40 private final Connection con; | 31 private final Connection con; |
42 private final PreparedStatement updateStmt; | 33 private final PreparedStatement updateStmt; |
43 private final PreparedStatement deleteStmt; | 34 private final PreparedStatement deleteStmt; |
44 private int trans = 0; | 35 private int trans = 0; |
45 private final LuanToString luanToString = new LuanToString(); | 36 private final LuanToString luanToString = new LuanToString(); |
46 | 37 |
47 private PostgresBackup() | 38 PostgresBackup(Map spec) |
48 throws ClassNotFoundException, SQLException | 39 throws ClassNotFoundException, SQLException, LuanException |
49 { | 40 { |
41 /* | |
50 Class.forName("org.postgresql.Driver"); | 42 Class.forName("org.postgresql.Driver"); |
51 | |
52 url = "jdbc:postgresql://localhost:5432/luan"; | 43 url = "jdbc:postgresql://localhost:5432/luan"; |
53 props.setProperty("user","postgres"); | 44 props.setProperty("user","postgres"); |
54 props.setProperty("password",""); | 45 props.setProperty("password",""); |
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); | |
55 | 55 |
56 con = newConnection(); | 56 con = newConnection(); |
57 | 57 |
58 Statement stmt = con.createStatement(); | 58 Statement stmt = con.createStatement(); |
59 boolean hasTable = stmt.executeQuery( | 59 boolean hasTable = stmt.executeQuery( |
86 | 86 |
87 Connection newConnection() throws SQLException { | 87 Connection newConnection() throws SQLException { |
88 return DriverManager.getConnection(url,props); | 88 return DriverManager.getConnection(url,props); |
89 } | 89 } |
90 | 90 |
91 void close() { | 91 void close() throws SQLException { |
92 try { | 92 insertStmt.close(); |
93 insertStmt.close(); | 93 updateStmt.close(); |
94 updateStmt.close(); | 94 deleteStmt.close(); |
95 deleteStmt.close(); | 95 con.close(); |
96 con.close(); | |
97 } catch(SQLException e) { | |
98 logger.error("close failed",e); | |
99 } | |
100 } | 96 } |
101 | 97 |
102 protected void finalize() throws Throwable { | 98 protected void finalize() throws Throwable { |
103 super.finalize(); | 99 super.finalize(); |
104 if( !con.isClosed() ) { | 100 if( !con.isClosed() ) { |
105 logger.error("con not closed"); | 101 logger.error("con not closed"); |
106 con.close(); | 102 con.close(); |
107 } | 103 } |
108 } | 104 } |
109 | 105 |
110 void add(LuanTable doc) throws LuanException { | 106 void add(LuanTable doc) throws LuanException, SQLException { |
111 try { | 107 Long id = (Long)doc.get("id"); |
112 Long id = (Long)doc.get("id"); | 108 String data = luanToString.toString(doc); |
113 String data = luanToString.toString(doc); | 109 insertStmt.setLong(1,id); |
114 insertStmt.setLong(1,id); | 110 insertStmt.setString(2,data); |
115 insertStmt.setString(2,data); | 111 insertStmt.executeUpdate(); |
116 insertStmt.executeUpdate(); | 112 } |
117 } catch(SQLException e) { | 113 |
118 logger.error("add failed",e); | 114 void update(LuanTable doc) throws LuanException, SQLException { |
119 } | 115 Long id = (Long)doc.get("id"); |
120 } | 116 String data = luanToString.toString(doc); |
121 | 117 updateStmt.setString(1,data); |
122 void update(LuanTable doc) throws LuanException { | 118 updateStmt.setLong(2,id); |
123 try { | 119 int n = updateStmt.executeUpdate(); |
124 Long id = (Long)doc.get("id"); | 120 if( n==0 ) { |
125 String data = luanToString.toString(doc); | 121 Logger logger = LuanLogger.getLogger(doc.luan(),PostgresBackup.class); |
126 updateStmt.setString(1,data); | 122 logger.error("update not found for id="+id+", trying add"); |
127 updateStmt.setLong(2,id); | 123 add(doc); |
128 int n = updateStmt.executeUpdate(); | 124 } else if( n!=1 ) |
129 if( n==0 ) { | 125 throw new RuntimeException(); |
130 logger.error("update not found for id="+id+", trying add"); | 126 } |
131 add(doc); | 127 |
132 } else if( n!=1 ) | 128 void deleteAll() throws SQLException { |
133 throw new RuntimeException(); | 129 Statement stmt = con.createStatement(); |
134 } catch(SQLException e) { | 130 stmt.executeUpdate("delete from lucene"); |
135 logger.error("update failed",e); | 131 stmt.close(); |
136 } | 132 } |
137 } | 133 |
138 | 134 void delete(long id) throws SQLException, LuanException { |
139 void deleteAll() { | 135 deleteStmt.setLong(1,id); |
140 try { | 136 int n = deleteStmt.executeUpdate(); |
141 Statement stmt = con.createStatement(); | 137 if( n==0 ) |
142 stmt.executeUpdate("delete from lucene"); | 138 throw new LuanException("delete not found for id="+id); |
143 stmt.close(); | 139 } |
144 } catch(SQLException e) { | 140 |
145 logger.error("update failed",e); | 141 void begin() throws SQLException { |
146 } | 142 if( trans++ == 0 ) |
147 } | 143 con.setAutoCommit(false); |
148 | 144 } |
149 void delete(long id) { | 145 |
150 try { | 146 void commit() throws SQLException, LuanException { |
151 deleteStmt.setLong(1,id); | 147 if( trans <= 0 ) |
152 int n = deleteStmt.executeUpdate(); | 148 throw new LuanException("commit not in transaction"); |
153 if( n==0 ) { | 149 if( --trans == 0 ) |
154 logger.error("delete not found for id="+id); | |
155 } | |
156 } catch(SQLException e) { | |
157 logger.error("update failed",e); | |
158 } | |
159 } | |
160 | |
161 void begin() { | |
162 try { | |
163 if( trans++ == 0 ) | |
164 con.setAutoCommit(false); | |
165 } catch(SQLException e) { | |
166 logger.error("begin failed",e); | |
167 } | |
168 } | |
169 | |
170 void commit() { | |
171 try { | |
172 if( trans <= 0 ) { | |
173 logger.error("commit not in transaction"); | |
174 return; | |
175 } | |
176 if( --trans == 0 ) | |
177 con.setAutoCommit(true); | |
178 } catch(SQLException e) { | |
179 logger.error("begin failed",e); | |
180 } | |
181 } | |
182 | |
183 void rollback() { | |
184 try { | |
185 if( --trans != 0 ) { | |
186 logger.error("rollback failed trans="+trans); | |
187 return; | |
188 } | |
189 con.rollback(); | |
190 con.setAutoCommit(true); | 150 con.setAutoCommit(true); |
191 } catch(SQLException e) { | 151 } |
192 logger.error("begin failed",e); | 152 |
193 } | 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); | |
194 } | 158 } |
195 | 159 |
196 private static LuanTable newEnv() { | 160 private static LuanTable newEnv() { |
197 LuanTable env = new LuanTable(new Luan()); | 161 LuanTable env = new LuanTable(new Luan()); |
198 LuanToString.addNumberTypes(env); | 162 LuanToString.addNumberTypes(env); |
203 LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env ); | 167 LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env ); |
204 return fn.call(); | 168 return fn.call(); |
205 } | 169 } |
206 | 170 |
207 void restoreLucene(LuceneIndex li) | 171 void restoreLucene(LuceneIndex li) |
208 throws LuanException, IOException | 172 throws LuanException, IOException, SQLException |
209 { | 173 { |
210 try { | 174 LuanTable env = newEnv(); |
211 LuanTable env = newEnv(); | 175 Statement stmt = con.createStatement(); |
212 Statement stmt = con.createStatement(); | 176 ResultSet rs = stmt.executeQuery("select data from lucene"); |
213 ResultSet rs = stmt.executeQuery("select data from lucene"); | 177 while( rs.next() ) { |
214 while( rs.next() ) { | 178 String data = rs.getString("data"); |
215 String data = rs.getString("data"); | 179 LuanTable doc = (LuanTable)eval(data,env); |
216 LuanTable doc = (LuanTable)eval(data,env); | 180 li.restore(doc); |
217 li.restore(doc); | 181 } |
218 } | 182 stmt.close(); |
219 stmt.close(); | |
220 } catch(SQLException e) { | |
221 logger.error("restoreLucene failed",e); | |
222 throw new RuntimeException(e); | |
223 } | |
224 } | 183 } |
225 | 184 |
226 long maxId() | 185 long maxId() |
227 throws LuanException, IOException | 186 throws LuanException, IOException, SQLException |
228 { | 187 { |
229 try { | 188 Statement stmt = con.createStatement(); |
230 Statement stmt = con.createStatement(); | 189 ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); |
231 ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); | 190 rs.next(); |
232 rs.next(); | 191 long m = rs.getLong("m"); |
233 long m = rs.getLong("m"); | 192 stmt.close(); |
234 stmt.close(); | 193 return m; |
235 return m; | |
236 } catch(SQLException e) { | |
237 logger.error("maxId failed",e); | |
238 throw new RuntimeException(e); | |
239 } | |
240 } | 194 } |
241 | 195 |
242 final class Checker { | 196 final class Checker { |
243 private final Connection con; | 197 private final Connection con; |
244 private final PreparedStatement pstmt; | 198 private final PreparedStatement pstmt; |