Google App Engine Objectify - load individual objects or list of keys?

I'm trying to understand the Google App Engine program and wondering what is the difference between the two methods - if there is even a practical difference.

Method A)

public Collection<Conference> getConferencesToAttend(Profile profile)
{
    List<String> keyStringsToAttend = profile.getConferenceKeysToAttend();
    List<Conference> conferences = new ArrayList<Conference>();
    for(String conferenceString : keyStringsToAttend)
    {
        conferences.add(ofy().load().key(Key.create(Conference.class,conferenceString)).now());
    }
    return conferences; 
}

      

Method B)

public Collection<Conference> getConferencesToAttend(Profile profile)
    List<String> keyStringsToAttend = profile.getConferenceKeysToAttend();
    List<Key<Conference>> keysToAttend = new ArrayList<>();
    for (String keyString : keyStringsToAttend) {
        keysToAttend.add(Key.<Conference>create(keyString));
    }
    return ofy().load().keys(keysToAttend).values();
}

      

only unique conferences are guaranteed in the conferenceKeysToAttend list - does it matter which of the two alternatives I choose? And if so, why?

+3


source to share


2 answers


Method A loads the objects one by one, while Method B does bulk loading, which is cheaper because you only take one network route to the Google datacenter. You can observe this by measuring the time taken by both methods while loading multiple keys at the same time.



When doing bulk load, you need to be careful about loaded objects if the data store operation throws an exception. The operation may complete successfully even if some of the objects are not loaded.

+5


source


The answer depends on the size of the list. If we're talking hundreds or more, you shouldn't do a single batch. I couldn't find any documentation as to what the limit is, but there is a limit. If that's not a lot, be sure to go with the download one at a time. But you have to make asynchronous calls without using the function now:

List<<Key<Conference>> conferences = new ArrayList<Key<Conference>>();    
conferences.add(ofy().load().key(Key.create(Conference.class,conferenceString));

      



And when you want the actual data:

for (Key<Conference> keyConference : conferences ) {
    Conference c = keyConference.get();
......
    }

      

0


source







All Articles