diff src/goodjava/lucene/backup/BackupIndexWriter.java @ 1488:af55cfad6e12

start lucene.backup
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 02 May 2020 15:38:48 -0600
parents
children 22e15cf73040
line wrap: on
line diff
--- /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) );
+		}
+	}
+
+}