Skip to content

Commit

Permalink
Major Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Taiizor committed Dec 20, 2024
1 parent afffadc commit d5d0283
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 15 deletions.
41 changes: 31 additions & 10 deletions demo/UUID.Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,28 @@ static async Task Main(string[] args)
Guid implicitToGuid = id; // Implicit conversion from UUID to Guid
Console.WriteLine($"Implicit conversions successful? {implicitFromGuid == id && implicitToGuid == guid}");

Console.WriteLine("\n5. Binary Operations:");
Console.WriteLine("\n5. Compact UUID Operations:");
Console.WriteLine("Creating compact UUIDs (12 characters):");

// Temel kompakt UUID
UUID compact = UUID.NewCompact();
Console.WriteLine($"Compact UUID: {compact.ToInt64()}");

// Belirli bir zaman damgası ile kompakt UUID
long timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
UUID compactWithTime = UUID.NewCompactWithTime(timestamp);
Console.WriteLine($"Compact UUID with specific time: {compactWithTime.ToInt64()}");
Console.WriteLine($"Time matches? {compactWithTime.Time.ToUnixTimeMilliseconds() == timestamp}");

// Çoklu kompakt UUID üretimi
Console.WriteLine("\nGenerating multiple compact UUIDs:");
for (int i = 0; i < 5; i++)
{
UUID compactId = UUID.NewCompact();
Console.WriteLine($"Compact UUID #{i + 1}: {compactId.ToInt64()}");
}

Console.WriteLine("\n6. Binary Operations:");
// TryWriteBytes example
byte[] byteBuffer = new byte[16];
bool writeSuccess = id.TryWriteBytes(byteBuffer);
Expand All @@ -58,7 +79,7 @@ static async Task Main(string[] args)
byte[] byteArray = id.ToByteArray();
Console.WriteLine($"Direct byte array: {BitConverter.ToString(byteArray).Replace("-", "")}");

Console.WriteLine("\n6. Int64 (Long) Conversions:");
Console.WriteLine("\n7. Int64 (Long) Conversions:");
// Generate multiple UUIDs to demonstrate conversion consistency
UUID[] uuids = new UUID[5];
ArrayExtension.Fill(uuids);
Expand Down Expand Up @@ -93,7 +114,7 @@ static async Task Main(string[] args)
Console.WriteLine($"Second Int64: {long2}");
Console.WriteLine($"Time ordering preserved?: {long2 > long1}");

Console.WriteLine("\n7. Base64 Operations:");
Console.WriteLine("\n8. Base64 Operations:");
string base64 = id.ToBase64();
Console.WriteLine($"UUID -> Base64: {base64}");
UUID fromBase64 = UUID.FromBase64(base64);
Expand All @@ -106,7 +127,7 @@ static async Task Main(string[] args)
Console.WriteLine($"Successfully parsed from Base64: {parsedFromBase64}");
}

Console.WriteLine("\n8. Byte Array Operations:");
Console.WriteLine("\n9. Byte Array Operations:");
byte[] bytes2 = id.ToByteArray();
Console.WriteLine($"UUID -> Bytes: {BitConverter.ToString(bytes2)}");
UUID fromBytes = UUID.FromByteArray(bytes2);
Expand All @@ -126,7 +147,7 @@ static async Task Main(string[] args)
Console.WriteLine($"Successfully wrote to byte array: {BitConverter.ToString(destination)}");
}

Console.WriteLine("\n9. Comparison Operations:");
Console.WriteLine("\n10. Comparison Operations:");
UUID id1 = new(); // Using parameterless constructor
await Task.Delay(1); // Wait to ensure different timestamp
UUID id2 = UUID.New();
Expand All @@ -138,7 +159,7 @@ static async Task Main(string[] args)
Console.WriteLine($"UUID1 > UUID2: {id1 > id2}");
Console.WriteLine($"UUID1 >= UUID2: {id1 >= id2}");

Console.WriteLine("\n10. Array Extension Methods:");
Console.WriteLine("\n11. Array Extension Methods:");
// Generate array of UUIDs
UUID[] generatedArray = ArrayExtension.Generate(5);
Console.WriteLine("Generated array of 5 UUIDs:");
Expand Down Expand Up @@ -177,7 +198,7 @@ static async Task Main(string[] args)
}
}

