public ArchiveOperation CreateArchiveOperation(IDocumentSession session, string groupId, ArchiveType archiveType, DateTime?cutOff, int numberOfMessages, string groupName, int batchSize) { var operation = new ArchiveOperation { Id = ArchiveOperation.MakeId(groupId, archiveType), RequestId = groupId, ArchiveType = archiveType, TotalNumberOfMessages = numberOfMessages, NumberOfMessagesArchived = 0, Started = DateTime.Now, GroupName = groupName, NumberOfBatches = (int)Math.Ceiling(numberOfMessages / (float)batchSize), CurrentBatch = 0 }; session.Store(operation); int documentCount = 0; var indexQuery = session.Query <FailureGroupMessageView>(new FailedMessages_ByGroup().IndexName); if (cutOff.HasValue) { indexQuery = indexQuery.Customize(x => x.WaitForNonStaleResultsAsOf(cutOff.Value)); } var docQuery = indexQuery .Where(failure => failure.FailureGroupId == groupId) .Where(failure => failure.Status == FailedMessageStatus.Unresolved) .AsProjection <FailureGroupMessageView>() .Select(document => document.Id); var docs = StreamResults(session, docQuery).ToArray(); var batches = docs .GroupBy(d => { return(documentCount++ / batchSize); }); foreach (var batch in batches) { var archiveBatch = new ArchiveBatch { Id = ArchiveBatch.MakeId(groupId, archiveType, batch.Key), DocumentIds = batch.ToList() }; session.Store(archiveBatch); } return(operation); }
public async Task <ArchiveOperation> CreateArchiveOperation(IAsyncDocumentSession session, string groupId, ArchiveType archiveType, int numberOfMessages, string groupName, int batchSize) { var operation = new ArchiveOperation { Id = ArchiveOperation.MakeId(groupId, archiveType), RequestId = groupId, ArchiveType = archiveType, TotalNumberOfMessages = numberOfMessages, NumberOfMessagesArchived = 0, Started = DateTime.Now, GroupName = groupName, NumberOfBatches = (int)Math.Ceiling(numberOfMessages / (float)batchSize), CurrentBatch = 0 }; await session.StoreAsync(operation).ConfigureAwait(false); var documentCount = 0; var indexQuery = session.Query <FailureGroupMessageView>(new FailedMessages_ByGroup().IndexName); var docQuery = indexQuery .Where(failure => failure.FailureGroupId == groupId) .Where(failure => failure.Status == FailedMessageStatus.Unresolved) .Select(document => document.Id); var docs = await StreamResults(session, docQuery).ConfigureAwait(false); var batches = docs .GroupBy(d => documentCount++ / batchSize); foreach (var batch in batches) { var archiveBatch = new ArchiveBatch { Id = ArchiveBatch.MakeId(groupId, archiveType, batch.Key), DocumentIds = batch.ToList() }; await session.StoreAsync(archiveBatch).ConfigureAwait(false); } return(operation); }