Iterator<Map.Entry<ObjectId, AssociationFeaturePojo>> it = evtCommunity.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<ObjectId, AssociationFeaturePojo> evtFeatureKV = it.next();
try {
AssociationFeaturePojo evtFeature = evtFeatureKV.getValue();
long nSavedDocCount = evtFeature.getDoccount();
ObjectId communityID = evtFeature.getCommunityId();
//try to update
BasicDBObject query = new BasicDBObject(AssociationFeaturePojo.index_, evtFeature.getIndex());
query.put(AssociationFeaturePojo.communityId_, communityID);
//Step1 try to update alias
//update arrays
BasicDBObject multiopAliasArrays = new BasicDBObject();
// Entity1 Alias:
if (null != evtFeature.getEntity1_index()) {
evtFeature.addEntity1(evtFeature.getEntity1_index());
}
if (null != evtFeature.getEntity1())
{
BasicDBObject multiopE = new BasicDBObject(MongoDbManager.each_, evtFeature.getEntity1());
multiopAliasArrays.put(AssociationFeaturePojo.entity1_, multiopE);
}
// Entity2 Alias:
if (null != evtFeature.getEntity2_index()) {
evtFeature.addEntity2(evtFeature.getEntity2_index());
}
if (null != evtFeature.getEntity2()) {
BasicDBObject multiopE = new BasicDBObject(MongoDbManager.each_, evtFeature.getEntity2());
multiopAliasArrays.put(AssociationFeaturePojo.entity2_, multiopE);
}
// verb/verb cat alias:
if (null != evtFeature.getVerb_category()) {
evtFeature.addVerb(evtFeature.getVerb_category());
}
if (null != evtFeature.getVerb()) {
BasicDBObject multiopE = new BasicDBObject(MongoDbManager.each_, evtFeature.getVerb());
multiopAliasArrays.put(AssociationFeaturePojo.verb_, multiopE);
}
BasicDBObject updateOp = new BasicDBObject();
updateOp.put(MongoDbManager.addToSet_, multiopAliasArrays);
// Document count for this event
BasicDBObject updateFreqDocCount = new BasicDBObject(AssociationFeaturePojo.doccount_, nSavedDocCount);
updateOp.put(MongoDbManager.inc_,updateFreqDocCount);
BasicDBObject fields = new BasicDBObject(AssociationFeaturePojo.doccount_, 1);
fields.put(AssociationFeaturePojo.entity1_, 1);
fields.put(AssociationFeaturePojo.entity2_, 1);
fields.put(AssociationFeaturePojo.verb_, 1);
//(slightly annoying, since only want these if updating dc but won't know
// until after i've got this object)
fields.put(AssociationFeaturePojo.db_sync_time_, 1);
fields.put(AssociationFeaturePojo.db_sync_doccount_, 1);
DBObject dboUpdate = null;
if (_diagnosticMode) {
dboUpdate = col.findOne(query,fields);
}
else {
dboUpdate = col.findAndModify(query,fields,new BasicDBObject(),false,updateOp,false,true);
// (can use findAndModify because specify index, ie the shard key)
// (returns event before the changes above, update the feature object below)
// (also atomically creates the object if it doesn't exist so is "distributed-safe")
}
if ( ( dboUpdate != null ) && !dboUpdate.keySet().isEmpty() )
{
AssociationFeaturePojo egp = AssociationFeaturePojo.fromDb(dboUpdate, AssociationFeaturePojo.class);
evtFeature.setDoccount(egp.getDoccount() + nSavedDocCount);
evtFeature.setDb_sync_doccount(egp.getDb_sync_doccount());
evtFeature.setDb_sync_time(egp.getDb_sync_time());
if (null != egp.getEntity1()) {
for (String ent: egp.getEntity1()) evtFeature.addEntity1(ent);
}
if (null != egp.getEntity2()) {
for (String ent: egp.getEntity2()) evtFeature.addEntity2(ent);
}
if (null != egp.getVerb()) {
for (String verb: egp.getVerb()) evtFeature.addVerb(verb);
}
if (_diagnosticMode) {
System.out.println("EventAggregationUtils.updateEventFeatures, found: " + ((BasicDBObject)egp.toDb()).toString());
System.out.println("EventAggregationUtils.updateEventFeatures, ^^^ found from query: " + query.toString() + " / " + updateOp.toString());
}
// (In background aggregation mode we update db_sync_prio when checking the -otherwise unused, unlike entities- document update schedule)
}
else // (the object in memory is now an accurate representation of the database, minus some fields we'll now add)