internal void LockOrUnlockMappings(PointMapping <TKey> mapping, Guid lockOwnerId, LockOwnerIdOpType lockOwnerIdOpType)
 {
     this.LockOrUnlockMappings <PointMapping <TKey> >(
         mapping,
         lockOwnerId,
         lockOwnerIdOpType,
         ShardManagementErrorCategory.ListShardMap);
 }
 /// <summary>
 /// Allows for update to a point mapping with the updates provided in
 /// the <paramref name="update"/> parameter.
 /// </summary>
 /// <param name="currentMapping">Mapping being updated.</param>
 /// <param name="update">Updated properties of the Shard.</param>
 /// <param name="lockOwnerId">Lock owner id of this mapping</param>
 /// <returns>New instance of mapping with updated information.</returns>
 internal PointMapping <TKey> Update(PointMapping <TKey> currentMapping, PointMappingUpdate update, Guid lockOwnerId = default(Guid))
 {
     return(this.Update <PointMapping <TKey>, PointMappingUpdate, MappingStatus>(
                currentMapping,
                update,
                (smm, sm, ssm) => new PointMapping <TKey>(smm, sm, ssm),
                pms => (int)pms,
                i => (MappingStatus)i,
                lockOwnerId));
 }
        /// <summary>
        /// Tries to looks up the key value and returns the corresponding mapping.
        /// </summary>
        /// <param name="key">Input key value.</param>
        /// <param name="useCache">Whether to use cache for lookups.</param>
        /// <param name="mapping">Mapping that contains the key value.</param>
        /// <returns><c>true</c> if mapping is found, <c>false</c> otherwise.</returns>
        public bool TryLookup(TKey key, bool useCache, out PointMapping <TKey> mapping)
        {
            PointMapping <TKey> p = this.Lookup <PointMapping <TKey>, TKey>(
                key,
                useCache,
                (smm, sm, ssm) => new PointMapping <TKey>(smm, sm, ssm),
                ShardManagementErrorCategory.ListShardMap);

            mapping = p;

            return(p != null);
        }
 /// <summary>
 /// Marks the given mapping online.
 /// </summary>
 /// <param name="mapping">Input point mapping.</param>
 /// <param name="lockOwnerId">Lock owner id of this mapping</param>
 /// <returns>An online mapping.</returns>
 public PointMapping <TKey> MarkMappingOnline(PointMapping <TKey> mapping, Guid lockOwnerId = default(Guid))
 {
     return(BaseShardMapper.SetStatus <PointMapping <TKey>, PointMappingUpdate, MappingStatus>(
                mapping,
                mapping.Status,
                s => MappingStatus.Online,
                s => new PointMappingUpdate()
     {
         Status = s
     },
                this.Update,
                lockOwnerId));
 }
        /// <summary>
        /// Determines whether the specified object is equal to the current object.
        /// </summary>
        /// <param name="obj">The object to compare with the current object.</param>
        /// <returns>True if the specified object is equal to the current object; otherwise, false.</returns>
        public override bool Equals(object obj)
        {
            PointMapping <TKey> other = obj as PointMapping <TKey>;

            if (other == null)
            {
                return(false);
            }

            if (this.Id.Equals(other.Id))
            {
                Debug.Assert(this.Key == other.Key);
                return(true);
            }

            return(false);
        }
        /// <summary>
        /// Tries to looks up the key value and place the corresponding mapping in <paramref name="pointMapping"/>.
        /// </summary>
        /// <param name="key">Input key value.</param>
        /// <param name="pointMapping">Mapping that contains the key value.</param>
        /// <returns><c>true</c> if mapping is found, <c>false</c> otherwise.</returns>
        public bool TryGetMappingForKey(TKey key, out PointMapping <TKey> pointMapping)
        {
            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.ListShardMap,
                                 "TryLookupPointMapping", "Start; ShardMap name: {0}; Point Mapping Key Type: {1}",
                                 this.Name, typeof(TKey));

                Stopwatch stopwatch = Stopwatch.StartNew();

                bool result = _lsm.TryLookup(key, false, out pointMapping);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.ListShardMap,
                                 "TryLookupPointMapping", "Complete; ShardMap name: {0}; Point Mapping Key Type: {1}; Duration: {2}",
                                 this.Name, typeof(TKey), stopwatch.Elapsed);

                return(result);
            }
        }
        /// <summary>
        /// Looks up the key value and returns the corresponding mapping.
        /// </summary>
        /// <param name="key">Input key value.</param>
        /// <param name="useCache">Whether to use cache for lookups.</param>
        /// <returns>Mapping that contains the key value.</returns>
        public PointMapping <TKey> Lookup(TKey key, bool useCache)
        {
            PointMapping <TKey> p = this.Lookup <PointMapping <TKey>, TKey>(
                key,
                useCache,
                (smm, sm, ssm) => new PointMapping <TKey>(smm, sm, ssm),
                ShardManagementErrorCategory.ListShardMap);

            if (p == null)
            {
                throw new ShardManagementException(
                          ShardManagementErrorCategory.ListShardMap,
                          ShardManagementErrorCode.MappingNotFoundForKey,
                          Errors._Store_ShardMapper_MappingNotFoundForKeyGlobal,
                          this.ShardMap.Name,
                          StoreOperationRequestBuilder.SpFindShardMappingByKeyGlobal,
                          "Lookup");
            }

            return(p);
        }
        public void DeleteMapping(PointMapping <TKey> mapping)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");

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

                Stopwatch stopwatch = Stopwatch.StartNew();

                _lsm.Remove(mapping);

                stopwatch.Stop();

                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.ListShardMap,
                                 "DeletePointMapping", "Completed; ShardMap name: {0}; Point Mapping: {1}; Duration: {2}",
                                 this.Name, mappingKey, stopwatch.Elapsed);
            }
        }
        public void UnlockMapping(PointMapping <TKey> mapping, MappingLockToken mappingLockToken)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");
            ExceptionUtils.DisallowNullArgument(mappingLockToken, "mappingLockToken");

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

                Stopwatch stopwatch = Stopwatch.StartNew();

                _lsm.LockOrUnlockMappings(mapping, lockOwnerId, LockOwnerIdOpType.UnlockMappingForId);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.ListShardMap,
                    "UnLock", "Complete; Duration: {0}; StoreLockOwnerId: {1}",
                    stopwatch.Elapsed, lockOwnerId);
            }
        }
        /// <summary>
        /// Gets the lock owner id of the specified mapping.
        /// </summary>
        /// <param name="mapping">Input range mapping.</param>
        /// <returns>An instance of <see cref="MappingLockToken"/></returns>
        public MappingLockToken GetMappingLockOwner(PointMapping <TKey> mapping)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");

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

                Stopwatch stopwatch = Stopwatch.StartNew();

                Guid storeLockOwnerId = _lsm.GetLockOwnerForMapping(mapping);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.ListShardMap,
                    "LookupLockOwner", "Complete; Duration: {0}; StoreLockOwnerId: {1}",
                    stopwatch.Elapsed, storeLockOwnerId);

                return(new MappingLockToken(storeLockOwnerId));
            }
        }
        /// <summary>
        /// Marks the specified mapping online.
        /// </summary>
        /// <param name="mapping">Input point mapping.</param>
        /// <returns>An online mapping.</returns>
        public PointMapping <TKey> MarkMappingOnline(PointMapping <TKey> mapping)
        {
            ExceptionUtils.DisallowNullArgument(mapping, "mapping");

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

                Stopwatch stopwatch = Stopwatch.StartNew();

                PointMapping <TKey> result = _lsm.MarkMappingOnline(mapping);

                stopwatch.Stop();

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

                return(result);
            }
        }
        public PointMapping <TKey> CreatePointMapping(PointMappingCreationInfo <TKey> creationInfo)
        {
            ExceptionUtils.DisallowNullArgument(creationInfo, "args");

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Stopwatch stopwatch = Stopwatch.StartNew();

                string mappingKey = BitConverter.ToString(creationInfo.Key.RawValue);
                Tracer.TraceInfo(TraceSourceConstants.ComponentNames.ListShardMap,
                                 "CreatePointMapping", "Start; ShardMap name: {0}; Point Mapping: {1} ",
                                 this.Name, mappingKey);

                PointMapping <TKey> pointMapping = _lsm.Add(new PointMapping <TKey>(this.Manager, creationInfo));

                stopwatch.Stop();

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

                return(pointMapping);
            }
        }
 public PointMapping <TKey> UpdateMapping(PointMapping <TKey> currentMapping, PointMappingUpdate update)
 {
     return(this.UpdateMapping(currentMapping, update, MappingLockToken.NoLock));
 }
 /// <summary>
 /// Gets the lock owner of a mapping.
 /// </summary>
 /// <param name="mapping">The mapping</param>
 /// <returns>Lock owner for the mapping.</returns>
 internal Guid GetLockOwnerForMapping(PointMapping <TKey> mapping)
 {
     return(this.GetLockOwnerForMapping <PointMapping <TKey> >(mapping, ShardManagementErrorCategory.ListShardMap));
 }
 /// <summary>
 /// Adds a point mapping.
 /// </summary>
 /// <param name="mapping">Mapping being added.</param>
 /// <returns>The added mapping object.</returns>
 public PointMapping <TKey> Add(PointMapping <TKey> mapping)
 {
     return(this.Add <PointMapping <TKey> >(
                mapping,
                (smm, sm, ssm) => new PointMapping <TKey>(smm, sm, ssm)));
 }