annotate src/goodjava/lucene/logging/LoggingIndexWriter.java @ 1476:7d145095cc0b

lucene.logging check
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 19 Apr 2020 20:42:26 -0600
parents c7b86342857f
children 1fa6e8ec2d53
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;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
19 import org.apache.lucene.index.Term;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
20 import org.apache.lucene.search.IndexSearcher;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import org.apache.lucene.search.Query;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
22 import org.apache.lucene.search.MatchAllDocsQuery;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
23 import org.apache.lucene.search.TopDocs;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
24 import org.apache.lucene.search.PrefixQuery;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
25 import org.apache.lucene.search.SortField;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
26 import org.apache.lucene.search.Sort;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
27 import org.apache.lucene.store.Directory;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
28 import org.apache.lucene.store.FSDirectory;
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
29 import goodjava.io.IoUtils;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 import goodjava.lucene.api.GoodIndexWriter;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
31 import goodjava.lucene.api.LuceneIndexWriter;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
32 import goodjava.lucene.api.GoodCollector;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
33 import goodjava.lucene.api.LuceneUtils;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
34 import goodjava.logging.Logger;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
35 import goodjava.logging.LoggerFactory;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
36
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
38 public final class LoggingIndexWriter implements GoodIndexWriter {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
39 private static final Logger logger = LoggerFactory.getLogger(LoggingIndexWriter.class);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
40 private static final int version = 1;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
41 private static final int OP_DELETE_ALL = 1;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
42 private static final int OP_DELETE_DOCUMENTS = 2;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
43 private static final int OP_ADD_DOCUMENT = 3;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 private static final int OP_UPDATE_DOCUMENT = 4;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
45 private static final Random rnd = new Random();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
47 public final LuceneIndexWriter indexWriter;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
48 private final File logDir;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
49 private final List<LogFile> logs = new ArrayList<LogFile>();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
50 private final File index;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
51 private boolean isMerging = false;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
53 public LoggingIndexWriter(LuceneIndexWriter indexWriter,File logDir) throws IOException {
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 this.indexWriter = indexWriter;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
55 this.logDir = logDir;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
56 logDir.mkdirs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
57 if( !logDir.isDirectory() )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
58 throw new RuntimeException();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
59 index = new File(logDir,"index");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
60 if( index.exists() ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
61 DataInputStream dis = new DataInputStream(new FileInputStream(index));
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
62 try {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
63 if( dis.readInt() == version ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
64 final int n = dis.readInt();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
65 for( int i=0; i<n; i++ ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
66 File file = new File( logDir, dis.readUTF() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
67 logs.add( new LogFile(file,"rwd") );
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 deleteUnusedFiles();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
70 return;
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 } finally {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
73 dis.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
74 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
75 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
76 newLogs();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
77 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
78
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
79 public synchronized boolean isMerging() {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
80 return isMerging;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
81 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
82
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
83 private synchronized void isNotMerging() {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
84 isMerging = false;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
85 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
86
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
87 public synchronized void newLogs() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
88 if( isMerging )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
89 throw new RuntimeException("merging");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
90 logger.info("building new logs");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
91 logs.clear();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
92 for( int i=0; i<2; i++ ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
93 logs.add( newLogFile() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
94 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
95 logLucene( System.currentTimeMillis(), logs.get(0), indexWriter );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
96 writeIndex();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
97 logger.info("done building new logs");
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
98 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
99
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
100 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
101 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
102 final IndexSearcher searcher = new IndexSearcher(reader);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
103 Query query = new MatchAllDocsQuery();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
104 searcher.search( query, new GoodCollector(){
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
105 public void collectDoc(int iDoc) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
106 Document doc = searcher.doc(iDoc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
107 Map<String,Object> storedFields = LuceneUtils.toMap(doc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
108 log.writeLong(time);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
109 log.writeByte(OP_ADD_DOCUMENT);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
110 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
111 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
112 });
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
113 reader.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
114 log.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
115 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
116
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
117 private LogFile newLogFile() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
118 File file;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
119 do {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
120 file = new File(logDir,"_"+rnd.nextInt(100)+".log");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
121 } while( file.exists() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
122 return new LogFile(file,"rwd");
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
125 private void deleteUnusedFiles() throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
126 Set<String> used = new HashSet<String>();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
127 used.add( index.getName() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
128 for( LogFile lf : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
129 used.add( lf.file.getName() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
130 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
131 for( File f : logDir.listFiles() ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
132 if( !used.contains(f.getName()) ) {
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
133 IoUtils.deleteRecursively(f);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
134 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
135 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
137
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
138 private void writeIndex() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
139 ByteArrayOutputStream baos = new ByteArrayOutputStream();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
140 DataOutputStream dos = new DataOutputStream(baos);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
141 dos.writeInt(version);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
142 dos.writeInt(logs.size());
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
143 for( LogFile lf : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
144 String fileName = lf.file.getName();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
145 dos.writeUTF(fileName);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
146 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
147 dos.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
148 RandomAccessFile raf = new RandomAccessFile( index, "rwd" );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
149 raf.write( baos.toByteArray() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
150 raf.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
151 deleteUnusedFiles();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
152 logger.info("writeIndex "+logs.toString());
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
153 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
154
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
155 private void mergeLogs() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
156 logger.info("merge");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
157 LogFile first = logs.get(0);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
158 LogFile second = logs.get(1);
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
159 long lastTime = second.file.lastModified();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
160 File dirFile = new File(logDir,"merge");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
161 if( dirFile.exists() )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
162 throw new RuntimeException();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
163 Directory dir = FSDirectory.open(dirFile);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
164 LuceneIndexWriter mergeWriter = new LuceneIndexWriter( indexWriter.luceneVersion, dir, indexWriter.goodConfig );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
165 playLog(first,mergeWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
166 playLog(second,mergeWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
167 mergeWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
168 LogFile merge = newLogFile();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
169 logLucene( lastTime, merge, mergeWriter );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
170 mergeWriter.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
171 synchronized(this) {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
172 //check();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
173 logs.remove(0);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
174 logs.set(0,merge);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
175 writeIndex();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
176 //check(null);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
177 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
178 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
179 private final Runnable mergeLogs = new Runnable() { public void run() {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
180 try {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
181 mergeLogs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
182 } catch(IOException e) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
183 throw new RuntimeException(e);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
184 } finally {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
185 isNotMerging();
1465
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 } };
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
188
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
189 private static class DocIter {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
190 final IndexReader reader;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
191 final TopDocs td;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
192 final int n;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
193 int i = 0;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
194
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
195 DocIter(IndexReader reader,Query query,Sort sort) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
196 this.reader = reader;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
197 IndexSearcher searcher = new IndexSearcher(reader);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
198 this.td = searcher.search(query,10000000,sort);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
199 this.n = td.scoreDocs.length;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
200 if( td.totalHits != n )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
201 throw new RuntimeException();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
202 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
203
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
204 Document next() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
205 return i < n ? reader.document(td.scoreDocs[i++].doc) : null;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
206 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
207 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
208
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
209 public void check(SortField sortField) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
210 IndexReader indexReader;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
211 List<LogFile> logs;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
212 synchronized(this) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
213 if( isMerging ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
214 logger.warn("is merging, check aborted");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
215 return;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
216 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
217 isMerging = true;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
218 indexReader = indexWriter.openReader();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
219 logs = new ArrayList<LogFile>(this.logs);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
220 int i = logs.size() - 1;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
221 LogFile last = logs.get(i);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
222 logs.set(i,last.snapshot());
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
223 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
224 try {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
225 logger.info("check start");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
226 indexWriter.check();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
227 File dirFile = new File(logDir,"check");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
228 IoUtils.deleteRecursively(dirFile);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
229 Directory dir = FSDirectory.open(dirFile);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
230 LuceneIndexWriter checkWriter = new LuceneIndexWriter( indexWriter.luceneVersion, dir, indexWriter.goodConfig );
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
231 playLogs(logs,checkWriter);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
232 logger.info("check lucene");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
233 IndexReader checkReader = checkWriter.openReader();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
234 if( sortField == null ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
235 int nCheck = checkReader.numDocs();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
236 int nOrig = indexReader.numDocs();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
237 if( nCheck != nOrig ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
238 logger.error("numDocs mismatch: lucene="+nOrig+" logs="+nCheck);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
239 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
240 logger.info("numDocs="+nOrig);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
241 if( hash(indexReader) != hash(checkReader) ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
242 logger.error("hash mismatch");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
243 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
244 } else {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
245 Sort sort = new Sort(sortField);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
246 String sortFieldName = sortField.getField();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
247 Query query = new PrefixQuery(new Term(sortFieldName));
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
248 DocIter origIter = new DocIter(indexReader,query,sort);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
249 DocIter checkIter = new DocIter(checkReader,query,sort);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
250 Map<String,Object> origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
251 Map<String,Object> checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
252 while( origFields!=null && checkFields!=null ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
253 Comparable origFld = (Comparable)origFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
254 Comparable checkFld = (Comparable)checkFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
255 int cmp = origFld.compareTo(checkFld);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
256 if( cmp==0 ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
257 if( !origFields.equals(checkFields) ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
258 logger.error(sortFieldName+" "+origFld+" not equal");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
259 logger.error("lucene = "+origFields);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
260 logger.error("logs = "+checkFields);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
261 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
262 origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
263 checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
264 } else if( cmp < 0 ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
265 logger.error(sortFieldName+" "+origFld+" found in lucene but not logs");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
266 origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
267 } else { // >
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
268 logger.error(sortFieldName+" "+checkFld+" found in logs but not lucene");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
269 checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
270 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
271 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
272 while( origFields!=null ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
273 Comparable origFld = (Comparable)origFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
274 logger.error(sortFieldName+" "+origFld+" found in lucene but not logs");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
275 origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
276 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
277 while( checkFields!=null ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
278 Comparable checkFld = (Comparable)checkFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
279 logger.error(sortFieldName+" "+checkFld+" found in logs but not lucene");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
280 checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
281 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
282 //logger.info("check done");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
283 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
284 checkReader.close();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
285 checkWriter.close();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
286 IoUtils.deleteRecursively(dirFile);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
287 logger.info("check done");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
288 } finally {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
289 indexReader.close();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
290 isNotMerging();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
291 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
292 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
293
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
294 private static abstract class HashCollector extends GoodCollector {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
295 int total = 0;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
296 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
297
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
298 private static int hash(IndexReader reader) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
299 final IndexSearcher searcher = new IndexSearcher(reader);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
300 Query query = new MatchAllDocsQuery();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
301 HashCollector col = new HashCollector() {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
302 public void collectDoc(int iDoc) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
303 Document doc = searcher.doc(iDoc);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
304 Map<String,Object> storedFields = LuceneUtils.toMap(doc);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
305 total += storedFields.hashCode();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
306 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
307 };
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
308 searcher.search(query,col);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
309 return col.total;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
310 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
311
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
312 private LogFile log() {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
313 return logs.get(logs.size()-1);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
314 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
315
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
316 public synchronized void close() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
317 indexWriter.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
318 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
319 log.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
320 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
321
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
322 public synchronized void commit() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
323 indexWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
324 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
325 log.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
326 if( isMerging )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
327 return;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
328 if( log.end() > logs.get(0).end() ) {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
329 logs.add( newLogFile() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
330 writeIndex();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
331 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
332 if( logs.size() > 3 ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
333 isMerging = true;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
334 new Thread(mergeLogs).start();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
335 // mergeLogs.run();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
336 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
337 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
338
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
339 public synchronized void rollback() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
340 indexWriter.rollback();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
341 LogFile log = log();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
342 log.rollback();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
343 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
344
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
345 public synchronized void deleteAll() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
346 indexWriter.deleteAll();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
347 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
348 writeOp(log,OP_DELETE_ALL);
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
349 }
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 public synchronized void deleteDocuments(Query query) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
352 indexWriter.deleteDocuments(query);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
353 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
354 writeOp(log,OP_DELETE_DOCUMENTS);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
355 log.writeQuery(query);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
356 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
357
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
358 public synchronized void addDocument(Map<String,Object> storedFields) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
359 indexWriter.addDocument(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
360 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
361 writeOp(log,OP_ADD_DOCUMENT);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
362 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
363 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
364
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
365 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
366 indexWriter.updateDocument(keyFieldName,storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
367 LogFile log = log();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
368 writeOp(log,OP_UPDATE_DOCUMENT);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
369 log.writeUTF(keyFieldName);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
370 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
371 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
372
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
373 public synchronized void reindexDocuments(String keyFieldName,Query query) throws IOException {
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
374 indexWriter.reindexDocuments(keyFieldName,query);
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
375 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
376
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
377 private void writeOp(LogFile log,int op) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
378 log.writeLong(System.currentTimeMillis());
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
379 log.writeByte(op);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
380 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
381
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
382 public synchronized void playLogs() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
383 playLogs(logs,indexWriter);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
384 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
385
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
386 private static void playLogs(List<LogFile> logs,LuceneIndexWriter indexWriter) throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
387 if( numDocs(indexWriter) != 0 )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
388 throw new RuntimeException ("not empty");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
389 for( LogFile log : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
390 playLog(log,indexWriter);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
391 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
392 indexWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
393 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
394
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
395 private static int numDocs(LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
396 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
397 int n = reader.numDocs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
398 reader.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
399 return n;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
400 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
401
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
402 private static void playLog(LogFile log,LuceneIndexWriter indexWriter) throws IOException {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
403 LogInputStream in = log.input();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
404 while( in.available() > 0 ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
405 playOp(in,indexWriter);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
406 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
407 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
408
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
409 private static void playOp(LogInputStream in,LuceneIndexWriter indexWriter) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
410 in.readLong(); // time
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
411 int op = in.readByte();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
412 switch(op) {
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
413 case OP_DELETE_ALL:
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
414 indexWriter.deleteAll();
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
415 return;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
416 case OP_DELETE_DOCUMENTS:
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
417 indexWriter.deleteDocuments( in.readQuery() );
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
418 return;
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
419 case OP_ADD_DOCUMENT:
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
420 {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
421 Map storedFields = in.readMap();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
422 indexWriter.addDocument(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
423 return;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
424 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
425 case OP_UPDATE_DOCUMENT:
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
426 {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
427 String keyFieldName = in.readUTF();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
428 Map storedFields = in.readMap();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
429 indexWriter.updateDocument(keyFieldName,storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
430 return;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
431 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
432 default:
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
433 throw new RuntimeException("invalid op "+op);
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
434 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
435 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
436
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
437 private static void dump(LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
438 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
439 IndexSearcher searcher = new IndexSearcher(reader);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
440 Query query = new MatchAllDocsQuery();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
441 TopDocs td = searcher.search(query,100);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
442 System.out.println("totalHits = "+td.totalHits);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
443 for( int i=0; i<td.scoreDocs.length; i++ ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
444 Document doc = searcher.doc(td.scoreDocs[i].doc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
445 System.out.println(LuceneUtils.toMap(doc));
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
446 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
447 System.out.println();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
448 reader.close();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
449 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
450
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
451 }