|
using System; |
|
using System.Collections.Generic; |
|
using System.Diagnostics; |
|
using System.Linq; |
|
using Sitecore.Caching; |
|
using Sitecore.Collections; |
|
using Sitecore.Data; |
|
using Sitecore.Data.Items; |
|
using Sitecore.Data.Managers; |
|
using Sitecore.Reflection; |
|
|
|
namespace Alan.Sitecore.Test |
|
{ |
|
public class Result |
|
{ |
|
public List<Item> Items { get; set; } |
|
public TimeSpan TimeSpan { get; set; } |
|
public string FunctionName { get; set; } |
|
|
|
public Result() |
|
{ |
|
Items = new ItemList(); |
|
} |
|
} |
|
|
|
public static class Tests |
|
{ |
|
public static bool ShouldClearCache { get; set; } = false; |
|
|
|
public static void ClearCache() |
|
{ |
|
foreach (ICacheInfo c in CacheManager.GetAllCaches()) |
|
{ |
|
c.Clear(); |
|
} |
|
|
|
TypeUtil.ClearSizeCache(); |
|
} |
|
|
|
public static Result GetResult(Func<Result> f) |
|
{ |
|
if (ShouldClearCache) |
|
{ |
|
ClearCache(); |
|
} |
|
var stopwatch = Stopwatch.StartNew(); |
|
var result = f.Invoke(); |
|
stopwatch.Stop(); |
|
result.TimeSpan = stopwatch.Elapsed; |
|
return result; |
|
} |
|
|
|
public static Result GetDescendantsSingleItem(Item item, ID id, int count) |
|
{ |
|
return GetResult(() => Logic.GetDescendantsSingleItem(item, id, count)); |
|
} |
|
|
|
public static Result GetDescendantsAllItems(Item item, ID id, int count) |
|
{ |
|
return GetResult(() => Logic.GetDescendantsAllItems(item, id, count)); |
|
} |
|
|
|
public static Result ChildrenSingleLevel(Item item, ID id, int count) |
|
{ |
|
return GetResult(() => Logic.ChildrenSingleLevel(item, id, count)); |
|
} |
|
|
|
public static Result ChildrenRecursive(Item item, ID id, int count) |
|
{ |
|
return GetResult(() => Logic.ChildrenRecursive(item, id, count)); |
|
} |
|
|
|
public static Result ChildrenSingleItem(Item item, ID id, int count) |
|
{ |
|
return GetResult(() => Logic.ChildrenSingleItem(item, id, count)); |
|
} |
|
|
|
public static Result SelectItems(Item item, string query, int count) |
|
{ |
|
return GetResult(() => Logic.SelectItems(item, query, count)); |
|
} |
|
|
|
public static Result SelectItem(Item item, string query, int count) |
|
{ |
|
return GetResult(() => Logic.SelectItem(item, query, count)); |
|
} |
|
|
|
|
|
public static Result SelectItemsFast(Item item, string query, int count) |
|
{ |
|
return GetResult(() => Logic.SelectItemsFast(item, query, count)); |
|
} |
|
|
|
public static Result SelectItemFast(Item item, string query, int count) |
|
{ |
|
return GetResult(() => Logic.SelectItemFast(item, query, count)); |
|
} |
|
} |
|
|
|
|
|
public static class Logic |
|
{ |
|
public static Result GetDescendantsSingleItem(Item item, ID id, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
var items = item.Axes.GetDescendants().FirstOrDefault(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)); |
|
r.Items.Add(items); |
|
} |
|
return r; |
|
} |
|
|
|
public static Result GetDescendantsAllItems(Item item, ID id, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
var items = item.Axes.GetDescendants().Where(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)).ToList(); |
|
r.Items.AddRange(items); |
|
} |
|
return r; |
|
} |
|
|
|
public static Result ChildrenSingleLevel(Item item, ID id, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
var items = item.Children.Where(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)).ToList(); |
|
r.Items.AddRange(items); |
|
} |
|
return r; |
|
} |
|
public static Result ChildrenSingleItem(Item item, ID id, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
var items = item.Children.FirstOrDefault(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)); |
|
r.Items.Add(items); |
|
} |
|
return r; |
|
} |
|
|
|
public static Result ChildrenRecursive(Item item, ID id, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
var itemChildren = item.Children; |
|
var items = itemChildren.Where(item1 => TemplateManager.GetTemplate(item1).InheritsFrom(id)).ToList(); |
|
r.Items.AddRange(items); |
|
foreach (Item item1 in itemChildren) |
|
{ |
|
if (item1.HasChildren) |
|
{ |
|
r.Items.AddRange(ChildrenRecursive(item1, id, 1).Items); |
|
} |
|
} |
|
} |
|
return r; |
|
} |
|
|
|
public static Result SelectItems(Item item, string name, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
r.Items.AddRange(item.Axes.SelectItems(name)); |
|
} |
|
return r; |
|
} |
|
|
|
public static Result SelectItem(Item item, string name, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
r.Items.Add(item?.Axes.SelectSingleItem(name)); |
|
} |
|
return r; |
|
} |
|
|
|
public static Result SelectItemFast(Item item, string name, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
r.Items.Add(item.Database.SelectSingleItem(name)); |
|
} |
|
return r; |
|
} |
|
|
|
public static Result SelectItemsFast(Item item, string name, int count) |
|
{ |
|
var r = new Result(); |
|
for (int i = 0; i < count; i++) |
|
{ |
|
r.Items.AddRange(item.Database.SelectItems(name)); |
|
} |
|
return r; |
|
} |
|
} |
|
} |