protected override Task <QueryOperation> ExecuteActualQueryAsync()
        {
            var results = shardStrategy.ShardAccessStrategy.ApplyAsync(ShardDatabaseCommands,
                                                                       new ShardRequestData
            {
                EntityType = typeof(T),
                Query      = IndexQuery,
                IndexName  = indexName
            }, (commands, i) =>
            {
                var queryOp = shardQueryOperations[i];

                var queryContext = queryOp.EnterQueryContext();
                return(commands.QueryAsync(indexName, queryOp.IndexQuery)
                       .ContinueWith(task =>
                {
                    if (queryContext != null)
                    {
                        queryContext.Dispose();
                    }

                    queryOp.EnsureIsAcceptable(task.Result);

                    return task.Result;
                }));
            });

            return(results.ContinueWith(task =>
            {
                task.AssertNotFailed();

                ShardedDocumentQuery <T> .AssertNoDuplicateIdsInResults(shardQueryOperations);

                var mergedQueryResult = shardStrategy.MergeQueryResults(IndexQuery, shardQueryOperations.Select(x => x.CurrentQueryResults).ToList());

                shardQueryOperations[0].ForceResult(mergedQueryResult);
                queryOperation = shardQueryOperations[0];

                return queryOperation;
            }));
        }
Example #2
0
        protected override Task <QueryOperation> ExecuteActualQueryAsync()
        {
            var results = CompletedTask.With(new bool[ShardDatabaseCommands.Count]).Task;

            Func <Task> loop = null;

            loop = () =>
            {
                var lastResults = results.Result;

                results = shardStrategy.ShardAccessStrategy.ApplyAsync(ShardDatabaseCommands,
                                                                       new ShardRequestData
                {
                    EntityType = typeof(T),
                    Query      = IndexQuery,
                    IndexName  = indexName
                }, (commands, i) =>
                {
                    if (lastResults[i])                             // if we already got a good result here, do nothing
                    {
                        return(CompletedTask.With(true));
                    }

                    var queryOp = shardQueryOperations[i];

                    var queryContext = queryOp.EnterQueryContext();
                    return(commands.QueryAsync(indexName, queryOp.IndexQuery, includes.ToArray())
                           .ContinueWith(task =>
                    {
                        if (queryContext != null)
                        {
                            queryContext.Dispose();
                        }

                        return queryOp.IsAcceptable(task.Result);
                    }));
                });

                return(results.ContinueWith(task =>
                {
                    task.AssertNotFailed();

                    if (lastResults.All(acceptable => acceptable))
                    {
                        return new CompletedTask().Task;
                    }

                    Thread.Sleep(100);

                    return loop();
                }).Unwrap());
            };

            return(loop().ContinueWith(task =>
            {
                task.AssertNotFailed();

                ShardedDocumentQuery <T> .AssertNoDuplicateIdsInResults(shardQueryOperations);

                var mergedQueryResult = shardStrategy.MergeQueryResults(IndexQuery, shardQueryOperations.Select(x => x.CurrentQueryResults).ToList());

                shardQueryOperations[0].ForceResult(mergedQueryResult);
                queryOperation = shardQueryOperations[0];

                return queryOperation;
            }));
        }