changeset 1488:af55cfad6e12

start lucene.backup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2020 15:38:48 -0600
parents 044a360c2300
children fe237d72b234
files src/goodjava/io/IoUtils.java src/goodjava/lucene/backup/BackupIndexWriter.java src/goodjava/lucene/logging/LoggingIndexWriter.java
diffstat 3 files changed, 48 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/io/IoUtils.java	Fri May 01 16:17:20 2020 -0600
+++ b/src/goodjava/io/IoUtils.java	Sat May 02 15:38:48 2020 -0600
@@ -29,4 +29,8 @@
 		delete(file);
 	}
 
+	public static void link(File from,File to) throws IOException {
+		Files.createLink( to.toPath(), from.toPath() );
+	}
+
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/goodjava/lucene/backup/BackupIndexWriter.java	Sat May 02 15:38:48 2020 -0600
@@ -0,0 +1,42 @@
+package goodjava.lucene.backup;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
+import goodjava.io.IoUtils;
+import goodjava.lucene.api.LuceneIndexWriter;
+import goodjava.lucene.logging.LoggingIndexWriter;
+import goodjava.lucene.logging.LogFile;
+
+
+public class BackupIndexWriter extends LoggingIndexWriter {
+	private final String name;
+	private final File dir;
+
+	public BackupIndexWriter(LuceneIndexWriter indexWriter,File logDir,String name) throws IOException {
+		super(indexWriter,logDir);
+		this.name = name;
+		File f = new File(System.getProperty("java.io.tmpdir"));
+		dir = new File(f,"goodjava.lucene/"+name);
+		dir.mkdirs();
+	}
+
+	public synchronized void commit() throws IOException {
+		super.commit();
+		sync();
+	}
+
+	private void sync() throws IOException {
+		for( File f : dir.listFiles() ) {
+			IoUtils.delete(f);
+		}
+		List<LogFile> logs = new ArrayList<LogFile>();
+		for( LogFile log : this.logs ) {
+			File f = new File(dir,log.file.getName());
+			IoUtils.link(log.file,f);
+			logs.add( new LogFile(f) );
+		}
+	}
+
+}
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Fri May 01 16:17:20 2020 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Sat May 02 15:38:48 2020 -0600
@@ -35,7 +35,7 @@
 import goodjava.logging.LoggerFactory;
 
 
-public final class LoggingIndexWriter implements GoodIndexWriter {
+public class LoggingIndexWriter implements GoodIndexWriter {
 	private static final Logger logger = LoggerFactory.getLogger(LoggingIndexWriter.class);
 	private static final int version = 1;
 	private static final int OP_DELETE_ALL = 1;
@@ -46,7 +46,7 @@
 
 	public final LuceneIndexWriter indexWriter;
 	private final File logDir;
-	private final List<LogFile> logs = new ArrayList<LogFile>();
+	protected final List<LogFile> logs = new ArrayList<LogFile>();
 	private LogOutputStream log;
 	private final File index;
 	private boolean isMerging = false;