Nick
Nick

Reputation: 19664

How do I create a LockFactory for Lucene.net?

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.");
}

Upvotes: 2

Views: 1126

Answers (2)

Michael Gorsich
Michael Gorsich

Reputation: 337

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:


    using Lucene.Net.Store;

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

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

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.

Upvotes: 1

Jf Beaulac
Jf Beaulac

Reputation: 5246

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.

Upvotes: 1

Related Questions