public RangeMapping <TKey> MergeMappings(RangeMapping <TKey> left, RangeMapping <TKey> right,
                                                 MappingLockToken leftMappingLockToken, MappingLockToken rightMappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(left, "left");
            ExceptionUtils.DisallowNullArgument(right, "right");
            ExceptionUtils.DisallowNullArgument(leftMappingLockToken, "leftMappingLockToken");
            ExceptionUtils.DisallowNullArgument(rightMappingLockToken, "rightMappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Start; Left Shard: {0}; Right Shard: {1}",
                                 left.Shard.Location, right.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> rangeMapping = this.rsm.Merge(left, right, leftMappingLockToken.LockOwnerId, rightMappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Complete; Duration: {0}", stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
        public RangeMapping <TKey> UpdateMapping(RangeMapping <TKey> currentMapping, RangeMappingUpdate update,
                                                 MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(currentMapping, "currentMapping");
            ExceptionUtils.DisallowNullArgument(update, "update");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "UpdateMapping", "Start; Current mapping shard: {0}",
                                 currentMapping.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> rangeMapping = this.rsm.Update(currentMapping, update, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "UpdateMapping", "Complete; Current mapping shard: {0}; Duration: {1}",
                                 currentMapping.Shard.Location, stopwatch.Elapsed);

                return(rangeMapping);
            }
        }
        public void LockMapping(RangeMapping <TKey> mapping, MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                // Generate a lock owner id
                Guid lockOwnerId = mappingLockToken.LockOwnerId;

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "Lock", "Start; LockOwnerId: {0}", lockOwnerId);

                Stopwatch stopwatch = Stopwatch.StartNew();

                this.rsm.LockOrUnlockMappings(mapping, lockOwnerId, LockOwnerIdOpType.Lock);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "Lock", "Complete; Duration: {0}; StoreLockOwnerId: {1}",
                    stopwatch.Elapsed, lockOwnerId);
            }
        }
        public RangeMapping <TKey> MarkMappingOnline(RangeMapping <TKey> mapping, MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "MarkMappingOnline", "Start; ");

                Stopwatch stopwatch = Stopwatch.StartNew();

                RangeMapping <TKey> result = this.rsm.MarkMappingOnline(mapping, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "MarkMappingOnline", "Complete; Duration: {0}",
                    stopwatch.Elapsed);

                return(result);
            }
        }
        public PointMapping <TKey> UpdateMapping(PointMapping <TKey> currentMapping, PointMappingUpdate update,
                                                 MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(currentMapping, "currentMapping");
            ExceptionUtils.DisallowNullArgument(update, "update");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                string mappingKey = BitConverter.ToString(currentMapping.Key.RawValue);
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.ListShardMap,
                                 "UpdatePointMapping", "Start; ShardMap name: {0}; Current Point Mapping: {1}",
                                 this.Name, mappingKey);

                Stopwatch stopwatch = Stopwatch.StartNew();

                PointMapping <TKey> pointMapping = _lsm.Update(currentMapping, update, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.ListShardMap,
                                 "UpdatePointMapping", "Complete; ShardMap name: {0}; Current Point Mapping: {1}; Duration: {2}",
                                 this.Name, mappingKey, stopwatch.Elapsed);

                return(pointMapping);
            }
        }
        public void DeleteMapping(RangeMapping <TKey> mapping, MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "DeleteMapping", "Start; Shard: {0}", mapping.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                this.rsm.Remove(mapping, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "DeleteMapping", "Complete; Shard: {0}; Duration: {1}",
                                 mapping.Shard.Location, stopwatch.Elapsed);
            }
        }
        public void UnlockMapping(MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Guid lockOwnerId = mappingLockToken.LockOwnerId;
                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "UnlockAllMappingsWithLockOwnerId", "Start; LockOwnerId: {0}", lockOwnerId);

                Stopwatch stopwatch = Stopwatch.StartNew();

                this.rsm.LockOrUnlockMappings(null, lockOwnerId, LockOwnerIdOpType.UnlockAllMappingsForId);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.RangeShardMap,
                    "UnlockAllMappingsWithLockOwnerId", "Complete; Duration: {0}",
                    stopwatch.Elapsed);
            }
        }
        public IReadOnlyList <RangeMapping <TKey> > SplitMapping(RangeMapping <TKey> existingMapping, TKey splitAt,
                                                                 MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(existingMapping, "existingMapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Start; Shard: {0}", existingMapping.Shard.Location);

                Stopwatch stopwatch = Stopwatch.StartNew();

                IReadOnlyList <RangeMapping <TKey> > rangeMapping = this.rsm.Split(existingMapping, splitAt, mappingLockToken.LockOwnerId);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.RangeShardMap,
                                 "SplitMapping", "Complete; Shard: {0}; Duration: {1}",
                                 existingMapping.Shard.Location, stopwatch.Elapsed);

                return(rangeMapping);
            }
        }