lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "kerr" <keelk...@hotmail.com>
Subject about increment update
Date Thu, 03 Apr 2003 03:20:31 GMT
Hello everyone,
Here I try to increment update index file and follow the idea to delete modified file first
and re-add it. Here is the source.
But when I execute it, the index directory create a file(write.lock) when execute the line
    reader.delete(i);, 
and caught a class java.io.IOException   with message: Index locked for write.
After that, when I execute the line
    IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), false);
caught a class java.io.IOException   with message: Index locked for write
if I delete the file(write.lock), the error will re-happen.
anyone can help and thanks.
       Kerr.


import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;

import java.io.File;
import java.util.Date;


public class UpdateIndexFiles {
  public static void main(String[] args) {
    try {
      Date start = new Date();

      Directory directory = FSDirectory.getDirectory("index", false);
      IndexReader reader = IndexReader.open(directory);
      System.out.println(reader.isLocked(directory));
      //reader.unlock(directory);
      IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), false);

      String base = "";
      if (args.length == 0){
        base = "D:\\Tomcat\\webapps\\ROOT\\test";
      } else {
        base = args[0];
      }
      removeModifiedFiles(reader);
      updateIndexDocs(reader, writer, new File(base));

      writer.optimize();
      writer.close();

      Date end = new Date();

      System.out.print(end.getTime() - start.getTime());
      System.out.println(" total milliseconds");

    } catch (Exception e) {
      System.out.println(" caught a " + e.getClass() +
       "\n with message: " + e.getMessage());
      e.printStackTrace();
    }
  }

  public static void removeModifiedFiles(IndexReader reader) throws Exception {
    Document adoc;
    String path;
    File aFile;
    for (int i=0; i<reader.numDocs(); i++){
      adoc = reader.document(i);
      path = adoc.get("path");
      aFile = new File(path);
      if (reader.lastModified(path) < aFile.lastModified()){
        System.out.println(reader.isLocked(path));
        reader.delete(i);
      }
    }
  }

  public static void updateIndexDocs(IndexReader reader, IndexWriter writer, File file)
       throws Exception {

    if (file.isDirectory()) {
      String[] files = file.list();
      for (int i = 0; i < files.length; i++)
  updateIndexDocs(reader, writer, new File(file, files[i]));
    } else {
      if (!reader.indexExists(file)){
        System.out.println("adding " + file);
        writer.addDocument(FileDocument.Document(file));
      } else {}
    }
  }
}
Mime
View raw message