From 9a716ada305e0b8e01cd0d29abfcd026a61afbdd Mon Sep 17 00:00:00 2001 From: Sebastien Ros Date: Fri, 13 Jul 2012 11:24:06 -0700 Subject: [PATCH] Optimizing Count() usage where needed. Prevents useless queries when huge amount of data is manipulated. --- .../Controllers/BlogAdminController.cs | 2 +- .../Handlers/BlogPartArchiveHandler.cs | 2 +- .../Orchard.Blogs/Handlers/BlogPostPartHandler.cs | 2 +- .../Controllers/AdminController.cs | 14 +++++++------- .../Modules/Orchard.Tags/Services/TagService.cs | 5 ++++- .../Orchard.Widgets/Controllers/AdminController.cs | 9 +++++---- 6 files changed, 19 insertions(+), 15 deletions(-) diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogAdminController.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogAdminController.cs index 73d41861d..a47cfeff7 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogAdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Controllers/BlogAdminController.cs @@ -154,7 +154,7 @@ namespace Orchard.Blogs.Controllers { list.AddRange(_blogService.Get(VersionOptions.Latest) .Select(b => { var blog = Services.ContentManager.BuildDisplay(b, "SummaryAdmin"); - blog.TotalPostCount = _blogPostService.Get(b, VersionOptions.Latest).Count(); + blog.TotalPostCount = _blogPostService.PostCount(b, VersionOptions.Latest); return blog; })); diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs index 775943dc8..31bc10d81 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPartArchiveHandler.cs @@ -32,7 +32,7 @@ namespace Orchard.Blogs.Handlers { var posts = blogPostService.Get(blogPostPart.BlogPart, VersionOptions.Published); // create a dictionary of all the year/month combinations and their count of posts that are published in this blog - var inMemoryBlogArchives = new Dictionary(posts.Count()); + var inMemoryBlogArchives = new Dictionary(); foreach (var post in posts) { if (!post.Has()) continue; diff --git a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs index f9e3c12bf..68c802cd6 100644 --- a/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.Blogs/Handlers/BlogPostPartHandler.cs @@ -43,7 +43,7 @@ namespace Orchard.Blogs.Handlers { // Ensure the "right" set of published posts for the blog is obtained blogPart.ContentItem.ContentManager.Flush(); - blogPart.PostCount = _blogPostService.Get(blogPart, VersionOptions.Published).Count(); + blogPart.PostCount = _blogPostService.PostCount(blogPart); } } diff --git a/src/Orchard.Web/Modules/Orchard.Comments/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.Comments/Controllers/AdminController.cs index a113b6824..697ff7517 100644 --- a/src/Orchard.Web/Modules/Orchard.Comments/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Comments/Controllers/AdminController.cs @@ -49,26 +49,26 @@ namespace Orchard.Comments.Controllers { options = new CommentIndexOptions(); // Filtering - IContentQuery comments; + IContentQuery commentsQuery; switch (options.Filter) { case CommentIndexFilter.All: - comments = _commentService.GetComments(); + commentsQuery = _commentService.GetComments(); break; case CommentIndexFilter.Approved: - comments = _commentService.GetComments(CommentStatus.Approved); + commentsQuery = _commentService.GetComments(CommentStatus.Approved); break; case CommentIndexFilter.Pending: - comments = _commentService.GetComments(CommentStatus.Pending); + commentsQuery = _commentService.GetComments(CommentStatus.Pending); break; case CommentIndexFilter.Spam: - comments = _commentService.GetComments(CommentStatus.Spam); + commentsQuery = _commentService.GetComments(CommentStatus.Spam); break; default: throw new ArgumentOutOfRangeException(); } - var pagerShape = Shape.Pager(pager).TotalItemCount(comments.Count()); - var entries = comments + var pagerShape = Shape.Pager(pager).TotalItemCount(commentsQuery.Count()); + var entries = commentsQuery .OrderByDescending(cpr => cpr.CommentDateUtc) .Slice(pager.GetStartIndex(), pager.PageSize) .ToList() diff --git a/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs b/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs index 2f27e5b1b..b5045086a 100644 --- a/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs +++ b/src/Orchard.Web/Modules/Orchard.Tags/Services/TagService.cs @@ -142,7 +142,10 @@ namespace Orchard.Tags.Services { } public int GetTaggedContentItemCount(int tagId, VersionOptions options) { - return GetTaggedContentItems(tagId, options).Count(); + return _orchardServices.ContentManager + .Query() + .Where(tpr => tpr.Tags.Any(tr => tr.TagRecord.Id == tagId)) + .Count(); } private void TagContentItem(TagsPartRecord tagsPartRecord, string tagName) { diff --git a/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs b/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs index 56fc03bb5..bb9c014c3 100644 --- a/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs +++ b/src/Orchard.Web/Modules/Orchard.Widgets/Controllers/AdminController.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Globalization; using System.Web.Mvc; using System.Linq; using Orchard.ContentManagement; @@ -116,9 +117,9 @@ namespace Orchard.Widgets.Controllers { return RedirectToAction("Index"); } - IEnumerable layers = _widgetsService.GetLayers(); + IEnumerable layers = _widgetsService.GetLayers().ToList(); - if (layers.Count() == 0) { + if (!layers.Any()) { Services.Notifier.Error(T("Layer not found: {0}", layerId)); return RedirectToAction("Index"); } @@ -147,8 +148,8 @@ namespace Orchard.Widgets.Controllers { if (widgetPart == null) return HttpNotFound(); try { - int widgetPosition = _widgetsService.GetWidgets().Where(widget => widget.Zone == widgetPart.Zone).Count() + 1; - widgetPart.Position = widgetPosition.ToString(); + int widgetPosition = _widgetsService.GetWidgets().Count(widget => widget.Zone == widgetPart.Zone) + 1; + widgetPart.Position = widgetPosition.ToString(CultureInfo.InvariantCulture); widgetPart.Zone = zone; widgetPart.LayerPart = _widgetsService.GetLayer(layerId); dynamic model = Services.ContentManager.BuildEditor(widgetPart);