def commit(self): """Commits the transaction. This is called automatically upon exiting a with statement, however it can be called explicitly if you don't want to use a context manager. This method has necessary side-effects: - Sets the current connection's transaction reference to None. - Sets the current transaction's ID to None. - Updates paths for any keys that needed an automatically generated ID. """ # It's possible that they called commit() already, in which case # we shouldn't do any committing of our own. if self.connection().transaction(): result = self.connection().commit(self.dataset().id(), self.mutation()) # For any of the auto-id entities, make sure we update their keys. for i, entity in enumerate(self._auto_id_entities): key_pb = result.insert_auto_id_key[i] key = helpers.key_from_protobuf(key_pb) entity.key(entity.key().path(key.path())) # Tell the connection that the transaction is over. self.connection().transaction(None) # Clear our own ID in case this gets accidentally reused. self._id = None
def get_multi(self, keys, missing=None, deferred=None, transaction=None): """Retrieve entities, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: The keys to be retrieved from the datastore. :type missing: list :param missing: (Optional) If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. If the list is not empty, an error will occur. :type deferred: list :param deferred: (Optional) If a list is passed, the keys returned by the backend as "deferred" will be copied into it. If the list is not empty, an error will occur. :type transaction: :class:`gcloud.datastore.transaction.Transaction` :param transaction: (Optional) Transaction to use for read consistency. If not passed, uses current transaction, if set. :rtype: list of :class:`gcloud.datastore.entity.Entity` :returns: The requested entities. :raises: :class:`ValueError` if one or more of ``keys`` has a project which does not match our project. """ if not keys: return [] ids = set(key.project for key in keys) for current_id in ids: if current_id != self.project: raise ValueError('Keys do not match project') if transaction is None: transaction = self.current_transaction entity_pbs = _extended_lookup( connection=self.connection, project=self.project, key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, transaction_id=transaction and transaction.id, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb) for missed_pb in missing ] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred ] return [ helpers.entity_from_protobuf(entity_pb) for entity_pb in entity_pbs ]
def get_multi(self, keys, missing=None, deferred=None, transaction=None): """Retrieve entities, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: The keys to be retrieved from the datastore. :type missing: list :param missing: (Optional) If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. If the list is not empty, an error will occur. :type deferred: list :param deferred: (Optional) If a list is passed, the keys returned by the backend as "deferred" will be copied into it. If the list is not empty, an error will occur. :type transaction: :class:`gcloud.datastore.transaction.Transaction` :param transaction: (Optional) Transaction to use for read consistency. If not passed, uses current transaction, if set. :rtype: list of :class:`gcloud.datastore.entity.Entity` :returns: The requested entities. :raises: :class:`ValueError` if one or more of ``keys`` has a project which does not match our project. """ if not keys: return [] ids = set(key.project for key in keys) for current_id in ids: if current_id != self.project: raise ValueError('Keys do not match project') if transaction is None: transaction = self.current_transaction entity_pbs = _extended_lookup( connection=self.connection, project=self.project, key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, transaction_id=transaction and transaction.id, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb) for missed_pb in missing] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred] return [helpers.entity_from_protobuf(entity_pb) for entity_pb in entity_pbs]
def get_multi(self, keys, missing=None, deferred=None): """Retrieve entities, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: The keys to be retrieved from the datastore. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. Use only as a keyword param. :type deferred: an empty list or None. :param deferred: If a list is passed, the keys returned by the backend as "deferred" will be copied into it. Use only as a keyword param. :rtype: list of :class:`gcloud.datastore.entity.Entity` :returns: The requested entities. :raises: ValueError if one or more of ``keys`` has a dataset ID which does not match our dataset ID. """ if not keys: return [] ids = list(set([key.dataset_id for key in keys])) if ids != [self.dataset_id]: raise ValueError('Keys do not match dataset ID') transaction = self.current_transaction entity_pbs = _extended_lookup( connection=self.connection, dataset_id=self.dataset_id, key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, transaction_id=transaction and transaction.id, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb) for missed_pb in missing ] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred ] return [ helpers.entity_from_protobuf(entity_pb) for entity_pb in entity_pbs ]
def get(keys, missing=None, deferred=None, connection=None): """Retrieves entities, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: The keys to be retrieved from the datastore. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. Use only as a keyword param. :type deferred: an empty list or None. :param deferred: If a list is passed, the keys returned by the backend as "deferred" will be copied into it. Use only as a keyword param. :type connection: :class:`gcloud.datastore.connection.Connection` :param connection: Optional. The connection used to connect to datastore. :rtype: list of :class:`gcloud.datastore.entity.Entity` :returns: The requested entities. """ if not keys: return [] connection = _require_connection(connection) dataset_id = _get_dataset_id_from_keys(keys) transaction = Transaction.current() entity_pbs = connection.lookup( dataset_id=dataset_id, key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, transaction_id=transaction and transaction.id, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb) for missed_pb in missing] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred] entities = [] for entity_pb in entity_pbs: entities.append(helpers.entity_from_protobuf(entity_pb)) return entities
def get_multi(self, keys, missing=None, deferred=None): """Retrieve entities, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: The keys to be retrieved from the datastore. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. Use only as a keyword param. :type deferred: an empty list or None. :param deferred: If a list is passed, the keys returned by the backend as "deferred" will be copied into it. Use only as a keyword param. :rtype: list of :class:`gcloud.datastore.entity.Entity` :returns: The requested entities. :raises: ValueError if one or more of ``keys`` has a dataset ID which does not match our dataset ID. """ if not keys: return [] ids = set(key.dataset_id for key in keys) for current_id in ids: if not _dataset_ids_equal(current_id, self.dataset_id): raise ValueError('Keys do not match dataset ID') transaction = self.current_transaction entity_pbs = _extended_lookup( connection=self.connection, dataset_id=self.dataset_id, key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, transaction_id=transaction and transaction.id, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb) for missed_pb in missing] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred] return [helpers.entity_from_protobuf(entity_pb) for entity_pb in entity_pbs]
def get_entities(self, keys, missing=None, deferred=None): """Retrieves entities from the dataset, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: List of keys to be retrieved. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. Use only as a keyword param. :type deferred: an empty list or None. :param deferred: If a list is passed, the keys returned by the backend as "deferred" will be copied into it. Use only as a keyword param. :rtype: list of :class:`gcloud.datastore.entity.Entity` :return: The requested entities. """ entity_pbs = self.connection().lookup( dataset_id=self.id(), key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb, dataset=self) for missed_pb in missing ] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred ] entities = [] for entity_pb in entity_pbs: entities.append( helpers.entity_from_protobuf(entity_pb, dataset=self)) return entities
def get_entities(self, keys, missing=None, deferred=None): """Retrieves entities from the dataset, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: List of keys to be retrieved. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. Use only as a keyword param. :type deferred: an empty list or None. :param deferred: If a list is passed, the keys returned by the backend as "deferred" will be copied into it. Use only as a keyword param. :rtype: list of :class:`gcloud.datastore.entity.Entity` :return: The requested entities. """ entity_pbs = self.connection().lookup( dataset_id=self.id(), key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb, dataset=self) for missed_pb in missing] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred] entities = [] for entity_pb in entity_pbs: entities.append(helpers.entity_from_protobuf( entity_pb, dataset=self)) return entities
def _callFUT(self, val): from gcloud.datastore.helpers import key_from_protobuf return key_from_protobuf(val)
def get_multi(keys, missing=None, deferred=None, connection=None, dataset_id=None): """Retrieves entities, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: The keys to be retrieved from the datastore. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. Use only as a keyword param. :type deferred: an empty list or None. :param deferred: If a list is passed, the keys returned by the backend as "deferred" will be copied into it. Use only as a keyword param. :type connection: :class:`gcloud.datastore.connection.Connection` :param connection: Optional. The connection used to connect to datastore. If not passed, inferred from the environment. :type dataset_id: :class:`gcloud.datastore.connection.Connection` :param dataset_id: Optional. The dataset ID used to connect to datastore. If not passed, inferred from the environment. :rtype: list of :class:`gcloud.datastore.entity.Entity` :returns: The requested entities. :raises: EnvironmentError if ``connection`` or ``dataset_id`` not passed, and cannot be inferred from the environment. ValueError if one or more of ``keys`` has a dataset ID which does not match the passed / inferred dataset ID. """ if not keys: return [] connection = _require_connection(connection) dataset_id = _require_dataset_id(dataset_id, keys[0]) if list(set([key.dataset_id for key in keys])) != [dataset_id]: raise ValueError('Keys do not match dataset ID') transaction = Transaction.current() entity_pbs = _extended_lookup( connection, dataset_id=dataset_id, key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, transaction_id=transaction and transaction.id, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb) for missed_pb in missing] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred] entities = [] for entity_pb in entity_pbs: entities.append(helpers.entity_from_protobuf(entity_pb)) return entities
def get(keys, missing=None, deferred=None, connection=None, dataset_id=None): """Retrieves entities, along with their attributes. :type keys: list of :class:`gcloud.datastore.key.Key` :param keys: The keys to be retrieved from the datastore. :type missing: an empty list or None. :param missing: If a list is passed, the key-only entities returned by the backend as "missing" will be copied into it. Use only as a keyword param. :type deferred: an empty list or None. :param deferred: If a list is passed, the keys returned by the backend as "deferred" will be copied into it. Use only as a keyword param. :type connection: :class:`gcloud.datastore.connection.Connection` :param connection: Optional. The connection used to connect to datastore. If not passed, inferred from the environment. :type dataset_id: :class:`gcloud.datastore.connection.Connection` :param dataset_id: Optional. The dataset ID used to connect to datastore. If not passed, inferred from the environment. :rtype: list of :class:`gcloud.datastore.entity.Entity` :returns: The requested entities. :raises: EnvironmentError if ``connection`` or ``dataset_id`` not passed, and cannot be inferred from the environment. ValueError if one or more of ``keys`` has a dataset ID which does not match the passed / inferred dataset ID. """ if not keys: return [] connection = _require_connection(connection) dataset_id = _require_dataset_id(dataset_id, keys[0]) if list(set([key.dataset_id for key in keys])) != [dataset_id]: raise ValueError('Keys do not match dataset ID') transaction = Transaction.current() entity_pbs = _extended_lookup( connection, dataset_id=dataset_id, key_pbs=[k.to_protobuf() for k in keys], missing=missing, deferred=deferred, transaction_id=transaction and transaction.id, ) if missing is not None: missing[:] = [ helpers.entity_from_protobuf(missed_pb) for missed_pb in missing] if deferred is not None: deferred[:] = [ helpers.key_from_protobuf(deferred_pb) for deferred_pb in deferred] entities = [] for entity_pb in entity_pbs: entities.append(helpers.entity_from_protobuf(entity_pb)) return entities