java.lang.Object
net.i2p.router.networkdb.kademlia.StoreState

class StoreStateextends Object
Manages state for network database store operations.

Tracks peer interaction status, timing information, and operation progress for store operations. Maintains separate sets for pending, attempted, and successful operations to coordinate concurrent store operations and prevent duplicate queries.

Thread-safe implementation using synchronized collections and atomic counters for safe concurrent access from multiple store jobs.

Provides comprehensive state management for reliable data persistence with validation, timeout handling, and operation completion tracking.

  • Field Details

    • _context

      private final RouterContext _context
      The router context.
    • _key

      private final Hash _key
      The key being stored.
    • _data

      private final DatabaseEntry _data
      The entry being stored.
    • _pendingPeers

      private final HashSet<Hash> _pendingPeers
      The peers not yet replied.
    • _pendingPeerTimes

      private final Map<Hash,Long> _pendingPeerTimes
      When each pending peer was contacted.
    • _pendingMessages

      private final Map<Hash, MessageWrapper.WrappedMessage> _pendingMessages
      The sent messages awaiting a reply.
    • _successfulPeers

      private final Set<Hash> _successfulPeers
      The peers that stored the entry.
    • _attemptedPeers

      private final Set<Hash> _attemptedPeers
      The peers contacted so far.
    • _completeCount

      private int _completeCount
      Replies received so far.
    • _attempted

      private int _attempted
      Peers contacted so far.
    • _completed

      private volatile long _completed
      When the store finished, zero while running.
    • _started

      private volatile long _started
      When the store started.
  • Constructor Details

    • StoreState

      public StoreState(RouterContext ctx, Hash key, DatabaseEntry data)
      Starts a store tracking no skipped peers.
      Parameters:
      ctx - the router context supplying the clock used to time the store
      key - the DatabaseEntry hash being stored
      data - the entry that will be sent to each peer that stores it
    • StoreState

      public StoreState(RouterContext ctx, Hash key, DatabaseEntry data, Set<Hash> toSkip)
      New store state tracking pending, attempted, and successful peers.
      Parameters:
      ctx - the router context supplying the clock used to time the store
      key - the DatabaseEntry hash
      data - the entry that will be sent to each peer that stores it
      toSkip - may be null, if non-null, all attempted and skipped targets will be added as of 0.9.53
  • Method Details

    • getTarget

      public Hash getTarget()
      The DatabaseEntry hash target.
      Returns:
      the DatabaseEntry hash being stored
    • getData

      public DatabaseEntry getData()
      The data being stored.
      Returns:
      the DatabaseEntry to be sent to each peer that stores the target
    • getPendingCount

      public int getPendingCount()
      Number of pending peers.
      Returns:
      the pending count
    • getAttempted

      public Set<Hash> getAttempted()
      The peers attempted OR skipped. DOES include skipped peers. Use getAttemptedCount for the number of attempts.
      Returns:
      a copy of the peers attempted so far, including skipped peers
    • getAttemptedCount

      public int getAttemptedCount()
      The number of peers attempted. Does not include skipped peers. Do not use getAttempted().size() as that does include skipped peers.
      Returns:
      the attempted count
    • getSuccessful

      public Hash getSuccessful()
      Return a successful peer (a random one if more than one was successful) or null.
      Returns:
      one of the peers that stored the entry, chosen at random when more than one did, or null if none has yet
    • completed

      public boolean completed()
      Whether the store has completed.
      Returns:
      true once complete(true) has recorded a completion time
    • complete

      public void complete(boolean completed)
      Mark the store as completed at the current time, if not already.
      Parameters:
      completed - whether to mark the store as completed
    • getCompleteCount

      public int getCompleteCount()
      Number of peers that confirmed the store.
      Returns:
      the complete count
    • getWhenStarted

      public long getWhenStarted()
      Time the store started.
      Returns:
      the when started
    • getWhenCompleted

      public long getWhenCompleted()
      Time the store completed, or -1 if not yet completed.
      Returns:
      the when completed
    • addPending

      public void addPending(Hash peer, MessageWrapper.WrappedMessage msg)
      Adds a pending message for tracking.
      Parameters:
      peer - the peer the message is queued to, which is also counted as attempted
      msg - the wrapped message to hand back from getPendingMessage(Hash)
    • getPendingMessage

      public MessageWrapper.WrappedMessage getPendingMessage(Hash peer)
      Returns the pending message for the specified peer.
      Parameters:
      peer - the peer whose pending message should be taken
      Returns:
      the message or null; will only return the message once, so tags are only acked or failed once.
    • addPending

      public void addPending(Hash peer)
      Increments attempted count.
      Parameters:
      peer - the peer now pending a store reply, and counted as attempted
    • addSkipped

      public void addSkipped(Hash peer)
      We aren't even going to try to contact this peer.
      Parameters:
      peer - the peer to count as attempted without being contacted
    • confirmed

      public long confirmed(Hash peer)
      Mark the peer as confirmed and return the response time.
      Parameters:
      peer - the peer that confirmed
      Returns:
      the response time in ms, or -1 if unknown
    • replyTimeout

      public void replyTimeout(Hash peer)
      Remove the peer from pending on timeout.
      Parameters:
      peer - the peer that timed out
    • toString

      public String toString()
      Debug string for this store state.
      Overrides:
      toString in class Object