コード例 #1
0
        public void Handle(UnArchiveMessagesByRange message)
        {
            var options = new BulkOperationOptions
            {
                AllowStale = true
            };
            var result = store.DatabaseCommands.UpdateByIndex(
                new FailedMessageViewIndex().IndexName,
                new IndexQuery
            {
                Query  = string.Format(CultureInfo.InvariantCulture, "LastModified:[{0} TO {1}] AND Status:{2}", message.From.Ticks, message.To.Ticks, (int)FailedMessageStatus.Archived),
                Cutoff = message.CutOff
            }, new ScriptedPatchRequest
            {
                Script = @"
if(this.Status === archivedStatus) {
    this.Status = unresolvedStatus;
}
",
                Values =
                {
                    { "archivedStatus",   (int)FailedMessageStatus.Archived   },
                    { "unresolvedStatus", (int)FailedMessageStatus.Unresolved }
                }
            }, options).WaitForCompletion();

            var patchedDocumentIds = result.JsonDeserialization <DocumentPatchResult[]>();

            domainEvents.Raise(new FailedMessagesUnArchived
            {
                MessagesCount = patchedDocumentIds.Length
            });
        }
コード例 #2
0
 public RavenJArray DeleteByIndex(string indexName, IndexQuery queryToDelete, BulkOperationOptions options = null)
 {
     return(PerformBulkOperation(indexName, queryToDelete, options, (docId, tx) =>
     {
         database.Documents.Delete(docId, null, tx);
         return new { Document = docId, Deleted = true };
     }));
 }
コード例 #3
0
        private HttpResponseMessage OnBulkOperation(Func <string, IndexQuery, BulkOperationOptions, Action <BulkOperationProgress>, RavenJArray> batchOperation, string index, CancellationTimeout timeout)
        {
            if (string.IsNullOrEmpty(index))
            {
                return(GetEmptyMessage(HttpStatusCode.BadRequest));
            }

            var option = new BulkOperationOptions
            {
                AllowStale      = GetAllowStale(),
                MaxOpsPerSec    = GetMaxOpsPerSec(),
                StaleTimeout    = GetStaleTimeout(),
                RetrieveDetails = GetRetrieveDetails()
            };

            var indexQuery = GetIndexQuery(maxPageSize: int.MaxValue);

            var  status = new BulkOperationStatus();
            long id;

            var task = Task.Factory.StartNew(() =>
            {
                using (DocumentCacher.SkipSetDocumentsInDocumentCache())
                {
                    status.State["Batch"] = batchOperation(index, indexQuery, option, x =>
                    {
                        status.MarkProgress(x);
                    });
                }
            }).ContinueWith(t =>
            {
                if (timeout != null)
                {
                    timeout.Dispose();
                }

                if (t.IsFaulted == false)
                {
                    status.MarkCompleted($"Processed {status.OperationProgress.ProcessedEntries} items");
                    return;
                }

                var exception = t.Exception.ExtractSingleInnerException();

                status.MarkFaulted(exception.Message);
            });

            Database.Tasks.AddTask(task, status, new TaskActions.PendingTaskDescription
            {
                StartTime   = SystemTime.UtcNow,
                TaskType    = TaskActions.PendingTaskType.IndexBulkOperation,
                Description = index
            }, out id, timeout.CancellationTokenSource);

            return(GetMessageWithObject(new { OperationId = id }, HttpStatusCode.Accepted));
        }
