/// <summary> /// Finds all mappings to be purged based on the given input ranges. /// </summary> /// <param name="ts">LSM transaction scope.</param> /// <returns>Mappings which are to be removed.</returns> private IEnumerable <IStoreMapping> GetMappingsToPurge(IStoreTransactionScope ts) { IEnumerable <IStoreMapping> lsmMappings = null; IStoreResults result; if (_rangesToRemove == null) { // If no ranges are specified, get all the mappings for the shard. result = ts.ExecuteOperation( StoreOperationRequestBuilder.SpGetAllShardMappingsLocal, StoreOperationRequestBuilder.GetAllShardMappingsLocal(_shardMap, _shard, null)); if (result.Result != StoreResult.Success) { // Possible errors are: // StoreResult.ShardMapDoesNotExist // StoreResult.StoreVersionMismatch // StoreResult.MissingParametersForStoredProcedure throw StoreOperationErrorHandler.OnRecoveryErrorLocal( result, _shardMap, this.Location, ShardManagementErrorCategory.Recovery, this.OperationName, StoreOperationRequestBuilder.SpGetAllShardMappingsLocal); } lsmMappings = result.StoreMappings; } else { // If any ranges are specified, only delete intersected ranges. IDictionary <ShardRange, IStoreMapping> mappingsToPurge = new Dictionary <ShardRange, IStoreMapping>(); foreach (ShardRange range in _rangesToRemove) { switch (_shardMap.MapType) { case ShardMapType.Range: result = ts.ExecuteOperation( StoreOperationRequestBuilder.SpGetAllShardMappingsLocal, StoreOperationRequestBuilder.GetAllShardMappingsLocal( _shardMap, _shard, range)); break; default: Debug.Assert(_shardMap.MapType == ShardMapType.List); result = ts.ExecuteOperation( StoreOperationRequestBuilder.SpFindShardMappingByKeyLocal, StoreOperationRequestBuilder.FindShardMappingByKeyLocal( _shardMap, ShardKey.FromRawValue(_shardMap.KeyType, range.Low.RawValue))); break; } if (result.Result != StoreResult.Success) { if (result.Result != StoreResult.MappingNotFoundForKey) { // Possible errors are: // StoreResult.ShardMapDoesNotExist // StoreResult.StoreVersionMismatch // StoreResult.MissingParametersForStoredProcedure throw StoreOperationErrorHandler.OnRecoveryErrorLocal( result, _shardMap, this.Location, ShardManagementErrorCategory.Recovery, this.OperationName, _shardMap.MapType == ShardMapType.Range ? StoreOperationRequestBuilder.SpGetAllShardMappingsLocal : StoreOperationRequestBuilder.SpFindShardMappingByKeyLocal); } else { // No intersections being found is fine. Skip to the next mapping. Debug.Assert(_shardMap.MapType == ShardMapType.List); } } else { foreach (IStoreMapping mapping in result.StoreMappings) { ShardRange intersectedRange = new ShardRange( ShardKey.FromRawValue(_shardMap.KeyType, mapping.MinValue), ShardKey.FromRawValue(_shardMap.KeyType, mapping.MaxValue)); mappingsToPurge[intersectedRange] = mapping; } } } lsmMappings = mappingsToPurge.Values; } return(lsmMappings); }