Question on how to project a collection field

33 views
Skip to first unread message

Jerry lin

unread,
Jul 27, 2021, 12:43:06 AM7/27/21
to objectify-appengine
Hey,

I tried to project on a collection field however, objectify keeps throwing errors. The following is my testing code
\\======================
@Entity
@Cache
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Trivial implements Serializable {
private static final long serialVersionUID = 1L;

@Id
private Long id;

@Index
private String someString;

@Index
private long someNumber;

@Index
private List<String> names;

/** Constructor to use when autogenerating an id */
public Trivial(String someString, long someNumber) {
this(null, someString, someNumber, Arrays.asList("a","b"));
}

public Trivial(Long id, String someString, long someNumber) {
this(id, someString, someNumber, Arrays.asList("a","b"));
}
}
\\======================
void simpleProjectionWorks() throws Exception {
       factory().register(Trivial.class);

        final Trivial triv = new Trivial(123L, "foo", 12, Arrays.asList("Perter", "Smith"));
        ofy().save().entity(triv).now();
         ofy().clear();

         final List<Trivial> projecteds = ofy().load().type(Trivial.class).project("names").list();
          assertThat(projecteds).hasSize(1);
}

I expect projecteds has a size of 1 but it always returns empty list.  I hope it works with Objectify 5.+. The modified test code is pull out from the repos' master (Objectify 6.+ )

Have I missed anything?  Is it possible to do such projection?

thank you

Jerry

Jeff Schnitzer

unread,
Jul 27, 2021, 4:46:44 PM7/27/21
to objectify-appengine
Hmmm. The problem is, projection queries are weird and multivalue properties are weird, and combining two weird things produces really inscrutable behavior in the datastore.

Read this:
https://cloud.google.com/datastore/docs/concepts/queries#projections_and_array-valued_properties

I don't think there's any code in Objectify that would take this behavior and do something sane with it. I'm not even sure what the sane thing would be. What you probably want (fully populate the multivalue field) does not appear to be offered by the underlying Google API.

Taking a step back: Why are you performing a projection query here? Do you know you have a performance bottleneck around this or is this premature optimization? Projections have a lot of unexpected behavior and should only be used as a last resort.

Jeff



--
You received this message because you are subscribed to the Google Groups "objectify-appengine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to objectify-appen...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/objectify-appengine/9231accb-7e2b-482f-9b67-ece4005eb2b7n%40googlegroups.com.

Jerry lin

unread,
Jul 27, 2021, 10:42:58 PM7/27/21
to objectify-appengine
Hi Jeff,

thank you for your reply.  

You are quite right.  I am addressing performance and memory exhausted issues.  The actual problem I am dealing with is to do data aggregation. 
I have a Person entity and Person entity has many fields.  One of the fields is a collection field that lists a person's qualifications. I need to find out
how many people have Higher Degree, Tertiary, High school, Diploma, etc. The memory consumption is very high if I have read entire Person from Datastore
and aggregate on fully populated Person entities.  Any suggestion are welcome.

Thank you

Jerry 

Reply all
Reply to author
Forward
0 new messages