|
1 |
| -using StackExchange.Redis; |
| 1 | +using System.Reflection.Emit; |
| 2 | +using StackExchange.Redis; |
2 | 3 |
|
3 | 4 | namespace NRedisStack.Search.DataTypes;
|
4 | 5 |
|
5 | 6 | public class InfoResult
|
6 | 7 | {
|
7 | 8 | private readonly Dictionary<string, RedisResult> _all = new();
|
8 |
| - private static readonly string[] booleanAttributes = { "SORTABLE", "UNF", "NOSTEM", "NOINDEX", "CASESENSITIVE", "WITHSUFFIXTRIE" }; |
| 9 | + private Dictionary<string, RedisResult>[] _attributes; |
| 10 | + private Dictionary<string, RedisResult> _indexOption; |
| 11 | + private Dictionary<string, RedisResult> _gcStats; |
| 12 | + private Dictionary<string, RedisResult> _cursorStats; |
| 13 | + |
| 14 | + private static readonly string[] booleanAttributes = { "SORTABLE", "UNF", "NOSTEM", "NOINDEX", "CASESENSITIVE", "WITHSUFFIXTRIE", "INDEXEMPTY", "INDEXMISSING" }; |
9 | 15 | public string IndexName => GetString("index_name")!;
|
10 |
| - public Dictionary<string, RedisResult> IndexOption => GetRedisResultDictionary("index_options")!; |
11 |
| - public Dictionary<string, RedisResult>[] Attributes => GetRedisResultDictionaryArray("attributes")!; |
| 16 | + public Dictionary<string, RedisResult> IndexOption => _indexOption = _indexOption ?? GetRedisResultDictionary("index_options")!; |
| 17 | + public Dictionary<string, RedisResult>[] Attributes => _attributes = _attributes ?? GetAttributesAsDictionaryArray()!; |
12 | 18 | public long NumDocs => GetLong("num_docs");
|
13 | 19 | public string MaxDocId => GetString("max_doc_id")!;
|
14 | 20 | public long NumTerms => GetLong("num_terms");
|
@@ -48,9 +54,9 @@ public class InfoResult
|
48 | 54 | public long NumberOfUses => GetLong("number_of_uses");
|
49 | 55 |
|
50 | 56 |
|
51 |
| - public Dictionary<string, RedisResult> GcStats => GetRedisResultDictionary("gc_stats")!; |
| 57 | + public Dictionary<string, RedisResult> GcStats => _gcStats = _gcStats ?? GetRedisResultDictionary("gc_stats")!; |
52 | 58 |
|
53 |
| - public Dictionary<string, RedisResult> CursorStats => GetRedisResultDictionary("cursor_stats")!; |
| 59 | + public Dictionary<string, RedisResult> CursorStats => _cursorStats = _cursorStats ?? GetRedisResultDictionary("cursor_stats")!; |
54 | 60 |
|
55 | 61 | public InfoResult(RedisResult result)
|
56 | 62 | {
|
@@ -94,24 +100,29 @@ private double GetDouble(string key)
|
94 | 100 | return result;
|
95 | 101 | }
|
96 | 102 |
|
97 |
| - private Dictionary<string, RedisResult>[]? GetRedisResultDictionaryArray(string key) |
| 103 | + private Dictionary<string, RedisResult>[]? GetAttributesAsDictionaryArray() |
98 | 104 | {
|
99 |
| - if (!_all.TryGetValue(key, out var value)) return default; |
| 105 | + if (!_all.TryGetValue("attributes", out var value)) return default; |
100 | 106 | var values = (RedisResult[])value!;
|
101 | 107 | var result = new Dictionary<string, RedisResult>[values.Length];
|
102 | 108 | for (int i = 0; i < values.Length; i++)
|
103 | 109 | {
|
104 |
| - var fv = (RedisResult[])values[i]!; |
105 | 110 | var dict = new Dictionary<string, RedisResult>();
|
106 |
| - for (int j = 0; j < fv.Length; j += 2) |
| 111 | + |
| 112 | + IEnumerable<RedisResult> enumerable = (RedisResult[])values[i]!; |
| 113 | + IEnumerator<RedisResult> results = enumerable.GetEnumerator(); |
| 114 | + while (results.MoveNext()) |
107 | 115 | {
|
108 |
| - if (booleanAttributes.Contains((string)fv[j]!)) |
| 116 | + string attribute = (string)results.Current; |
| 117 | + // if its boolean attributes add itself to the dictionary and continue |
| 118 | + if (booleanAttributes.Contains(attribute)) |
109 | 119 | {
|
110 |
| - dict.Add((string)fv[j]!, fv[j--]); |
| 120 | + dict.Add(attribute, results.Current); |
111 | 121 | }
|
112 | 122 | else
|
113 |
| - { |
114 |
| - dict.Add((string)fv[j]!, fv[j + 1]); |
| 123 | + {//if its not a boolean attribute, add the next item as value to the dictionary |
| 124 | + results.MoveNext(); ; |
| 125 | + dict.Add(attribute, results.Current); |
115 | 126 | }
|
116 | 127 | }
|
117 | 128 | result[i] = dict;
|
|
0 commit comments