如何为 Lucene.net 创建 LockFactory?
我有一个 WCF 服务,它将索引写入文件系统。我担心如果多个客户端尝试同时执行此操作,我可能会遇到线程问题。我看到 FSDirectory.Open() 有一个重载,允许我传递“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.");
}
I have a WCF service that writes indexes out to the file system. I am concerned that I may run into threading issues if more than one client attempt to execute this operation at the same time. I see that FSDirectory.Open() has an overload that allows me to pass a "LockFactory".
I have not been able to find any documentation on how to create one of these LockFactories for Lucene .net. Can someone tell me where I might find some documentation on LockFactory or what interfaces I should be implementing?
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.");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从您发布的代码中,我不明白为什么您需要比默认更多的东西 NativeFSLockFactory。不使用锁工厂参数的 FSDirectory.Open() 重载使用此重载。
要制作自定义的,您必须实现抽象 LockFactory 类。
From the code you posted I dont see why you'd need something more than the default NativeFSLockFactory. FSDirectory.Open() overloads that do not take a lock factory in parameter use this one.
To make a custom one, you'd have to implement the abstract LockFactory class.
不知道为什么 Jf Beaulac 的答案被接受,因为它没有回答问题。我在弄清楚这一点时遇到了很多麻烦,并且“Lucene In Action”中没有任何示例。因此,对于那些需要回答这个问题的人,这就是我最终弄清楚的。
您不直接创建LockFactory,它是一个抽象类。您创建 LockFactory 的实现之一,例如 SingleInstanceLockFactory。像这样:
还需要注意的是,如果您向构造函数提供路径字符串,则无法在创建 FSDirectory 时直接添加 LockFactory 实例;仅当您向构造函数提供 DirectoryInfo 时才能执行此操作。否则,您可以使用 SetLockFactory() 方法来执行此操作,如图所示。
Not sure why Jf Beaulac's answer was accepted since it does not answer the question. I had a lot of trouble figuring this out and there are no examples of it in "Lucene In Action". So for those of you who need this question answered, here's what I eventually figured out.
You don't directly create a LockFactory, it is an abstract class. You create one of the implementations of LockFactory, such as SingleInstanceLockFactory. Like so:
Also important to note is that you can't add your LockFactory instance directly when creating your FSDirectory if you are supplying a path string to the constructor; you can only do that if you are supplying a DirectoryInfo to the constructor. Otherwise you do it with the SetLockFactory() method, as shown.