我想構建一個Lucene序列化類,該序列化將用DataMember裝飾的屬性序列化/反序列化對象(類),並指定如何存儲屬性的特殊屬性/ Lucene索引中的字段。在C#中的Lucene串行器,需要性能建議
當我需要通過某個鍵/值對檢索單個對象時,該類正常工作。 但我注意到,如果有時我需要檢索所有項目,那麼我們假設有100,000個文檔 - 然後MySQL做到這一點〜大約快10倍...由於某種原因...
您能否檢查此代碼(Lucene專家),並提出任何可能的性能改進相關的想法?
public IEnumerable<T> LoadAll()
{
IndexReader reader = IndexReader.Open(this.PathToLuceneIndex);
int itemsCount = reader.NumDocs();
for (int i = 0; i < itemsCount; i++)
{
if (!reader.IsDeleted(i))
{
Document doc = reader.Document(i);
if (doc != null)
{
T item = Deserialize(doc);
yield return item;
}
}
}
if (reader != null) reader.Close();
}
private T Deserialize(Document doc)
{
T itemInstance = Activator.CreateInstance<T>();
foreach (string fieldName in fieldTypes.Keys)
{
Field myField = doc.GetField(fieldName);
//Not every document may have the full collection of indexable fields
if (myField != null)
{
object fieldValue = myField.StringValue();
Type fieldType = fieldTypes[fieldName];
if (fieldType == typeof(bool))
fieldValue = fieldValue == "1" ? true : false;
if (fieldType == typeof(DateTime))
fieldValue = DateTools.StringToDate((string)fieldValue);
pF.SetValue(itemInstance, fieldName, fieldValue);
}
}
return itemInstance;
}
預先感謝您!
謝謝您的意見。我已經按照建議實施了RAMDirectory,而讀者只讀爲...拳擊費用並不顯着,我會嘗試通過以某種方式調整Activator.CreateInstance();並找到用反射設置屬性值的最快方法。 –
Denis
有沒有比Document doc = reader.Document(i)更快速獲取/閱讀文檔的方法; ? - 因爲,例如對於我的文件集 - 大約需要700毫秒。檢索100,000個項目......這是在創建對象實例(新建)之前,將DateTime和布爾值(從字符串轉換爲..),使用反射設置值等等。 – Denis
這是獲取對象的首選方法文件。獲取文檔的一部分問題是,當您加載文檔時,這些字段會加載到內存中。國際海事組織,Lucene應該總是懶洋洋地加載文件字段,但是當前版本的lucene不這樣做。您可以使用MapFieldSelector並只選擇您需要的字段或將字典傳遞給字符串FieldSelectorResult,並且您可以將其告知LazyLoad某些字段。你將不得不加載你最終需要的數據,所以它只是推遲了成本。 –