Skip to content

Commit 867b110

Browse files
committed
Implemented new GetTopicByUniqueKey() extension
The new `GetTopicByUniqueKey()` extension takes the functionality previously implemented in the private `GetTopic()` method and makes it broadly available for other aspects of the application. This allows us to greatly simplify the code in `CachedTopicRepository` by replacing `GetTopic()` entirely with a simple call to `GetByUniqueKey()`. Ironically, this new approach should actually be _faster_, and certainly requires _less code_, even despite having to start from an arbitrary place within the topic graph. This is because of a more economical approach to how the tree is traversed by simply breaking the `uniqueKey` into an array and looping over that array as part of the traversal.
1 parent f2cb109 commit 867b110

1 file changed

Lines changed: 1 addition & 71 deletions

File tree

OnTopic.Data.Caching/CachedTopicRepository.cs

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ public CachedTopicRepository(ITopicRepository dataProvider) : base() {
102102
| Lookup by TopicKey
103103
\-----------------------------------------------------------------------------------------------------------------------*/
104104
if (topicKey != null && !topicKey.Length.Equals(0)) {
105-
return GetTopic(_cache, topicKey);
105+
return _cache.GetByUniqueKey(topicKey);
106106
}
107107

108108
/*------------------------------------------------------------------------------------------------------------------------
@@ -131,76 +131,6 @@ public CachedTopicRepository(ITopicRepository dataProvider) : base() {
131131

132132
}
133133

134-
/*==========================================================================================================================
135-
| METHOD: GET TOPIC
136-
\-------------------------------------------------------------------------------------------------------------------------*/
137-
/// <summary>
138-
/// Retrieves a topic object based on the specified partial or full (prefixed) topic key.
139-
/// </summary>
140-
/// <param name="sourceTopic">The root topic to search from.</param>
141-
/// <param name="uniqueKey">
142-
/// The partial or full string value representing the key (or <see cref="Topic.GetUniqueKey"/>) for the topic.
143-
/// </param>
144-
/// <returns>The topic or null, if the topic is not found.</returns>
145-
private Topic? GetTopic(Topic? sourceTopic, string uniqueKey) {
146-
147-
/*------------------------------------------------------------------------------------------------------------------------
148-
| Validate input
149-
\-----------------------------------------------------------------------------------------------------------------------*/
150-
if (sourceTopic == null) return null;
151-
if (String.IsNullOrWhiteSpace(uniqueKey)) return null;
152-
153-
/*------------------------------------------------------------------------------------------------------------------------
154-
| Provide shortcut for local calls
155-
\-----------------------------------------------------------------------------------------------------------------------*/
156-
if (uniqueKey.IndexOf(":", StringComparison.InvariantCulture) < 0 && uniqueKey != "Root") {
157-
if (sourceTopic.Children.Contains(uniqueKey)) {
158-
return sourceTopic.Children[uniqueKey];
159-
}
160-
return null;
161-
}
162-
163-
/*------------------------------------------------------------------------------------------------------------------------
164-
| Provide implicit root
165-
>-------------------------------------------------------------------------------------------------------------------------
166-
| ###NOTE JJC080313: While a root topic is required by the data structure, it should be implicit from the perspective of
167-
| the calling application. A developer should be able to call GetTopic("Namespace:TopicPath") to get to a topic, without
168-
| needing to be aware of the root.
169-
\-----------------------------------------------------------------------------------------------------------------------*/
170-
if (
171-
!uniqueKey.StartsWith("Root:", StringComparison.OrdinalIgnoreCase) &&
172-
!uniqueKey.Equals("Root", StringComparison.OrdinalIgnoreCase)
173-
) {
174-
uniqueKey = "Root:" + uniqueKey;
175-
}
176-
177-
/*------------------------------------------------------------------------------------------------------------------------
178-
| Validate parameters
179-
\-----------------------------------------------------------------------------------------------------------------------*/
180-
if (!uniqueKey.StartsWith(sourceTopic.GetUniqueKey(), StringComparison.OrdinalIgnoreCase)) return null;
181-
if (uniqueKey.Equals(sourceTopic.GetUniqueKey(), StringComparison.OrdinalIgnoreCase)) return sourceTopic;
182-
183-
/*------------------------------------------------------------------------------------------------------------------------
184-
| Define variables
185-
\-----------------------------------------------------------------------------------------------------------------------*/
186-
var remainder = uniqueKey.Substring(sourceTopic.GetUniqueKey().Length + 1);
187-
var marker = remainder.IndexOf(":", StringComparison.Ordinal);
188-
var nextChild = (marker < 0) ? remainder : remainder.Substring(0, marker);
189-
190-
/*------------------------------------------------------------------------------------------------------------------------
191-
| Find topic
192-
\-----------------------------------------------------------------------------------------------------------------------*/
193-
if (!sourceTopic.Children.Contains(nextChild)) return null;
194-
195-
if (nextChild == remainder) return sourceTopic.Children[nextChild];
196-
197-
/*------------------------------------------------------------------------------------------------------------------------
198-
| Return the topic
199-
\-----------------------------------------------------------------------------------------------------------------------*/
200-
return GetTopic(sourceTopic.Children[nextChild], uniqueKey);
201-
202-
}
203-
204134
/*==========================================================================================================================
205135
| METHOD: SAVE
206136
\-------------------------------------------------------------------------------------------------------------------------*/

0 commit comments

Comments
 (0)