/// <summary>
        /// Ensures that the shard map and shard map manager information for given
        /// shard matches the one for current shard map.
        /// </summary>
        /// <param name="currentShardMapManager">Current shard map manager.</param>
        /// <param name="currentShardMap">Current shard map.</param>
        /// <param name="shard">Input shard.</param>
        /// <param name="operation">Operation being performed.</param>
        /// <param name="mappingType">Type of mapping.</param>
        internal static void EnsureShardBelongsToShardMap(
            ShardMapManager currentShardMapManager,
            ShardMap currentShardMap,
            Shard shard,
            string operation,
            string mappingType)
        {
            // Ensure that shard is associated with current shard map.
            if (shard.ShardMapId != currentShardMap.Id)
            {
                throw new InvalidOperationException(
                          StringUtils.FormatInvariant(
                              Errors._Shard_DifferentShardMap,
                              shard.Location,
                              mappingType,
                              currentShardMap.Name,
                              operation));
            }

            // Ensure that shard is associated with current shard map manager instance.
            if (shard.Manager != currentShardMapManager)
            {
                throw new InvalidOperationException(
                          StringUtils.FormatInvariant(
                              Errors._Shard_DifferentShardMapManager,
                              shard.Location,
                              mappingType,
                              currentShardMapManager.Credentials.ShardMapManagerLocation,
                              operation));
            }
        }
        /// <summary>
        /// Performs lookup and conversion operation for shard map with given name.
        /// </summary>
        /// <typeparam name="TShardMap">Type to convert shard map to.</typeparam>
        /// <param name="operationName">Operation name, useful for diagnostics.</param>
        /// <param name="shardMapName">Shard map name.</param>
        /// <param name="converter">Function to downcast a shard map to List/Range/Hash.</param>
        /// <param name="throwOnFailure">Whether to throw exception or return null on failure.</param>
        /// <returns>The converted shard map.</returns>
        private TShardMap LookupAndConvertShardMapHelper <TShardMap>(
            string operationName,
            string shardMapName,
            Func <ShardMap, bool, TShardMap> converter,
            bool throwOnFailure) where TShardMap : class
        {
            ShardMap sm = this.LookupShardMapByName(operationName, shardMapName, true);

            if (sm == null)
            {
                if (throwOnFailure)
                {
                    throw new ShardManagementException(
                              ShardManagementErrorCategory.ShardMapManager,
                              ShardManagementErrorCode.ShardMapLookupFailure,
                              Errors._ShardMapManager_ShardMapLookupFailed,
                              shardMapName,
                              this.Credentials.ShardMapManagerLocation);
                }

                return((TShardMap)null);
            }

            return(converter(sm, throwOnFailure));
        }
        /// <summary>
        /// Tries to obtains a <see cref="ShardMap"/> given the name.
        /// </summary>
        /// <param name="shardMapName">Name of shard map.</param>
        /// <param name="shardMap">Shard map with the specified name.</param>
        /// <returns><c>true</c> if shard map with the specified name was found, <c>false</c> otherwise.</returns>
        public bool TryGetShardMap(string shardMapName, out ShardMap shardMap)
        {
            ShardMapManager.ValidateShardMapName(shardMapName);

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceVerbose(
                    TraceSourceConstants.ComponentNames.ShardMapManager,
                    "TryGetShardMap",
                    "Start; ShardMap: {0}",
                    shardMapName);

                shardMap = this.LookupAndConvertShardMapHelper <ShardMap>(
                    "TryGetShardMap",
                    shardMapName,
                    (sm, t) => sm,
                    false);

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.ShardMapManager,
                    "TryGetShardMap",
                    "Complete; ShardMap: {0}",
                    shardMapName);

                return(shardMap != null);
            }
        }
        public void DeleteShardMap(ShardMap shardMap)
        {
            this.ValidateShardMap(shardMap);

            using (ActivityIdScope activityIdScope = new ActivityIdScope(Guid.NewGuid()))
            {
                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.ShardMapManager,
                    "DeleteShardMap",
                    "Start; ShardMap: {0}",
                    shardMap.Name);

                Stopwatch stopwatch = Stopwatch.StartNew();

                this.RemoveShardMapFromStore(shardMap.StoreShardMap);

                stopwatch.Stop();

                Tracer.TraceInfo(
                    TraceSourceConstants.ComponentNames.ShardMapManager,
                    "DeleteShardMap",
                    "Complete; ShardMap: {0}; Duration: {1}",
                    shardMap.Name,
                    stopwatch.Elapsed);
            }
        }
