Class LoadController

java.lang.Object
com.dna.jopt.member.bucket.route.controller.loadcontroller.LoadController
All Implemented Interfaces:
ILoadController, Serializable

public class LoadController extends Object implements ILoadController
Manages the dynamic per-node INodeDepot state for one entity.

Design: flat one-level COW with lazy isolation

Every instance has two fields:

Only depots with flexible loads and the depots of nodes that are ignored on zero load are kept here. The assessor evaluates any other node depot directly.

Performance

partialCopy() copies the map entries, not the depots: the child's parentMap is a snapshot of the parent's entries and the child gets a fresh empty ownMap. No depot copying happens at clone time. Copying is deferred to getFlexLoadNodeDepot(String): only the specific depot that an operator actually touches is copied at the moment it is first accessed on the clone.

Correctness: shallow copy in lazy isolation

Lazy isolation calls INodeDepot.partialCopy(): a new depot with its own item list whose ILoad objects are still shared with the parent's depot. A load of an isolated depot is therefore never changed in place: code that changes one first calls INodeDepot.replaceWithCopy(String), which puts a copy of the load into this depot's item list, and changes that copy. FlexLoadOperator does so for every load it changes, so the parent's depot state stays untouched.

Thread safety

Both maps are ConcurrentHashMap instances. get and put are safe under concurrent assessor threads. parentMap is never modified after construction, so reads are always safe. ownMap is iterated with a weakly-consistent iterator in partialCopy(), which never throws a ConcurrentModificationException.

See Also:
  • Constructor Details

    • LoadController

      public LoadController()
      Creates a standalone instance with an empty depot map.
  • Method Details

    • getNodeDepotMap

      public Map<String, INodeDepot> getNodeDepotMap()
      Description copied from interface: ILoadController
      Returns a merged view of this instance's map and all ancestors.

      Not on the hot path — used by result-extraction code that needs a complete snapshot. For hot-path reads, use ILoadController.getFlexLoadNodeDepot(String) directly.

      Specified by:
      getNodeDepotMap in interface ILoadController
    • setNodeDepotMap

      public void setNodeDepotMap(Map<String, INodeDepot> m)
      Specified by:
      setNodeDepotMap in interface ILoadController
    • putFlexLoadNodeDepot

      public boolean putFlexLoadNodeDepot(String nodeId, INodeDepot depot)
      Specified by:
      putFlexLoadNodeDepot in interface ILoadController
    • getFlexLoadNodeDepot

      public Optional<INodeDepot> getFlexLoadNodeDepot(String nodeId)
      Returns the depot for nodeId.

      Checks ownMap first (O(1)). On miss, checks parentMap and lazily isolates the depot into ownMap via a shallow copy (INodeDepot.partialCopy()): the copy has its own item list, but its ILoad objects stay shared with the parent's depot until INodeDepot.replaceWithCopy(String) replaces one (see the class comment). Subsequent accesses for the same node hit ownMap directly (O(1)).

      Specified by:
      getFlexLoadNodeDepot in interface ILoadController
    • partialCopy

      public ILoadController partialCopy()
      Creates a COW child.

      The child's parentMap is a snapshot of the entries this instance can see at this moment. The child gets a fresh empty ownMap. No depots are copied at this point: copying is deferred to the first getFlexLoadNodeDepot(String) call that misses ownMap.

      A standalone instance keeps writing to its ownMap when it is assessed again, so its map is copied as well instead of being shared with the child. If this instance is itself a COW copy (parentMap != null), the complete visible state (parent + own entries, own wins) is merged into the snapshot so the child always sees the full picture.

      Specified by:
      partialCopy in interface ILoadController
    • copy

      public ILoadController copy()
      Specified by:
      copy in interface ILoadController
    • flatten

      public void flatten()
      Promotes this COW copy to a standalone instance by merging parentMap into ownMap and clearing the parent reference.

      Optional but beneficial: makes the next partialCopy() O(1) (Case 1) instead of requiring a merge step (Case 2).