diff --git a/csharp/Platform.Data.Doublets/ILinksExtensions.cs b/csharp/Platform.Data.Doublets/ILinksExtensions.cs index a94c8f579..d696061ab 100644 --- a/csharp/Platform.Data.Doublets/ILinksExtensions.cs +++ b/csharp/Platform.Data.Doublets/ILinksExtensions.cs @@ -1599,5 +1599,54 @@ public static void ClearGarbage(this ILinks links, T } #endregion + + /// + /// + /// Prints the contents of the links database using the specified message handler. + /// + /// + /// + /// + /// The type of link address. + /// + /// + /// + /// The links. + /// + /// + /// + /// The message handler for outputting formatted messages. + /// + /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static void PrintContents(this ILinks links, Action messageHandler) where TLinkAddress : IUnsignedNumber, IComparisonOperators + { + var count = links.Count(); + if (count == TLinkAddress.Zero) + { + messageHandler("Database is empty."); + } + else + { + messageHandler("Contents:"); + var linksTotalLength = count.ToString().Length; + var printFormatBase = new string('0', linksTotalLength); + var printFormat = $"\t[{{0:{printFormatBase}}}]: {{1:{printFormatBase}}} -> {{2:{printFormatBase}}}"; + + // Iterate through all existing links + var allLinks = links.All(); + for (var i = 0; i < allLinks.Count; i++) + { + var link = allLinks[i]; + if (link != null) + { + var linkIndex = links.GetIndex(link); + var source = links.GetSource(link); + var target = links.GetTarget(link); + messageHandler(string.Format(printFormat, linkIndex, source, target)); + } + } + } + } } } diff --git a/experiments/TestPrintContents.cs b/experiments/TestPrintContents.cs new file mode 100644 index 000000000..60d64bc65 --- /dev/null +++ b/experiments/TestPrintContents.cs @@ -0,0 +1,49 @@ +using System; +using System.Collections.Generic; +using Platform.Data.Doublets; +using Platform.Data.Doublets.Memory.United.Generic; + +namespace Platform.Data.Doublets.Experiments +{ + class TestPrintContents + { + static void Main() + { + Console.WriteLine("Testing PrintContents extension method..."); + + // Create a simple in-memory links storage + using var links = new UnitedMemoryLinks("test.db"); + + var messages = new List(); + void CaptureMessage(string message) => messages.Add(message); + + // Test with empty database + Console.WriteLine("1. Testing with empty database:"); + links.PrintContents(CaptureMessage); + foreach (var message in messages) + { + Console.WriteLine($"Output: {message}"); + } + Console.WriteLine(); + + // Clear messages and add some links + messages.Clear(); + + // Create some test links + var link1 = links.Create(); + var link2 = links.Create(); + var link3 = links.CreateAndUpdate(link1, link2); + + Console.WriteLine("2. Testing with some links:"); + Console.WriteLine($"Created links: {link1}, {link2}, {link3}"); + + links.PrintContents(CaptureMessage); + foreach (var message in messages) + { + Console.WriteLine($"Output: {message}"); + } + + Console.WriteLine("\nTest completed successfully!"); + } + } +} \ No newline at end of file diff --git a/experiments/TestPrintContents.csproj b/experiments/TestPrintContents.csproj new file mode 100644 index 000000000..9720a93b9 --- /dev/null +++ b/experiments/TestPrintContents.csproj @@ -0,0 +1,13 @@ + + + Exe + net8 + true + latest + enable + + + + + + \ No newline at end of file diff --git a/experiments/test.db b/experiments/test.db new file mode 100644 index 000000000..40f7749f4 Binary files /dev/null and b/experiments/test.db differ