1 package org.apache.lucene.index; 2 3 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.PrintStream ; 22 23 import org.apache.lucene.analysis.Analyzer; 24 import org.apache.lucene.document.Document; 25 import org.apache.lucene.store.Directory; 26 import org.apache.lucene.store.FSDirectory; 27 28 86 public class IndexModifier { 87 88 protected IndexWriter indexWriter = null; 89 protected IndexReader indexReader = null; 90 91 protected Directory directory = null; 92 protected Analyzer analyzer = null; 93 protected boolean open = false; 94 95 protected PrintStream infoStream = null; 97 protected boolean useCompoundFile = true; 98 protected int maxBufferedDocs = IndexWriter.DEFAULT_MAX_BUFFERED_DOCS; 99 protected int maxFieldLength = IndexWriter.DEFAULT_MAX_FIELD_LENGTH; 100 protected int mergeFactor = IndexWriter.DEFAULT_MERGE_FACTOR; 101 102 110 public IndexModifier(Directory directory, Analyzer analyzer, boolean create) throws IOException { 111 init(directory, analyzer, create); 112 } 113 114 122 public IndexModifier(String dirName, Analyzer analyzer, boolean create) throws IOException { 123 Directory dir = FSDirectory.getDirectory(dirName, create); 124 init(dir, analyzer, create); 125 } 126 127 135 public IndexModifier(File file, Analyzer analyzer, boolean create) throws IOException { 136 Directory dir = FSDirectory.getDirectory(file, create); 137 init(dir, analyzer, create); 138 } 139 140 144 protected void init(Directory directory, Analyzer analyzer, boolean create) throws IOException { 145 this.directory = directory; 146 synchronized(this.directory) { 147 this.analyzer = analyzer; 148 indexWriter = new IndexWriter(directory, analyzer, create); 149 open = true; 150 } 151 } 152 153 157 protected void assureOpen() { 158 if (!open) { 159 throw new IllegalStateException ("Index is closed"); 160 } 161 } 162 163 167 protected void createIndexWriter() throws IOException { 168 if (indexWriter == null) { 169 if (indexReader != null) { 170 indexReader.close(); 171 indexReader = null; 172 } 173 indexWriter = new IndexWriter(directory, analyzer, false); 174 indexWriter.setInfoStream(infoStream); 175 indexWriter.setUseCompoundFile(useCompoundFile); 176 indexWriter.setMaxBufferedDocs(maxBufferedDocs); 177 indexWriter.setMaxFieldLength(maxFieldLength); 178 indexWriter.setMergeFactor(mergeFactor); 179 } 180 } 181 182 186 protected void createIndexReader() throws IOException { 187 if (indexReader == null) { 188 if (indexWriter != null) { 189 indexWriter.close(); 190 indexWriter = null; 191 } 192 indexReader = IndexReader.open(directory); 193 } 194 } 195 196 200 public void flush() throws IOException { 201 synchronized(directory) { 202 assureOpen(); 203 if (indexWriter != null) { 204 indexWriter.close(); 205 indexWriter = null; 206 createIndexWriter(); 207 } else { 208 indexReader.close(); 209 indexReader = null; 210 createIndexReader(); 211 } 212 } 213 } 214 215 223 public void addDocument(Document doc, Analyzer docAnalyzer) throws IOException { 224 synchronized(directory) { 225 assureOpen(); 226 createIndexWriter(); 227 if (docAnalyzer != null) 228 indexWriter.addDocument(doc, docAnalyzer); 229 else 230 indexWriter.addDocument(doc); 231 } 232 } 233 234 241 public void addDocument(Document doc) throws IOException { 242 addDocument(doc, null); 243 } 244 245 255 public int deleteDocuments(Term term) throws IOException { 256 synchronized(directory) { 257 assureOpen(); 258 createIndexReader(); 259 return indexReader.deleteDocuments(term); 260 } 261 } 262 263 274 public int delete(Term term) throws IOException { 275 return deleteDocuments(term); 276 } 277 278 283 public void deleteDocument(int docNum) throws IOException { 284 synchronized(directory) { 285 assureOpen(); 286 createIndexReader(); 287 indexReader.deleteDocument(docNum); 288 } 289 } 290 291 297 public void delete(int docNum) throws IOException { 298 deleteDocument(docNum); 299 } 300 301 307 public int docCount() { 308 synchronized(directory) { 309 assureOpen(); 310 if (indexWriter != null) { 311 return indexWriter.docCount(); 312 } else { 313 return indexReader.numDocs(); 314 } 315 } 316 } 317 318 324 public void optimize() throws IOException { 325 synchronized(directory) { 326 assureOpen(); 327 createIndexWriter(); 328 indexWriter.optimize(); 329 } 330 } 331 332 339 public void setInfoStream(PrintStream infoStream) { 340 synchronized(directory) { 341 assureOpen(); 342 if (indexWriter != null) { 343 indexWriter.setInfoStream(infoStream); 344 } 345 this.infoStream = infoStream; 346 } 347 } 348 349 353 public PrintStream getInfoStream() throws IOException { 354 synchronized(directory) { 355 assureOpen(); 356 createIndexWriter(); 357 return indexWriter.getInfoStream(); 358 } 359 } 360 361 368 public void setUseCompoundFile(boolean useCompoundFile) { 369 synchronized(directory) { 370 assureOpen(); 371 if (indexWriter != null) { 372 indexWriter.setUseCompoundFile(useCompoundFile); 373 } 374 this.useCompoundFile = useCompoundFile; 375 } 376 } 377 378 382 public boolean getUseCompoundFile() throws IOException { 383 synchronized(directory) { 384 assureOpen(); 385 createIndexWriter(); 386 return indexWriter.getUseCompoundFile(); 387 } 388 } 389 390 404 public void setMaxFieldLength(int maxFieldLength) { 405 synchronized(directory) { 406 assureOpen(); 407 if (indexWriter != null) { 408 indexWriter.setMaxFieldLength(maxFieldLength); 409 } 410 this.maxFieldLength = maxFieldLength; 411 } 412 } 413 414 418 public int getMaxFieldLength() throws IOException { 419 synchronized(directory) { 420 assureOpen(); 421 createIndexWriter(); 422 return indexWriter.getMaxFieldLength(); 423 } 424 } 425 426 440 public void setMaxBufferedDocs(int maxBufferedDocs) { 441 synchronized(directory) { 442 assureOpen(); 443 if (indexWriter != null) { 444 indexWriter.setMaxBufferedDocs(maxBufferedDocs); 445 } 446 this.maxBufferedDocs = maxBufferedDocs; 447 } 448 } 449 450 454 public int getMaxBufferedDocs() throws IOException { 455 synchronized(directory) { 456 assureOpen(); 457 createIndexWriter(); 458 return indexWriter.getMaxBufferedDocs(); 459 } 460 } 461 462 475 public void setMergeFactor(int mergeFactor) { 476 synchronized(directory) { 477 assureOpen(); 478 if (indexWriter != null) { 479 indexWriter.setMergeFactor(mergeFactor); 480 } 481 this.mergeFactor = mergeFactor; 482 } 483 } 484 485 489 public int getMergeFactor() throws IOException { 490 synchronized(directory) { 491 assureOpen(); 492 createIndexWriter(); 493 return indexWriter.getMergeFactor(); 494 } 495 } 496 497 502 public void close() throws IOException { 503 synchronized(directory) { 504 if (!open) 505 throw new IllegalStateException ("Index is closed already"); 506 if (indexWriter != null) { 507 indexWriter.close(); 508 indexWriter = null; 509 } else { 510 indexReader.close(); 511 indexReader = null; 512 } 513 open = false; 514 } 515 } 516 517 public String toString() { 518 return "Index@" + directory; 519 } 520 521 537 538 } 539 | Popular Tags |