|
| 1 | +// Copyright (c) Ugo Lattanzi. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information. |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Collections.Generic; |
| 5 | + |
| 6 | +namespace StackExchange.Redis.Extensions.Core.Helpers; |
| 7 | + |
| 8 | +/// <summary> |
| 9 | +/// A helper class that provides methods to parse Redis info strings into structured data. |
| 10 | +/// </summary> |
| 11 | +/// <remarks> |
| 12 | +/// See <see href="https://redis.io/docs/latest/commands/info/#return-information"/> for details about the format of the info command output. |
| 13 | +/// </remarks> |
| 14 | +internal static class InfoDetailsParser |
| 15 | +{ |
| 16 | + private const string LineSeparator = "\r\n"; |
| 17 | + |
| 18 | + /// <summary> |
| 19 | + /// Parses the given info string into a list of sections, where each section contains a list of key-value pairs. |
| 20 | + /// </summary> |
| 21 | + /// <param name="info"></param> |
| 22 | + /// <returns></returns> |
| 23 | + public static List<List<KeyValuePair<string, string>>> ParseAsTreeRows(ReadOnlySpan<char> info) |
| 24 | + { |
| 25 | + List<List<KeyValuePair<string, string>>> sections = []; |
| 26 | +#if NET9_0_OR_GREATER |
| 27 | + foreach (var r in info.Split(LineSeparator)) |
| 28 | + { |
| 29 | + var line = info[r].Trim(); |
| 30 | + if (line.IsEmpty) |
| 31 | + continue; |
| 32 | + |
| 33 | + if (TryParseSection(line, out var section)) |
| 34 | + sections.Add([]); |
| 35 | + else if (TryParseDetail(line, out var detail)) |
| 36 | + sections[^1].Add(detail); |
| 37 | + } |
| 38 | +#else |
| 39 | + var start = 0; |
| 40 | + while (start < info.Length) |
| 41 | + { |
| 42 | + var end = info[start..].IndexOf(LineSeparator); |
| 43 | + ReadOnlySpan<char> line; |
| 44 | + if (end < 0) |
| 45 | + { |
| 46 | + line = info[start..].Trim(); |
| 47 | + start = info.Length; |
| 48 | + } |
| 49 | + else |
| 50 | + { |
| 51 | + line = info[start..(start + end)].Trim(); |
| 52 | + start += end + LineSeparator.Length; |
| 53 | + } |
| 54 | + |
| 55 | + if (line.IsEmpty) |
| 56 | + continue; |
| 57 | + |
| 58 | + if (TryParseSection(line, out var section)) |
| 59 | + sections.Add([]); |
| 60 | + else if (TryParseDetail(line, out var detail)) |
| 61 | + sections[^1].Add(detail); |
| 62 | + } |
| 63 | + |
| 64 | +#endif |
| 65 | + return sections; |
| 66 | + } |
| 67 | + |
| 68 | + /// <summary> |
| 69 | + /// Parses the given info string into a flat list of tuples, where each tuple contains the section name, key, and value. |
| 70 | + /// </summary> |
| 71 | + /// <param name="info"></param> |
| 72 | + /// <returns></returns> |
| 73 | + public static List<(string Section, string Key, string Value)> ParseAsFlatRows(ReadOnlySpan<char> info) |
| 74 | + { |
| 75 | + List<(string Section, string Key, string Value)> rows = []; |
| 76 | + |
| 77 | + var currentSection = string.Empty; |
| 78 | +#if NET9_0_OR_GREATER |
| 79 | + foreach (var r in info.Split(LineSeparator)) |
| 80 | + { |
| 81 | + var line = info[r].Trim(); |
| 82 | + if (line.IsEmpty) |
| 83 | + continue; |
| 84 | + |
| 85 | + if (TryParseSection(line, out var section)) |
| 86 | + currentSection = section; |
| 87 | + else if (TryParseDetail(line, out var detail)) |
| 88 | + rows.Add((currentSection, detail.Key, detail.Value)); |
| 89 | + } |
| 90 | +#else |
| 91 | + var start = 0; |
| 92 | + while (start < info.Length) |
| 93 | + { |
| 94 | + var end = info[start..].IndexOf(LineSeparator); |
| 95 | + ReadOnlySpan<char> line; |
| 96 | + if (end < 0) |
| 97 | + { |
| 98 | + line = info[start..].Trim(); |
| 99 | + start = info.Length; |
| 100 | + } |
| 101 | + else |
| 102 | + { |
| 103 | + line = info[start..(start + end)].Trim(); |
| 104 | + start += end + LineSeparator.Length; |
| 105 | + } |
| 106 | + |
| 107 | + if (line.IsEmpty) |
| 108 | + continue; |
| 109 | + |
| 110 | + if (TryParseSection(line, out var section)) |
| 111 | + currentSection = section; |
| 112 | + else if (TryParseDetail(line, out var detail)) |
| 113 | + rows.Add((currentSection, detail.Key, detail.Value)); |
| 114 | + } |
| 115 | + |
| 116 | +#endif |
| 117 | + return rows; |
| 118 | + } |
| 119 | + |
| 120 | + /// <summary> |
| 121 | + /// Parses the given info string into a dictionary of key-value pairs. Ingores section. |
| 122 | + /// </summary> |
| 123 | + /// <param name="info"></param> |
| 124 | + /// <returns></returns> |
| 125 | + public static Dictionary<string, string> ParseAsDictionary(ReadOnlySpan<char> info) |
| 126 | + { |
| 127 | + var dict = new Dictionary<string, string>(StringComparer.Ordinal); |
| 128 | + |
| 129 | + // No need to trim the line, as TryParseDetail will handle it |
| 130 | +#if NET9_0_OR_GREATER |
| 131 | + foreach (var r in info.Split(LineSeparator)) |
| 132 | + { |
| 133 | + var line = info[r]; |
| 134 | + if (TryParseDetail(line, out var detail)) |
| 135 | + dict.Add(detail.Key, detail.Value); |
| 136 | + } |
| 137 | +#else |
| 138 | + var start = 0; |
| 139 | + while (start < info.Length) |
| 140 | + { |
| 141 | + var end = info[start..].IndexOf(LineSeparator); |
| 142 | + ReadOnlySpan<char> line; |
| 143 | + if (end < 0) |
| 144 | + { |
| 145 | + line = info[start..]; |
| 146 | + start = info.Length; |
| 147 | + } |
| 148 | + else |
| 149 | + { |
| 150 | + line = info[start..(start + end)]; |
| 151 | + start += end + LineSeparator.Length; |
| 152 | + } |
| 153 | + |
| 154 | + if (TryParseDetail(line, out var detail)) |
| 155 | + dict.Add(detail.Key, detail.Value); |
| 156 | + } |
| 157 | + |
| 158 | +#endif |
| 159 | + return dict; |
| 160 | + } |
| 161 | + |
| 162 | + private static bool TryParseSection(ReadOnlySpan<char> line, out string section) |
| 163 | + { |
| 164 | + section = string.Empty; |
| 165 | + |
| 166 | + if (line[0] != '#') |
| 167 | + return false; |
| 168 | + |
| 169 | + section = new(line[1..].Trim()); |
| 170 | + return true; |
| 171 | + } |
| 172 | + |
| 173 | + private static bool TryParseDetail(ReadOnlySpan<char> line, out KeyValuePair<string, string> detail) |
| 174 | + { |
| 175 | + var idx = line.IndexOf(':'); |
| 176 | + if (idx <= 0) |
| 177 | + { |
| 178 | + detail = default; |
| 179 | + return false; |
| 180 | + } |
| 181 | + |
| 182 | + var key = new string(line[..idx].Trim()); |
| 183 | + var value = new string(line[(idx + 1)..].Trim()); |
| 184 | + detail = new(key, value); |
| 185 | + return true; |
| 186 | + } |
| 187 | +} |
0 commit comments