2012-01-30 5 views
0

사이트를 크롤링하고 컨텐츠를 lucene 인덱스 파일로 실제 디렉토리에 씁니다.Lucene 인덱스 파일에 쓰레드를 안전하게 쓰기

이 목적을 위해 스레드를 사용할 때 잠금으로 인해 오류 또는 오류가 발생합니다.

여러 스레드를 사용하고 스레드의 작업을 놓치지 않고 색인 파일에 쓰기를 원합니다. 위의 클래스에

public class WriteDocument 
{ 
    private static Analyzer _analyzer; 

    private static IndexWriter indexWriter; 

    private static string Host; 

    public WriteDocument(string _Host) 
    { 
     Host = _Host; 
     Lucene.Net.Store.Directory _directory = FSDirectory.GetDirectory(Host, false); 
     _analyzer = new StandardAnalyzer(); 
     bool indexExists = IndexReader.IndexExists(_directory); 
     bool createIndex = !indexExists; 

     indexWriter = new IndexWriter(_directory, _analyzer, true); 
    } 
    public void AddDocument(object obj) 
    { 
      DocumentSettings doc = (DocumentSettings)obj;    
      Field urlField = new Field("Url", doc.downloadedDocument.Uri.ToString(), Field.Store.YES, Field.Index.TOKENIZED); 
      document.Add(urlField); 
      indexWriter.AddDocument(document); 

      document = null; 
      doc.downloadedDocument = null; 

      indexWriter.Optimize(); 
      indexWriter.Close(); 
     } 
} 

는,이 같은 값을 전달하고있다 :

DocumentSettings writedoc = new DocumentSettings() 
{ 
     Host = Host, 
     downloadedDocument = downloadDocument 
}; 
Thread t = new Thread(() => 
{ 
doc.AddDocument(writedoc); 
}); 
t.Start(); 

나는 코드가 오류없이 나를 위해 작동 t.Join();t.Start(); 후 추가하는 경우. 하지만 이렇게하면 프로세스가 느려지고 사실상 스레드를 사용하지 않고 얻을 수있는 출력과 같습니다.

Cannot rename /indexes/Segments.new to /indexes/Segments 
the file is used by some other process. 

사람이 코드를 좀 도와 줄래 : 같은

나는 점점 오전 오류?

+0

어떤 오류가 발생합니까? 사용중인 코드의 작은 예를 볼 수 있습니까? – unholysampler

+0

명확성을 위해 코드가 추가되었습니다. –

답변

0

IndexWriter는 스레드로부터 안전하지 않으므로 사용할 수 없습니다.

다운로드 할 때 여러 스레드를 사용하려는 경우 다운로드 및 생성중인 문서를 피드에 넣고 큐에 넣을 수있는 일종의 "메시지 펌프"를 빌드해야합니다.

예를 들어, AddDocument 메서드에서 인덱스를 직접 사용하지 않고 인덱스를 직접 인덱싱 할 서비스로 보내기 만하면됩니다.

해당 서비스는 항상 큐에있는 모든 것을 인덱싱하려고 시도해야하며 당분간 큐가없는 경우에는 잠시 동안 대기해야합니다.

+0

글쎄, 혼란 스럽네요. - Lucene.net 3.0.3 docs는 IndexWriter가 [thread-safe]라고 말합니다 (https://lucenenet.apache.org/docs/3.0.3/d2/d1d/). class_lucene_1_1_net_1_1_index_1_1_index_writer.html # details) – chester89

0

취할 수있는 접근 방법은 각 스레드에 대해 별도의 색인을 생성하고 모든 스레드를 끝에 병합하는 것입니다. 예 : index1, index2 ... indexn (스레드 1..n에 해당) 병합합니다.