/// <summary> /// Adds matching entities and removes removed entities from index /// </summary> private void UpdateIndex(RedisTransactionWrapper transaction, string indexKey, SearchConditions filter, IDictionary <EntityKey, Document> addedEntities, IDictionary <EntityKey, Document> modifiedEntities, ICollection <EntityKey> removedEntities) { bool indexChanged = false; // adding added entities, if they match the index conditions foreach ( var entityPair in addedEntities.Union(modifiedEntities).Where ( entityPair => filter.MatchesSearchConditions(entityPair.Value, this._tableEntityType) ) ) { transaction.HashSet(indexKey, entityPair.Key.ToRedisValue(), string.Empty); indexChanged = true; } // removing modified entities, if they do not match the index conditions any more foreach ( var entityPair in modifiedEntities.Where ( entityPair => !filter.MatchesSearchConditions(entityPair.Value, this._tableEntityType) ) ) { transaction.HashRemove(indexKey, entityPair.Key.ToRedisValue()); indexChanged = true; } // removing removed entities foreach (var entityKey in removedEntities) { transaction.HashRemove(indexKey, entityKey.ToRedisValue()); indexChanged = true; } if (indexChanged) { // The index should exist in the cache at the moment transaction is being executed. // Otherwise this update operation will cause the index to occasionally "resurrect" after being expired. transaction.AddHashFieldExistsCondition(indexKey, IndexVersionField.Name); // also incrementing the version field transaction.HashIncrement(indexKey, IndexVersionField.Name); } }
/// <summary> /// Checks if a projection index should be dropped because of some added/modified/removed entities /// </summary> private void UpdateProjectionIndex(RedisTransactionWrapper transaction, string hashKeyValue, string indexKey, SearchConditions filter, IDictionary <EntityKey, Document> addedEntities, IDictionary <EntityKey, Document> modifiedEntities, ICollection <EntityKey> removedEntities) { if ( (modifiedEntities.Count > 0) || (removedEntities.Count > 0) || addedEntities.Values.Any // or some entities were added, that satisfy the index condition ( en => filter.MatchesSearchConditions(en, this._tableEntityType) ) ) { // then the only option for us is to drop the index - as we don't know, if these entities conform to index's conditions or not this.Log("Projection index ({0}) removed because of some modified entities", indexKey); transaction.HashRemove(this.GetIndexListKeyInCache(hashKeyValue), indexKey); transaction.Remove(indexKey); } }