diff src/luan/modules/lucene/LuceneIndex.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 179c4882c6b6
line wrap: on
line diff
--- a/src/luan/modules/lucene/LuceneIndex.java	Mon Sep 02 22:23:12 2019 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Tue Sep 03 22:12:53 2019 -0600
@@ -182,7 +182,7 @@
 			rebuild_postgres_backup(completer);
 	}
 
-	public void reopen() throws LuanException, IOException {
+	public void reopen() throws IOException {
 		IndexWriterConfig conf = new IndexWriterConfig(version,analyzer);
 		snapshotDeletionPolicy = new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy());
 		conf.setIndexDeletionPolicy(snapshotDeletionPolicy);
@@ -345,7 +345,7 @@
 	private long idLim;
 	private final int idBatch = 10;
 
-	private void initId() throws LuanException, IOException {
+	private void initId() throws IOException {
 		TopDocs td = searcher.search(new TermQuery(new Term("type","next_id")),1);
 		switch(td.totalHits) {
 		case 0:
@@ -361,13 +361,17 @@
 		}
 	}
 
+	private void saveNextId(long nextId) throws LuanException, IOException {
+		Map doc = new HashMap();
+		doc.put( "type", "next_id" );
+		doc.put( FLD_NEXT_ID, idLim );
+		writer.updateDocument(new Term("type","next_id"),toLucene(doc.entrySet(),null));
+	}
+
 	public synchronized long nextId() throws LuanException, IOException {
 		if( ++id > idLim ) {
 			idLim += idBatch;
-			Map doc = new HashMap();
-			doc.put( "type", "next_id" );
-			doc.put( FLD_NEXT_ID, idLim );
-			writer.updateDocument(new Term("type","next_id"),toLucene(doc.entrySet(),null));
+			saveNextId(idLim);
 			wrote();
 		}
 		return id;
@@ -824,4 +828,36 @@
 		}
 	}
 
+	public void restore_from_postgres()
+		throws IOException, LuanException
+	{
+		if( postgresBackup==null )
+			throw new NullPointerException();
+		if( writeLock.isHeldByCurrentThread() )
+			throw new RuntimeException();
+		writeLock.lock();
+		boolean ok = false;
+		try {
+			writer.deleteAll();
+			long nextId = postgresBackup.maxId() + 1;
+			postgresBackup.restoreLucene(this);
+			id = idLim = nextId;
+			ok = true;
+			writer.commit();
+		} finally {
+			if( !ok ) {
+				writer.rollback();
+				reopen();
+			}
+			wrote();
+			writeLock.unlock();
+		}
+	}
+
+	void restore(LuanTable doc)
+		throws LuanException, IOException
+	{
+		writer.addDocument(toLucene(doc,null));
+	}
+
 }