Example #5
0
        /// <summary>
        /// Base shard mapper, which is just a holder of some fields.
        /// </summary>
        /// <param name="manager">Reference to ShardMapManager.</param>
        /// <param name="sm">Containing shard map.</param>
        protected BaseShardMapper(ShardMapManager manager, ShardMap sm)
        {
            Debug.Assert(manager != null);
            Debug.Assert(sm != null);

            this.Manager  = manager;
            this.ShardMap = sm;
        }
        /// <summary>
        /// Validates the input shard map. This includes:
        /// * Ensuring that shard map belongs to this instance of shard map manager.
        /// </summary>
        /// <param name="shardMap">Input shard map.</param>
        private void ValidateShardMap(ShardMap shardMap)
        {
            ExceptionUtils.DisallowNullArgument(shardMap, "shardMap");

            if (shardMap.Manager != this)
            {
                throw new InvalidOperationException(
                          StringUtils.FormatInvariant(
                              Errors._ShardMapManager_DifferentShardMapManager,
                              shardMap.Name,
                              this.Credentials.ShardMapManagerLocation));
            }
        }
Example #7
0
        /// <summary>
        /// Internal constructor that uses storage representation.
        /// </summary>
        /// <param name="manager">Owning ShardMapManager.</param>
        /// <param name="shardMap">Owning shard map.</param>
        /// <param name="storeShard">Storage representation of the shard.</param>
        internal Shard(
            ShardMapManager manager,
            ShardMap shardMap,
            IStoreShard storeShard)
        {
            Debug.Assert(manager != null);
            this.Manager = manager;

            Debug.Assert(shardMap != null);
            this.ShardMap = shardMap;

            Debug.Assert(storeShard.ShardMapId != default(Guid));
            this.StoreShard = storeShard;

            _hashCode = this.CalculateHashCode();
        }
        internal static ListShardMap <TKey> AsListShardMap <TKey>(this ShardMap shardMap, bool throwOnFailure)
        {
            Debug.Assert(shardMap != null);
            ListShardMap <TKey> lsm = null;

            if (shardMap.MapType == ShardMapType.List)
            {
                lsm = shardMap as ListShardMap <TKey>;
            }

            if (lsm == null && throwOnFailure)
            {
                throw ShardMapExtensions.GetConversionException <TKey>(shardMap.StoreShardMap, "List");
            }

            return(lsm);
        }
        /// <summary>
        /// Internal constructor used for deserialization from store representation of
        /// the mapping object.
        /// </summary>
        /// <param name="manager">Owning ShardMapManager.</param>
        /// <param name="shardMap">Owning shard map.</param>
        /// <param name="mapping">Storage representation of the mapping.</param>
        internal PointMapping(
            ShardMapManager manager,
            ShardMap shardMap,
            IStoreMapping mapping)
        {
            Debug.Assert(manager != null);
            this.Manager = manager;

            Debug.Assert(mapping != null);
            Debug.Assert(mapping.ShardMapId != default(Guid));
            Debug.Assert(mapping.StoreShard.ShardMapId != default(Guid));
            this.StoreMapping = mapping;

            _shard = new Shard(this.Manager, shardMap, mapping.StoreShard);

            this.Key   = ShardKey.FromRawValue(ShardKey.ShardKeyTypeFromType(typeof(TKey)), mapping.MinValue);
            this.Value = (TKey)this.Key.Value;
        }
        internal static RangeShardMap <TKey> AsRangeShardMap <TKey>(this ShardMap shardMap, bool throwOnFailure)
        {
            Debug.Assert(shardMap != null);
            RangeShardMap <TKey> rsm = null;

            if (shardMap.MapType == ShardMapType.Range)
            {
                rsm = shardMap as RangeShardMap <TKey>;
            }


            if (rsm == null && throwOnFailure)
            {
                throw ShardMapExtensions.GetConversionException <TKey>(shardMap.StoreShardMap, "Range");
            }

            return(rsm);
        }
