entity_manager
This module defines the abstract base class EntityManager, which provides a generic framework
for managing database-backed entities in the Merlin system.
EntityManager is designed to be subclassed for specific entity types (e.g., studies, runs,
workers), allowing consistent implementation of core CRUD operations (Create, Read, Update, Delete)
across different entity managers. It provides shared helper methods for common operations such as
creating entities if they do not exist, retrieving entities by ID, and deleting entities.
EntityManager
Bases: Generic[T, M], ABC
Abstract base class for managing database entity lifecycles.
This class defines a common interface and helper methods for managing entities stored in the database, such as studies, runs, and workers. Subclasses must implement methods to create, retrieve, and delete these entities using the provided backend.
Generic Parameters
T (DatabaseEntity): The entity class managed by this manager. M (BaseDataModel): The data model class corresponding to the entity.
Attributes:
| Name | Type | Description |
|---|---|---|
backend |
ResultsBackend
|
The backend interface used to persist and retrieve entity data. |
_filter_accessor_map |
Dict[str, Callable[[T], Any]]
|
A dictionary mapping supported filter keys to accessor functions
for the entity type. Used by filtering logic (e.g., in |
Methods:
| Name | Description |
|---|---|
create |
Abstract method to create a new entity. |
get |
Abstract method to retrieve a single entity by its identifier. |
get_all |
Abstract method to retrieve all entities of this type. |
delete |
Abstract method to delete a specific entity by its identifier. |
delete_all |
Abstract method to delete all entities of this type. |
_create_entity_if_not_exists |
Creates an entity only if it does not already exist in the backend. |
_get_entity |
Retrieves an entity instance using its identifier. |
_get_all_entities |
Retrieves all entities of a specific type from the backend. |
_delete_entity |
Deletes an individual entity, optionally calling a cleanup function before deletion. |
_delete_all_by_type |
Deletes all entities of a certain type using the provided getter and deleter functions. |
Source code in merlin/db_scripts/entity_managers/entity_manager.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | |
__init__(backend)
Initialize the EntityManager with a backend.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
ResultsBackend
|
The backend interface used to persist and retrieve entities. |
required |
Source code in merlin/db_scripts/entity_managers/entity_manager.py
create(*args, **kwargs)
abstractmethod
Create a new entity.
This method must be implemented by subclasses to define how a specific entity is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Optional positional arguments for create context. |
()
|
**kwargs
|
Any
|
Optional keyword arguments for create context. |
{}
|
Returns:
| Type | Description |
|---|---|
T
|
The newly created entity instance. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If a subclass has not implemented this method. |
Source code in merlin/db_scripts/entity_managers/entity_manager.py
delete(identifier, **kwargs)
abstractmethod
Delete a specific entity by its identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
The unique identifier of the entity to delete. |
required |
**kwargs
|
Any
|
Optional keyword arguments for deletion context (e.g., cleanup flags). |
{}
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If a subclass has not implemented this method. |
Source code in merlin/db_scripts/entity_managers/entity_manager.py
delete_all(**kwargs)
abstractmethod
Delete all entities managed by this entity manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Optional keyword arguments for deletion context. |
{}
|
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If a subclass has not implemented this method. |
Source code in merlin/db_scripts/entity_managers/entity_manager.py
get(identifier)
abstractmethod
Retrieve a single entity by its identifier.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
identifier
|
str
|
The unique identifier of the entity. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The entity instance corresponding to the identifier. |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If a subclass has not implemented this method. |
Source code in merlin/db_scripts/entity_managers/entity_manager.py
get_all(filters=None)
Retrieve all entities managed by this entity manager, optionally filtered by attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
Dict
|
A dictionary of filter keys and values used to narrow down the query results. Filter keys must correspond to supported filters defined in the ENTITY_REGISTRY for the given entity type. Values are compared against entity attributes or accessor methods (e.g., {"name": "foo"}, {"queues": ["queue1", "queue2"]}). |
None
|
Returns:
| Type | Description |
|---|---|
List[T]
|
A list of all entities of the specified type matching the filters. |