// Determine the IndexCase:
// The indexing of all versions is determined by the record type of the non-versioned scope.
// This makes that the indexing behavior of all versions is equal, and can be changed (the
// record type of the versioned scope is immutable).
IndexCase indexCase = lilyIndexerConf.getIndexCase(table.getTableName(), vtRecord.getRecord());
if (indexCase == null) {
// The record should not be indexed
// The record might however have been previously indexed, therefore we need to perform a delete
// (note that IndexAwareMQFeeder only sends events to us if either the current or old record
// state matched the inclusion rules)
solrUpdateWriter.deleteByQuery("lily.id:" + ClientUtils.escapeQueryChars(vtRecord.getId().toString()));
if (log.isDebugEnabled()) {
log.debug(String.format("Record %1$s: no index case found, record deleted from index.",
vtRecord.getId()));
}
// The data from this record might be denormalized into other index entries
// After this we go to update denormalized data
} else {
final Set<SchemaId> vtagsToIndex;
if (event.getType().equals(CREATE)) {
// New record: just index everything
vtagsToIndex = Sets.intersection(indexCase.getVersionTags(), vtRecord.getVTags().keySet());
//vtagsToIndex = indexer.retainExistingVtagsOnly(indexCase.getVersionTags(), vtRecord);
// After this we go to the indexing
} else if (event.getRecordTypeChanged()) {
// When the record type changes, the rules to index (= the IndexCase) change
// Delete everything: we do not know the previous record type, so we do not know what
// version tags were indexed, so we simply delete everything
solrUpdateWriter.deleteByQuery("lily.id:" + ClientUtils.escapeQueryChars(vtRecord.getId().toString()));
if (log.isDebugEnabled()) {
log.debug(String.format("Record %1$s: deleted existing entries from index (if present) " +
"because of record type change", vtRecord.getId()));
}
// Reindex all needed vtags
vtagsToIndex = Sets.intersection(indexCase.getVersionTags(), vtRecord.getVTags().keySet());
// After this we go to the indexing
} else { // a normal update
//
// Handle changes to non-versioned fields
//
if (lilyIndexerConf.changesAffectIndex(vtRecord, Scope.NON_VERSIONED)) {
vtagsToIndex = Sets.intersection(indexCase.getVersionTags(), vtRecord.getVTags().keySet());
// After this we go to the treatment of changed vtag fields
if (log.isDebugEnabled()) {
log.debug(
String.format("Record %1$s: non-versioned fields changed, will reindex all vtags.",
vtRecord.getId()));
}
} else {
vtagsToIndex = new HashSet<SchemaId>();
}
//
// Handle changes to versioned(-mutable) fields
//
// If there were non-versioned fields changed, then we already reindex all versions
// so this can be skipped.
//
// In the case of newly created versions that should be indexed: this will often be
// accompanied by corresponding changes to vtag fields, which are handled next, and in which case
// it would work as well if this code would not be here.
//
if (vtagsToIndex.isEmpty() && (event.getVersionCreated() != -1 || event.getVersionUpdated() != -1)) {
if (lilyIndexerConf.changesAffectIndex(vtRecord, Scope.VERSIONED)
|| lilyIndexerConf.changesAffectIndex(vtRecord, Scope.VERSIONED_MUTABLE)) {
long version =
event.getVersionCreated() != -1 ? event.getVersionCreated() : event.getVersionUpdated();
if (vtagsByVersion.containsKey(version)) {
Set<SchemaId> tmp = new HashSet<SchemaId>();
tmp.addAll(indexCase.getVersionTags());
tmp.retainAll(vtagsByVersion.get(version));
vtagsToIndex.addAll(tmp);
if (log.isDebugEnabled()) {
log.debug(String.format("Record %1$s: versioned(-mutable) fields changed, will " +
"index for all tags of modified version %2$s that require indexing: %3$s",
vtRecord.getId(), version, Joiner.on(",").join(vtagsToIndex)));
}
}
}
}
//
// Handle changes to vtag fields themselves
//
Map<SchemaId, Long> vtags = vtRecord.getVTags();
Set<SchemaId> changedVTagFields = vtRecord.getRecordEventHelper().getModifiedVTags();
// Remove the vtags which are going to be reindexed anyway
changedVTagFields.removeAll(vtagsToIndex);
for (SchemaId vtag : changedVTagFields) {
if (vtags.containsKey(vtag) && indexCase.getVersionTags().contains(vtag)) {
if (log.isDebugEnabled()) {
log.debug(String.format("Record %1$s: will index for created or updated vtag %2$s",
vtRecord.getId(), this.safeLoadTagName(vtag)));
}
vtagsToIndex.add(vtag);