Example #11
0
        /// <summary>
        /// Constructs a Shard given shard creation arguments.
        /// </summary>
        /// <param name="manager">Owning ShardMapManager.</param>
        /// <param name="shardMap">Owning shard map.</param>
        /// <param name="creationInfo">Shard creation information.</param>
        internal Shard(
            ShardMapManager manager,
            ShardMap shardMap,
            ShardCreationInfo creationInfo)
        {
            Debug.Assert(manager != null);
            Debug.Assert(shardMap != null);
            Debug.Assert(creationInfo != null);

            this.Manager  = manager;
            this.ShardMap = shardMap;

            this.StoreShard = new DefaultStoreShard(
                Guid.NewGuid(),
                Guid.NewGuid(),
                shardMap.Id,
                creationInfo.Location,
                (int)creationInfo.Status);

            _hashCode = this.CalculateHashCode();
        }
        /// <summary>
        /// Internal constructor used for deserialization from store representation of
        /// the mapping object.
        /// </summary>
        /// <param name="manager">Owning ShardMapManager.</param>
        /// <param name="shardMap">Owning shard map.</param>
        /// <param name="mapping">Storage representation of the mapping.</param>
        internal RangeMapping(
            ShardMapManager manager,
            ShardMap shardMap,
            IStoreMapping mapping)
        {
            Debug.Assert(manager != null);
            this.Manager = manager;

            Debug.Assert(mapping != null);
            Debug.Assert(mapping.ShardMapId != default(Guid));
            Debug.Assert(mapping.StoreShard.ShardMapId != default(Guid));
            this.StoreMapping = mapping;

            _shard = new Shard(this.Manager, shardMap, mapping.StoreShard);

            this.Range = new ShardRange(
                ShardKey.FromRawValue(ShardKey.ShardKeyTypeFromType(typeof(TKey)), mapping.MinValue),
                ShardKey.FromRawValue(ShardKey.ShardKeyTypeFromType(typeof(TKey)), mapping.MaxValue));

            this.Value = this.Range.High.IsMax ?
                         new Range <TKey>(this.Range.Low.GetValue <TKey>()) :
                         new Range <TKey>(this.Range.Low.GetValue <TKey>(), this.Range.High.GetValue <TKey>());
        }
Example #13
0
 /// <summary>
 /// Hash shard mapper, which managers hashed ranges.
 /// </summary>
 /// <param name="manager">Reference to ShardMapManager.</param>
 /// <param name="sm">Containing shard map.</param>
 internal HashShardMapper(ShardMapManager manager, ShardMap sm)
     : base(manager, sm)
 {
 }
 /// <summary>
 /// Default shard mapper, which just manages Shards.
 /// </summary>
 /// <param name="manager">Reference to ShardMapManager.</param>
 /// <param name="sm">Containing shard map.</param>
 internal DefaultShardMapper(ShardMapManager manager, ShardMap sm) : base(manager, sm)
 {
 }
 /// <summary>
 /// List shard mapper, which managers point mappings.
 /// </summary>
 /// <param name="manager">Reference to ShardMapManager.</param>
 /// <param name="sm">Containing shard map.</param>
 internal ListShardMapper(ShardMapManager manager, ShardMap sm) : base(manager, sm)
 {
 }
        public static RangeShardMap <TKey> AsRangeShardMap <TKey>(this ShardMap shardMap)
        {
            ExceptionUtils.DisallowNullArgument(shardMap, "shardMap");

            return(ShardMapExtensions.AsRangeShardMap <TKey>(shardMap, true));
        }
Example #17
0
 /// <summary>
 /// Range shard mapper, which managers range mappings.
 /// </summary>
 /// <param name="manager">Reference to ShardMapManager.</param>
 /// <param name="sm">Containing shard map.</param>
 internal RangeShardMapper(ShardMapManager manager, ShardMap sm) : base(manager, sm)
 {
 }