From 27283754c0d5f709dcc33cde940fb927f4feaf8f Mon Sep 17 00:00:00 2001 From: Shannon Date: Wed, 1 Feb 2017 16:48:09 +1100 Subject: [PATCH] Changes IndexFieldPolicies to be a StaticFieldCollection which is a keyed collection instead of using Linq and iterating to find the values in critical code --- .../Config/IndexSetExtensions.cs | 4 +- .../Config/LazyIndexCriteria.cs | 12 ++-- src/UmbracoExamine/StaticFieldCollection.cs | 23 +++++++ src/UmbracoExamine/UmbracoContentIndexer.cs | 60 +++++++++++-------- src/UmbracoExamine/UmbracoExamine.csproj | 1 + src/UmbracoExamine/UmbracoMemberIndexer.cs | 15 +++-- 6 files changed, 78 insertions(+), 37 deletions(-) create mode 100644 src/UmbracoExamine/StaticFieldCollection.cs diff --git a/src/UmbracoExamine/Config/IndexSetExtensions.cs b/src/UmbracoExamine/Config/IndexSetExtensions.cs index 1255f50a3c..f4bd2e24b2 100644 --- a/src/UmbracoExamine/Config/IndexSetExtensions.cs +++ b/src/UmbracoExamine/Config/IndexSetExtensions.cs @@ -14,7 +14,7 @@ namespace UmbracoExamine.Config public static class IndexSetExtensions { internal static IIndexCriteria ToIndexCriteria(this IndexSet set, IDataService svc, - IEnumerable indexFieldPolicies) + StaticFieldCollection indexFieldPolicies) { return new LazyIndexCriteria(set, svc, indexFieldPolicies); } @@ -29,7 +29,7 @@ namespace UmbracoExamine.Config /// public static IIndexCriteria ToIndexCriteria(this IndexSet set, IDataService svc) { - return set.ToIndexCriteria(svc, Enumerable.Empty()); + return set.ToIndexCriteria(svc, new StaticFieldCollection()); } } diff --git a/src/UmbracoExamine/Config/LazyIndexCriteria.cs b/src/UmbracoExamine/Config/LazyIndexCriteria.cs index 72ab3f31ba..ee58431930 100644 --- a/src/UmbracoExamine/Config/LazyIndexCriteria.cs +++ b/src/UmbracoExamine/Config/LazyIndexCriteria.cs @@ -12,7 +12,7 @@ namespace UmbracoExamine.Config public LazyIndexCriteria( IndexSet set, IDataService svc, - IEnumerable indexFieldPolicies) + StaticFieldCollection indexFieldPolicies) { if (set == null) throw new ArgumentNullException("set"); if (indexFieldPolicies == null) throw new ArgumentNullException("indexFieldPolicies"); @@ -35,8 +35,9 @@ namespace UmbracoExamine.Config foreach (var u in userProps) { var field = new IndexField() { Name = u }; - var policy = indexFieldPolicies.FirstOrDefault(x => x.Name == u); - if (policy != null) + + StaticField policy; + if (indexFieldPolicies.TryGetValue(u, out policy)) { field.Type = policy.Type; field.EnableSorting = policy.EnableSorting; @@ -55,8 +56,9 @@ namespace UmbracoExamine.Config foreach (var s in sysProps) { var field = new IndexField() { Name = s }; - var policy = indexFieldPolicies.FirstOrDefault(x => x.Name == s); - if (policy != null) + + StaticField policy; + if (indexFieldPolicies.TryGetValue(s, out policy)) { field.Type = policy.Type; field.EnableSorting = policy.EnableSorting; diff --git a/src/UmbracoExamine/StaticFieldCollection.cs b/src/UmbracoExamine/StaticFieldCollection.cs new file mode 100644 index 0000000000..2b54eb8043 --- /dev/null +++ b/src/UmbracoExamine/StaticFieldCollection.cs @@ -0,0 +1,23 @@ +using System.Collections.ObjectModel; + +namespace UmbracoExamine +{ + internal class StaticFieldCollection : KeyedCollection + { + protected override string GetKeyForItem(StaticField item) + { + return item.Name; + } + + /// + /// Implements TryGetValue using the underlying dictionary + /// + /// + /// + /// + public bool TryGetValue(string key, out StaticField field) + { + return Dictionary.TryGetValue(key, out field); + } + } +} \ No newline at end of file diff --git a/src/UmbracoExamine/UmbracoContentIndexer.cs b/src/UmbracoExamine/UmbracoContentIndexer.cs index 0f3779034c..37382d2647 100644 --- a/src/UmbracoExamine/UmbracoContentIndexer.cs +++ b/src/UmbracoExamine/UmbracoContentIndexer.cs @@ -196,27 +196,27 @@ namespace UmbracoExamine /// Alot of standard umbraco fields shouldn't be tokenized or even indexed, just stored into lucene /// for retreival after searching. /// - internal static readonly List IndexFieldPolicies - = new List + internal static readonly StaticFieldCollection IndexFieldPolicies + = new StaticFieldCollection { new StaticField("id", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), new StaticField("key", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "version", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "parentID", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "level", FieldIndexTypes.NOT_ANALYZED, true, "NUMBER"), - new StaticField( "writerID", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "creatorID", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "nodeType", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "template", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "sortOrder", FieldIndexTypes.NOT_ANALYZED, true, "NUMBER"), - new StaticField( "createDate", FieldIndexTypes.NOT_ANALYZED, false, "DATETIME"), - new StaticField( "updateDate", FieldIndexTypes.NOT_ANALYZED, false, "DATETIME"), - new StaticField( "nodeName", FieldIndexTypes.ANALYZED, false, string.Empty), - new StaticField( "urlName", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), - new StaticField( "writerName", FieldIndexTypes.ANALYZED, false, string.Empty), - new StaticField( "creatorName", FieldIndexTypes.ANALYZED, false, string.Empty), - new StaticField( "nodeTypeAlias", FieldIndexTypes.ANALYZED, false, string.Empty), - new StaticField( "path", FieldIndexTypes.NOT_ANALYZED, false, string.Empty) + new StaticField("version", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), + new StaticField("parentID", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), + new StaticField("level", FieldIndexTypes.NOT_ANALYZED, true, "NUMBER"), + new StaticField("writerID", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), + new StaticField("creatorID", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), + new StaticField("nodeType", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), + new StaticField("template", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), + new StaticField("sortOrder", FieldIndexTypes.NOT_ANALYZED, true, "NUMBER"), + new StaticField("createDate", FieldIndexTypes.NOT_ANALYZED, false, "DATETIME"), + new StaticField("updateDate", FieldIndexTypes.NOT_ANALYZED, false, "DATETIME"), + new StaticField("nodeName", FieldIndexTypes.ANALYZED, false, string.Empty), + new StaticField("urlName", FieldIndexTypes.NOT_ANALYZED, false, string.Empty), + new StaticField("writerName", FieldIndexTypes.ANALYZED, false, string.Empty), + new StaticField("creatorName", FieldIndexTypes.ANALYZED, false, string.Empty), + new StaticField("nodeTypeAlias", FieldIndexTypes.ANALYZED, false, string.Empty), + new StaticField("path", FieldIndexTypes.NOT_ANALYZED, false, string.Empty) }; #endregion @@ -804,9 +804,13 @@ namespace UmbracoExamine /// /// protected override FieldIndexTypes GetPolicy(string fieldName) - { - var def = IndexFieldPolicies.Where(x => x.Name == fieldName).ToArray(); - return (def.Any() == false ? FieldIndexTypes.ANALYZED : def.Single().IndexType); + { + StaticField def; + if (IndexFieldPolicies.TryGetValue(fieldName, out def)) + { + return def.IndexType; + } + return FieldIndexTypes.ANALYZED; } /// @@ -816,14 +820,18 @@ namespace UmbracoExamine /// protected override bool ValidateDocument(XElement node) { - var nodeId = int.Parse(node.Attribute("id").Value); // Test for access if we're only indexing published content // return nothing if we're not supporting protected content and it is protected, and we're not supporting unpublished content - if (!SupportUnpublishedContent - && (!SupportProtectedContent - && DataService.ContentService.IsProtected(nodeId, node.Attribute("path").Value))) + if (SupportUnpublishedContent == false + && SupportProtectedContent == false) { - return false; + + var nodeId = int.Parse(node.Attribute("id").Value); + + if (DataService.ContentService.IsProtected(nodeId, node.Attribute("path").Value)) + { + return false; + } } return base.ValidateDocument(node); } diff --git a/src/UmbracoExamine/UmbracoExamine.csproj b/src/UmbracoExamine/UmbracoExamine.csproj index 8720849744..25c1f1bcf0 100644 --- a/src/UmbracoExamine/UmbracoExamine.csproj +++ b/src/UmbracoExamine/UmbracoExamine.csproj @@ -134,6 +134,7 @@ + diff --git a/src/UmbracoExamine/UmbracoMemberIndexer.cs b/src/UmbracoExamine/UmbracoMemberIndexer.cs index 2e48dff64e..ad393932be 100644 --- a/src/UmbracoExamine/UmbracoMemberIndexer.cs +++ b/src/UmbracoExamine/UmbracoMemberIndexer.cs @@ -117,8 +117,9 @@ namespace UmbracoExamine if (indexerData.UserFields.Any(x => x.Name == "_searchEmail") == false) { var field = new IndexField { Name = "_searchEmail" }; - var policy = IndexFieldPolicies.FirstOrDefault(x => x.Name == "_searchEmail"); - if (policy != null) + + StaticField policy; + if (IndexFieldPolicies.TryGetValue("_searchEmail", out policy)) { field.Type = policy.Type; field.EnableSorting = policy.EnableSorting; @@ -237,10 +238,16 @@ namespace UmbracoExamine { var fields = base.GetSpecialFieldsToIndex(allValuesForIndexing); - //adds the special path property to the index + //adds the special key property to the index string valuesForIndexing; if (allValuesForIndexing.TryGetValue("__key", out valuesForIndexing)) - fields.Add("__key", valuesForIndexing); + { + if (fields.ContainsKey("__key") == false) + { + fields.Add("__key", valuesForIndexing); + } + } + return fields;