Class ModificationTally

java.lang.Object
org.tentackle.dbms.ModificationTally

public class ModificationTally extends Object
Counter to track modifications for a class/table.
The ModificationTracker maintains a list of ModificationTally-objects, one for each tracked name.

ModificationTallies are only used in local sessions, never remote.

Author:
harald
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final Object
    Transaction property to rollback pending counts.
    Set to Boolean.TRUE when the session performed a countPending within a transaction, and that pending count must be adjusted when the transaction is rolled back.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates a modification counter for a given tracked name.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds a deletion.
    A deletion is represented by an IdSerialTuple while the serial is the tableserial indicating the modification count of the whole table.
    If the table provides a tableserial column, deletions are memorized in the TableSerialHistory, since they cannot be derived from the table itself.
    void
    Adds this counter to the modification table.
    long
    Adds a pending modification count.
    Gets the modification PO.
    long
    Gets the ID of the counter.
    long
    Gets the latest serial updated by this counter.
    long
    Gets the current modification count by tablename.
    long
    Gets the pending count.
    Gets the tracked name.
    boolean
    Returns whether this tally is retired and has no pending counts left.
    void
    Performs the physical pending count if pending count > 0.
    This is only done if no transactions related to the data monitored by this ModificationTally are currently running.
    void
    Retires this tally.
    Used by the tracker when replacing its tallies, while other threads may still hold a reference to this one.
    void
    rollbackPending(Session session, long txNumber)
    Rolls back the pending count and deletions for a given session.
    Avoids invalidation of the associated caches when a transaction is rolled back that caused all the modifications.
    void
    setLastSerial(long lastSerial)
    Sets the last serial.
    The serial never decreases: in local client mode, performPendingCount() advances it before the flush is committed, so the committed serial read by the tracker meanwhile may still be lower.

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SESSION_COUNT_PENDING

      public static final Object SESSION_COUNT_PENDING
      Transaction property to rollback pending counts.
      Set to Boolean.TRUE when the session performed a countPending within a transaction, and that pending count must be adjusted when the transaction is rolled back.
  • Constructor Details

    • ModificationTally

      public ModificationTally(DbModificationTracker tracker, String trackedName)
      Creates a modification counter for a given tracked name.
      Parameters:
      tracker - the modification tracker
      trackedName - the tracked name
  • Method Details

    • getTrackedName

      public String getTrackedName()
      Gets the tracked name.
      Returns:
      the symbolic name of the tracked entity
    • getDbModification

      public DbModification getDbModification()
      Gets the modification PO.
      Returns:
      the modification PO
    • getId

      public long getId()
      Gets the ID of the counter.
      Returns:
      the unique id
    • countPending

      public long countPending(Session session)
      Adds a pending modification count.
      Parameters:
      session - the session persisting the modification, null if thread-local session
      Returns:
      the serial assigned to the modification, unique for this counter, 0 if retired and the modification must be counted by its successor
    • retire

      public void retire()
      Retires this tally.
      Used by the tracker when replacing its tallies, while other threads may still hold a reference to this one. From now on, no more counts are accepted, but the pending counts are still performed.
    • isRetiredAndDone

      public boolean isRetiredAndDone()
      Returns whether this tally is retired and has no pending counts left.
      Returns:
      true if the tally can be dropped
    • addDeletion

      public void addDeletion(Session session, IdSerialTuple tuple)
      Adds a deletion.
      A deletion is represented by an IdSerialTuple while the serial is the tableserial indicating the modification count of the whole table.
      If the table provides a tableserial column, deletions are memorized in the TableSerialHistory, since they cannot be derived from the table itself.
      Parameters:
      session - the session associated with the deletion; it determines the transaction context
      tuple - the ID and negative(!) tableserial that represents the deletion to be added
    • rollbackPending

      public void rollbackPending(Session session, long txNumber)
      Rolls back the pending count and deletions for a given session.
      Avoids invalidation of the associated caches when a transaction is rolled back that caused all the modifications.
      Parameters:
      session - the session
      txNumber - the transaction number
    • getPendingCount

      public long getPendingCount()
      Gets the pending count.
      Returns:
      the pending count, 0 for none
    • setLastSerial

      public void setLastSerial(long lastSerial)
      Sets the last serial.
      The serial never decreases: in local client mode, performPendingCount() advances it before the flush is committed, so the committed serial read by the tracker meanwhile may still be lower. Setting it back would hand out the same table serial twice.
      Parameters:
      lastSerial - the serial
    • getLatestSerial

      public long getLatestSerial()
      Gets the latest serial updated by this counter.
      Returns:
      the latest serial from the last update plus pending count
    • performPendingCount

      public void performPendingCount()
      Performs the physical pending count if pending count > 0.
      This is only done if no transactions related to the data monitored by this ModificationTally are currently running. This also implies that all modifications have been committed to the database.

      The database update is performed without holding the monitor of this tally. In local client mode, the update runs within the transaction of the counting session and locks the modification rows until the end of that transaction. A thread blocked by such a lock while holding the monitor would deadlock with the lock owner counting its next modification. The counts taken over are moved to the last serial, so that the serials assigned in the meantime by countPending(Session) continue seamlessly. If the update fails, they are given back.

    • addToModificationTable

      public void addToModificationTable()
      Adds this counter to the modification table.
    • getModificationCount

      public long getModificationCount()
      Gets the current modification count by tablename.
      Returns:
      the modification count