2011-07-25 25 views
2

我有一個將索引寫入文件系統的WCF服務。我擔心如果多個客戶端同時嘗試執行此操作,可能會遇到線程問題。我看到FSDirectory.Open()有一個重載,允許我傳遞一個「LockFactory」。如何爲Lucene.net創建LockFactory?

我一直沒有找到任何有關如何爲Lucene.net創建這些LockFactories的文檔。有人可以告訴我在哪裏可以找到關於LockFactory的一些文檔或我應該實現哪些接口?

DirectoryInfo indexDirectory = new DirectoryInfo(ConfigurationManager.AppSettings["indexpath"]); 
Directory luceneDirectory = FSDirectory.Open(indexDirectory); 
try 
{ 
    IndexWriter indexWriter = new IndexWriter(luceneDirectory, new StandardAnalyzer()); 
    Document document = new Document(); 

    foreach (KeyValuePair<string,string> keyValuePair in _metaDataDictionary) 
    { 
     document.Add(new Field(keyValuePair.Key, keyValuePair.Value, Field.Store.YES, Field.Index.ANALYZED)); 
     indexWriter.AddDocument(document); 
    } 

    indexWriter.Optimize(); 
    indexWriter.Flush(); 
    indexWriter.Close(); 
} 
catch(IOException e) 
{ 
    throw new IOException("Could not read Lucene index file."); 
} 

回答

1

從你的代碼貼出我不明白爲什麼你需要的東西比默認NativeFSLockFactory更多。 FSDirectory.Open()重載不參加鎖工廠使用這一個。

要定製一個,您必須實現抽象LockFactory類。

0

不知道爲什麼Jf Beaulac的答案被接受,因爲它沒有回答這個問題。我在解決這個問題時遇到了很多麻煩,在「Lucene In Action」中沒有這方面的例子。所以對於那些需要這個問題的人來說,這是我最終想到的。

你不直接創建一個LockFactory,它是一個抽象類。您創建LockFactory的一個實現,例如SingleInstanceLockFactory。像這樣:


    using Lucene.Net.Store; 

    class Ydude{ 
     FSDirectory fsd; 
     SingleInstanceLockFactory silf = new SingleInstanceLockFactory(); 

     fsd = FSDirectory.Open(@"C:\My\Index\Path"); 
     fsd.SetLockFactory(silf); 
    } 

另外需要注意的是,創建FSDirectory如果提供的路徑字符串構造函數的時候,你不能直接添加您的LockFactory實例;你只能這樣做,如果你正在向構造函數提供一個DirectoryInfo。否則,您可以使用SetLockFactory()方法執行此操作,如圖所示。