def make_ancestor_query(kind, namespace, ancestor): """Creates a Cloud Datastore ancestor query.""" ancestor_key = entity_pb2.Key() datastore_helper.add_key_path(ancestor_key, kind, ancestor) if namespace is not None: ancestor_key.partition_id.namespace_id = namespace query = query_pb2.Query() query.kind.add().name = kind datastore_helper.set_property_filter( query.filter, '__key__', PropertyFilter.HAS_ANCESTOR, ancestor_key) return query
def make_query(kind): """Creates a Cloud Datastore query to retrieve all entities with a 'created_at' date > N days ago. """ days = 4 now = datetime.datetime.now() earlier = now - datetime.timedelta(days=days) query = query_pb2.Query() query.kind.add().name = kind datastore_helper.set_property_filter(query.filter, 'created_at', PropertyFilter.GREATER_THAN, earlier) return query
def query(self): # Instantiate a filter protobuf # You MUST instantiate the filter before the query, then instantiate # the query with the filter. filter_pb = query_pb2.Filter() # Get all non-deleted model instances helper.set_property_filter(filter_pb, 'deleted', query_pb2.PropertyFilter.EQUAL, False) # Instantiate a query protobuf query_pb = query_pb2.Query(filter=filter_pb) helper.set_kind(query_pb, self.model) return query_pb
def expand(self, pcoll): query = query_pb2.Query() query.kind.add().name = 'Tweet' now = datetime.datetime.now() # The 'earlier' var will be set to a static value on template creation. # That is, because of the way that templates work, the value is defined # at template compile time, not runtime. # But defining a filter based on this value will still serve to make the # query more efficient than if we didn't filter at all. earlier = now - datetime.timedelta(days=self.days) datastore_helper.set_property_filter(query.filter, 'created_at', PropertyFilter.GREATER_THAN, earlier) return (pcoll | 'read from datastore' >> ReadFromDatastore( self.project, query, None))
def make_ancestor_query(kind, namespace, ancestor): """Creates a Cloud Datastore ancestor query. The returned query will fetch all the entities that have the parent key name set to the given `ancestor`. """ ancestor_key = entity_pb2.Key() datastore_helper.add_key_path(ancestor_key, kind, ancestor) if namespace is not None: ancestor_key.partition_id.namespace_id = namespace query = query_pb2.Query() query.kind.add().name = kind datastore_helper.set_property_filter( query.filter, '__key__', PropertyFilter.HAS_ANCESTOR, ancestor_key) return query
def make_kind_stats_query(namespace, kind, latest_timestamp): """Make a Query to fetch the latest kind statistics.""" kind_stat_query = query_pb2.Query() if namespace is None: kind_stat_query.kind.add().name = '__Stat_Kind__' else: kind_stat_query.kind.add().name = '__Stat_Ns_Kind__' kind_filter = datastore_helper.set_property_filter( query_pb2.Filter(), 'kind_name', PropertyFilter.EQUAL, unicode(kind)) timestamp_filter = datastore_helper.set_property_filter( query_pb2.Filter(), 'timestamp', PropertyFilter.EQUAL, latest_timestamp) datastore_helper.set_composite_filter(kind_stat_query.filter, CompositeFilter.AND, kind_filter, timestamp_filter) return kind_stat_query
def testFilter(self): f = datastore.Filter() helper.set_composite_filter( f, datastore.CompositeFilter.AND, helper.set_property_filter(datastore.Filter(), 'foo', datastore.PropertyFilter.EQUAL, u'bar'), helper.set_property_filter(datastore.Filter(), 'hop', datastore.PropertyFilter.GREATER_THAN, 2.0)) cf = f.composite_filter pf = cf.filter[0].property_filter self.assertEquals('foo', pf.property.name) self.assertEquals('bar', pf.value.string_value) self.assertEquals(datastore.PropertyFilter.EQUAL, pf.operator) pf = cf.filter[1].property_filter self.assertEquals('hop', pf.property.name) self.assertEquals(2.0, pf.value.double_value) self.assertEquals(datastore.PropertyFilter.GREATER_THAN, pf.operator) self.assertEquals(datastore.CompositeFilter.AND, cf.operator)