public virtual Task <MessageJournalReadResult> Read(MessageJournalPosition start, int count, MessageJournalFilter filter = null, CancellationToken cancellationToken = default(CancellationToken)) { var myStart = (Position)start; IList <MessageJournalEntry> myEntries; lock (_syncRoot) { myEntries = _entries.Skip(myStart.Index) .Where(entry => MatchesFilter(filter, entry)) .Take(count + 1) .ToList(); } var next = myStart; var endOfJournal = myEntries.Count <= count; if (myEntries.Any()) { var lastIndex = myEntries.Select(e => e.Position).OfType <Position>().Max(p => p.Index); next = new Position(lastIndex + 1); } var readResult = new MessageJournalReadResult(start, next, endOfJournal, myEntries.Take(count)); return(Task.FromResult(readResult)); }
/// <inheritdoc /> public override Task <MessageJournalReadResult> Read(MessageJournalPosition start, int count, MessageJournalFilter filter = null, CancellationToken cancellationToken = new CancellationToken()) { CheckDisposed(); return(_commandExecutor.ExecuteRead( () => base.Read(start, count, filter, cancellationToken), cancellationToken)); }
protected async Task WhenReadingToEndOfJournal() { MessageJournalReadResult result; do { result = await MessageJournal.Read(Start, Count, Filter); Pages.Add(result); Start = result.Next; } while (!result.EndOfJournal); }
public async Task Update(string consumer, MessageJournalPosition next) { if (string.IsNullOrWhiteSpace(consumer)) { throw new ArgumentNullException(nameof(consumer)); } if (next == null) { throw new ArgumentNullException(nameof(next)); } var filter = Builders <JournalConsumerProgress> .Filter.Eq(x => x.Consumer, consumer.Trim().ToLower()); var update = Builders <JournalConsumerProgress> .Update.Set(sp => sp.Next, next.ToString()); await _collection.UpdateOneAsync(filter, update, new UpdateOptions { IsUpsert = true }); }
/// <inheritdoc /> public async Task <MessageJournalReadResult> Read(MessageJournalPosition start, int count, MessageJournalFilter filter = null, CancellationToken cancellationToken = new CancellationToken()) { var fb = Builders <MessageJournalEntryDocument> .Filter; var filterDef = fb.Gte(mje => mje.Id, ((MongoDBMessageJournalPosition)start).Id); filterDef = BuildFilter(filter, filterDef, fb); var options = new FindOptions(); if (_collationSupported) { options.Collation = _collation; } var entryDocuments = await _messageJournalEntries.Find(filterDef, options) .Limit(count + 1) .ToListAsync(cancellationToken); var endOfJournal = entryDocuments.Count <= count; var nextId = entryDocuments.Select(e => e.Id).LastOrDefault(); if (endOfJournal) { nextId = new ObjectId(nextId.Timestamp, nextId.Machine, nextId.Pid, nextId.Increment + 1); } var nextPosition = new MongoDBMessageJournalPosition(nextId); var entries = new List <MessageJournalEntry>(); foreach (var entryDocument in entryDocuments.Take(count)) { var position = new MongoDBMessageJournalPosition(entryDocument.Id); var timestamp = entryDocument.Timestamp; var category = entryDocument.Category; var headers = new MessageHeaders(entryDocument.Headers); var message = new Message(headers, entryDocument.Content); entries.Add(new MessageJournalEntry(category, position, timestamp, message)); } return(new MessageJournalReadResult(start, nextPosition, endOfJournal, entries)); }
protected async Task GivenAtBeginningOfJournal() { Start = await MessageJournal.GetBeginningOfJournal(); }
/// <inheritdoc /> public virtual async Task <MessageJournalReadResult> Read(MessageJournalPosition start, int count, MessageJournalFilter filter = null, CancellationToken cancellationToken = default(CancellationToken)) { var myFilter = filter ?? new MessageJournalFilter(); var next = start; var journaledMessages = new List <MessageJournalEntry>(); var endOfJournal = true; var connection = ConnectionProvider.GetConnection(); try { var commandBuilder = CommandBuilders.NewSelectJournaledMessagesCommandBuilder(); commandBuilder.Categories = myFilter.Categories.Select(c => (string)c).ToList(); commandBuilder.Topics = myFilter.Topics.Select(t => (string)t).ToList(); commandBuilder.From = myFilter.From; commandBuilder.To = myFilter.To; commandBuilder.Origination = myFilter.Origination; commandBuilder.Destination = myFilter.Destination; commandBuilder.RelatedTo = myFilter.RelatedTo; commandBuilder.MessageName = myFilter.MessageName; commandBuilder.Start = ((SQLMessageJournalPosition)start).Id; commandBuilder.Count = count + 1; using (var scope = new TransactionScope(TransactionScopeOption.Suppress, TransactionScopeAsyncFlowOption.Enabled)) { using (var command = commandBuilder.BuildDbCommand(connection)) { using (var reader = await command.ExecuteReaderAsync(cancellationToken)) { while (await reader.ReadAsync(cancellationToken)) { var record = commandBuilder.BuildJournaledMessageRecord(reader); next = new SQLMessageJournalPosition(record.Id); if (journaledMessages.Count < count) { var category = record.Category; var timestamp = record.Timestamp; var headers = DeserializeHeaders(record.Headers); var messageContent = record.Content; var offset = new SQLMessageJournalPosition(record.Id); var message = new Message(headers, messageContent); var journaledMessage = new MessageJournalEntry(category, offset, timestamp, message); journaledMessages.Add(journaledMessage); next = new SQLMessageJournalPosition(record.Id + 1); } else { endOfJournal = false; } } } } scope.Complete(); } } finally { ConnectionProvider.ReleaseConnection(connection); } return(new MessageJournalReadResult(start, next, endOfJournal, journaledMessages)); }