changeset 1677:ea7075b7afe1

switch to index.json
author Franklin Schmidt <fschmidt@gmail.com>
date Tue, 31 May 2022 14:36:16 -0600
parents daa4214fa91a
children f54484aae295
files src/goodjava/lucene/backup/Backup.java src/goodjava/lucene/logging/LoggingIndexWriter.java
diffstat 2 files changed, 40 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
diff -r daa4214fa91a -r ea7075b7afe1 src/goodjava/lucene/backup/Backup.java
--- a/src/goodjava/lucene/backup/Backup.java	Mon May 30 19:29:32 2022 -0600
+++ b/src/goodjava/lucene/backup/Backup.java	Tue May 31 14:36:16 2022 -0600
@@ -25,11 +25,11 @@
 	private static final Logger logger = LoggerFactory.getLogger(Backup.class);
 
 	private final File dir;
-	private final File index;
+	private final File indexFile;
 
 	Backup(File dir) {
 		this.dir = dir;
-		this.index = new File(dir,"index");
+		this.indexFile = new File(dir,"index.json");
 		dir.setLastModified(System.currentTimeMillis());
 	}
 
@@ -98,7 +98,7 @@
 			if( checksumObj != null ) {
 				long checksum = (Long)checksumObj;
 				if( log.checksum() != checksum ) {
-					index.delete();
+					indexFile.delete();
 					result = new RpcResult(new Object[]{"bad_checksum",name});
 					break;
 				}
@@ -118,7 +118,7 @@
 				logs[i] = new LogFile(f);
 			}
 			if( complete ) {
-				LoggingIndexWriter.writeIndex(logs,index);
+				LoggingIndexWriter.writeIndex(logs,indexFile);
 				//logger.info("write index");
 			}
 		}
diff -r daa4214fa91a -r ea7075b7afe1 src/goodjava/lucene/logging/LoggingIndexWriter.java
--- a/src/goodjava/lucene/logging/LoggingIndexWriter.java	Mon May 30 19:29:32 2022 -0600
+++ b/src/goodjava/lucene/logging/LoggingIndexWriter.java	Tue May 31 14:36:16 2022 -0600
@@ -3,13 +3,16 @@
 import java.io.File;
 import java.io.RandomAccessFile;
 import java.io.ByteArrayOutputStream;
-import java.io.DataOutputStream;
-import java.io.DataInputStream;
-import java.io.FileInputStream;
+import java.io.OutputStreamWriter;
+import java.io.FileReader;
+import java.io.Writer;
 import java.io.IOException;
+import java.util.List;
+import java.util.ArrayList;
 import java.util.Map;
 import java.util.Set;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.Random;
 import java.util.concurrent.TimeUnit;
 import org.apache.lucene.document.Document;
@@ -27,6 +30,9 @@
 import org.apache.lucene.store.Directory;
 import org.apache.lucene.store.FSDirectory;
 import goodjava.io.IoUtils;
+import goodjava.json.JsonParser;
+import goodjava.json.JsonToString;
+import goodjava.parser.ParseException;
 import goodjava.lucene.api.GoodIndexWriter;
 import goodjava.lucene.api.LuceneIndexWriter;
 import goodjava.lucene.api.GoodCollector;
@@ -51,7 +57,7 @@
 	private final long logTime;
 	protected final LogFile[] logs = new LogFile[3];
 	private LogOutputStream log;
-	private final File index;
+	private final File indexFile;
 	private final SemaphoreLock mergeLock = new SemaphoreLock();
 
 	public LoggingIndexWriter(LuceneIndexWriter indexWriter,File logDir,long logTime)
@@ -63,13 +69,14 @@
 		IoUtils.mkdirs(logDir);
 		if( !logDir.isDirectory() )
 			throw new RuntimeException();
-		index = new File(logDir,"index");
-		if( index.exists() ) {
-			DataInputStream dis = new DataInputStream(new FileInputStream(index));
+		indexFile = new File(logDir,"index.json");
+		if( indexFile.exists() ) {
 			try {
-				if( dis.readInt() == version ) {
+				Map map = (Map)JsonParser.parse( IoUtils.readAll(new FileReader(indexFile)) );
+				if( (Integer)map.get("version") == version ) {
+					List fileNames = (List)map.get("files");
 					for( int i=0; i<logs.length; i++ ) {
-						File file = new File( logDir, dis.readUTF() );
+						File file = new File( logDir, (String)fileNames.get(i) );
 						logs[i] = new LogFile(file);
 					}
 					deleteUnusedFiles();
@@ -77,8 +84,8 @@
 					wasCreated = false;
 					return;
 				}
-			} finally {
-				dis.close();
+			} catch(ParseException e) {
+				logger.error("bad index.json",e);
 			}
 		}
 		logger.info("building new logs");
@@ -157,16 +164,16 @@
 	}
 
 	private void deleteUnusedFiles() throws IOException {
-		deleteUnusedFiles(logs,index);
+		deleteUnusedFiles(logs,indexFile);
 	}
 
-	private static void deleteUnusedFiles(LogFile[] logs,File index) throws IOException {
+	private static void deleteUnusedFiles(LogFile[] logs,File indexFile) throws IOException {
 		Set<String> used = new HashSet<String>();
-		used.add( index.getName() );
+		used.add( indexFile.getName() );
 		for( LogFile lf : logs ) {
 			used.add( lf.file.getName() );
 		}
-		for( File f : index.getParentFile().listFiles() ) {
+		for( File f : indexFile.getParentFile().listFiles() ) {
 			if( !used.contains(f.getName()) ) {
 				IoUtils.deleteRecursively(f);
 			}
@@ -174,24 +181,29 @@
 	}
 
 	private void writeIndex() throws IOException {
-		writeIndex(logs,index);
+		writeIndex(logs,indexFile);
 	}
 
-	public static void writeIndex(LogFile[] logs,File index) throws IOException {
+	public static void writeIndex(LogFile[] logs,File indexFile) throws IOException {
 		if( logs.length != 3 )
 			throw new RuntimeException();
-		ByteArrayOutputStream baos = new ByteArrayOutputStream();
-		DataOutputStream dos = new DataOutputStream(baos);
-		dos.writeInt(version);
+		Map map = new LinkedHashMap();
+		map.put("version",version);
+		List fileNames = new ArrayList();
 		for( LogFile lf : logs ) {
 			String fileName = lf.file.getName();
-			dos.writeUTF(fileName);
+			fileNames.add(fileName);
 		}
-		dos.close();
-		RandomAccessFile raf = new RandomAccessFile( index, "rwd" );
+		map.put("files",fileNames);
+		ByteArrayOutputStream baos = new ByteArrayOutputStream();
+		Writer writer = new OutputStreamWriter(baos);
+		writer.write( new JsonToString().toString(map) );
+		writer.write( '\n' );
+		writer.close();
+		RandomAccessFile raf = new RandomAccessFile( indexFile, "rwd" );
 		raf.write( baos.toByteArray() );
 		raf.close();
-		deleteUnusedFiles(logs,index);
+		deleteUnusedFiles(logs,indexFile);
 		//logger.info("writeIndex "+logs.toString());
 	}