コード例 #4
0
        public static Operation DeleteByIndex <T>(
            this IDatabaseCommands databaseCommands,
            IndexQuery queryToDelete,
            BulkOperationOptions options = null)
            where T : AbstractIndexCreationTask
        {
            var indexName = typeof(T).Name.Replace("_", "/");

            return(databaseCommands.DeleteByIndex(indexName, queryToDelete, options));
        }
        private HttpResponseMessage OnBulkOperation(Func <string, IndexQuery, BulkOperationOptions, RavenJArray> batchOperation, string index, CancellationTimeout timeout)
        {
            if (string.IsNullOrEmpty(index))
            {
                return(GetEmptyMessage(HttpStatusCode.BadRequest));
            }

            var option = new BulkOperationOptions
            {
                AllowStale      = GetAllowStale(),
                MaxOpsPerSec    = GetMaxOpsPerSec(),
                StaleTimeout    = GetStaleTimeout(),
                RetrieveDetails = GetRetrieveDetails()
            };

            var indexQuery = GetIndexQuery(maxPageSize: int.MaxValue);

            var  status = new BulkOperationStatus();
            long id;

            var task = Task.Factory.StartNew(() =>
            {
                status.State = batchOperation(index, indexQuery, option);
            }).ContinueWith(t =>
            {
                if (timeout != null)
                {
                    timeout.Dispose();
                }

                if (t.IsFaulted == false)
                {
                    status.Completed = true;
                    return;
                }

                var exception = t.Exception.ExtractSingleInnerException();

                status.State     = RavenJObject.FromObject(new { Error = exception.Message });
                status.Faulted   = true;
                status.Completed = true;
            });

            Database.Tasks.AddTask(task, status, new TaskActions.PendingTaskDescription
            {
                StartTime = SystemTime.UtcNow,
                TaskType  = TaskActions.PendingTaskType.IndexBulkOperation,
                Payload   = index
            }, out id, timeout.CancellationTokenSource);

            return(GetMessageWithObject(new { OperationId = id }));
        }
コード例 #6
0
        public async Task RemoveEntriesOlderThan(DateTime dateTime, CancellationToken cancellationToken = default(CancellationToken))
        {
            var query = new IndexQuery
            {
                Query = $"Dispatched:true AND DispatchedAt:[* TO {dateTime:o}]"
            };

            var bulkOpts = new BulkOperationOptions
            {
                AllowStale = true
            };

            var operation = await documentStore.AsyncDatabaseCommands.DeleteByIndexAsync(indexName, query, bulkOpts, cancellationToken)
                            .ConfigureAwait(false);

            // This is going to execute multiple "status check" requests to Raven, but this does
            // not currently support CancellationToken.
            await operation.WaitForCompletionAsync().ConfigureAwait(false);
        }
コード例 #7
0
        private RavenJArray PerformBulkOperation(string index, IndexQuery indexQuery, BulkOperationOptions options, Func <string, TransactionInformation, object> batchOperation)
        {
            options = options ?? new BulkOperationOptions();
            var array          = new RavenJArray();
            var bulkIndexQuery = new IndexQuery
            {
                Query  = indexQuery.Query,
                Start  = indexQuery.Start,
                Cutoff = indexQuery.Cutoff ?? SystemTime.UtcNow,
                WaitForNonStaleResultsAsOfNow = indexQuery.WaitForNonStaleResultsAsOfNow,
                PageSize            = int.MaxValue,
                FieldsToFetch       = new[] { Constants.DocumentIdFieldName },
                SortedFields        = indexQuery.SortedFields,
                HighlighterPreTags  = indexQuery.HighlighterPreTags,
                HighlighterPostTags = indexQuery.HighlighterPostTags,
                HighlightedFields   = indexQuery.HighlightedFields,
                SortHints           = indexQuery.SortHints
            };

            bool stale;
            var  queryResults = database.Queries.QueryDocumentIds(index, bulkIndexQuery, tokenSource, out stale);

            if (stale && options.AllowStale == false)
            {
                if (options.StaleTimeout != null)
                {
                    var staleWaitTimeout = Stopwatch.StartNew();
                    while (stale && staleWaitTimeout.Elapsed < options.StaleTimeout)
                    {
                        queryResults = database.Queries.QueryDocumentIds(index, bulkIndexQuery, tokenSource, out stale);
                        if (stale)
                        {
                            SystemTime.Wait(100);
                        }
                    }
                }
                if (stale)
                {
                    if (options.StaleTimeout != null)
                    {
                        throw new InvalidOperationException("Bulk operation cancelled because the index is stale and StaleTimout  of " + options.StaleTimeout + "passed");
                    }

                    throw new InvalidOperationException("Bulk operation cancelled because the index is stale and allowStale is false");
                }
            }

            var       token        = tokenSource.Token;
            const int batchSize    = 1024;
            int       maxOpsPerSec = options.MaxOpsPerSec ?? int.MaxValue;

            using (var enumerator = queryResults.GetEnumerator())
            {
                var duration   = Stopwatch.StartNew();
                var operations = 0;
                while (true)
                {
                    database.WorkContext.UpdateFoundWork();
                    if (timeout != null)
                    {
                        timeout.Delay();
                    }
                    var batchCount    = 0;
                    var shouldWaitNow = false;
                    token.ThrowIfCancellationRequested();
                    using (database.DocumentLock.Lock())
                    {
                        database.TransactionalStorage.Batch(actions =>
                        {
                            while (batchCount < batchSize && enumerator.MoveNext())
                            {
                                batchCount++;
                                operations++;
                                var result = batchOperation(enumerator.Current, transactionInformation);

                                if (options.RetrieveDetails)
                                {
                                    array.Add(RavenJObject.FromObject(result));
                                }

                                if (operations >= maxOpsPerSec && duration.ElapsedMilliseconds < 1000)
                                {
                                    shouldWaitNow = true;
                                    break;
                                }
                            }
                        });
                        if (shouldWaitNow)
                        {
                            SystemTime.Wait(500);
                            operations = 0;
                            duration.Restart();
                            continue;
                        }
                    }
                    if (batchCount < batchSize)
                    {
                        break;
                    }
                }
            }
            return(array);
        }
