コード例 #1
0
ファイル: batch.py プロジェクト: coolsvap/gcloud-python
    def put(self, entity):
        """Remember an entity's state to be saved during ``commit``.

        .. note::
           Any existing properties for the entity will be replaced by those
           currently set on this instance.  Already-stored properties which do
           not correspond to keys set on this instance will be removed from
           the datastore.

        .. note::
           Property values which are "text" ('unicode' in Python2, 'str' in
           Python3) map to 'string_value' in the datastore;  values which are
           "bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.

        :type entity: :class:`gcloud.datastore.entity.Entity`
        :param entity: the entity to be saved.

        :raises: ValueError if entity has no key assigned, or if the key's
                 ``dataset_id`` does not match ours.
        """
        if entity.key is None:
            raise ValueError("Entity must have a key")

        if not _dataset_ids_equal(self.dataset_id, entity.key.dataset_id):
            raise ValueError("Key must be from same dataset as batch")

        _assign_entity_to_mutation(
            self.mutation, entity, self._auto_id_entities)
コード例 #2
0
    def put(self, entity):
        """Remember an entity's state to be saved during ``commit``.

        .. note::
           Any existing properties for the entity will be replaced by those
           currently set on this instance.  Already-stored properties which do
           not correspond to keys set on this instance will be removed from
           the datastore.

        .. note::
           Property values which are "text" ('unicode' in Python2, 'str' in
           Python3) map to 'string_value' in the datastore;  values which are
           "bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.

        :type entity: :class:`gcloud.datastore.entity.Entity`
        :param entity: the entity to be saved.

        :raises: ValueError if entity has no key assigned, or if the key's
                 ``dataset_id`` does not match ours.
        """
        if entity.key is None:
            raise ValueError("Entity must have a key")

        if not _dataset_ids_equal(self._dataset_id, entity.key.dataset_id):
            raise ValueError("Key must be from same dataset as batch")

        _assign_entity_to_mutation(self.mutation, entity,
                                   self._auto_id_entities)
コード例 #3
0
    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: 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.

        :rtype: list of :class:`gcloud.datastore.entity.Entity`
        :returns: The requested entities.
        :raises: :class:`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
        ]
コード例 #4
0
ファイル: client.py プロジェクト: scrapinghub/gcloud-python
    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: 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.

        :rtype: list of :class:`gcloud.datastore.entity.Entity`
        :returns: The requested entities.
        :raises: :class:`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]
コード例 #5
0
    def delete(self, key):
        """Remember a key to be deleted durring ``commit``.

        :type key: :class:`gcloud.datastore.key.Key`
        :param key: the key to be deleted.

        :raises: ValueError if key is not complete, or if the key's
                 ``dataset_id`` does not match ours.
        """
        if key.is_partial:
            raise ValueError("Key must be complete")

        if not _dataset_ids_equal(self._dataset_id, key.dataset_id):
            raise ValueError("Key must be from same dataset as batch")

        key_pb = key.to_protobuf()
        helpers._add_keys_to_request(self.mutation.delete, [key_pb])
コード例 #6
0
ファイル: batch.py プロジェクト: coolsvap/gcloud-python
    def delete(self, key):
        """Remember a key to be deleted durring ``commit``.

        :type key: :class:`gcloud.datastore.key.Key`
        :param key: the key to be deleted.

        :raises: ValueError if key is not complete, or if the key's
                 ``dataset_id`` does not match ours.
        """
        if key.is_partial:
            raise ValueError("Key must be complete")

        if not _dataset_ids_equal(self.dataset_id, key.dataset_id):
            raise ValueError("Key must be from same dataset as batch")

        key_pb = helpers._prepare_key_for_request(key.to_protobuf())
        self.mutation.delete.add().CopyFrom(key_pb)
