Skip to content
Permalink
Browse files

QNTM-5867 Search dictionary should only be recreated when necessary (#…

…9301)

* update

* Created a TagDictionary property that is cached and only rebuilt when necessary.

* Revert AssemblySharedInfo.cs..

* Simplified tagDictionary
  • Loading branch information...
scottmitchell committed Dec 12, 2018
1 parent f3267e1 commit 86bbd78064dd4727587ec9c42a9e00480809b60b
Showing with 25 additions and 10 deletions.
  1. +25 −10 src/DynamoCore/Search/SearchDictionary.cs
@@ -14,6 +14,8 @@ public class SearchDictionary<V>
{
protected readonly Dictionary<V, Dictionary<string, double>> entryDictionary =
new Dictionary<V, Dictionary<string, double>>();

private List<IGrouping<string, Tuple<V, double>>> tagDictionary;

/// <summary>
/// All the current entries in search.
@@ -48,6 +50,7 @@ protected virtual void OnEntryAdded(V entry)
{
var handler = EntryAdded;
if (handler != null) handler(entry);
tagDictionary = null;
}

/// <summary>
@@ -59,6 +62,7 @@ protected virtual void OnEntryRemoved(V entry)
{
var handler = EntryRemoved;
if (handler != null) handler(entry);
tagDictionary = null;
}

/// <summary>
@@ -70,6 +74,7 @@ protected virtual void OnEntryUpdated(V entry)
{
var handler = EntryUpdated;
if (handler != null) handler(entry);
tagDictionary = null;
}

/// <summary>
@@ -280,18 +285,13 @@ private static bool ContainsSpecialCharacters(string element)
#endregion

/// <summary>
/// Search for elements in the dictionary based on the query
/// Converts entryDictionary from a dictionary of searchElement:(tag,weight)
/// to a dictionary of tag:(list(searchelement,weight))
/// which contains all nodes which share a tag
/// </summary>
/// <param name="query"> The query </param>
/// <param name="minResultsForTolerantSearch">Minimum number of results in the original search strategy to justify doing more tolerant search</param>
internal IEnumerable<V> Search(string query, int minResultsForTolerantSearch = 0)
internal void RebuildTagDictionary()
{
var searchDict = new Dictionary<V, double>();
// convert from a dictionary of searchElement:<tag,weight>
// to a dictionary of tag:<list<searchelement,weight>>
// which contains all nodes which share a tag

var tagDictionary = entryDictionary
tagDictionary = entryDictionary
.SelectMany(
entryAndTags =>
entryAndTags.Value.Select(
@@ -306,6 +306,21 @@ internal IEnumerable<V> Search(string query, int minResultsForTolerantSearch = 0
tagWeightAndEntry => tagWeightAndEntry.Tag,
tagWeightAndEntry =>
Tuple.Create(tagWeightAndEntry.Entry, tagWeightAndEntry.Weight)).ToList();
}

/// <summary>
/// Search for elements in the dictionary based on the query
/// </summary>
/// <param name="query"> The query </param>
/// <param name="minResultsForTolerantSearch">Minimum number of results in the original search strategy to justify doing more tolerant search</param>
internal IEnumerable<V> Search(string query, int minResultsForTolerantSearch = 0)
{
var searchDict = new Dictionary<V, double>();

if (tagDictionary == null)
{
RebuildTagDictionary();
}

query = query.ToLower();

0 comments on commit 86bbd78

Please sign in to comment.
You can’t perform that action at this time.