Skip to content

Instantly share code, notes, and snippets.

@shin1ogawa
Created January 21, 2010 17:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shin1ogawa/282950 to your computer and use it in GitHub Desktop.
Save shin1ogawa/282950 to your computer and use it in GitHub Desktop.
public static List<Entity> asyncQuery(List<Query> queries, FetchOptions fetchOptions)
throws InterruptedException, ExecutionException {
@SuppressWarnings("unchecked")
Delegate<Environment> delegate = ApiProxy.getDelegate();
Environment env = ApiProxy.getCurrentEnvironment();
ApiConfig config = new ApiProxy.ApiConfig();
config.setDeadlineInSeconds(5.0);
List<Future<byte[]>> futures = new ArrayList<Future<byte[]>>(queries.size());
for (Query query : queries) {
futures.add(delegate.makeAsyncCall(env, "datastore_v3", "RunQuery", PbUtil
.toQueryRequestPb(query, fetchOptions).toByteArray(), config));
}
List<List<Entity>> lists = new ArrayList<List<Entity>>();
for (Future<byte[]> future : futures) {
DatastorePb.QueryResult rPb = new DatastorePb.QueryResult();
rPb.mergeFrom(future.get());
Iterator<EntityProto> it = rPb.resultIterator();
List<Entity> entities = new ArrayList<Entity>();
while (it.hasNext()) {
entities.add(EntityTranslator.createFromPb(it.next()));
}
lists.add(entities);
}
Map<Key, Entity> map = new HashMap<Key, Entity>();
for (List<Entity> list : lists) {
for (Entity entity : list) {
if (map.containsKey(entity.getKey()) == false) {
map.put(entity.getKey(), entity);
}
}
}
return new ArrayList<Entity>(map.values());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment