FileDocCategorySizeDatePackage
NativeFSLockFactory.javaAPI DocApache Lucene 2.1.010198Wed Feb 14 10:46:40 GMT 2007org.apache.lucene.store

NativeFSLockFactory

public class NativeFSLockFactory extends LockFactory
Implements {@link LockFactory} using native OS file locks (available through java.nio.*). Note that for certain filesystems native locks are possible but must be explicity configured and enabled (and may be disabled by default). For example, for NFS servers there sometimes must be a separate lockd process running, and other configuration may be required such as running the server in kernel mode. Other filesystems may not even support native OS locks in which case you must use a different {@link LockFactory} implementation.

The advantage of this lock factory over {@link SimpleFSLockFactory} is that the locks should be "correct", whereas {@link SimpleFSLockFactory} uses java.io.File.createNewFile which has warnings about not using it for locking. Furthermore, if the JVM crashes, the OS will free any held locks, whereas {@link SimpleFSLockFactory} will keep the locks held, requiring manual removal before re-running Lucene.

Note that, unlike {@link SimpleFSLockFactory}, the existence of leftover lock files in the filesystem on exiting the JVM is fine because the OS will free the locks held against these files even though the files still remain.

Native locks file names have the substring "-n-", which you can use to differentiate them from lock files created by {@link SimpleFSLockFactory}.

see
LockFactory

Fields Summary
private File
lockDir
Directory specified by org.apache.lucene.lockDir system property. If that is not set, then java.io.tmpdir system property is used.
Constructors Summary
public NativeFSLockFactory(String lockDirName)
Create a NativeFSLockFactory instance, storing lock files into the specified lockDirName:

param
lockDirName where lock files are created.

    this(new File(lockDirName));
  
public NativeFSLockFactory(File lockDir)
Create a NativeFSLockFactory instance, storing lock files into the specified lockDir:

param
lockDir where lock files are created.


    this.lockDir = lockDir;

    // Ensure that lockDir exists and is a directory.
    if (!lockDir.exists()) {
      if (!lockDir.mkdirs())
        throw new IOException("Cannot create directory: " +
                              lockDir.getAbsolutePath());
    } else if (!lockDir.isDirectory()) {
      throw new IOException("Found regular file where directory expected: " + 
                            lockDir.getAbsolutePath());
    }

    acquireTestLock();
  
Methods Summary
private voidacquireTestLock()

    String randomLockName = "lucene-" + Long.toString(new Random().nextInt(), Character.MAX_RADIX) + "-test.lock";
    
    Lock l = makeLock(randomLockName);
    try {
      l.obtain();
    } catch (IOException e) {
      IOException e2 = new IOException("Failed to acquire random test lock; please verify filesystem for lock directory '" + lockDir + "' supports locking");
      e2.initCause(e);
      throw e2;
    }

    l.release();
  
public voidclearLock(java.lang.String lockName)

    // Note that this isn't strictly required anymore
    // because the existence of these files does not mean
    // they are locked, but, still do this in case people
    // really want to see the files go away:
    if (lockDir.exists()) {
      if (lockPrefix != null) {
        lockName = lockPrefix + "-n-" + lockName;
      }
      File lockFile = new File(lockDir, lockName);
      if (lockFile.exists() && !lockFile.delete()) {
        throw new IOException("Cannot delete " + lockFile);
      }
    }
  
public synchronized org.apache.lucene.store.LockmakeLock(java.lang.String lockName)

    if (lockPrefix != null)
      lockName = lockPrefix + "-n-" + lockName;
    return new NativeFSLock(lockDir, lockName);