Ejemplo n.º 1
0
        /// <summary>
        /// Constructs and queues a token pair rec with two original tokens.
        /// This is a 'join' of two event chains.
        /// </summary>
        /// <param name="eventId">The event id representing the join event of both tokens.</param>
        /// <param name="leftRecord">The original token.</param>
        /// <param name="rightRecord">The new token that represents the joined chain.</param>
        /// <param name="factory">An ITokenPairRecord factory(T1, T2, Ts).</param>
        /// <returns>The ITokenPairRecord that continues the chain.</returns>
        public ITokenPairRecord JoinTokens(byte eventId,
            ITokenPairRecord leftRecord, ITokenPairRecord rightRecord,
            Func<Token, Token, long, ITokenPairRecord> factory = null)
        {
            if (!_isEnabled
                || (leftRecord == SkippedTokenReference)
                || (rightRecord == SkippedTokenReference))
                return SkippedTokenReference;

            long timestamp = Stopwatch.GetTimestamp();
            var leftToken = leftRecord.T2;
            var rightToken = rightRecord.T2;
            leftToken.EventId = eventId;
            rightToken.EventId = eventId;
            var nextRecord = (factory ?? _defaultFactory)(leftToken, rightToken, timestamp);
            _processingQueue.Enqueue(nextRecord);
            return nextRecord;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Constructs and queues a new token with the original token as a token pair rec.
        /// This represents a 'split' in the event chain.
        /// </summary>
        /// <param name="eventId">The event id porition of the new token.</param>
        /// <param name="currentRecord">The original TokenRecord.</param>
        /// <param name="factory">An ITokenPairRecord factory(T1, T2, Ts).</param>
        /// <returns>A new ITokenPairRecord.</returns>
        public ITokenPairRecord SplitToken(byte eventId, ITokenPairRecord currentRecord,
            Func<Token, Token, long, ITokenPairRecord> factory = null)
        {
            if (!_isEnabled
                || (currentRecord == SkippedTokenReference)
                ||  ShouldNotSample())
                return SkippedTokenReference;

            var currentToken = currentRecord.T2;
            long timestamp = Stopwatch.GetTimestamp();
            long id = Interlocked.Increment(ref _previousId);
            var nextToken = new Token(_applicationId, eventId, (ulong)id);
            var nextRecord = (factory ?? _defaultFactory)(currentToken, nextToken, timestamp);
            _processingQueue.Enqueue(nextRecord);
            return nextRecord;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Changes the event state of the token.
        /// </summary>
        /// <param name="eventId">The new event id.</param>
        /// <param name="currentRecord">The token.</param>
        /// <param name="factory">An ITokenPairRecord factory(T1, T2, Ts).</param>
        /// <returns>A new ITokenPairRecord.</returns>
        public ITokenPairRecord AddEvent(byte eventId, ITokenPairRecord currentRecord,
            Func<Token, Token, long, ITokenPairRecord> factory = null)
        {
            if (!_isEnabled || (currentRecord == SkippedTokenReference))
                return SkippedTokenReference;

            long timestamp = Stopwatch.GetTimestamp();
            var currentToken = currentRecord.T2;
            var nextToken = new Token(currentToken.Value) { EventId = eventId };
            var nextRecord = (factory ?? _defaultFactory)(currentToken, nextToken, timestamp);
            _processingQueue.Enqueue(nextRecord);
            return nextRecord;
        }