コード例 #8
0
 public RavenJArray UpdateByIndex(string indexName, IndexQuery queryToUpdate, ScriptedPatchRequest patch, BulkOperationOptions options = null)
 {
     return(PerformBulkOperation(indexName, queryToUpdate, options, (docId, tx) =>
     {
         var patchResult = database.Patches.ApplyPatch(docId, null, patch, tx);
         return new { Document = docId, Result = patchResult.Item1, Debug = patchResult.Item2 };
     }));
 }
コード例 #9
0
 public Operation UpdateByIndex(string indexName, IndexQuery queryToUpdate, ScriptedPatchRequest patch,
                                BulkOperationOptions options = null)
 {
     return(AsyncHelpers.RunSync(() => asyncServerClient.UpdateByIndexAsync(indexName, queryToUpdate, patch, options)));
 }
コード例 #10
0
 public Operation DeleteByIndex(string indexName, IndexQuery queryToDelete, BulkOperationOptions options = null)
 {
     return(AsyncHelpers.RunSync(() => asyncServerClient.DeleteByIndexAsync(indexName, queryToDelete, options)));
 }
コード例 #11
0
 public RavenJArray UpdateByIndex(string indexName, IndexQuery queryToUpdate, PatchRequest[] patchRequests, BulkOperationOptions options = null, Action <BulkOperationProgress> reportProgress = null)
 {
     return(PerformBulkOperation(indexName, queryToUpdate, options, (docId, tx) =>
     {
         var patchResult = database.Patches.ApplyPatch(docId, null, patchRequests, tx);
         return new { Document = docId, Result = patchResult };
     }, reportProgress));
 }
コード例 #12
0
ファイル: ServerClient.cs プロジェクト: tryadiadi/ravendb
 public Operation UpdateByIndex(string indexName, IndexQuery queryToUpdate, ScriptedPatchRequest patch,
                                BulkOperationOptions options = null)
 {
     return(asyncServerClient.UpdateByIndexAsync(indexName, queryToUpdate, patch, options).ResultUnwrap());
 }
コード例 #13
0
ファイル: ServerClient.cs プロジェクト: tryadiadi/ravendb
 public Operation DeleteByIndex(string indexName, IndexQuery queryToDelete, BulkOperationOptions options = null)
 {
     return(asyncServerClient.DeleteByIndexAsync(indexName, queryToDelete, options).ResultUnwrap());
 }