Exemple #1
0
        /// <summary>
        /// Gets the string representation of the ksuid.
        /// </summary>
        public override string ToString()
        {
            if (str != null)
            {
                return(str);
            }

            var sb = new StringBuilder();

            sb.Append(Environment == "prod" ? "" : $"{Environment}_");
            sb.Append(Resource);
            sb.Append("_");

            var decoded    = new byte[Constants.DecodedLength];
            var timestamp  = BitConverter.GetBytes(Timestamp);
            var instance   = InstanceIdentifier.ToByteArray();
            var sequenceId = BitConverter.GetBytes(SequenceId);

            if (BitConverter.IsLittleEndian)
            {
                Array.Reverse(timestamp);
                Array.Reverse(sequenceId);
            }

            Array.Copy(timestamp, decoded, 8);
            Array.Copy(instance, 0, decoded, 8, 9);
            Array.Copy(sequenceId, 0, decoded, 17, 4);

            sb.Append(Base62.Encode(decoded).PadLeft(Constants.EncodedLength, '0'));

            return(str = sb.ToString());
        }
Exemple #2
0
        /// <summary>
        /// Creates a new Node for generating Ksuid's.
        /// </summary>
        /// <param name="environment">The environment of the Ksuid.</param>
        /// <param name="instanceIdentifier">The instance identifier of the Ksuid.</param>
        public Node(string environment = "prod", InstanceIdentifier instanceIdentifier = null)
        {
            if (instanceIdentifier == null)
            {
                instanceIdentifier = InstanceIdentifier.GetInstanceIdentifier();
            }

            Environment        = environment;
            InstanceIdentifier = instanceIdentifier;
        }
Exemple #3
0
        public Id(string environment, string resource, UInt64 timestamp,
                  InstanceIdentifier instanceIdentifier, UInt32 sequenceId)
        {
            Validation.ValidatePrefix(nameof(environment), environment);
            Validation.ValidatePrefix(nameof(resource), resource);

            if (instanceIdentifier == null)
            {
                throw new ArgumentNullException(nameof(instanceIdentifier), $"{nameof(instanceIdentifier)} cannot be null.");
            }

            Environment        = environment;
            Resource           = resource;
            Timestamp          = timestamp;
            InstanceIdentifier = instanceIdentifier;
            SequenceId         = sequenceId;
        }
Exemple #4
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public string ToFriendlyString()
        {
            if (friendlyStr != null)
            {
                return(friendlyStr);
            }

            var sb   = new StringBuilder();
            var time = Extensions.UnixEpoch.AddSeconds(Timestamp);

            sb.AppendLine($"Environment: {Environment}");
            sb.AppendLine($"Resource: {Resource}");
            sb.AppendLine($"Timestamp: {time.ToLongDateString()} {time.ToLongTimeString()}");
            sb.AppendLine(InstanceIdentifier.ToFriendlyString());
            sb.AppendLine($"Sequence Id: {SequenceId}");

            return(friendlyStr = sb.ToString());
        }