Is there a good pattern for reusing Morphine queries?

I just profiled some code where I increment some frequency counters with the following code:

   Datastore ds = ...
   final Query<Unit> query = ds.createQuery(Unit.class);
   query.field("text").equal(text);
   query.field("langCode").equal(lang.getCode());
   UpdateOperations ops = ds.createUpdateOperations(Unit.class);
   ops.inc("frequency", value);
   ds.update(query, ops, false);

Creating a query takes almost 50% of the execution time, and I would like to somehow reuse it. Is it safe to store objects queryand opsin ThreadLocal and just call query.field("text").equal(text)again to replace the "text" field? It also appears that validation takes up about 30% of the total time.

+5
source share
1 answer

Yes you can do it. Each field is saved on the map, so it will be replaced several times when called.

+3
source

All Articles