java.lang.Object
org.apache.tinkerpop.gremlin.tinkergraph.structure.storage.AbstractLogStorage
All Implemented Interfaces:
AutoCloseable, TinkerStorage
Direct Known Subclasses:
GraphBinaryStorage

public abstract class AbstractLogStorage extends Object implements TinkerStorage
Log-structured durable storage machinery shared by TinkerStorage engines, independent of how an element is encoded. It persists a TinkerStorageGraph as an append-only commit log (log.gbin) plus an optional folded snapshot.gbin; on open the snapshot is read followed by the log, last-write-wins per element id.

This base owns everything that is not the element codec: the on-disk file layout, the single-source-of-truth VERSION marker, the store id and compaction generation that tie a log to its snapshot, length+CRC frame framing (which tells an interrupted trailing append apart from genuine corruption, with log frames also checksumming their length), the replay fold loop, durability via SyncMode, and crash-safe atomic compaction with a size threshold. Concrete engines supply only the codec through encodeCommit(long, java.util.Collection<org.apache.tinkerpop.gremlin.tinkergraph.structure.storage.TinkerStorageMutation<org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerVertex>>, java.util.Collection<org.apache.tinkerpop.gremlin.tinkergraph.structure.storage.TinkerStorageMutation<org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerEdge>>), decodeFrame(byte[], java.util.Map<java.lang.Object, org.apache.tinkerpop.gremlin.structure.util.detached.DetachedVertex>, java.util.Map<java.lang.Object, org.apache.tinkerpop.gremlin.structure.util.detached.DetachedEdge>), writeSnapshot(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph, java.io.DataOutputStream), and (optionally) beginReplay() for per-replay decode state.

The in-memory graph remains authoritative (write-through); this machinery does not support graphs larger than memory.

  • Constructor Details

    • AbstractLogStorage

      public AbstractLogStorage()
  • Method Details

    • encodeCommit

      protected abstract byte[] encodeCommit(long txVersion, Collection<TinkerStorageMutation<TinkerVertex>> changedVertices, Collection<TinkerStorageMutation<TinkerEdge>> changedEdges) throws IOException
      Encode a committing transaction's changeset into a single record payload (the framing is added by the caller). A failed encode fails only its own transaction and later commits are still accepted, so on failure an implementation must undo any codec state it changed while encoding, such as dictionary entries.
      Throws:
      IOException
    • decodeFrame

      protected abstract void decodeFrame(byte[] record, Map<Object,DetachedVertex> vertices, Map<Object,DetachedEdge> edges) throws IOException
      Decode one record payload, folding its puts and deletes into the supplied maps (last-write-wins per id).
      Throws:
      IOException
    • writeSnapshot

      protected abstract void writeSnapshot(TinkerStorageGraph graph, DataOutputStream out) throws IOException
      Write the entire current committed state of the graph to out as framed records (via writeFrame(java.io.DataOutputStream, byte[])), for compaction. The file header has already been written to out.
      Throws:
      IOException
    • beginReplay

      protected void beginReplay()
      Reset any per-replay decode state (e.g. a dictionary) before a fold begins. Default is a no-op.
    • open

      public void open(TinkerStorageGraph graph, org.apache.commons.configuration2.Configuration config)
      Description copied from interface: TinkerStorage
      Prepare the storage engine for use, resolving its backing location from the supplied configuration. Called once during graph construction before TinkerStorage.replay(TinkerStorageGraph).
      Specified by:
      open in interface TinkerStorage
      Parameters:
      graph - the graph that owns this engine
      config - the graph configuration, including gremlin.tinkergraph.storage.directory
    • configureCodec

      protected void configureCodec(org.apache.commons.configuration2.Configuration config)
    • replay

      public void replay(TinkerStorageGraph graph)
      Description copied from interface: TinkerStorage
      Rebuild the in-memory state of the graph from previously persisted data. Called once during graph construction. Implementations should re-apply persisted elements through the graph's own mutation API; the graph sets its loading guard for the duration so this does not re-persist.
      Specified by:
      replay in interface TinkerStorage
      Parameters:
      graph - the graph to populate
    • persist

      public void persist(long txVersion, Collection<TinkerStorageMutation<TinkerVertex>> changedVertices, Collection<TinkerStorageMutation<TinkerEdge>> changedEdges)
      Description copied from interface: TinkerStorage
      Durably record the changeset of a committing transaction. Called from within the transaction commit, while the changed elements are locked, before the in-memory commit is applied. Each mutation is either a put (added or modified element) or a delete (see TinkerStorageMutation).
      Specified by:
      persist in interface TinkerStorage
      Parameters:
      txVersion - the version number of the committing transaction
      changedVertices - the vertex mutations in this transaction
      changedEdges - the edge mutations in this transaction
    • flush

      public void flush()
      Description copied from interface: TinkerStorage
      Force any buffered writes to durable storage. Called after TinkerStorage.persist(long, Collection, Collection) as the commit's durability point.
      Specified by:
      flush in interface TinkerStorage
    • compact

      public void compact(TinkerStorageGraph graph)
      Description copied from interface: TinkerStorage
      Fold accumulated changes into a compact representation of the current committed state, reclaiming space. Safe to call at any time; typically invoked on TinkerStorage.close() or on a size/commit-count threshold.
      Specified by:
      compact in interface TinkerStorage
      Parameters:
      graph - the graph whose current committed state should be snapshotted
    • compact

      protected void compact(TinkerStorageGraph graph, boolean force)
      Every compaction, explicit, automatic or for clear(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph), runs through here.
      Parameters:
      force - rewrite the store even when nothing was logged since the last compaction, as clear(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph) must, since the graph it writes then no longer matches the snapshot
    • maybeCompact

      public void maybeCompact(TinkerStorageGraph graph)
      Description copied from interface: TinkerStorage
      Compact if the engine's accumulated data has grown past its own threshold, otherwise do nothing. Called on the commit path after TinkerStorage.flush() (while the graph's commit lock is held) so a long-running graph that is never explicitly closed does not grow its backing storage without bound. The default is a no-op, leaving compaction entirely under the control of TinkerStorage.compact(TinkerStorageGraph) and TinkerStorage.close().
      Specified by:
      maybeCompact in interface TinkerStorage
      Parameters:
      graph - the graph whose current committed state would be snapshotted
    • clear

      public void clear(TinkerStorageGraph graph)
      Description copied from interface: TinkerStorage
      Replace everything the engine holds with the graph's current committed state, which the caller has just emptied, so that the store holds nothing and a reopen finds an empty graph. Called by TinkerGraph.clear() under the graph's commit lock, and never on a read-only engine. If the store cannot be emptied, the engine must refuse further commits until the graph is reopened: appending them to the old data would bring the cleared elements back on reopen, mixed with the new ones. The graph fail-stops the engine through TinkerStorage.failStop(Throwable) when this throws. Rewriting the store from the emptied graph, as a compaction does, is one way to empty it, but it must then run even when nothing was written since the last compaction, since the store still holds the data the graph no longer has.
      Specified by:
      clear in interface TinkerStorage
      Parameters:
      graph - the graph, already emptied, whose committed state the store should now hold
    • isReadOnly

      public boolean isReadOnly()
      Description copied from interface: TinkerStorage
      Whether this engine accepts no writes at all, such as when a damaged store was opened for recovery. The graph then also leaves its other files in the storage directory, such as index definitions, unchanged.
      Specified by:
      isReadOnly in interface TinkerStorage
    • isFailed

      public boolean isFailed()
      Description copied from interface: TinkerStorage
      Whether this engine has stopped accepting writes after a storage failure, such as a failed log write or the store being removed while the graph was open. It stays so until the graph is reopened. The graph's committed data remains readable in memory for export, so operations that would discard or rewrite it are refused.
      Specified by:
      isFailed in interface TinkerStorage
    • failStop

      public void failStop(Throwable cause)
      Description copied from interface: TinkerStorage
      Stop accepting writes, as after a storage failure, until the graph is reopened. The graph calls this when its memory no longer matches the store, so that nothing written from that memory, by a commit or a compaction, may reach the store: when a transaction's changes were persisted and flushed but could not then be applied in memory, so the transaction is durable and returns on the next open, and when TinkerStorage.clear(org.apache.tinkerpop.gremlin.tinkergraph.structure.TinkerStorageGraph) fails after the graph was emptied in memory. TinkerStorage.isFailed() reports true afterwards.
      Specified by:
      failStop in interface TinkerStorage
      Parameters:
      cause - why the graph's memory no longer matches the store
    • close

      public void close()
      Description copied from interface: TinkerStorage

      Flush and release resources. Does not delete persisted data.

      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface TinkerStorage
    • openLogForAppend

      protected FileOutputStream openLogForAppend(File file) throws IOException
      Open the log for appending. Exists so tests can substitute a stream that fails on demand.
      Throws:
      IOException
    • openEmptyLog

      protected FileOutputStream openEmptyLog(File tmp) throws IOException
      Open the temporary file an empty log is written to before it replaces the log. A seam for tests that fail installing a log, as for openLogForAppend(java.io.File).
      Throws:
      IOException
    • writeFrame

      protected static void writeFrame(DataOutputStream out, byte[] payload) throws IOException
      Write a snapshot frame: a 4-byte big-endian payload length, a 4-byte CRC32 of the payload, then the payload. The checksum lets a reader tell a bit-flip inside a complete frame from an incomplete one. Available to codec subclasses writing per-element snapshot frames; log frames are written by writeLogFrame(java.io.DataOutputStream, int, byte[]).
      Throws:
      IOException