コード例 #7
0
    def delete(self, key):
        """Remember a key to be deleted during :meth:`commit`.

        :type key: :class:`gcloud.datastore.key.Key`
        :param key: the key to be deleted.

        :raises: ValueError if key is not complete, or if the key's
                 ``dataset_id`` does not match ours.
        """
        if key.is_partial:
            raise ValueError("Key must be complete")

        if not _dataset_ids_equal(self.dataset_id, key.dataset_id):
            raise ValueError("Key must be from same dataset as batch")

        key_pb = helpers._prepare_key_for_request(key.to_protobuf())
        self._add_delete_key_pb().CopyFrom(key_pb)
コード例 #8
0
ファイル: batch.py プロジェクト: scrapinghub/gcloud-python
    def put(self, entity):
        """Remember an entity's state to be saved during :meth:`commit`.

        .. note::
           Any existing properties for the entity will be replaced by those
           currently set on this instance.  Already-stored properties which do
           not correspond to keys set on this instance will be removed from
           the datastore.

        .. note::
           Property values which are "text" ('unicode' in Python2, 'str' in
           Python3) map to 'string_value' in the datastore;  values which are
           "bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.

        When an entity has a partial key, calling :meth:`commit` sends it as
        an ``insert_auto_id`` mutation and the key is completed. On return,
        the key for the ``entity`` passed in is updated to match the key ID
        assigned by the server.

        :type entity: :class:`gcloud.datastore.entity.Entity`
        :param entity: the entity to be saved.

        :raises: ValueError if entity has no key assigned, or if the key's
                 ``dataset_id`` does not match ours.
        """
        if entity.key is None:
            raise ValueError("Entity must have a key")

        if not _dataset_ids_equal(self.dataset_id, entity.key.dataset_id):
            raise ValueError("Key must be from same dataset as batch")

        if entity.key.is_partial:
            entity_pb = self._add_partial_key_entity_pb()
            self._partial_key_entities.append(entity)
        else:
            entity_pb = self._add_complete_key_entity_pb()

        _assign_entity_to_pb(entity_pb, entity)
コード例 #9
0
    def put(self, entity):
        """Remember an entity's state to be saved during :meth:`commit`.

        .. note::
           Any existing properties for the entity will be replaced by those
           currently set on this instance.  Already-stored properties which do
           not correspond to keys set on this instance will be removed from
           the datastore.

        .. note::
           Property values which are "text" ('unicode' in Python2, 'str' in
           Python3) map to 'string_value' in the datastore;  values which are
           "bytes" ('str' in Python2, 'bytes' in Python3) map to 'blob_value'.

        When an entity has a partial key, calling :meth:`commit` sends it as
        an ``insert_auto_id`` mutation and the key is completed. On return,
        the key for the ``entity`` passed in is updated to match the key ID
        assigned by the server.

        :type entity: :class:`gcloud.datastore.entity.Entity`
        :param entity: the entity to be saved.

        :raises: ValueError if entity has no key assigned, or if the key's
                 ``dataset_id`` does not match ours.
        """
        if entity.key is None:
            raise ValueError("Entity must have a key")

        if not _dataset_ids_equal(self.dataset_id, entity.key.dataset_id):
            raise ValueError("Key must be from same dataset as batch")

        if entity.key.is_partial:
            entity_pb = self._add_partial_key_entity_pb()
            self._partial_key_entities.append(entity)
        else:
            entity_pb = self._add_complete_key_entity_pb()

        _assign_entity_to_pb(entity_pb, entity)
コード例 #10
0
ファイル: test_key.py プロジェクト: Parthi10/gcloud-python-1
 def _callFUT(self, dataset_id1, dataset_id2):
     from gcloud.datastore.key import _dataset_ids_equal
     return _dataset_ids_equal(dataset_id1, dataset_id2)
コード例 #11
0
ファイル: test_key.py プロジェクト: scrapinghub/gcloud-python
 def _callFUT(self, dataset_id1, dataset_id2):
     from gcloud.datastore.key import _dataset_ids_equal
     return _dataset_ids_equal(dataset_id1, dataset_id2)