annotate src/goodjava/lucene/logging/LoggingIndexWriter.java @ 1551:9cc4cee39b8b

add LuanOpDoer
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 04 Oct 2020 16:29:54 -0600
parents 41c32da4cbd1
children 52241b69c339
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.Random;
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
14 import java.util.concurrent.TimeUnit;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
15 import org.apache.lucene.document.Document;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
16 import org.apache.lucene.index.DirectoryReader;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
17 import org.apache.lucene.index.IndexReader;
1539
c27dc6af87ca remove nextId
Franklin Schmidt <fschmidt@gmail.com>
parents: 1538
diff changeset
18 import org.apache.lucene.index.IndexWriter;
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
1488
af55cfad6e12 start lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1487
diff changeset
38 public class LoggingIndexWriter implements GoodIndexWriter {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
39 private static final Logger logger = LoggerFactory.getLogger(LoggingIndexWriter.class);
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
40 private static final int version = 2;
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;
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
45 private static final int OP_TAG = 5;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
46 private static final Random rnd = new Random();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
48 public final LuceneIndexWriter indexWriter;
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
49 public boolean wasCreated;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
50 private final File logDir;
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
51 private final long logTime;
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
52 protected final LogFile[] logs = new LogFile[3];
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
53 private LogOutputStream log;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
54 private final File index;
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
55 private final SemaphoreLock mergeLock = new SemaphoreLock();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
57 public LoggingIndexWriter(LuceneIndexWriter indexWriter,File logDir,long logTime)
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
58 throws IOException
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
59 {
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
60 this.indexWriter = indexWriter;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
61 this.logDir = logDir;
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
62 this.logTime = logTime;
1501
Franklin Schmidt <fschmidt@gmail.com>
parents: 1499
diff changeset
63 IoUtils.mkdirs(logDir);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
64 if( !logDir.isDirectory() )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
65 throw new RuntimeException();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
66 index = new File(logDir,"index");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
67 if( index.exists() ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
68 DataInputStream dis = new DataInputStream(new FileInputStream(index));
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
69 try {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
70 if( dis.readInt() == version ) {
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
71 for( int i=0; i<logs.length; i++ ) {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
72 File file = new File( logDir, dis.readUTF() );
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
73 logs[i] = new LogFile(file);
1465
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 deleteUnusedFiles();
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
76 setLog();
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
77 wasCreated = false;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
78 return;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
79 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
80 } finally {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
81 dis.close();
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 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
84 newLogs();
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
85 wasCreated = true;
1476
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
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
88 public IndexReader openReader() throws IOException {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
89 return indexWriter.openReader();
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
90 }
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
91
1539
c27dc6af87ca remove nextId
Franklin Schmidt <fschmidt@gmail.com>
parents: 1538
diff changeset
92 public IndexWriter getLuceneIndexWriter() {
c27dc6af87ca remove nextId
Franklin Schmidt <fschmidt@gmail.com>
parents: 1538
diff changeset
93 return indexWriter.getLuceneIndexWriter();
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
94 }
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
95
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
96 private void setLog() throws IOException {
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
97 if( log != null )
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
98 log.close();
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
99 log = logs[2].output();
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
100 }
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
101 /*
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
102 public synchronized boolean isMerging() {
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
103 return mergeLock.isLocked();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
104 }
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
105 */
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
106 private void getMergeLock() {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
107 try {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
108 if( !mergeLock.tryLock(1,TimeUnit.MINUTES) )
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
109 throw new RuntimeException("failed to acquire lock");
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
110 } catch(InterruptedException e) {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
111 throw new RuntimeException(e);
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
112 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
113 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
114
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
115 public synchronized void newLogs() throws IOException {
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
116 getMergeLock();
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
117 try {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
118 newLogs2();
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
119 } finally {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
120 mergeLock.unlock();
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
121 }
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
122 }
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
123
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
124 private void newLogs2() throws IOException {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
125 logger.info("building new logs");
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
126 for( int i=0; i<logs.length; i++ ) {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
127 logs[i] = newLogFile();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
128 }
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
129 LogOutputStream log = logs[0].output();
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
130 logLucene( System.currentTimeMillis(), log, indexWriter );
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
131 log.close();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
132 writeIndex();
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
133 setLog();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
134 logger.info("done building new logs");
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
135 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
136
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
137 public synchronized void logLucene()
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
138 throws IOException
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
139 {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
140 //log.rollback(); ?
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
141 logLucene( System.currentTimeMillis(), log, indexWriter );
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
142 }
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
143
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
144 private static void logLucene(long time,LogOutputStream log,LuceneIndexWriter indexWriter)
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
145 throws IOException
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
146 {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
147 log.writeLong(time);
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
148 log.writeByte(OP_DELETE_ALL);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
149 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
150 final IndexSearcher searcher = new IndexSearcher(reader);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
151 Query query = new MatchAllDocsQuery();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
152 searcher.search( query, new GoodCollector(){
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
153 public void collectDoc(int iDoc) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
154 Document doc = searcher.doc(iDoc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
155 Map<String,Object> storedFields = LuceneUtils.toMap(doc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
156 log.writeLong(time);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
157 log.writeByte(OP_ADD_DOCUMENT);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
158 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
159 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
160 });
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
161 reader.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
162 log.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
163 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
164
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
165 private LogFile newLogFile() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
166 File file;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
167 do {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
168 file = new File(logDir,"_"+rnd.nextInt(100)+".log");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
169 } while( file.exists() );
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
170 return new LogFile(file);
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
171 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
172
1473
6c6ce14db6a8 add goodjava.io
Franklin Schmidt <fschmidt@gmail.com>
parents: 1465
diff changeset
173 private void deleteUnusedFiles() throws IOException {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
174 deleteUnusedFiles(logs,index);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
175 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
176
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
177 private static void deleteUnusedFiles(LogFile[] logs,File index) throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
178 Set<String> used = new HashSet<String>();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
179 used.add( index.getName() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
180 for( LogFile lf : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
181 used.add( lf.file.getName() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
182 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
183 for( File f : index.getParentFile().listFiles() ) {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
184 if( !used.contains(f.getName()) ) {
1475
c7b86342857f more IoUtils
Franklin Schmidt <fschmidt@gmail.com>
parents: 1473
diff changeset
185 IoUtils.deleteRecursively(f);
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 }
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 writeIndex() throws IOException {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
191 writeIndex(logs,index);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
192 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
193
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
194 public static void writeIndex(LogFile[] logs,File index) throws IOException {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
195 if( logs.length != 3 )
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
196 throw new RuntimeException();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
197 ByteArrayOutputStream baos = new ByteArrayOutputStream();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
198 DataOutputStream dos = new DataOutputStream(baos);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
199 dos.writeInt(version);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
200 for( LogFile lf : logs ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
201 String fileName = lf.file.getName();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
202 dos.writeUTF(fileName);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
203 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
204 dos.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
205 RandomAccessFile raf = new RandomAccessFile( index, "rwd" );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
206 raf.write( baos.toByteArray() );
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
207 raf.close();
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
208 deleteUnusedFiles(logs,index);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1488
diff changeset
209 //logger.info("writeIndex "+logs.toString());
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
210 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
211
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
212 private void mergeLogs() throws IOException {
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
213 logger.info("merge");
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
214 if( !mergeLock.isLocked() ) {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
215 logger.error("merge without lock");
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
216 return;
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
217 }
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
218 LogFile first = logs[0];
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
219 LogFile second = logs[1];
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
220 long lastTime = second.file.lastModified();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
221 File dirFile = new File(logDir,"merge");
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
222 if( dirFile.exists() )
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
223 throw new RuntimeException();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
224 Directory dir = FSDirectory.open(dirFile);
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
225 LuceneIndexWriter mergeWriter = new LuceneIndexWriter( dir, indexWriter.goodConfig );
1551
9cc4cee39b8b add LuanOpDoer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1549
diff changeset
226 OpDoer opDoer = new BasicOpDoer(mergeWriter);
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
227 playLog( first.input(), opDoer );
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
228 playLog( second.input(), opDoer );
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
229 mergeWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
230 LogFile merge = newLogFile();
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
231 LogOutputStream log = merge.output();
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
232 logLucene( lastTime, log, mergeWriter );
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
233 log.close();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
234 mergeWriter.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
235 synchronized(this) {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
236 //check();
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
237 logs[0] = merge;
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
238 logs[1] = logs[2];
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
239 logs[2] = newLogFile();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
240 writeIndex();
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
241 setLog();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
242 //check(null);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
243 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
244 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
245 private final Runnable mergeLogs = new Runnable() { public void run() {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
246 try {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
247 mergeLogs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
248 } catch(IOException e) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
249 throw new RuntimeException(e);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
250 } finally {
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
251 mergeLock.unlock();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
252 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
253 } };
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
254
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
255 private static class DocIter {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
256 final IndexReader reader;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
257 final TopDocs td;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
258 final int n;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
259 int i = 0;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
260
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
261 DocIter(IndexReader reader,Query query,Sort sort) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
262 this.reader = reader;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
263 IndexSearcher searcher = new IndexSearcher(reader);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
264 this.td = searcher.search(query,10000000,sort);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
265 this.n = td.scoreDocs.length;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
266 if( td.totalHits != n )
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
267 throw new RuntimeException();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
268 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
269
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
270 Document next() throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
271 return i < n ? reader.document(td.scoreDocs[i++].doc) : null;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
272 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
273 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
274
1487
Franklin Schmidt <fschmidt@gmail.com>
parents: 1486
diff changeset
275 private volatile boolean isChecking = false;
Franklin Schmidt <fschmidt@gmail.com>
parents: 1486
diff changeset
276
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
277 public boolean check(SortField sortField) throws IOException {
1487
Franklin Schmidt <fschmidt@gmail.com>
parents: 1486
diff changeset
278 if( isChecking )
Franklin Schmidt <fschmidt@gmail.com>
parents: 1486
diff changeset
279 throw new RuntimeException("another check is running");
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1504
diff changeset
280 isChecking = true;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1504
diff changeset
281 try {
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
282 return doCheck(sortField);
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1504
diff changeset
283 } finally {
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1504
diff changeset
284 isChecking = false;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1504
diff changeset
285 }
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1504
diff changeset
286 }
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1504
diff changeset
287
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
288 protected boolean doCheck(SortField sortField) throws IOException {
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
289 boolean ok = true;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
290 IndexReader indexReader;
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
291 LogInputStream[] logReaders;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
292 synchronized(this) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
293 indexReader = indexWriter.openReader();
1484
1fa6e8ec2d53 lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
294 logReaders = logReaders(logs);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
295 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
296 try {
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
297 //logger.info("check start");
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
298 indexWriter.check();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
299 File dirFile = new File(logDir,"check");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
300 IoUtils.deleteRecursively(dirFile);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
301 Directory dir = FSDirectory.open(dirFile);
1528
3bd4d7963456 use goodjava/lucene/api
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
302 LuceneIndexWriter checkWriter = new LuceneIndexWriter( dir, indexWriter.goodConfig );
1551
9cc4cee39b8b add LuanOpDoer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1549
diff changeset
303 playLogs(logReaders,new BasicOpDoer(checkWriter));
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
304 //logger.info("check lucene");
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
305 IndexReader checkReader = checkWriter.openReader();
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
306 int nCheck = checkReader.numDocs();
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
307 int nOrig = indexReader.numDocs();
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
308 if( nCheck != nOrig ) {
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
309 logger.error("numDocs mismatch: lucene="+nOrig+" logs="+nCheck);
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
310 ok = false;
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
311 }
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
312 if( sortField == null ) {
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
313 if( ok && hash(indexReader) != hash(checkReader) ) {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
314 logger.error("hash mismatch");
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
315 ok = false;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
316 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
317 } else {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
318 Sort sort = new Sort(sortField);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
319 String sortFieldName = sortField.getField();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
320 Query query = new PrefixQuery(new Term(sortFieldName));
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
321 DocIter origIter = new DocIter(indexReader,query,sort);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
322 DocIter checkIter = new DocIter(checkReader,query,sort);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
323 Map<String,Object> origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
324 Map<String,Object> checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
325 while( origFields!=null && checkFields!=null ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
326 Comparable origFld = (Comparable)origFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
327 Comparable checkFld = (Comparable)checkFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
328 int cmp = origFld.compareTo(checkFld);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
329 if( cmp==0 ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
330 if( !origFields.equals(checkFields) ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
331 logger.error(sortFieldName+" "+origFld+" not equal");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
332 logger.error("lucene = "+origFields);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
333 logger.error("logs = "+checkFields);
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
334 ok = false;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
335 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
336 origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
337 checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
338 } else if( cmp < 0 ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
339 logger.error(sortFieldName+" "+origFld+" found in lucene but not logs");
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
340 ok = false;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
341 origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
342 } else { // >
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
343 logger.error(sortFieldName+" "+checkFld+" found in logs but not lucene");
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
344 ok = false;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
345 checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
346 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
347 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
348 while( origFields!=null ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
349 Comparable origFld = (Comparable)origFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
350 logger.error(sortFieldName+" "+origFld+" found in lucene but not logs");
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
351 ok = false;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
352 origFields = LuceneUtils.toMap(origIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
353 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
354 while( checkFields!=null ) {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
355 Comparable checkFld = (Comparable)checkFields.get(sortFieldName);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
356 logger.error(sortFieldName+" "+checkFld+" found in logs but not lucene");
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
357 ok = false;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
358 checkFields = LuceneUtils.toMap(checkIter.next());
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
359 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
360 //logger.info("check done");
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
361 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
362 checkReader.close();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
363 checkWriter.close();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
364 IoUtils.deleteRecursively(dirFile);
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
365 //logger.info("check done");
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
366 } finally {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
367 indexReader.close();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
368 }
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
369 return ok;
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
370 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
371
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
372 private static abstract class HashCollector extends GoodCollector {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
373 int total = 0;
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
374 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
375
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
376 private static int hash(IndexReader reader) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
377 final IndexSearcher searcher = new IndexSearcher(reader);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
378 Query query = new MatchAllDocsQuery();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
379 HashCollector col = new HashCollector() {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
380 public void collectDoc(int iDoc) throws IOException {
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
381 Document doc = searcher.doc(iDoc);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
382 Map<String,Object> storedFields = LuceneUtils.toMap(doc);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
383 total += storedFields.hashCode();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
384 }
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
385 };
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
386 searcher.search(query,col);
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
387 return col.total;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
388 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
389
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
390 public synchronized void close() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
391 indexWriter.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
392 log.commit();
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
393 log.close();
1465
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
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
396 public synchronized void commit() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
397 indexWriter.commit();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
398 log.commit();
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
399 if( mergeLock.isLocked() )
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
400 return;
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
401 if( logs[1].file.lastModified() < System.currentTimeMillis() - logTime ) {
1538
634f6765830e use goodjava/lucene/logging
Franklin Schmidt <fschmidt@gmail.com>
parents: 1528
diff changeset
402 getMergeLock();
1504
f443542d8650 threading
Franklin Schmidt <fschmidt@gmail.com>
parents: 1502
diff changeset
403 new Thread(mergeLogs).start();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
404 // mergeLogs.run();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
405 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
406 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
407
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
408 public synchronized void rollback() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
409 indexWriter.rollback();
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
410 log.rollback();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
411 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
412
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
413 public synchronized void deleteAll() throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
414 indexWriter.deleteAll();
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
415 writeOp(OP_DELETE_ALL);
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
416 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
417
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
418 public synchronized void deleteDocuments(Query query) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
419 indexWriter.deleteDocuments(query);
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
420 writeOp(OP_DELETE_DOCUMENTS);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
421 log.writeQuery(query);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
422 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
423
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
424 public synchronized void addDocument(Map<String,Object> storedFields) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
425 indexWriter.addDocument(storedFields);
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
426 writeOp(OP_ADD_DOCUMENT);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
427 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
428 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
429
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
430 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
431 indexWriter.updateDocument(keyFieldName,storedFields);
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
432 writeOp(OP_UPDATE_DOCUMENT);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
433 log.writeUTF(keyFieldName);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
434 log.writeMap(storedFields);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
435 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
436
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
437 public synchronized void tag(String tag) throws IOException {
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
438 writeOp(OP_TAG);
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
439 log.writeUTF(tag);
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
440 }
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
441
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
442 public synchronized void reindexDocuments(String keyFieldName,Query query) throws IOException {
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
443 indexWriter.reindexDocuments(keyFieldName,query);
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
444 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
445
1486
2469aa31f31b LogOutputStream
Franklin Schmidt <fschmidt@gmail.com>
parents: 1484
diff changeset
446 private void writeOp(int op) throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
447 log.writeLong(System.currentTimeMillis());
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
448 log.writeByte(op);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
449 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
450
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
451 public synchronized void playLogs(OpDoer opDoer) throws IOException {
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
452 if( opDoer == null )
1551
9cc4cee39b8b add LuanOpDoer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1549
diff changeset
453 opDoer = new BasicOpDoer(indexWriter);
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
454 playLogs( logReaders(logs), opDoer );
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
455 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
456
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
457 private static LogInputStream[] logReaders(LogFile[] logs) throws IOException {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
458 LogInputStream[] logReaders = new LogInputStream[logs.length];
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
459 for( int i=0; i<logs.length; i++ ) {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
460 logReaders[i] = logs[i].input();
1484
1fa6e8ec2d53 lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
461 }
1fa6e8ec2d53 lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
462 return logReaders;
1fa6e8ec2d53 lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
463 }
1fa6e8ec2d53 lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
464
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
465 private static void playLogs(LogInputStream[] logReaders,OpDoer opDoer)
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
466 throws IOException
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
467 {
1551
9cc4cee39b8b add LuanOpDoer
Franklin Schmidt <fschmidt@gmail.com>
parents: 1549
diff changeset
468 if( numDocs(opDoer.writer()) != 0 )
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
469 throw new RuntimeException ("not empty");
1484
1fa6e8ec2d53 lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
470 for( LogInputStream reader : logReaders ) {
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
471 playLog(reader,opDoer);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
472 }
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
473 opDoer.commit();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
474 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
475
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
476 private static int numDocs(GoodIndexWriter indexWriter) throws IOException {
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
477 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
478 int n = reader.numDocs();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
479 reader.close();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
480 return n;
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
481 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
482
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
483 private static void playLog(LogInputStream in,OpDoer opDoer)
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
484 throws IOException
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
485 {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
486 while( in.available() > 0 ) {
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
487 playOp(in,opDoer);
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
488 }
1484
1fa6e8ec2d53 lucene.logging cleanup
Franklin Schmidt <fschmidt@gmail.com>
parents: 1476
diff changeset
489 in.close();
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
490 }
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
491
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
492 private static void playOp(LogInputStream in,OpDoer opDoer)
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
493 throws IOException
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
494 {
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
495 long time = in.readLong(); // time
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
496 int op = in.readByte();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
497 switch(op) {
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
498 case OP_DELETE_ALL:
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
499 opDoer.deleteAll(time);
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
500 return;
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
501 case OP_DELETE_DOCUMENTS:
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
502 {
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
503 Query query = in.readQuery();
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
504 //System.out.println("OP_DELETE_DOCUMENTS "+query);
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
505 opDoer.deleteDocuments(time,query);
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
506 return;
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
507 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
508 case OP_ADD_DOCUMENT:
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
509 {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
510 Map storedFields = in.readMap();
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
511 opDoer.addDocument(time,storedFields);
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
512 return;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
513 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
514 case OP_UPDATE_DOCUMENT:
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
515 {
1476
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
516 String keyFieldName = in.readUTF();
7d145095cc0b lucene.logging check
Franklin Schmidt <fschmidt@gmail.com>
parents: 1475
diff changeset
517 Map storedFields = in.readMap();
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
518 opDoer.updateDocument(time,keyFieldName,storedFields);
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
519 return;
1544
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
520 }
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
521 case OP_TAG:
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
522 {
35601f15ecc3 add lucene log tag and restore_from_log
Franklin Schmidt <fschmidt@gmail.com>
parents: 1539
diff changeset
523 String tag = in.readUTF();
1549
41c32da4cbd1 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
524 opDoer.tag(time,tag);
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1544
diff changeset
525 return;
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
526 }
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
527 default:
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
528 throw new RuntimeException("invalid op "+op);
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
529 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
530 }
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
531
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
532 private static void dump(LuceneIndexWriter indexWriter) throws IOException {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
533 IndexReader reader = indexWriter.openReader();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
534 IndexSearcher searcher = new IndexSearcher(reader);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
535 Query query = new MatchAllDocsQuery();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
536 TopDocs td = searcher.search(query,100);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
537 System.out.println("totalHits = "+td.totalHits);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
538 for( int i=0; i<td.scoreDocs.length; i++ ) {
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
539 Document doc = searcher.doc(td.scoreDocs[i].doc);
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
540 System.out.println(LuceneUtils.toMap(doc));
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
541 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
542 System.out.println();
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
543 reader.close();
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
544 }
1465
5e3870618377 lucene.logging dir
Franklin Schmidt <fschmidt@gmail.com>
parents: 1461
diff changeset
545
1461
e5d48b85351c start lucene.logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
546 }