/// <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 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 (CorrelationVector.IsOversized(this.BaseVector, next, this.Version)) { this.immutable = true; return(this.Value); } }while (snapshot != Interlocked.CompareExchange(ref this.extension, next, snapshot)); return(string.Concat(this.BaseVector, ".", next)); }
private CorrelationVector(string baseVector, int extension, CorrelationVectorVersion version, bool immutable) { this.BaseVector = baseVector; this.extension = extension; this.Version = version; this.immutable = immutable || CorrelationVector.IsOversized(baseVector, extension, version); }
/// <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 static CorrelationVector Spin(string correlationVector, SpinParameters parameters) { if (CorrelationVector.IsImmutable(correlationVector)) { return(CorrelationVector.Parse(correlationVector)); } CorrelationVectorVersion version = CorrelationVector.InferVersion( correlationVector, CorrelationVector.ValidateCorrelationVectorDuringCreation); if (CorrelationVector.ValidateCorrelationVectorDuringCreation) { CorrelationVector.Validate(correlationVector, version); } 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 (CorrelationVector.IsOversized(baseVector, 0, version)) { return(CorrelationVector.Parse(correlationVector + CorrelationVector.TerminationSign)); } return(new CorrelationVector(baseVector, 0, version, false)); }
/// <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 static CorrelationVector Extend(string correlationVector) { if (CorrelationVector.IsImmutable(correlationVector)) { return(CorrelationVector.Parse(correlationVector)); } CorrelationVectorVersion version = CorrelationVector.InferVersion( correlationVector, CorrelationVector.ValidateCorrelationVectorDuringCreation); if (CorrelationVector.ValidateCorrelationVectorDuringCreation) { CorrelationVector.Validate(correlationVector, version); } if (CorrelationVector.IsOversized(correlationVector, 0, version)) { return(CorrelationVector.Parse(correlationVector + CorrelationVector.TerminationSign)); } return(new CorrelationVector(correlationVector, 0, version, false)); }