Mercurial Hosting > luan
view src/goodjava/lucene/logging/LoggingIndexWriter.java @ 1464:465b4a0dae4a
empty list vs map
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 03 Apr 2020 10:04:52 -0600 |
parents | e5d48b85351c |
children | 5e3870618377 |
line wrap: on
line source
package goodjava.lucene.logging; import java.io.IOException; import java.util.Map; import org.apache.lucene.search.Query; import goodjava.lucene.api.GoodIndexWriter; public class LoggingIndexWriter implements GoodIndexWriter { private static final int OP_DELETE_ALL = 1; private static final int OP_DELETE_DOCUMENTS = 2; private static final int OP_ADD_DOCUMENT = 3; private static final int OP_UPDATE_DOCUMENT = 4; public final GoodIndexWriter indexWriter; private final LogFile logFile; public LoggingIndexWriter(GoodIndexWriter indexWriter) throws IOException { this.indexWriter = indexWriter; logFile = new LogFile("lucene.log","rw"); logFile.gotoStart(); // for now } public void close() throws IOException { indexWriter.close(); logFile.commit(); } public void commit() throws IOException { indexWriter.commit(); logFile.commit(); } public void rollback() throws IOException { indexWriter.rollback(); logFile.gotoEnd(); } public void deleteAll() throws IOException { indexWriter.deleteAll(); logFile.writeByte(OP_DELETE_ALL); } public void deleteDocuments(Query query) throws IOException { indexWriter.deleteDocuments(query); logFile.writeByte(OP_DELETE_DOCUMENTS); logFile.writeQuery(query); } public void addDocument(Map<String,Object> storedFields) throws IOException { indexWriter.addDocument(storedFields); logFile.writeByte(OP_ADD_DOCUMENT); logFile.writeMap(storedFields); } public void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException { indexWriter.updateDocument(keyFieldName,storedFields); logFile.writeByte(OP_UPDATE_DOCUMENT); logFile.writeUTF(keyFieldName); logFile.writeMap(storedFields); } public void reindexDocuments(String keyFieldName,Query query) throws IOException { indexWriter.reindexDocuments(keyFieldName,query); } private void playOp() throws IOException { int op = logFile.readByte(); switch(op) { case OP_DELETE_ALL: indexWriter.deleteAll(); return; case OP_DELETE_DOCUMENTS: indexWriter.deleteDocuments( logFile.readQuery() ); return; case OP_ADD_DOCUMENT: indexWriter.addDocument( logFile.readMap() ); return; case OP_UPDATE_DOCUMENT: indexWriter.updateDocument( logFile.readUTF(), logFile.readMap() ); return; default: throw new RuntimeException("invalid op "+op); } } public void playLog() throws IOException { logFile.gotoStart(); while( logFile.hasMore() ) { playOp(); } indexWriter.commit(); } }