if (event.getType().equals(INDEX)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Record %1$s: reindex requested for these vtags: %2$s", record.getId(),
vtagSetToNameString(event.getVtagsToIndex())));
}
RecordEventHelper eventHelper = new RecordEventHelper(event, null, repository.getTypeManager());
VTaggedRecord vtRecord = new VTaggedRecord(record.getId(), eventHelper, table, repository);
IndexCase indexCase = lilyIndexerConf.getIndexCase(table.getTableName(), vtRecord.getRecord());
Set<SchemaId> vtagsToIndex = event.getVtagsToIndex();
if (indexCase == null) {
return;
}
// Only keep vtags which should be indexed
vtagsToIndex.retainAll(indexCase.getVersionTags());
// Only keep vtags which exist on the record
vtagsToIndex.retainAll(vtRecord.getVTags().keySet());
log.debug(vtagsToIndex.toString());
index(table, vtRecord, vtagsToIndex, solrUpdateWriter);
} else if (event.getType().equals(DELETE)) {
solrUpdateWriter.deleteByQuery("lily.id:" + ClientUtils.escapeQueryChars(record.getId().toString()));
if (log.isDebugEnabled()) {
log.debug(String.format("Record %1$s: deleted from index (if present) because of " +
"delete record event", record.getId()));
}
// After this we can go to update denormalized data
if (derefMap != null) {
updateDenormalizedData(repository.getRepositoryName(), event.getTableName(), record.getId(),
null, null);
}
} else {
// CREATE or UPDATE
VTaggedRecord vtRecord;
// Based on the partial old/new record state stored in the RecordEvent, determine whether we
// now match a different IndexCase than before, and if so, if the new case would have less vtags
// than the old one, perform the necessary deletes on Solr.
Pair<Record,Record> oldAndNewRecords =
IndexRecordFilterUtil.getOldAndNewRecordForRecordFilterEvaluation(record.getId(), event,
repository);
Record oldRecord = oldAndNewRecords.getV1();
Record newRecord = oldAndNewRecords.getV2();
IndexCase caseOld = oldRecord != null ? lilyIndexerConf.getIndexCase(
event.getTableName(), oldRecord) : null;
IndexCase caseNew = newRecord != null ? lilyIndexerConf.getIndexCase(
event.getTableName(), newRecord) : null;
if (oldRecord != null && newRecord != null) {
if (caseOld != null && caseNew != null) {
Set<SchemaId> droppedVtags = new HashSet<SchemaId>(caseOld.getVersionTags());
droppedVtags.removeAll(caseNew.getVersionTags());
if (droppedVtags.size() > 0) {
// Perform deletes
for (SchemaId vtag : droppedVtags) {
solrUpdateWriter.deleteById(LilyResultToSolrMapper.getIndexId(tableName,
record.getId(), vtag));
}
}
}
}
// This is an optimization: an IndexCase with empty vtags list means that this record is
// included in this index only to trigger updating of denormalized data.
boolean doIndexing = true;
if (caseNew != null) {
doIndexing = caseNew.getVersionTags().size() > 0;
} else if (caseNew == null && caseOld != null) {
// caseNew == null means either the record has been deleted, or means the record does
// not match the recordFilter anymore. In either case, we only need to trigger update
// of denormalized data (if the vtags list was empty on caseOld).
doIndexing = caseOld.getVersionTags().size() > 0;
}
RecordEventHelper eventHelper = new RecordEventHelper(event, null, repository.getTypeManager());
if (doIndexing) {
try {
// Read the vtags of the record. Note that while this algorithm is running, the record can
// meanwhile undergo changes. However, we continuously work with the snapshot of the vtags
// mappings read here. The processing of later events will bring the index up to date with
// any new changes.
vtRecord = new VTaggedRecord(record.getId(), eventHelper,
repository.getTable(event.getTableName()), repository);
} catch (RecordNotFoundException e) {
// The record has been deleted in the meantime.
// For now, we do nothing, when the delete event is received the record will be removed
// from the index (as well as update of denormalized data).
// TODO: we should process all outstanding messages for the record (up to delete) in one go
return;
}
handleRecordCreateUpdate(vtRecord, table, solrUpdateWriter);
}
if (derefMap != null) {
updateDenormalizedData(repository.getRepositoryName(), event.getTableName(), record.getId(),
eventHelper.getUpdatedFieldsByScope(), eventHelper.getModifiedVTags());
}
}
} catch (Exception e) {
log.warn("Something went wrong while indexing", e);
}