def _set_db_extra(self, key, value, exclusive=False): from aiida.backends.djsite.db.models import DbExtra DbExtra.set_value_for_node(self.dbnode, key, value, stop_if_existing=exclusive) self._increment_version_number_db()
def set_extra_exclusive(self, key, value): from aiida.backends.djsite.db.models import DbExtra DbExtra.validate_key(key) if self._to_be_stored: raise ModificationNotAllowed( "The extras of a node can be set only after " "storing the node") DbExtra.set_value_for_node(self.dbnode, key, value, stop_if_existing=True) self._increment_version_number_db()
def test_replacement_1(self): from aiida.backends.djsite.db.models import DbExtra n1 = Node().store() n2 = Node().store() DbExtra.set_value_for_node(n1._dbnode, "pippo", [1, 2, 'a']) DbExtra.set_value_for_node(n1._dbnode, "pippobis", [5, 6, 'c']) DbExtra.set_value_for_node(n2._dbnode, "pippo2", [3, 4, 'b']) self.assertEquals(n1.get_extras(), {'pippo': [1, 2, 'a'], 'pippobis': [5, 6, 'c'], '_aiida_hash': n1.get_hash() }) self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash() }) new_attrs = {"newval1": "v", "newval2": [1, {"c": "d", "e": 2}]} DbExtra.reset_values_for_node(n1._dbnode, attributes=new_attrs) self.assertEquals(n1.get_extras(), new_attrs) self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash()}) DbExtra.del_value_for_node(n1._dbnode, key='newval2') del new_attrs['newval2'] self.assertEquals(n1.get_extras(), new_attrs) # Also check that other nodes were not damaged self.assertEquals(n2.get_extras(), {'pippo2': [3, 4, 'b'], '_aiida_hash': n2.get_hash()})
def _db_store(self, with_transaction=True): """ Store a new node in the DB, also saving its repository directory and attributes. After being called attributes cannot be changed anymore! Instead, extras can be changed only AFTER calling this store() function. :note: After successful storage, those links that are in the cache, and for which also the parent node is already stored, will be automatically stored. The others will remain unstored. :parameter with_transaction: if False, no transaction is used. This is meant to be used ONLY if the outer calling function has already a transaction open! :param bool use_cache: Whether I attempt to find an equal node in the DB. """ # TODO: This needs to be generalized, allowing for flexible methods # for storing data and its attributes. from django.db import transaction from aiida.common.utils import EmptyContextManager from aiida.common.exceptions import ValidationError from aiida.backends.djsite.db.models import DbAttribute import aiida.orm.autogroup if with_transaction: context_man = transaction.atomic() else: context_man = EmptyContextManager() # I save the corresponding django entry # I set the folder # NOTE: I first store the files, then only if this is successful, # I store the DB entry. In this way, # I assume that if a node exists in the DB, its folder is in place. # On the other hand, periodically the user might need to run some # bookkeeping utility to check for lone folders. self._repository_folder.replace_with_folder( self._get_temp_folder().abspath, move=True, overwrite=True) # I do the transaction only during storage on DB to avoid timeout # problems, especially with SQLite try: with context_man: # Save the row self._dbnode.save() # Save its attributes 'manually' without incrementing # the version for each add. DbAttribute.reset_values_for_node(self._dbnode, attributes=self._attrs_cache, with_transaction=False) # This should not be used anymore: I delete it to # possibly free memory del self._attrs_cache self._temp_folder = None self._to_be_stored = False # Here, I store those links that were in the cache and # that are between stored nodes. self._store_cached_input_links() # This is one of the few cases where it is ok to do a 'global' # except, also because I am re-raising the exception except: # I put back the files in the sandbox folder since the # transaction did not succeed self._get_temp_folder().replace_with_folder( self._repository_folder.abspath, move=True, overwrite=True) raise from aiida.backends.djsite.db.models import DbExtra # I store the hash without cleaning and without incrementing the nodeversion number DbExtra.set_value_for_node(self._dbnode, _HASH_EXTRA_KEY, self.get_hash()) return self