Mercurial Hosting > luan
diff src/luan/modules/lucene/PostgresBackup.java @ 1388:2024d23ddd64
add restore_from_postgres
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Tue, 03 Sep 2019 22:12:53 -0600 |
parents | bc40bc9aab3a |
children | 94f48cc76de8 |
line wrap: on
line diff
--- a/src/luan/modules/lucene/PostgresBackup.java Mon Sep 02 22:23:12 2019 -0600 +++ b/src/luan/modules/lucene/PostgresBackup.java Tue Sep 03 22:12:53 2019 -0600 @@ -1,12 +1,16 @@ package luan.modules.lucene; +import java.io.IOException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; import java.sql.PreparedStatement; import java.sql.SQLException; +import java.sql.ResultSet; import java.util.Properties; +import luan.Luan; import luan.LuanTable; +import luan.LuanFunction; import luan.LuanException; import luan.modules.parsers.LuanToString; import luan.lib.logging.Logger; @@ -34,6 +38,8 @@ private final PreparedStatement updateStmt; private final PreparedStatement deleteStmt; private int trans = 0; + private final LuanToString luanToString = new LuanToString(); + private final LuanTable env = new LuanTable(new Luan()); private PostgresBackup() throws ClassNotFoundException, SQLException @@ -71,6 +77,10 @@ deleteStmt = con.prepareStatement( "delete from lucene where id=?" ); + + luanToString.strict = true; + luanToString.numberTypes = true; + LuanToString.addNumberTypes(env); } void close() { @@ -95,7 +105,7 @@ void add(long id,LuanTable doc) throws LuanException { try { //logger.info("getAutoCommit="+con.getAutoCommit()); - String data = LuanToString.toString(doc,true); + String data = luanToString.toString(doc); insertStmt.setLong(1,id); insertStmt.setString(2,data); insertStmt.executeUpdate(); @@ -106,7 +116,7 @@ void update(long id,LuanTable doc) throws LuanException { try { - String data = LuanToString.toString(doc,true); + String data = luanToString.toString(doc); updateStmt.setString(1,data); updateStmt.setLong(2,id); int n = updateStmt.executeUpdate(); @@ -177,4 +187,45 @@ } } + private final Luan luanEval = new Luan(); + + private Object eval(String s) throws LuanException { + LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env ); + return fn.call(); + } + + void restoreLucene(LuceneIndex li) + throws LuanException, IOException + { + try { + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("select data from lucene"); + while( rs.next() ) { + String data = rs.getString("data"); + LuanTable doc = (LuanTable)eval(data); + li.restore(doc); + } + stmt.close(); + } catch(SQLException e) { + logger.error("restoreLucene failed",e); + throw new RuntimeException(e); + } + } + + long maxId() + throws LuanException, IOException + { + try { + Statement stmt = con.createStatement(); + ResultSet rs = stmt.executeQuery("select max(id) as m from lucene"); + rs.next(); + long m = rs.getLong("m"); + stmt.close(); + return m; + } catch(SQLException e) { + logger.error("maxId failed",e); + throw new RuntimeException(e); + } + } + }