java.lang.Object
org.apache.tinkerpop.gremlin.tinkergraph.structure.storage.DirectoryLock
All Implemented Interfaces:
AutoCloseable

public final class DirectoryLock extends Object implements AutoCloseable
An exclusive, whole-process lock on a TinkerStorageGraph storage directory. A persistent, transactional TinkerGraph is a single-writer embedded store: two graphs opened on the same directory — whether in one JVM or across processes — would both append to and compact the same files, corrupting them. This holds an OS advisory lock (FileLock) on a LOCK file in the directory for the lifetime of the graph, so a second open fails fast rather than silently corrupting data.

An OS lock (rather than a mere marker file) is used so the kernel releases it automatically if the JVM dies, avoiding a stale lock that would wedge the store after a crash.

Note: FileLock semantics are unreliable on some network filesystems (notably NFS); this guarantee holds on local filesystems.

  • Method Details

    • acquire

      public static DirectoryLock acquire(File directory)
      Acquire an exclusive lock on the LOCK file within directory.
      Parameters:
      directory - the storage directory, which must already exist
      Returns:
      the held lock, released by close()
      Throws:
      IllegalStateException - if another graph (in this or another process) already holds the lock
    • acquireForRecovery

      public static DirectoryLock acquireForRecovery(File directory)
      Lock directory for a recovery open, which only reads it. A writable directory is locked exclusively, as by acquire(java.io.File). A directory the process cannot write, such as a read-only mount or copy of a damaged store, can hold no new lock file: an existing one is locked shared, which still fails while a writer holds it, and with none the directory is read unlocked, since recovery writes nothing to it.
      Parameters:
      directory - the storage directory to lock
      Returns:
      the held lock
      Throws:
      IllegalStateException - if another graph holds the lock
    • closeAndRemoveIfCreated

      public void closeAndRemoveIfCreated()
      Release the lock and, if acquiring it created the LOCK file, delete that file, leaving the directory as it was found. For a graph that must not change the directory: one whose open failed, or one opened for recovery.
    • close

      public void close()
      Release the lock and close the channel. Idempotent-friendly: safe to call once per acquired lock.
      Specified by:
      close in interface AutoCloseable