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 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;
                }
            }
        }
Beispiel #4
0
 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);
     }
 }