def entity(self): """Get an entity for an entity result. Args: projection (Optional[Sequence[str]]): Sequence of property names to be projected in the query results. Returns: Union[model.Model, key.Key]: The processed result. """ if self.result_type == RESULT_TYPE_FULL: entity = model._entity_from_protobuf(self.result_pb.entity) return entity elif self.result_type == RESULT_TYPE_PROJECTION: entity = model._entity_from_protobuf(self.result_pb.entity) projection = tuple(self.result_pb.entity.properties.keys()) entity._set_projection(projection) return entity elif self.result_type == RESULT_TYPE_KEY_ONLY: key_pb = self.result_pb.entity.key ds_key = helpers.key_from_protobuf(key_pb) key = key_module.Key._from_ds_key(ds_key) return key raise NotImplementedError( "Got unexpected entity result type for query.")
def entity(self): """Get an entity for an entity result. Use or update the cache if available. Args: projection (Optional[Sequence[str]]): Sequence of property names to be projected in the query results. Returns: Union[model.Model, key.Key]: The processed result. """ if self.result_type == RESULT_TYPE_FULL: # First check the cache. context = context_module.get_context() entity = self.check_cache(context) if entity is _KEY_NOT_IN_CACHE: # entity not in cache, create one, and then add it to cache entity = model._entity_from_protobuf(self.result_pb.entity) if context._use_cache(entity.key): context.cache[entity.key] = entity return entity elif self.result_type == RESULT_TYPE_PROJECTION: entity = model._entity_from_protobuf(self.result_pb.entity) projection = tuple(self.result_pb.entity.properties.keys()) entity._set_projection(projection) return entity elif self.result_type == RESULT_TYPE_KEY_ONLY: return self.key() raise NotImplementedError("Got unexpected entity result type for query.")
def entity(self): """Get an entity for an entity result. Use the cache if available. Args: projection (Optional[Sequence[str]]): Sequence of property names to be projected in the query results. Returns: Union[model.Model, key.Key]: The processed result. """ if self.result_type == RESULT_TYPE_FULL: # First check the cache. context = context_module.get_context() key_pb = self.result_pb.entity.key ds_key = helpers.key_from_protobuf(key_pb) key = key_module.Key._from_ds_key(ds_key) entity = _KEY_NOT_IN_CACHE use_cache = context._use_cache(key) if use_cache: try: entity = context.cache.get_and_validate(key) except KeyError: pass if entity is _KEY_NOT_IN_CACHE: # entity not in cache, create one. entity = model._entity_from_protobuf(self.result_pb.entity) return entity elif self.result_type == RESULT_TYPE_PROJECTION: entity = model._entity_from_protobuf(self.result_pb.entity) projection = tuple(self.result_pb.entity.properties.keys()) entity._set_projection(projection) return entity elif self.result_type == RESULT_TYPE_KEY_ONLY: key_pb = self.result_pb.entity.key ds_key = helpers.key_from_protobuf(key_pb) key = key_module.Key._from_ds_key(ds_key) return key raise NotImplementedError( "Got unexpected entity result type for query.")
def test_entity_from_protobuf(): class Animal(polymodel.PolyModel): pass class Cat(Animal): pass key = datastore.Key("Cat", 123, project="testing") datastore_entity = datastore.Entity(key=key) protobuf = helpers.entity_to_protobuf(datastore_entity) entity = model._entity_from_protobuf(protobuf) assert isinstance(entity, Cat)
def get_async(self, **options): """Asynchronously get the entity for this key. The result for the returned future will either be the retrieved :class:`.Model` or :data:`None` if there is no such entity. Args: options (Dict[str, Any]): The options for the request. For example, ``{"read_consistency": EVENTUAL}``. Returns: :class:`~google.cloud.ndb.tasklets.Future` """ from google.cloud.ndb import model # avoid circular import entity_pb = yield _datastore_api.lookup(self._key, **options) if entity_pb is not _datastore_api._NOT_FOUND: return model._entity_from_protobuf(entity_pb)