diff --git a/src/Umbraco.Infrastructure/Examine/ContentIndexPopulator.cs b/src/Umbraco.Infrastructure/Examine/ContentIndexPopulator.cs index b31f9ca5f7..08fc7da901 100644 --- a/src/Umbraco.Infrastructure/Examine/ContentIndexPopulator.cs +++ b/src/Umbraco.Infrastructure/Examine/ContentIndexPopulator.cs @@ -8,9 +8,9 @@ using Umbraco.Cms.Core.Persistence.Querying; using Umbraco.Cms.Core.Services; using Umbraco.Core; using Umbraco.Core.Models; -using Umbraco.Core.Services; using Umbraco.Core.Persistence; using Umbraco.Core.Persistence.Querying; +using Umbraco.Core.Services; namespace Umbraco.Examine { @@ -21,12 +21,14 @@ namespace Umbraco.Examine public class ContentIndexPopulator : IndexPopulator { private readonly IContentService _contentService; + private readonly IUmbracoDatabaseFactory _umbracoDatabaseFactory; private readonly IValueSetBuilder _contentValueSetBuilder; /// /// This is a static query, it's parameters don't change so store statically /// - private static IQuery _publishedQuery; + private IQuery _publishedQuery; + private IQuery PublishedQuery => _publishedQuery ??= _umbracoDatabaseFactory.SqlContext.Query().Where(x => x.Published); private readonly bool _publishedValuesOnly; private readonly int? _parentId; @@ -37,26 +39,20 @@ namespace Umbraco.Examine /// /// /// - public ContentIndexPopulator(IContentService contentService, ISqlContext sqlContext, IContentValueSetBuilder contentValueSetBuilder) - : this(false, null, contentService, sqlContext, contentValueSetBuilder) + public ContentIndexPopulator(IContentService contentService, IUmbracoDatabaseFactory umbracoDatabaseFactory, IContentValueSetBuilder contentValueSetBuilder) + : this(false, null, contentService, umbracoDatabaseFactory, contentValueSetBuilder) { } /// /// Optional constructor allowing specifying custom query parameters /// - /// - /// - /// - /// - /// - public ContentIndexPopulator(bool publishedValuesOnly, int? parentId, IContentService contentService, ISqlContext sqlContext, IValueSetBuilder contentValueSetBuilder) + public ContentIndexPopulator(bool publishedValuesOnly, int? parentId, IContentService contentService, IUmbracoDatabaseFactory umbracoDatabaseFactory, IValueSetBuilder contentValueSetBuilder) { - if (sqlContext == null) throw new ArgumentNullException(nameof(sqlContext)); + if (umbracoDatabaseFactory == null) throw new ArgumentNullException(nameof(umbracoDatabaseFactory)); _contentService = contentService ?? throw new ArgumentNullException(nameof(contentService)); + _umbracoDatabaseFactory = umbracoDatabaseFactory; _contentValueSetBuilder = contentValueSetBuilder ?? throw new ArgumentNullException(nameof(contentValueSetBuilder)); - if (_publishedQuery == null) - _publishedQuery = sqlContext.Query().Where(x => x.Published); _publishedValuesOnly = publishedValuesOnly; _parentId = parentId; } @@ -124,7 +120,7 @@ namespace Umbraco.Examine { //add the published filter //note: We will filter for published variants in the validator - content = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out _, _publishedQuery, + content = _contentService.GetPagedDescendants(contentParentId, pageIndex, pageSize, out _, PublishedQuery, Ordering.By("Path", Direction.Ascending)).ToArray(); diff --git a/src/Umbraco.Infrastructure/Examine/PublishedContentIndexPopulator.cs b/src/Umbraco.Infrastructure/Examine/PublishedContentIndexPopulator.cs index 83089659b4..cd5b8fc07b 100644 --- a/src/Umbraco.Infrastructure/Examine/PublishedContentIndexPopulator.cs +++ b/src/Umbraco.Infrastructure/Examine/PublishedContentIndexPopulator.cs @@ -1,6 +1,4 @@ using Umbraco.Cms.Core.Services; -using Umbraco.Core.Models; -using Umbraco.Core.Services; using Umbraco.Core.Persistence; namespace Umbraco.Examine @@ -15,8 +13,8 @@ namespace Umbraco.Examine /// public class PublishedContentIndexPopulator : ContentIndexPopulator { - public PublishedContentIndexPopulator(IContentService contentService, ISqlContext sqlContext, IPublishedContentValueSetBuilder contentValueSetBuilder) : - base(true, null, contentService, sqlContext, contentValueSetBuilder) + public PublishedContentIndexPopulator(IContentService contentService, IUmbracoDatabaseFactory umbracoDatabaseFactory, IPublishedContentValueSetBuilder contentValueSetBuilder) : + base(true, null, contentService, umbracoDatabaseFactory, contentValueSetBuilder) { } } diff --git a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs index e46638ce57..38b553c46b 100644 --- a/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs +++ b/src/Umbraco.Tests/TestHelpers/TestWithDatabaseBase.cs @@ -61,6 +61,7 @@ namespace Umbraco.Tests.TestHelpers protected IVariationContextAccessor VariationContextAccessor => new TestVariationContextAccessor(); internal ScopeProvider ScopeProvider => Current.ScopeProvider as ScopeProvider; + internal IUmbracoDatabaseFactory UmbracoDatabaseFactory => Factory.GetRequiredService(); protected ISqlContext SqlContext => Factory.GetRequiredService(); diff --git a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs index b713a5237b..22e84650d2 100644 --- a/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs +++ b/src/Umbraco.Tests/UmbracoExamine/IndexInitializer.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using Examine; -using Examine.LuceneEngine.Providers; using Lucene.Net.Analysis; using Lucene.Net.Analysis.Standard; using Lucene.Net.Store; @@ -16,6 +15,7 @@ using Umbraco.Cms.Core.Persistence.Querying; using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Core.Strings; +using Umbraco.Core.Persistence; using Umbraco.Core.Scoping; using Umbraco.Examine; using Umbraco.Tests.TestHelpers; @@ -43,10 +43,10 @@ namespace Umbraco.Tests.UmbracoExamine return contentValueSetBuilder; } - public static ContentIndexPopulator GetContentIndexRebuilder(PropertyEditorCollection propertyEditors, IContentService contentService, IScopeProvider scopeProvider, bool publishedValuesOnly) + public static ContentIndexPopulator GetContentIndexRebuilder(PropertyEditorCollection propertyEditors, IContentService contentService, IScopeProvider scopeProvider, IUmbracoDatabaseFactory umbracoDatabaseFactory, bool publishedValuesOnly) { var contentValueSetBuilder = GetContentValueSetBuilder(propertyEditors, scopeProvider, publishedValuesOnly); - var contentIndexDataSource = new ContentIndexPopulator(publishedValuesOnly, null, contentService, scopeProvider.SqlContext, contentValueSetBuilder); + var contentIndexDataSource = new ContentIndexPopulator(publishedValuesOnly, null, contentService, umbracoDatabaseFactory, contentValueSetBuilder); return contentIndexDataSource; } diff --git a/src/Umbraco.Tests/UmbracoExamine/IndexTest.cs b/src/Umbraco.Tests/UmbracoExamine/IndexTest.cs index 1a740aef66..bcd0732fce 100644 --- a/src/Umbraco.Tests/UmbracoExamine/IndexTest.cs +++ b/src/Umbraco.Tests/UmbracoExamine/IndexTest.cs @@ -1,25 +1,21 @@ -using System.Linq; +using System; +using System.Collections.Generic; +using System.Linq; using System.Threading; using Examine; using Examine.LuceneEngine.Providers; using Lucene.Net.Index; using Lucene.Net.Search; +using Microsoft.Extensions.DependencyInjection; +using Newtonsoft.Json; using NUnit.Framework; -using Umbraco.Tests.Testing; using Umbraco.Examine; -using Umbraco.Core.PropertyEditors; using Umbraco.Tests.TestHelpers.Entities; using Umbraco.Core.Models; -using Newtonsoft.Json; -using System.Collections.Generic; -using System; -using Microsoft.Extensions.DependencyInjection; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Tests.Common.Testing; -using Umbraco.Core; using Umbraco.Tests.TestHelpers; -using Constants = Umbraco.Cms.Core.Constants; namespace Umbraco.Tests.UmbracoExamine { @@ -48,7 +44,7 @@ namespace Umbraco.Tests.UmbracoExamine { Alias = "grid", Name = "Grid", - PropertyEditorAlias = Constants.PropertyEditors.Aliases.Grid + PropertyEditorAlias = Cms.Core.Constants.PropertyEditors.Aliases.Grid }); var content = MockedContent.CreateBasicContent(contentType); content.Id = 555; @@ -126,7 +122,7 @@ namespace Umbraco.Tests.UmbracoExamine [Test] public void Rebuild_Index() { - var contentRebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, false); + var contentRebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, UmbracoDatabaseFactory,false); var mediaRebuilder = IndexInitializer.GetMediaIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockMediaService()); using (var luceneDir = new RandomIdRamDirectory()) @@ -154,7 +150,7 @@ namespace Umbraco.Tests.UmbracoExamine [Test] public void Index_Protected_Content_Not_Indexed() { - var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, false); + var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, UmbracoDatabaseFactory,false); using (var luceneDir = new RandomIdRamDirectory()) @@ -279,7 +275,7 @@ namespace Umbraco.Tests.UmbracoExamine [Test] public void Index_Reindex_Content() { - var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, false); + var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, UmbracoDatabaseFactory,false); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, HostingEnvironment, RuntimeState, luceneDir, validator: new ContentValueSetValidator(false))) @@ -320,7 +316,7 @@ namespace Umbraco.Tests.UmbracoExamine public void Index_Delete_Index_Item_Ensure_Heirarchy_Removed() { - var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, false); + var rebuilder = IndexInitializer.GetContentIndexRebuilder(Factory.GetRequiredService(), IndexInitializer.GetMockContentService(), ScopeProvider, UmbracoDatabaseFactory,false); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, HostingEnvironment, RuntimeState, luceneDir)) diff --git a/src/Umbraco.Tests/UmbracoExamine/SearchTests.cs b/src/Umbraco.Tests/UmbracoExamine/SearchTests.cs index 8d9bc33d13..8594a34edf 100644 --- a/src/Umbraco.Tests/UmbracoExamine/SearchTests.cs +++ b/src/Umbraco.Tests/UmbracoExamine/SearchTests.cs @@ -4,19 +4,13 @@ using System.Linq; using Examine; using Examine.Search; using Microsoft.Extensions.DependencyInjection; -using NUnit.Framework; using Moq; using Umbraco.Cms.Core.Models; using Umbraco.Cms.Core.Persistence.Querying; using Umbraco.Cms.Core.PropertyEditors; using Umbraco.Cms.Core.Services; using Umbraco.Cms.Tests.Common.Testing; -using Umbraco.Core; -using Umbraco.Core.Models; -using Umbraco.Core.Persistence.Querying; -using Umbraco.Core.Services; -using Umbraco.Tests.Testing; -using Umbraco.Core.PropertyEditors; +using NUnit.Framework; using Umbraco.Examine; namespace Umbraco.Tests.UmbracoExamine @@ -60,7 +54,7 @@ namespace Umbraco.Tests.UmbracoExamine allRecs); var propertyEditors = Factory.GetRequiredService(); - var rebuilder = IndexInitializer.GetContentIndexRebuilder(propertyEditors, contentService, ScopeProvider, true); + var rebuilder = IndexInitializer.GetContentIndexRebuilder(propertyEditors, contentService, ScopeProvider, UmbracoDatabaseFactory,true); using (var luceneDir = new RandomIdRamDirectory()) using (var indexer = IndexInitializer.GetUmbracoIndexer(ProfilingLogger, HostingEnvironment, RuntimeState, luceneDir))