bool ReadNextMessage(IPositionedMessagesParser parser) { var tmp = parser.ReadNextAndPostprocess(); lastReadMessage = tmp.Message; return(lastReadMessage != null); }
public static long?FindPrevMessagePosition(IPositionedMessagesReader reader, long originalMessagePos) { long nextMessagePos; using (IPositionedMessagesParser p = reader.CreateParser(new CreateParserParams(originalMessagePos, null, MessagesParserFlag.HintMessageContentIsNotNeeed | MessagesParserFlag.HintMessageContentIsNotNeeed, MessagesParserDirection.Forward))) { var msgAtOriginalPos = p.ReadNext(); if (msgAtOriginalPos != null) { nextMessagePos = msgAtOriginalPos.Position; } else { nextMessagePos = reader.EndPosition; } } using (IPositionedMessagesParser p = reader.CreateParser(new CreateParserParams(nextMessagePos, null, MessagesParserFlag.HintMessageContentIsNotNeeed | MessagesParserFlag.HintMessageContentIsNotNeeed, MessagesParserDirection.Backward))) { IMessage msg = p.ReadNext(); if (msg != null) { return(msg.Position); } return(null); } }
/// <summary> /// Finds the first and the last available messages in the reader. /// </summary> /// <param name="reader">Messages reader to read from</param> /// <param name="cachedFirstMessage">When the caller passes non-null value /// the function doesn't search for the first message in the reader and return the /// value precalculated by the client instead. That can be user for optimization: /// if the client is sure that the first message didn't change then it can /// pass the value calculated before. If <paramref name="firstMessage"/> is <value>null</value> /// the function will search for the first message in the reader.</param> /// <param name="firstMessage">When the function returns <paramref name="firstMessage"/> receives /// the message with the smallest available position.</param> /// <param name="lastMessage">When the function returns /// <paramref name="lastMessage"/> receives the message with the largest available position.</param> public static void GetBoundaryMessages( IPositionedMessagesReader reader, IMessage cachedFirstMessage, out IMessage firstMessage, out IMessage lastMessage) { if (cachedFirstMessage == null) { firstMessage = ReadNearestMessage(reader, reader.BeginPosition); } else { firstMessage = cachedFirstMessage; } lastMessage = firstMessage; using (IPositionedMessagesParser parser = reader.CreateParser(new CreateParserParams(reader.EndPosition, null, MessagesParserFlag.Default, MessagesParserDirection.Backward))) { IMessage tmp = parser.ReadNext(); if (tmp != null) { lastMessage = tmp; } } }
public static long?FindNextMessagePosition(IPositionedMessagesReader reader, long originalMessagePos) { // Validate the input. if (originalMessagePos < reader.BeginPosition) { return(null); } if (originalMessagePos >= reader.EndPosition) { return(null); } using (IPositionedMessagesParser parser = reader.CreateParser(new CreateParserParams(originalMessagePos, null, MessagesParserFlag.HintMessageContentIsNotNeeed | MessagesParserFlag.HintMessageTimeIsNotNeeded, MessagesParserDirection.Forward, null))) { if (parser.ReadNext() == null) { return(null); } IMessage p = parser.ReadNext(); if (p == null) { return(null); } return(p.Position); } }
public static IEnumerable <IMessage> ParserAsEnumerator(IPositionedMessagesParser parser, ParserAsEnumeratorFlag flags = ParserAsEnumeratorFlag.Default, Action <Exception> readExceptionHandler = null) { for (; ;) { IMessage msg = null; try { msg = parser.ReadNext(); } catch (Exception e) { if (readExceptionHandler != null) { readExceptionHandler(e); } else { throw; } } if (msg == null) { if ((flags & ParserAsEnumeratorFlag.YieldLastNullMessage) != 0) { yield return(msg); } break; } yield return(msg); } }
static public IMessage ReadNearestMessage(IPositionedMessagesReader reader, long position, MessagesParserFlag flags) { using (IPositionedMessagesParser parser = reader.CreateParser(new CreateParserParams(position, null, flags, MessagesParserDirection.Forward))) { IMessage ret = parser.ReadNext(); return(ret); } }
void CreateUnderlyingParserAndInitJitterBuffer(Func <CreateParserParams, IPositionedMessagesParser> underlyingParserFactory) { CreateParserParams reversedParserParams = originalParams; reversedParserParams.Range = null; reversedParserParams.Direction = GetOppositeDirection(originalParams.Direction); reversedParserParams.Flags |= MessagesParserFlag.DisableMultithreading; int reversedMessagesQueued = 0; using (IPositionedMessagesParser reversedParser = underlyingParserFactory(reversedParserParams)) { var tmp = new List <PostprocessedMessage>(); for (int i = 0; i < jitterBufferSize; ++i) { var tmpMsg = reversedParser.ReadNextAndPostprocess(); if (tmpMsg.Message == null) { break; } tmp.Add(tmpMsg); } tmp.Reverse(); foreach (var tmpMsg in tmp) { jitterBuffer.Enqueue(new Entry() { data = tmpMsg, index = currentIndex++ }); positionsBuffer.Push(new MessagesPositions(tmpMsg.Message)); ++reversedMessagesQueued; } } enumerator = ReadAddMessagesFromRangeCompleteJitterBuffer(underlyingParserFactory).GetEnumerator(); for (int i = 0; i < jitterBufferSize; ++i) { var tmp = LoadNextMessage(); reversedMessagesQueued -= tmp.DequeuedMessages; if (tmp.LoadedMessage == null) { break; } } for (int i = 0; i < reversedMessagesQueued && jitterBuffer.Count > 0; ++i) { jitterBuffer.Dequeue(); positionsBuffer.Pop(); } }
void ReadNextMessage(IPositionedMessagesParser parser) { try { var tmp = parser.ReadNextAndPostprocess(); lastReadMessage = tmp.Message; if (lastReadMessage == null) { breakAlgorithm = true; } } catch (Exception e) { loadError = e; breakAlgorithm = true; } }
void FillCacheRanges(CancellationToken cancellationToken) { using (tracer.NewFrame) using (var perfop = new Profiling.Operation(tracer, "FillRanges")) { bool updateStarted = false; // Iterate through the ranges for (; ;) { cancellationToken.ThrowIfCancellationRequested(); currentRange = buffer.GetNextRangeToFill(); if (currentRange == null) // Nothing to fill { break; } currentMessagesContainer = buffer; tracer.Info("currentRange={0}", currentRange); bool failed = false; try { if (!updateStarted) { tracer.Info("Starting to update the messages."); updateStarted = true; } long messagesRead = 0; ResetFlags(); // Start reading elements using (IPositionedMessagesParser parser = reader.CreateParser(new CreateParserParams( currentRange.GetPositionToStartReadingFrom(), currentRange.DesirableRange, MessagesParserFlag.HintParserWillBeUsedForMassiveSequentialReading, MessagesParserDirection.Forward, postprocessor: null, cancellation: cancellationToken))) { tracer.Info("parser created"); for (; ;) { cancellationToken.ThrowIfCancellationRequested(); ResetFlags(); if (!ReadNextMessage(parser)) { cancellationToken.ThrowIfCancellationRequested(); break; } ProcessLastReadMessageAndFlush(); ++messagesRead; } } tracer.Info("reading finished"); FlushBuffer(); } catch { failed = true; throw; } finally { if (!failed) { tracer.Info("Loading of the range finished successfully. Completing the range."); currentRange.Complete(); tracer.Info("Disposing the range."); } else { tracer.Info("Loading failed. Disposing the range without completion."); } currentRange.Dispose(); currentRange = null; currentMessagesContainer = null; perfop.Milestone("range completed"); } } UpdateLoadedTimeStats(reader); if (updateStarted) { perfop.Milestone("great success"); owner.SetMessagesCache(new AsyncLogProviderDataCache() { Messages = new MessagesContainers.ListBasedCollection( buffer.Forward(0, int.MaxValue).Select(m => m.Message)), MessagesRange = buffer.ActiveRange, }); } } }
void FillCacheRanges(CancellationToken preemptionToken) { using (tracer.NewFrame) using (var perfop = new Profiling.Operation(tracer, "FillRanges")) { bool updateStarted = false; try { // Iterate through the ranges for (; ;) { currentRange = buffer.GetNextRangeToFill(); if (currentRange == null) // Nothing to fill { break; } currentMessagesContainer = buffer; tracer.Info("currentRange={0}", currentRange); try { if (!updateStarted) { tracer.Info("Starting to update the messages."); updateStarted = true; } long messagesRead = 0; ResetFlags(); // Start reading elements using (IPositionedMessagesParser parser = reader.CreateParser(new CreateParserParams( currentRange.GetPositionToStartReadingFrom(), currentRange.DesirableRange, MessagesParserFlag.HintParserWillBeUsedForMassiveSequentialReading, MessagesParserDirection.Forward))) { tracer.Info("parser created"); for (; ;) { if (preemptionToken.IsCancellationRequested) { loadingInterrupted = true; preemptionToken.ThrowIfCancellationRequested(); } ResetFlags(); ReadNextMessage(parser); ProcessLastReadMessageAndFlush(); ReportLoadErrorIfAny(); if (breakAlgorithm) { break; } ++messagesRead; } } tracer.Info("reading finished"); FlushBuffer(); } finally { if (!loadingInterrupted) { tracer.Info("Loading of the range finished completly. Completing the range."); currentRange.Complete(); tracer.Info("Disposing the range."); } else { tracer.Info("Loading was interrupted. Disposing the range without completion."); } currentRange.Dispose(); currentRange = null; currentMessagesContainer = null; perfop.Milestone("range completed"); } if (loadingInterrupted) { tracer.Info("Loading interrupted. Stopping to read ranges."); break; } } UpdateLoadedTimeStats(reader); } catch (Exception e) { tracer.Error(e, "failed to update cache"); loadingInterrupted = true; } finally { if (updateStarted) { tracer.Info("Finishing to update the messages."); if (!loadingInterrupted) { owner.SetMessagesCache(new AsyncLogProviderDataCache() { Messages = new MessagesContainers.ListBasedCollection( buffer.Forward(0, int.MaxValue).Select(m => m.Message)), MessagesRange = buffer.ActiveRange, AvailableRange = currentStats.PositionsRange, AvailableTime = currentStats.AvailableTime, }); } } else { tracer.Info("There were no ranges to read"); } } } }