Пример #1
0
        public IList <Tuple <string, RavenFileSystemClient> > GetShardsToOperateOn(ShardRequestData resultionData)
        {
            var shardIds = ShardStrategy.ShardResolutionStrategy.PotentialShardsFor(resultionData);

            IEnumerable <KeyValuePair <string, RavenFileSystemClient> > cmds = ShardClients;

            if (shardIds == null)
            {
                return(cmds.Select(x => Tuple.Create(x.Key, x.Value)).ToList());
            }

            var list = new List <Tuple <string, RavenFileSystemClient> >();

            foreach (var shardId in shardIds)
            {
                RavenFileSystemClient value;
                if (ShardClients.TryGetValue(shardId, out value) == false)
                {
                    throw new InvalidOperationException("Could not find shard id: " + shardId);
                }

                list.Add(Tuple.Create(shardId, value));
            }
            return(list);
        }
        /// <summary>
        ///  Selects the shard ids appropriate for the specified data.
        ///  </summary><returns>Return a list of shards ids that will be search. Returning null means search all shards.</returns>
        public virtual IList <string> PotentialShardsFor(ShardRequestData requestData)
        {
            if (requestData.Keys.Count == 0) // we are only optimized for keys
            {
                return(null);
            }

            // we are looking for search by key, let us see if we can narrow it down by using the
            // embedded shard id.
            var list = new List <string>();

            foreach (var key in requestData.Keys)
            {
                var start = key.IndexOf(shardStrategy.Conventions.IdentityPartsSeparator, StringComparison.OrdinalIgnoreCase);
                if (start == -1)
                {
                    return(null); // if we couldn't figure it out, select from all
                }
                var maybeShardId = key.Substring(0, start);

                if (ShardIds.Any(x => string.Equals(maybeShardId, x, StringComparison.OrdinalIgnoreCase)))
                {
                    list.Add(maybeShardId);
                }
                else
                {
                    return(null); // we couldn't find it there, select from all
                }
            }
            return(list.ToArray());
        }
        public Task <T[]> ApplyAsync <T>(IList <RavenFileSystemClient> commands, ShardRequestData request, Func <RavenFileSystemClient, int, Task <T> > operation)
        {
            var resultsTask = new TaskCompletionSource <List <T> >();
            var results     = new List <T>();
            var errors      = new List <Exception>();

            Action <int> executer = null;

            executer = index =>
            {
                if (index >= commands.Count)
                {
                    if (errors.Count == commands.Count)
                    {
                        throw new AggregateException(errors);
                    }
                    // finished all commands successfully
                    resultsTask.SetResult(results);
                    return;
                }

                operation(commands[index], index).ContinueWith(task =>
                {
                    if (task.IsFaulted)
                    {
                        var error = OnAsyncError;
                        if (error == null)
                        {
                            resultsTask.SetException(task.Exception);
                            return;
                        }
                        if (error(commands[index], request, task.Exception) == false)
                        {
                            resultsTask.SetException(task.Exception);
                            return;
                        }
                        errors.Add(task.Exception);
                    }
                    else
                    {
                        results.Add(task.Result);
                    }

                    // After we've dealt with one result, we call the operation on the next shard
                    executer(index + 1);
                });
            };

            executer(0);
            return(resultsTask.Task.ContinueWith(task => task.Result.ToArray()));
        }
		public IList<Tuple<string, RavenFileSystemClient>> GetShardsToOperateOn(ShardRequestData resultionData)
		{
			var shardIds = ShardStrategy.ShardResolutionStrategy.PotentialShardsFor(resultionData);

			IEnumerable<KeyValuePair<string, RavenFileSystemClient>> cmds = ShardClients;

			if (shardIds == null)
			{
				return cmds.Select(x => Tuple.Create(x.Key, x.Value)).ToList();
			}

			var list = new List<Tuple<string, RavenFileSystemClient>>();
			foreach (var shardId in shardIds)
			{
				RavenFileSystemClient value;
				if (ShardClients.TryGetValue(shardId, out value) == false)
					throw new InvalidOperationException("Could not find shard id: " + shardId);

				list.Add(Tuple.Create(shardId, value));

			}
			return list;
		}
        /// <summary>
        ///  Selects the shard ids appropriate for the specified data.
        ///  </summary><returns>Return a list of shards ids that will be search. Returning null means search all shards.</returns>
        public virtual IList<string> PotentialShardsFor(ShardRequestData requestData)
        {
            if (requestData.Keys.Count == 0) // we are only optimized for keys
                return null;

            // we are looking for search by key, let us see if we can narrow it down by using the 
            // embedded shard id.
            var list = new List<string>();
            foreach (var key in requestData.Keys)
            {
                var start = key.IndexOf(shardStrategy.Conventions.IdentityPartsSeparator, StringComparison.OrdinalIgnoreCase);
                if (start == -1)
                    return null; // if we couldn't figure it out, select from all

                var maybeShardId = key.Substring(0, start);

                if (ShardIds.Any(x => string.Equals(maybeShardId, x, StringComparison.OrdinalIgnoreCase)))
                    list.Add(maybeShardId);
                else
                    return null; // we couldn't find it there, select from all

            }
            return list.ToArray();
        }
		protected IList<RavenFileSystemClient> GetCommandsToOperateOn(ShardRequestData resultionData)
		{
			return GetShardsToOperateOn(resultionData).Select(x => x.Item2).ToList();
		}
Пример #7
0
 protected IList <RavenFileSystemClient> GetCommandsToOperateOn(ShardRequestData resultionData)
 {
     return(GetShardsToOperateOn(resultionData).Select(x => x.Item2).ToList());
 }