aiidateam / aiida-core

The official repository for the AiiDA code
https://aiida-core.readthedocs.io
Other
437 stars 192 forks source link

Bulk update extras of Nodes #6587

Open GeigerJ2 opened 1 month ago

GeigerJ2 commented 1 month ago

As mentioned by @giovannipizzi, it would be nice if bulk-updating Node extras could be achieved using, e.g., a dictionary of the form:

{
    <UUID>: {
        'extra_key1': <VALUE>,
        'extra_key2': <VALUE>,
    },
    <UUID>: ...
}

Currently, the various storage backends already implement bulk_insert and bulk_update methods, which are being used when importing an archive. Using the existing bulk_update method, the following works to ~update~ replace the extras:

storage_backend.bulk_update(
    EntityTypes.NODE,
    [
        {
            # "uuid": "da44c605-70ee-4a24-9844-045c47fbebd9",
            'id': 1,
            "extras": {"a": 1, "b": 2, "c": 3},
        },
        {
            # "uuid": "56e619b0-f894-4417-bb85-aa430f4bcacb",
            'id': 2,
            "extras": {"a": 4, "b": 5, "c": 6},
        }
    ],
)

However, in the current implementation, node selection only works using the id (probably for efficiency reasons), and previous extras, e.g., _aiida_hash would be overwritten, so it doesn't function in the way of an extend feature, which is usually what one wants. One could add an additional, specific method for updating the node extras, or extend the current one. Though, care has to be taken to keep it efficient, and not iterate again over individual nodes in the implementation, possibly slowing down things.

As all other Node properties should be immutable once stored, I currently cannot think of other modifications than changing the extras which would be interesting in bulk for nodes.