diff src/luan/modules/lucene/PostgresBackup.java @ 1391:94f48cc76de8

add lucene check
author Franklin Schmidt <fschmidt@gmail.com>
date Thu, 05 Sep 2019 01:29:57 -0600
parents 2024d23ddd64
children 002152af497a
line wrap: on
line diff
--- a/src/luan/modules/lucene/PostgresBackup.java	Wed Sep 04 00:06:42 2019 -0600
+++ b/src/luan/modules/lucene/PostgresBackup.java	Thu Sep 05 01:29:57 2019 -0600
@@ -8,6 +8,8 @@
 import java.sql.SQLException;
 import java.sql.ResultSet;
 import java.util.Properties;
+import java.util.List;
+import java.util.ArrayList;
 import luan.Luan;
 import luan.LuanTable;
 import luan.LuanFunction;
@@ -33,25 +35,25 @@
 	}
 
 	final boolean wasCreated;
+	private final String url;
+	private final Properties props = new Properties();
 	private final Connection con;
 	private final PreparedStatement insertStmt;
 	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
 	{
 		Class.forName("org.postgresql.Driver");
 
-		String url = "jdbc:postgresql://localhost:5432/luan";
-		Properties props = new Properties();
+		url = "jdbc:postgresql://localhost:5432/luan";
 		props.setProperty("user","postgres");
 		props.setProperty("password","");
 
-		con = DriverManager.getConnection(url,props);
+		con = newConnection();
 
 		Statement stmt = con.createStatement();
 		boolean hasTable = stmt.executeQuery(
@@ -80,7 +82,10 @@
 
 		luanToString.strict = true;
 		luanToString.numberTypes = true;
-		LuanToString.addNumberTypes(env);
+	}
+
+	Connection newConnection() throws SQLException {
+		return DriverManager.getConnection(url,props);
 	}
 
 	void close() {
@@ -102,9 +107,9 @@
 		}
 	}
 
-	void add(long id,LuanTable doc) throws LuanException {
+	void add(LuanTable doc) throws LuanException {
 		try {
-//logger.info("getAutoCommit="+con.getAutoCommit());
+			Long id = (Long)doc.get("id");
 			String data = luanToString.toString(doc);
 			insertStmt.setLong(1,id);
 			insertStmt.setString(2,data);
@@ -114,15 +119,16 @@
 		}
 	}
 
-	void update(long id,LuanTable doc) throws LuanException {
+	void update(LuanTable doc) throws LuanException {
 		try {
+			Long id = (Long)doc.get("id");
 			String data = luanToString.toString(doc);
 			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);
+				add(doc);
 			} else if( n!=1 )
 				throw new RuntimeException();
 		} catch(SQLException e) {
@@ -187,9 +193,13 @@
 		}
 	}
 
-	private final Luan luanEval = new Luan();
+	private static LuanTable newEnv() {
+		LuanTable env = new LuanTable(new Luan());
+		LuanToString.addNumberTypes(env);
+		return env;
+	}
 
-	private Object eval(String s) throws LuanException {
+	private static Object eval(String s,LuanTable env) throws LuanException {
 		LuanFunction fn = env.luan().load( "return "+s, "PostgresBackup", env );
 		return fn.call();
 	}
@@ -198,11 +208,12 @@
 		throws LuanException, IOException
 	{
 		try {
+			LuanTable env = newEnv();
 			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);
+				LuanTable doc = (LuanTable)eval(data,env);
 				li.restore(doc);
 			}
 			stmt.close();
@@ -228,4 +239,48 @@
 		}
 	}
 
+	final class Checker {
+		private final Connection con;
+		private final PreparedStatement pstmt;
+		private final LuanTable env = newEnv();
+
+		Checker() throws SQLException {
+			con = newConnection();
+			con.setAutoCommit(false);
+			pstmt = con.prepareStatement(
+				"select data from lucene where id=?"
+			);
+		}
+
+		void close() throws SQLException {
+			pstmt.close();
+			con.close();
+		}
+
+		List<Long> getIds() throws SQLException {
+			List<Long> ids = new ArrayList<Long>();
+			Statement stmt = con.createStatement();
+			ResultSet rs = stmt.executeQuery("select id from lucene order by id");
+			while( rs.next() ) {
+				long id = rs.getLong("id");
+				ids.add(id);
+			}
+			stmt.close();
+			return ids;
+		}
+
+		LuanTable getDoc(long id) throws SQLException, LuanException {
+			pstmt.setLong(1,id);
+			ResultSet rs = pstmt.executeQuery();
+			rs.next();
+			String data = rs.getString("data");
+			LuanTable doc = (LuanTable)eval(data,env);
+			return doc;
+		}
+	}
+
+	Checker newChecker() throws SQLException {
+		return new Checker();
+	}
+
 }