Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a good pattern for reusing Morphia queries?

I've just been profiling 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);

The creation of the query takes almost 50% of the execution time, and I'd like to reuse the work somehow. Is it safe to save the query and ops objects in a ThreadLocal and just call query.field("text").equal(text) again to replace the "text" field? It also looks like validation is taking up some 30% of the total time.

like image 316
Nic Cottrell Avatar asked Oct 23 '22 06:10

Nic Cottrell


1 Answers

Yes, you can do that. Each field is stored in a map so it will replace it when called multiple times.

like image 94
Scott Hernandez Avatar answered Oct 26 '22 23:10

Scott Hernandez