comparison src/luan/modules/lucene/PostgresBackup.java @ 1387:bc40bc9aab3a

start postgres backup
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 02 Sep 2019 22:23:12 -0600
parents
children 2024d23ddd64
comparison
equal deleted inserted replaced
1386:dc36dd8bf839 1387:bc40bc9aab3a
1 package luan.modules.lucene;
2
3 import java.sql.Connection;
4 import java.sql.DriverManager;
5 import java.sql.Statement;
6 import java.sql.PreparedStatement;
7 import java.sql.SQLException;
8 import java.util.Properties;
9 import luan.LuanTable;
10 import luan.LuanException;
11 import luan.modules.parsers.LuanToString;
12 import luan.lib.logging.Logger;
13 import luan.lib.logging.LoggerFactory;
14
15
16 final class PostgresBackup {
17 private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class);
18
19 static PostgresBackup newInstance() {
20 try {
21 return new PostgresBackup();
22 } catch(ClassNotFoundException e) {
23 logger.error("creation failed",e);
24 return null;
25 } catch(SQLException e) {
26 logger.error("creation failed",e);
27 return null;
28 }
29 }
30
31 final boolean wasCreated;
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;
37
38 private PostgresBackup()
39 throws ClassNotFoundException, SQLException
40 {
41 Class.forName("org.postgresql.Driver");
42
43 String url = "jdbc:postgresql://localhost:5432/luan";
44 Properties props = new Properties();
45 props.setProperty("user","postgres");
46 props.setProperty("password","");
47
48 con = DriverManager.getConnection(url,props);
49
50 Statement stmt = con.createStatement();
51 boolean hasTable = stmt.executeQuery(
52 "select * from information_schema.tables where table_name='lucene'"
53 ).next();
54 if( !hasTable ) {
55 stmt.executeUpdate(
56 "create table lucene ("
57 +" id integer not null primary key,"
58 +" data text not null"
59 +")"
60 );
61 }
62 stmt.close();
63 wasCreated = !hasTable;
64
65 insertStmt = con.prepareStatement(
66 "insert into lucene (id,data) values (?,?)"
67 );
68 updateStmt = con.prepareStatement(
69 "update lucene set data=? where id=?"
70 );
71 deleteStmt = con.prepareStatement(
72 "delete from lucene where id=?"
73 );
74 }
75
76 void close() {
77 try {
78 insertStmt.close();
79 updateStmt.close();
80 deleteStmt.close();
81 con.close();
82 } catch(SQLException e) {
83 logger.error("close failed",e);
84 }
85 }
86
87 protected void finalize() throws Throwable {
88 super.finalize();
89 if( !con.isClosed() ) {
90 logger.error("con not closed");
91 con.close();
92 }
93 }
94
95 void add(long id,LuanTable doc) throws LuanException {
96 try {
97 //logger.info("getAutoCommit="+con.getAutoCommit());
98 String data = LuanToString.toString(doc,true);
99 insertStmt.setLong(1,id);
100 insertStmt.setString(2,data);
101 insertStmt.executeUpdate();
102 } catch(SQLException e) {
103 logger.error("add failed",e);
104 }
105 }
106
107 void update(long id,LuanTable doc) throws LuanException {
108 try {
109 String data = LuanToString.toString(doc,true);
110 updateStmt.setString(1,data);
111 updateStmt.setLong(2,id);
112 int n = updateStmt.executeUpdate();
113 if( n==0 ) {
114 logger.error("update not found for id="+id+", trying add");
115 add(id,doc);
116 } else if( n!=1 )
117 throw new RuntimeException();
118 } catch(SQLException e) {
119 logger.error("update failed",e);
120 }
121 }
122
123 void deleteAll() {
124 try {
125 Statement stmt = con.createStatement();
126 stmt.executeUpdate("delete from lucene");
127 stmt.close();
128 } catch(SQLException e) {
129 logger.error("update failed",e);
130 }
131 }
132
133 void delete(long id) {
134 try {
135 deleteStmt.setLong(1,id);
136 int n = deleteStmt.executeUpdate();
137 if( n==0 ) {
138 logger.error("delete not found for id="+id);
139 }
140 } catch(SQLException e) {
141 logger.error("update failed",e);
142 }
143 }
144
145 void begin() {
146 try {
147 if( trans++ == 0 )
148 con.setAutoCommit(false);
149 } catch(SQLException e) {
150 logger.error("begin failed",e);
151 }
152 }
153
154 void commit() {
155 try {
156 if( trans <= 0 ) {
157 logger.error("commit not in transaction");
158 return;
159 }
160 if( --trans == 0 )
161 con.setAutoCommit(true);
162 } catch(SQLException e) {
163 logger.error("begin failed",e);
164 }
165 }
166
167 void rollback() {
168 try {
169 if( --trans != 0 ) {
170 logger.error("rollback failed trans="+trans);
171 return;
172 }
173 con.rollback();
174 con.setAutoCommit(true);
175 } catch(SQLException e) {
176 logger.error("begin failed",e);
177 }
178 }
179
180 }