diff src/luan/modules/lucene/LuceneIndex.java @ 1392:002152af497a

hosted postgres
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 06 Sep 2019 00:19:47 -0600
parents 94f48cc76de8
children cc0dbca576dc
line wrap: on
line diff
--- a/src/luan/modules/lucene/LuceneIndex.java	Thu Sep 05 01:29:57 2019 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Fri Sep 06 00:19:47 2019 -0600
@@ -79,6 +79,7 @@
 import luan.LuanException;
 import luan.LuanRuntimeException;
 import luan.modules.parsers.LuanToString;
+import luan.modules.logging.LuanLogger;
 import luan.lib.logging.Logger;
 import luan.lib.logging.LoggerFactory;
 
@@ -98,7 +99,11 @@
 
 		public void close() throws IOException {
 			if( !isClosed ) {
-				li.close();
+				try {
+					li.close();
+				} catch(SQLException e) {
+					throw new RuntimeException(e);
+				}
 				isClosed = true;
 			}
 		}
@@ -114,14 +119,14 @@
 
 	private static Map<String,LuceneIndex> indexes = new HashMap<String,LuceneIndex>();
 
-	public static Object[] getLuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,LuanFunction completer)
-		throws LuanException, IOException
+	public static Object[] getLuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,LuanTable postgresSpec)
+		throws LuanException, IOException, ClassNotFoundException, SQLException
 	{
 		String key = indexDir.getCanonicalPath();
 		synchronized(indexes) {
 			LuceneIndex li = indexes.get(key);
 			if( li == null ) {
-				li = new LuceneIndex(indexDir,defaultFieldParser,defaultFields,key,completer);
+				li = new LuceneIndex(luan,indexDir,defaultFieldParser,defaultFields,key,postgresSpec);
 				li.openCount = 1;
 				indexes.put(key,li);
 			} else {
@@ -163,9 +168,10 @@
 
 	private final PostgresBackup postgresBackup;
 
-	private LuceneIndex(File indexDir,FieldParser defaultFieldParser,String[] defaultFields,String key,LuanFunction completer)
-		throws LuanException, IOException
+	private LuceneIndex(Luan luan,File indexDir,FieldParser defaultFieldParser,String[] defaultFields,String key,LuanTable postgresSpec)
+		throws LuanException, IOException, ClassNotFoundException, SQLException
 	{
+		final Logger logger = LuanLogger.getLogger(luan,LuceneIndex.class);
 		this.key = key;
 		this.defaultFieldParser = defaultFieldParser;
 		this.defaultFields = defaultFields;
@@ -180,14 +186,20 @@
 		}
 		this.analyzer = analyzer;
 		boolean wasCreated = reopen();
-		postgresBackup = completer!=null ? PostgresBackup.newInstance() : null;
-		if( postgresBackup != null ) {
-			if( !wasCreated && postgresBackup.wasCreated ) {
-				logger.error("rebuilding postgres backup");
-				rebuild_postgres_backup(completer);
-			} else if( wasCreated && !postgresBackup.wasCreated ) {
-				logger.error("restoring from postgres");
-				restore_from_postgres();
+		if( postgresSpec == null ) {
+			postgresBackup = null;
+		} else {
+			Map spec = postgresSpec.asMap();
+			LuanFunction completer = Utils.removeRequiredFunction(spec,"completer");
+			postgresBackup = new PostgresBackup(spec);
+			if( postgresBackup != null ) {
+				if( !wasCreated && postgresBackup.wasCreated ) {
+					logger.error("rebuilding postgres backup");
+					rebuild_postgres_backup(completer);
+				} else if( wasCreated && !postgresBackup.wasCreated ) {
+					logger.error("restoring from postgres");
+					restore_from_postgres(luan);
+				}
 			}
 		}
 	}
@@ -210,7 +222,7 @@
 		writeCounter.incrementAndGet();
 	}
 
-	public void delete_all() throws IOException {
+	public void delete_all() throws IOException, SQLException {
 		boolean commit = !writeLock.isHeldByCurrentThread();
 		writeLock.lock();
 		try {
@@ -232,7 +244,7 @@
 	}
 
 	private void backupDelete(Query query)
-		throws IOException
+		throws IOException, SQLException, LuanException
 	{
 		if( postgresBackup != null ) {
 			final List<Long> ids = new ArrayList<Long>();
@@ -258,7 +270,7 @@
 	}
 
 	public void delete(String queryStr)
-		throws IOException, ParseException
+		throws IOException, ParseException, SQLException, LuanException
 	{
 		Query query = SaneQueryParser.parseQuery(mfp,queryStr);
 
@@ -279,10 +291,10 @@
 	}
 
 	public void save(LuanTable doc,LuanTable boosts)
-		throws LuanException, IOException
+		throws LuanException, IOException, SQLException
 	{
 		if( boosts!=null && postgresBackup!=null )
-			logger.error("boosts are not saved to postgres backup");
+			throw new LuanException("boosts are not saved to postgres backup");
 
 		Object obj = doc.get("id");
 		Long id;
@@ -313,7 +325,9 @@
 		}
 	}
 
-	public Object run_in_transaction(LuanFunction fn) throws IOException, LuanException {
+	public Object run_in_transaction(LuanFunction fn)
+		throws IOException, LuanException, SQLException
+	{
 		boolean commit = !writeLock.isHeldByCurrentThread();
 		writeLock.lock();
 		boolean ok = false;
@@ -433,7 +447,7 @@
 		return writer.getDirectory().toString();
 	}
 
-	private synchronized void close() throws IOException {
+	private synchronized void close() throws IOException, SQLException {
 		if( openCount > 0 ) {
 			if( --openCount == 0 ) {
 				doClose();
@@ -444,11 +458,11 @@
 		}
 	}
 
-	public void doClose() throws IOException {
+	public void doClose() throws IOException, SQLException {
+		writer.close();
+		reader.close();
 		if( postgresBackup != null )
 			postgresBackup.close();
-		writer.close();
-		reader.close();
 	}
 
 
@@ -806,8 +820,10 @@
 	}
 
 	public void rebuild_postgres_backup(LuanFunction completer)
-		throws IOException, LuanException
+		throws IOException, LuanException, SQLException
 	{
+		final Logger logger = LuanLogger.getLogger(completer.luan(),LuceneIndex.class);
+		logger.info("start rebuild_postgres_backup");
 		writeLock.lock();
 		IndexSearcher searcher = openSearcher();
 		boolean ok = false;
@@ -824,6 +840,8 @@
 						postgresBackup.add(tbl);
 					} catch(LuanException e) {
 						throw new LuanRuntimeException(e);
+					} catch(SQLException e) {
+						throw new RuntimeException(e);
 					}
 				}
 			};
@@ -840,11 +858,14 @@
 				postgresBackup.rollback();
 			writeLock.unlock();
 		}
+		logger.info("end rebuild_postgres_backup");
 	}
 
-	public void restore_from_postgres()
-		throws IOException, LuanException
+	public void restore_from_postgres(Luan luan)
+		throws IOException, LuanException, SQLException
 	{
+		final Logger logger = LuanLogger.getLogger(luan,LuceneIndex.class);
+		logger.warn("start restore_from_postgres");
 		if( postgresBackup==null )
 			throw new NullPointerException();
 		if( writeLock.isHeldByCurrentThread() )
@@ -866,6 +887,7 @@
 			wrote();
 			writeLock.unlock();
 		}
+		logger.warn("end restore_from_postgres");
 	}
 
 	void restore(LuanTable doc)
@@ -875,6 +897,7 @@
 	}
 
 	public void check(LuanFunction completer) throws IOException, SQLException, LuanException {
+		final Logger logger = LuanLogger.getLogger(completer.luan(),LuceneIndex.class);
 		logger.info("start check");
 		CheckIndex.Status status = new CheckIndex(fsDir).checkIndex();
 		if( !status.clean )
@@ -885,6 +908,7 @@
 	}
 
 	private void checkPostgres(LuanFunction completer) throws IOException, SQLException, LuanException {
+		final Logger logger = LuanLogger.getLogger(completer.luan(),LuceneIndex.class);
 		final PostgresBackup.Checker postgresChecker;
 		final IndexSearcher searcher;
 		writeLock.lock();