Пример #1
0
 private CorrelationVectorV2(string baseVector, int extension, bool immutable)
 {
     this.BaseVector = baseVector;
     this.extension  = extension;
     this.Version    = CorrelationVectorVersion.V2;
     this.immutable  = immutable || CorrelationVectorV2.IsOversized(baseVector, extension);
 }
Пример #2
0
        /// <summary>
        /// Increments the current extension by one. Do this before passing the value to an
        /// outbound message header.
        /// </summary>
        /// <returns>
        /// The new value as a string that you can add to the outbound message header
        /// indicated by <see cref="HeaderName"/>.
        /// </returns>
        public override string Increment()
        {
            if (this.immutable)
            {
                return(this.Value);
            }
            int snapshot = 0;
            int next     = 0;

            do
            {
                snapshot = this.extension;
                if (snapshot == int.MaxValue)
                {
                    return(this.Value);
                }
                next = snapshot + 1;
                if (CorrelationVectorV2.IsOversized(this.BaseVector, next))
                {
                    this.immutable = true;
                    return(this.Value);
                }
            }while (snapshot != Interlocked.CompareExchange(ref this.extension, next, snapshot));
            return(string.Concat(this.BaseVector, ".", next));
        }
Пример #3
0
        public static CorrelationVector Spin(string correlationVector, SpinParameters parameters)
        {
            CorrelationVectorVersion version = InferVersion(correlationVector);

            switch (version)
            {
            case CorrelationVectorVersion.V1:
                return(CorrelationVectorV1.Spin(correlationVector, parameters));

            case CorrelationVectorVersion.V2:
                return(CorrelationVectorV2.Spin(correlationVector, parameters));

            default:
                return(null);
            }
        }
Пример #4
0
        /// <summary>
        /// Creates a new correlation vector by applying the Spin operator to an existing value.
        /// This should be done at the entry point of an operation.
        /// </summary>
        /// <param name="correlationVector">
        /// Taken from the message header indicated by <see cref="HeaderName"/>.
        /// </param>
        /// <param name="parameters">
        /// The parameters to use when applying the Spin operator.
        /// </param>
        /// <returns>A new correlation vector extended from the current vector.</returns>
        public new static CorrelationVectorV2 Spin(string correlationVector, SpinParameters parameters)
        {
            if (CorrelationVectorV2.IsImmutable(correlationVector))
            {
                return(CorrelationVectorV2.Parse(correlationVector));
            }

            if (CorrelationVectorV2.ValidateCorrelationVectorDuringCreation)
            {
                CorrelationVectorV2.Validate(correlationVector);
            }

            byte[] entropy = new byte[parameters.EntropyBytes];
            rng.NextBytes(entropy);

            ulong value = (ulong)(DateTime.UtcNow.Ticks >> parameters.TicksBitsToDrop);

            for (int i = 0; i < parameters.EntropyBytes; i++)
            {
                value = (value << 8) | Convert.ToUInt64(entropy[i]);
            }

            // Generate a bitmask and mask the lower TotalBits in the value.
            // The mask is generated by (1 << TotalBits) - 1. We need to handle the edge case
            // when shifting 64 bits, as it wraps around.
            value &= (parameters.TotalBits == 64 ? 0 : (ulong)1 << parameters.TotalBits) - 1;

            string s = unchecked ((uint)value).ToString();

            if (parameters.TotalBits > 32)
            {
                s = string.Concat((value >> 32).ToString(), ".", s);
            }

            string baseVector = string.Concat(correlationVector, ".", s);

            if (CorrelationVectorV2.IsOversized(baseVector, 0))
            {
                return(CorrelationVectorV2.Parse(correlationVector + CorrelationVectorV2.TerminationSign));
            }

            return(new CorrelationVectorV2(baseVector, 0, false));
        }
Пример #5
0
        /// <summary>
        /// Creates a new correlation vector by extending an existing value. This should be
        /// done at the entry point of an operation.
        /// </summary>
        /// <param name="correlationVector">
        /// Taken from the message header indicated by <see cref="HeaderName"/>.
        /// </param>
        /// <returns>A new correlation vector extended from the current vector.</returns>
        public new static CorrelationVectorV2 Extend(string correlationVector)
        {
            if (CorrelationVectorV2.IsImmutable(correlationVector))
            {
                return(CorrelationVectorV2.Parse(correlationVector));
            }

            if (CorrelationVectorV2.ValidateCorrelationVectorDuringCreation)
            {
                CorrelationVectorV2.Validate(correlationVector);
            }

            if (CorrelationVectorV2.IsOversized(correlationVector, 0))
            {
                return(CorrelationVectorV2.Parse(correlationVector + CorrelationVectorV2.TerminationSign));
            }

            return(new CorrelationVectorV2(correlationVector, 0, false));
        }
Пример #6
0
        /// <summary>
        /// Creates a new correlation vector by parsing its string representation
        /// </summary>
        /// <param name="correlationVector">correlationVector</param>
        /// <returns>CorrelationVector</returns>
        public new static CorrelationVectorV2 Parse(string correlationVector)
        {
            if (!string.IsNullOrEmpty(correlationVector))
            {
                int  p         = correlationVector.LastIndexOf('.');
                bool immutable = CorrelationVectorV2.IsImmutable(correlationVector);
                if (p > 0)
                {
                    string extensionValue = immutable ? correlationVector.Substring(p + 1, correlationVector.Length - p - 1 - CorrelationVectorV2.TerminationSign.Length)
                        : correlationVector.Substring(p + 1);
                    int extension;
                    if (int.TryParse(extensionValue, out extension) && extension >= 0)
                    {
                        return(new CorrelationVectorV2(correlationVector.Substring(0, p), extension, immutable));
                    }
                }
            }

            return(new CorrelationVectorV2());
        }
Пример #7
0
 /// <summary>
 /// Determines whether two instances of the <see cref="CorrelationVectorV2"/> class
 /// are equal.
 /// </summary>
 /// <param name="vector">
 /// The correlation vector you want to compare with the current correlation vector.
 /// </param>
 /// <returns>
 /// True if the specified correlation vector is equal to the current correlation
 /// vector; otherwise, false.
 /// </returns>
 public bool Equals(CorrelationVectorV2 vector)
 {
     return(string.Equals(this.Value, vector.Value, StringComparison.Ordinal));
 }
Пример #8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CorrelationVectorV2"/> class.
 /// This should only be called when no correlation
 /// vector was found in the message header.
 /// </summary>
 public CorrelationVectorV2()
     : this(CorrelationVectorV2.GetUniqueValue(), 0, false)
 {
 }