Class PeerStore
Peer instances.
PeerStore acts as the single source of truth for the peer list maintained
by PeerManager. It provides atomic put/get/remove operations and supports
snapshot queries. In a production deployment this class would back its map with a
persistent store (e.g., a small embedded JSON file or LevelDB entry) so that the
node can reconnect to previously known peers after a restart without relying solely
on seed peers.
Thread safety
All methods are safe for concurrent use. Reads and writes are serialized via
ConcurrentHashMap which provides lock-free reads and striped-lock writes.
Key design decision
Peers are keyed by nodeId (not by host:port) because a peer's
IP address can change on reconnect while its cryptographic identity stays constant.
This ensures that a reconnected peer replaces its stale entry rather than creating
a duplicate.
- Since:
- 1.0.0
- See Also:
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Removes all peers from the store.booleanReturnstrueif the store contains a peer with the given node ID.Returns the peer with the given node ID, orOptional.empty()if not found.getAll()Returns a point-in-time snapshot of all known peers.getAll(Collection<String> nodeIds) Returns a snapshot of all peers whose node ID is in the given collection.booleanisEmpty()Returnstrueif the store contains no peers.voidInserts or replaces a peer entry keyed byPeer.getNodeId().voidRemoves a peer from the store by node ID.intsize()Returns the total number of peers currently stored.toString()Returns a human-readable summary of the peer store (for logging).
-
Constructor Details
-
PeerStore
public PeerStore()
-
-
Method Details
-
put
Inserts or replaces a peer entry keyed byPeer.getNodeId().If a peer with the same node ID already exists (e.g., reconnecting on a new IP address), the old entry is overwritten atomically.
- Parameters:
peer- the peer to store (non-null)- Throws:
NullPointerException- if peer is null
-
remove
Removes a peer from the store by node ID.No-op if the node ID is not present.
- Parameters:
nodeId- the node ID to remove (non-null, non-blank)- Throws:
NullPointerException- if nodeId is nullIllegalArgumentException- if nodeId is blank
-
clear
public void clear()Removes all peers from the store. -
get
Returns the peer with the given node ID, orOptional.empty()if not found.- Parameters:
nodeId- the node ID to look up (non-null, non-blank)- Returns:
- optional peer
- Throws:
NullPointerException- if nodeId is nullIllegalArgumentException- if nodeId is blank
-
contains
Returnstrueif the store contains a peer with the given node ID.- Parameters:
nodeId- the node ID to check (non-null, non-blank)- Returns:
trueif present
-
getAll
Returns a point-in-time snapshot of all known peers.Modifications to the returned list do not affect the store. Peers are returned in an unspecified order; callers that need a deterministic order should sort the list themselves.
- Returns:
- unmodifiable list of all current peers
-
getAll
Returns a snapshot of all peers whose node ID is in the given collection.- Parameters:
nodeIds- the set of node IDs to include (non-null)- Returns:
- unmodifiable list of matched peers
-
size
public int size()Returns the total number of peers currently stored.- Returns:
- peer count (≥ 0)
-
isEmpty
public boolean isEmpty()Returnstrueif the store contains no peers.- Returns:
trueif empty
-
toString
Returns a human-readable summary of the peer store (for logging).
-