annotate src/goodjava/lucene/logging/LoggingIndexWriter.java @ 1475:c7b86342857f

more IoUtils
author Franklin Schmidt <fschmidt@gmail.com>
date Sat, 18 Apr 2020 11:02:18 -0600
parents 6c6ce14db6a8
children 7d145095cc0b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.logging;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
3 import java.io.File;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
4 import java.io.RandomAccessFile;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
5 import java.io.ByteArrayOutputStream;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
6 import java.io.DataOutputStream;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
7 import java.io.DataInputStream;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
8 import java.io.FileInputStream;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.io.IOException;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.util.Map;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
11 import java.util.Set;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
12 import java.util.HashSet;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
13 import java.util.List;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
14 import java.util.ArrayList;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
15 import java.util.Random;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
16 import org.apache.lucene.document.Document;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
17 import org.apache.lucene.index.DirectoryReader;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
18 import org.apache.lucene.index.IndexReader;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
19 import org.apache.lucene.search.IndexSearcher;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 import org.apache.lucene.search.Query;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
21 import org.apache.lucene.search.MatchAllDocsQuery;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
22 import org.apache.lucene.search.TopDocs;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
23 import org.apache.lucene.store.Directory;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
24 import org.apache.lucene.store.FSDirectory;
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
25 import goodjava.io.IoUtils;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26 import goodjava.lucene.api.GoodIndexWriter;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
27 import goodjava.lucene.api.LuceneIndexWriter;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
28 import goodjava.lucene.api.GoodCollector;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
29 import goodjava.lucene.api.LuceneUtils;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
30 import goodjava.logging.Logger;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
31 import goodjava.logging.LoggerFactory;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
32
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
33
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
34 public final class LoggingIndexWriter implements GoodIndexWriter {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
35 private static final Logger logger = LoggerFactory.getLogger(LoggingIndexWriter.class);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
36 private static final int version = 1;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 private static final int OP_DELETE_ALL = 1;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
38 private static final int OP_DELETE_DOCUMENTS = 2;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
39 private static final int OP_ADD_DOCUMENT = 3;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
40 private static final int OP_UPDATE_DOCUMENT = 4;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
41 private static final Random rnd = new Random();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
43 public final LuceneIndexWriter indexWriter;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
44 private final File logDir;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
45 private final List<LogFile> logs = new ArrayList<LogFile>();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
46 private final File index;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
47 private boolean isMerging = false;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
49 public LoggingIndexWriter(LuceneIndexWriter indexWriter,File logDir) throws IOException {
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
50 this.indexWriter = indexWriter;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
51 this.logDir = logDir;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
52 logDir.mkdirs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
53 if( !logDir.isDirectory() )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
54 throw new RuntimeException();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
55 index = new File(logDir,"index");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
56 if( index.exists() ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
57 DataInputStream dis = new DataInputStream(new FileInputStream(index));
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
58 try {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
59 if( dis.readInt() == version ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
60 final int n = dis.readInt();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
61 for( int i=0; i<n; i++ ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
62 File file = new File( logDir, dis.readUTF() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
63 logs.add( new LogFile(file,"rwd") );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
64 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
65 deleteUnusedFiles();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
66 log().gotoEnd();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
67 return;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
68 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
69 } finally {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
70 dis.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
71 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
72 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
73 for( int i=0; i<2; i++ ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
74 logs.add( newLogFile() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
75 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
76 isMerging = true;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
77 new Thread(new Runnable(){public void run(){
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
78 try {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
79 logLucene( System.currentTimeMillis(), logs.get(0), indexWriter );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
80 synchronized(LoggingIndexWriter.this) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
81 writeIndex();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
82 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
83 } catch(IOException e) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
84 throw new RuntimeException(e);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
85 } finally {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
86 synchronized(LoggingIndexWriter.this) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
87 isMerging = false;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
88 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
89 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
90 }}).start();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
93 private static void logLucene(long time,LogFile log,LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
94 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
95 final IndexSearcher searcher = new IndexSearcher(reader);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
96 Query query = new MatchAllDocsQuery();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
97 searcher.search( query, new GoodCollector(){
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
98 public void collectDoc(int iDoc) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
99 Document doc = searcher.doc(iDoc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
100 Map<String,Object> storedFields = LuceneUtils.toMap(doc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
101 log.writeLong(time);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
102 log.writeByte(OP_ADD_DOCUMENT);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
103 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
104 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
105 });
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
106 reader.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
107 log.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
108 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
109
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
110 private LogFile newLogFile() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
111 File file;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
112 do {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
113 file = new File(logDir,"_"+rnd.nextInt(100)+".log");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
114 } while( file.exists() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
115 return new LogFile(file,"rwd");
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
118 private void deleteUnusedFiles() throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
119 Set<String> used = new HashSet<String>();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
120 used.add( index.getName() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
121 for( LogFile lf : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
122 used.add( lf.file.getName() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
123 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
124 for( File f : logDir.listFiles() ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
125 if( !used.contains(f.getName()) ) {
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
126 IoUtils.deleteRecursively(f);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
127 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
128 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
129 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
130
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
131 private void writeIndex() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
132 ByteArrayOutputStream baos = new ByteArrayOutputStream();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
133 DataOutputStream dos = new DataOutputStream(baos);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
134 dos.writeInt(version);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
135 dos.writeInt(logs.size());
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
136 for( LogFile lf : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
137 String fileName = lf.file.getName();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
138 dos.writeUTF(fileName);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
139 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
140 dos.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
141 RandomAccessFile raf = new RandomAccessFile( index, "rwd" );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
142 raf.write( baos.toByteArray() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
143 raf.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
144 deleteUnusedFiles();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
145 logger.info("writeIndex "+logs.toString());
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
146 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
147
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
148 private void mergeLogs() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
149 logger.info("merge");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
150 LogFile first = logs.get(0);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
151 LogFile second = logs.get(1);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
152 second.gotoEnd();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
153 long lastTime = second.readLong();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
154 File dirFile = new File(logDir,"merge");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
155 if( dirFile.exists() )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
156 throw new RuntimeException();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
157 Directory dir = FSDirectory.open(dirFile);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
158 LuceneIndexWriter mergeWriter = new LuceneIndexWriter( indexWriter.luceneVersion, dir, indexWriter.goodConfig );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
159 playLog(first,mergeWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
160 playLog(second,mergeWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
161 mergeWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
162 LogFile merge = newLogFile();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
163 logLucene( lastTime, merge, mergeWriter );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
164 mergeWriter.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
165 synchronized(this) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
166 check();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
167 logs.remove(0);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
168 logs.set(0,merge);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
169 writeIndex();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
170 check();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
171 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
173 private final Runnable mergeLogs = new Runnable() { public void run() {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
174 try {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
175 mergeLogs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
176 /*
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
177 } catch(IOException e) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
178 throw new RuntimeException(e);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
179 */
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
180 } catch(Exception e) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
181 e.printStackTrace();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
182 System.exit(-1);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
183 } finally {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
184 synchronized(LoggingIndexWriter.this) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
185 isMerging = false;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
186 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
187 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
188 } };
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
189
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
190 private void check() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
191 File dirFile = new File(logDir,"check");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
192 if( dirFile.exists() )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
193 throw new RuntimeException();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
194 Directory dir = FSDirectory.open(dirFile);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
195 LuceneIndexWriter checkWriter = new LuceneIndexWriter( indexWriter.luceneVersion, dir, indexWriter.goodConfig );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
196 playLog(checkWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
197 int nCheck = numDocs(checkWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
198 int nOrig = numDocs(indexWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
199 if( nCheck != nOrig ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
200 logger.error("nCheck = "+nCheck);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
201 logger.error("nOrig = "+nOrig);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
202 //new Exception().printStackTrace();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
203 Thread.dumpStack();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
204 System.out.println();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
205 System.out.println("indexWriter");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
206 dump(indexWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
207 System.out.println("checkWriter");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
208 dump(checkWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
209 System.exit(-1);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
210 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
211 checkWriter.close();
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
212 IoUtils.deleteRecursively(dirFile);
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
213 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
214
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
215 private LogFile log() {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
216 return logs.get(logs.size()-1);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
217 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
218
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
219 public synchronized void close() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
220 indexWriter.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
221 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
222 log.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
223 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
224
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
225 public synchronized void commit() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
226 indexWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
227 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
228 log.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
229 if( isMerging )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
230 return;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
231 if( log.length() > logs.get(0).length() ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
232 log.writeLong( System.currentTimeMillis() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
233 logs.add( newLogFile() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
234 writeIndex();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
235 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
236 if( logs.size() > 3 ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
237 isMerging = true;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
238 // new Thread(mergeLogs).start();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
239 mergeLogs.run();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
240 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
241 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
242
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
243 public synchronized void rollback() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
244 indexWriter.rollback();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
245 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
246 log.gotoEnd();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
247 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
248
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
249 public synchronized void deleteAll() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
250 indexWriter.deleteAll();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
251 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
252 writeOp(log,OP_DELETE_ALL);
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
253 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
255 public synchronized void deleteDocuments(Query query) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
256 indexWriter.deleteDocuments(query);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
257 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
258 writeOp(log,OP_DELETE_DOCUMENTS);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
259 log.writeQuery(query);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
260 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
261
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
262 public synchronized void addDocument(Map<String,Object> storedFields) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
263 indexWriter.addDocument(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
264 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
265 writeOp(log,OP_ADD_DOCUMENT);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
266 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
267 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
268
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
269 public synchronized void updateDocument(String keyFieldName,Map<String,Object> storedFields) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
270 indexWriter.updateDocument(keyFieldName,storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
271 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
272 writeOp(log,OP_UPDATE_DOCUMENT);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
273 log.writeUTF(keyFieldName);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
274 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
275 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
276
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
277 public synchronized void reindexDocuments(String keyFieldName,Query query) throws IOException {
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
278 indexWriter.reindexDocuments(keyFieldName,query);
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
279 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
280
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
281 private void writeOp(LogFile log,int op) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
282 log.writeLong(System.currentTimeMillis());
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
283 log.writeByte(op);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
284 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
285
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
286 public synchronized void playLog() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
287 playLog(indexWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
288 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
289
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
290 private void playLog(LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
291 if( numDocs(indexWriter) != 0 )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
292 throw new RuntimeException ("not empty");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
293 for( LogFile log : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
294 playLog(log,indexWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
295 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
296 indexWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
297 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
298
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
299 private static int numDocs(LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
300 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
301 int n = reader.numDocs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
302 reader.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
303 return n;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
304 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
305
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
306 private static void playLog(LogFile log,LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
307 log.gotoStart();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
308 while( log.hasMore() ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
309 playOp(log,indexWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
310 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
311 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
312
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
313 private static void playOp(LogFile log,LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
314 log.readLong(); // time
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
315 int op = log.readByte();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
316 switch(op) {
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
317 case OP_DELETE_ALL:
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
318 indexWriter.deleteAll();
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
319 return;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
320 case OP_DELETE_DOCUMENTS:
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
321 indexWriter.deleteDocuments( log.readQuery() );
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
322 return;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
323 case OP_ADD_DOCUMENT:
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
324 {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
325 Map storedFields = log.readMap();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
326 indexWriter.addDocument(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
327 return;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
328 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
329 case OP_UPDATE_DOCUMENT:
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
330 {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
331 String keyFieldName = log.readUTF();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
332 Map storedFields = log.readMap();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
333 indexWriter.updateDocument(keyFieldName,storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
334 return;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
335 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
336 default:
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
337 throw new RuntimeException("invalid op "+op);
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
338 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
339 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
340
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
341 private static void dump(LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
342 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
343 IndexSearcher searcher = new IndexSearcher(reader);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
344 Query query = new MatchAllDocsQuery();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
345 TopDocs td = searcher.search(query,100);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
346 System.out.println("totalHits = "+td.totalHits);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
347 for( int i=0; i<td.scoreDocs.length; i++ ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
348 Document doc = searcher.doc(td.scoreDocs[i].doc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
349 System.out.println(LuceneUtils.toMap(doc));
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
350 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
351 System.out.println();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
352 reader.close();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
353 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
354
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
355 }