State management for the LocationManager.
Uses document database (FerretDB) for persistent location CRUD, and cache (Valkey) for transient state (locks, change counters).
Classes¶
LocationStateHandler(settings: madsci.common.types.location_types.LocationManagerSettings, manager_id: str, redis_connection: Any | None = None, cache_handler: madsci.common.db_handlers.cache_handler.CacheHandler | None = None, document_handler: madsci.common.db_handlers.document_storage_handler.DocumentStorageHandler | None = None)Manages state for a MADSci Location Manager.
Document storage handler: persistent location CRUD
Cache handler: transient state (locks, change counters)
Initialize a LocationStateHandler.
Parameters¶
settings: Location manager settings. manager_id: Unique identifier for this manager instance. redis_connection: Deprecated. Use cache_handler instead. cache_handler: Cache handler for transient state (locks, change counters). document_handler: Document storage handler for persistent location storage.
Instance variables¶
document_handler: madsci.common.db_handlers.document_storage_handler.DocumentStorageHandler- Public accessor for the document storage handler.
Methods¶
add_location(self, location: madsci.common.types.location_types.Location | dict[str, typing.Any]) ‑> madsci.common.types.location_types.Location | NoneAdds a location by name and returns the stored location. Returns None if the name already exists.
add_location_template(self, template: madsci.common.types.location_types.LocationTemplate | dict[str, typing.Any]) ‑> madsci.common.types.location_types.LocationTemplate | NoneAdds a location template. Returns None if the name already exists.
add_representation_template(self, template: madsci.common.types.location_types.LocationRepresentationTemplate | dict[str, typing.Any]) ‑> madsci.common.types.location_types.LocationRepresentationTemplate | NoneAdds a representation template. Returns None if the name already exists.
close(self) ‑> NoneRelease both cache and document storage connections and resources.
count_location_templates(self) ‑> intReturns the total number of location templates.
count_locations(self) ‑> intReturns the total number of locations.
count_locations_by_managed_by(self, managed_by: str) ‑> intReturns the number of locations with the given managed_by value.
count_representation_templates(self) ‑> intReturns the total number of representation templates.
count_unresolved_locations(self) ‑> intReturns the number of locations with unresolved resource template references.
delete_location(self, location_name: str) ‑> boolDeletes a location by name. Returns True if the location was deleted, False if it didn’t exist.
delete_location_template(self, template_name: str) ‑> boolDeletes a location template by name. Returns True if deleted, False if not found.
delete_representation_template(self, template_name: str) ‑> boolDeletes a representation template by name. Returns True if deleted, False if not found.
get_location(self, location_name: str) ‑> madsci.common.types.location_types.Location | NoneReturns a location by name.
get_location_by_id(self, location_id: str) ‑> madsci.common.types.location_types.Location | NoneReturns a location by ID (O(1) with MongoDB index).
get_location_template(self, template_name: str) ‑> madsci.common.types.location_types.LocationTemplate | NoneReturns a location template by name.
get_location_templates(self) ‑> list[madsci.common.types.location_types.LocationTemplate]Returns all location templates.
get_locations(self) ‑> list[madsci.common.types.location_types.Location]Returns all locations as a list.
get_representation_template(self, template_name: str) ‑> madsci.common.types.location_types.LocationRepresentationTemplate | NoneReturns a representation template by name.
get_representation_templates(self) ‑> list[madsci.common.types.location_types.LocationRepresentationTemplate]Returns all representation templates.
get_unresolved_locations(self) ‑> list[madsci.common.types.location_types.Location]Returns locations that may need reconciliation.
Matches locations with either:
An unresolved resource template (resource_template_name set, resource_id null)
A location template with node bindings (may need representation defaults)
has_state_changed(self) ‑> boolReturns True if the state has changed since the last time this method was called.
location_state_lock(self) ‑> AnyGets a lock on the location state. This should be called before any state updates are made, or where we don’t want the state to be changing underneath us.
mark_state_changed(self) ‑> intMarks the state as changed and returns the current state change counter.
update_location(self, location: madsci.common.types.location_types.Location | dict) ‑> madsci.common.types.location_types.LocationUpdates a location and returns the updated location. Raises KeyError if the location doesn’t exist, ValueError if ID mismatches.
update_location_template(self, template: madsci.common.types.location_types.LocationTemplate | dict[str, typing.Any]) ‑> madsci.common.types.location_types.LocationTemplateUpdates a location template. Raises KeyError if not found, ValueError if ID mismatch.
update_representation_template(self, template: madsci.common.types.location_types.LocationRepresentationTemplate | dict[str, typing.Any]) ‑> madsci.common.types.location_types.LocationRepresentationTemplateUpdates a representation template. Raises KeyError if not found, ValueError if ID mismatch.