Console.WriteLine("\n11. Sorting and Thread Safety:");
Console.WriteLine("\n12. Sorting and Thread Safety:");
List<UUID> ids = new();
for (int i = 0; i < 5; i++)
{
Expand All @@ -198,7 +219,7 @@ static async Task Main(string[] args)
Console.WriteLine($" {uuid} - Time: {uuid.Time:yyyy-MM-dd HH:mm:ss.fff}");
}

Console.WriteLine("\n12. Thread-Safe UUID Generation:");
Console.WriteLine("\n13. Thread-Safe UUID Generation:");
HashSet<UUID> set = new();
List<Task> tasks = new();

Expand All @@ -223,7 +244,7 @@ static async Task Main(string[] args)
await Task.WhenAll(tasks);
Console.WriteLine($"Generated {set.Count} unique UUIDs across multiple threads");

Console.WriteLine("\n13. Comparing UUID and Guid initialization behaviors:\n");
Console.WriteLine("\n14. Comparing UUID and Guid initialization behaviors:\n");

// UUID initialization - always creates a unique identifier
UUID uuid3 = new();
Expand Down Expand Up @@ -259,7 +280,7 @@ static async Task Main(string[] args)
Console.WriteLine($"Are they equal? {newGuid1 == newGuid2}");
Console.WriteLine($"Is first empty? {newGuid1 == default}");

Console.WriteLine("\n14. Bulk UUID Generation Performance:");
Console.WriteLine("\n15. Bulk UUID Generation Performance:");
const int batchSize = 1000;
Console.WriteLine($"Generating {batchSize} UUIDs in batch...");

Expand Down
37 changes: 32 additions & 5 deletions src/UUID/Constructor/UUID.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,41 @@ public static UUID New()
return new(GenerateTimestamp(), GenerateRandom());
}

/// <summary>
/// Creates a new compact UUID that is 12 characters long.
/// This method provides a shorter, more compact identifier format while maintaining uniqueness.
/// </summary>
/// <returns>A new UUID instance optimized for compact representation (12 characters).</returns>
/// <example>
/// <code>
/// // Generate a compact 12-character UUID
/// var uuid = UUID.NewCompact();
/// // Output example: 867349715132
/// </code>
/// </example>
public static UUID NewCompact()
{
UUID uuid = new();
return new UUID((ulong)(uuid.ToInt64() >> 15), (ulong)(uuid.ToInt64() & 0x7FFF));
}

/// <summary>
/// Creates a new compact UUID using the specified timestamp.
/// Useful when you need to generate a compact UUID with a specific timestamp.
/// </summary>
/// <param name="timestamp">The timestamp to use (Unix milliseconds).</param>
/// <returns>A new UUID instance with the specified timestamp in compact format.</returns>
public static UUID NewCompactWithTime(long timestamp)
{
UUID uuid = new((ulong)(timestamp << 16), GenerateRandom());
return new UUID((ulong)(uuid.ToInt64() >> 15), (ulong)(uuid.ToInt64() & 0x7FFF));
}

/// <summary>
/// Generates a timestamp component for the UUID.
/// </summary>
/// <returns>A 64-bit unsigned integer containing the timestamp and additional random data.</returns>
/// <remarks>
/// The timestamp format varies by UUID version:
/// - V7: First 48 bits contain Unix timestamp in milliseconds
/// - V8: Timestamp is stored in the last 48 bits
/// Random data is added to ensure uniqueness within the same millisecond.
/// Thread safety is handled by the _lock object when necessary.
/// </remarks>
Expand Down Expand Up @@ -170,11 +197,11 @@ public static bool TryParse(string? input, out UUID result)
#if NET6_0_OR_GREATER
ReadOnlySpan<char> span = input.AsSpan();

if (!ulong.TryParse(span.Slice(0, 16), NumberStyles.HexNumber, null, out ulong timestamp))
if (!ulong.TryParse(span[..16], NumberStyles.HexNumber, null, out ulong timestamp))
{
return false;
}
if (!ulong.TryParse(span.Slice(16), NumberStyles.HexNumber, null, out ulong random))
if (!ulong.TryParse(span[16..], NumberStyles.HexNumber, null, out ulong random))
{
return false;
}
Expand Down

0 comments on commit d5d0283

Please sign in to comment.