Example #1
0
        public IList <Tuple <string, IAsyncFilesCommands> > GetShardsToOperateOn(ShardRequestData resultionData)
        {
            var shardIds = Strategy.ShardResolutionStrategy.PotentialShardsFor(resultionData);

            IEnumerable <KeyValuePair <string, IAsyncFilesCommands> > cmds = Clients;

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

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

            foreach (var shardId in shardIds)
            {
                IAsyncFilesCommands value;
                if (Clients.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 <IAsyncFilesCommands> commands, ShardRequestData request, Func <IAsyncFilesCommands, 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, IAsyncFilesCommands>> GetShardsToOperateOn(ShardRequestData resultionData)
        {
            var shardIds = Strategy.ShardResolutionStrategy.PotentialShardsFor(resultionData);

            IEnumerable<KeyValuePair<string, IAsyncFilesCommands>> cmds = Clients;

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

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

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

            }
            return list;
        }
Example #5
0
        public async Task <T[]> ApplyAsync <T>(IList <IAsyncFilesCommands> commands, ShardRequestData request, Func <IAsyncFilesCommands, int, Task <T> > operation)
        {
            await runFirst;

            var list   = new List <T>();
            var errors = new List <Exception>();

            for (int i = 0; i < commands.Count; i++)
            {
                try
                {
                    list.Add(await operation(commands[i], i));
                }
                catch (Exception e)
                {
                    var error = OnAsyncError;
                    if (error == null)
                    {
                        throw;
                    }
                    if (error(commands[i], request, e) == false)
                    {
                        throw;
                    }
                    errors.Add(e);
                }
            }

            // if ALL nodes failed, we still throw
            if (errors.Count == commands.Count)
            {
                throw new AggregateException(errors);
            }

            return(list.ToArray());
        }
        /// <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();
        }
Example #7
0
 protected IList <IAsyncFilesCommands> GetCommandsToOperateOn(ShardRequestData resultionData)
 {
     return(GetShardsToOperateOn(resultionData).Select(x => x.Item2).ToList());
 }
 protected IList<IAsyncFilesCommands> GetCommandsToOperateOn(ShardRequestData resultionData)
 {
     return GetShardsToOperateOn(resultionData).Select(x => x.Item2).ToList();
 }
 public IList<string> PotentialShardsFor(ShardRequestData requestData)
 {
     // for future use
     throw new NotImplementedException();
 }