Class SharedMemoryRegistry
Manages shared memory namespaces and provides lookup for agents
that need to share memory. Used by AgentMemoryResolver when
processing @Memory(shared = true, shareWith = {...}).
This is a singleton registry. Each namespace maps to a single
SharedMemoryStore instance that multiple agents can access.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all shared memory namespaces and recorded access grants.Looks up an existing shared memory store by namespace.static SharedMemoryRegistryReturns the singleton instance.getOrCreate(String namespace) Gets or creates a shared memory store for the given namespace.getOrCreate(String namespace, Supplier<MemoryStore> baseStoreSupplier) Gets or creates a shared memory store wrapping a base store from the given supplier.voidgrantAccess(String namespace, String agentId) Grants an agent access to a namespace and records the grant in the agent index.namespacesGrantedTo(String agentId) Returns the namespaces the given agent was granted access to viagrantAccess(String, String).booleanRemoves a shared memory namespace.intsize()Returns the number of active namespaces.
-
Method Details
-
getInstance
Returns the singleton instance.- Returns:
- the shared memory registry
-
getOrCreate
Gets or creates a shared memory store for the given namespace.If the namespace already exists, the existing store is returned. Otherwise, a new
SharedMemoryStorewrapping anInMemoryStoreis created and registered.- Parameters:
namespace- the namespace identifier- Returns:
- the shared memory store for the namespace
-
getOrCreate
Gets or creates a shared memory store wrapping a base store from the given supplier.The supplier is only invoked when the namespace does not exist yet; for an existing namespace the registered store is returned and the supplier is never called. Callers can therefore pass a supplier that builds an expensive store (e.g. a
FileMemoryStore) without wasting instances.- Parameters:
namespace- the namespace identifierbaseStoreSupplier- supplies the base memory store to wrap (used only if namespace is new)- Returns:
- the shared memory store for the namespace
-
grantAccess
Grants an agent access to a namespace and records the grant in the agent index.The recorded index lets
AgentMemoryResolverroute an agent to the namespaces it was granted via other agents'shareWithlists. The namespace is created with a defaultInMemoryStoreif it does not exist yet; callers that need a specific base store should callgetOrCreate(String, Supplier)first.- Parameters:
namespace- the namespace to grant access toagentId- the agent ID to authorize
-
namespacesGrantedTo
Returns the namespaces the given agent was granted access to viagrantAccess(String, String).- Parameters:
agentId- the agent ID to look up- Returns:
- unmodifiable set of granted namespaces, empty if none
-
find
Looks up an existing shared memory store by namespace.- Parameters:
namespace- the namespace to look up- Returns:
- the store if it exists
-
remove
Removes a shared memory namespace.- Parameters:
namespace- the namespace to remove- Returns:
- true if the namespace existed and was removed
-
clear
public void clear()Clears all shared memory namespaces and recorded access grants. Primarily useful for testing. -
size
public int size()Returns the number of active namespaces.- Returns:
- namespace count
-