Mercurial Hosting > luan
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/luan/modules/lucene/PostgresBackup.java Mon Sep 02 22:23:12 2019 -0600 @@ -0,0 +1,180 @@ +package luan.modules.lucene; + +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.Statement; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.Properties; +import luan.LuanTable; +import luan.LuanException; +import luan.modules.parsers.LuanToString; +import luan.lib.logging.Logger; +import luan.lib.logging.LoggerFactory; + + +final class PostgresBackup { + private static final Logger logger = LoggerFactory.getLogger(PostgresBackup.class); + + static PostgresBackup newInstance() { + try { + return new PostgresBackup(); + } catch(ClassNotFoundException e) { + logger.error("creation failed",e); + return null; + } catch(SQLException e) { + logger.error("creation failed",e); + return null; + } + } + + final boolean wasCreated; + private final Connection con; + private final PreparedStatement insertStmt; + private final PreparedStatement updateStmt; + private final PreparedStatement deleteStmt; + private int trans = 0; + + private PostgresBackup() + throws ClassNotFoundException, SQLException + { + Class.forName("org.postgresql.Driver"); + + String url = "jdbc:postgresql://localhost:5432/luan"; + Properties props = new Properties(); + props.setProperty("user","postgres"); + props.setProperty("password",""); + + con = DriverManager.getConnection(url,props); + + Statement stmt = con.createStatement(); + boolean hasTable = stmt.executeQuery( + "select * from information_schema.tables where table_name='lucene'" + ).next(); + if( !hasTable ) { + stmt.executeUpdate( + "create table lucene (" + +" id integer not null primary key," + +" data text not null" + +")" + ); + } + stmt.close(); + wasCreated = !hasTable; + + insertStmt = con.prepareStatement( + "insert into lucene (id,data) values (?,?)" + ); + updateStmt = con.prepareStatement( + "update lucene set data=? where id=?" + ); + deleteStmt = con.prepareStatement( + "delete from lucene where id=?" + ); + } + + void close() { + try { + insertStmt.close(); + updateStmt.close(); + deleteStmt.close(); + con.close(); + } catch(SQLException e) { + logger.error("close failed",e); + } + } + + protected void finalize() throws Throwable { + super.finalize(); + if( !con.isClosed() ) { + logger.error("con not closed"); + con.close(); + } + } + + void add(long id,LuanTable doc) throws LuanException { + try { +//logger.info("getAutoCommit="+con.getAutoCommit()); + String data = LuanToString.toString(doc,true); + insertStmt.setLong(1,id); + insertStmt.setString(2,data); + insertStmt.executeUpdate(); + } catch(SQLException e) { + logger.error("add failed",e); + } + } + + void update(long id,LuanTable doc) throws LuanException { + try { + String data = LuanToString.toString(doc,true); + updateStmt.setString(1,data); + updateStmt.setLong(2,id); + int n = updateStmt.executeUpdate(); + if( n==0 ) { + logger.error("update not found for id="+id+", trying add"); + add(id,doc); + } else if( n!=1 ) + throw new RuntimeException(); + } catch(SQLException e) { + logger.error("update failed",e); + } + } + + void deleteAll() { + try { + Statement stmt = con.createStatement(); + stmt.executeUpdate("delete from lucene"); + stmt.close(); + } catch(SQLException e) { + logger.error("update failed",e); + } + } + + void delete(long id) { + try { + deleteStmt.setLong(1,id); + int n = deleteStmt.executeUpdate(); + if( n==0 ) { + logger.error("delete not found for id="+id); + } + } catch(SQLException e) { + logger.error("update failed",e); + } + } + + void begin() { + try { + if( trans++ == 0 ) + con.setAutoCommit(false); + } catch(SQLException e) { + logger.error("begin failed",e); + } + } + + void commit() { + try { + if( trans <= 0 ) { + logger.error("commit not in transaction"); + return; + } + if( --trans == 0 ) + con.setAutoCommit(true); + } catch(SQLException e) { + logger.error("begin failed",e); + } + } + + void rollback() { + try { + if( --trans != 0 ) { + logger.error("rollback failed trans="+trans); + return; + } + con.rollback(); + con.setAutoCommit(true); + } catch(SQLException e) { + logger.error("begin failed",e); + } + } + +}