Skip to content

Commit

Permalink
Use FrozenDictionary (#15040)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeAlhayek authored Jan 11, 2024
1 parent a063f68 commit 2877187
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 55 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading.Tasks;
using OrchardCore.DisplayManagement.Descriptors.ShapePlacementStrategy;
Expand All @@ -10,13 +10,14 @@ public class PlacementsManager
{
private readonly IPlacementStore _placementStore;

public PlacementsManager(IPlacementStore placementStore) => _placementStore = placementStore;
public PlacementsManager(IPlacementStore placementStore)
=> _placementStore = placementStore;

public async Task<IReadOnlyDictionary<string, IEnumerable<PlacementNode>>> ListShapePlacementsAsync()
{
var document = await _placementStore.GetPlacementsAsync();

return document.Placements.ToImmutableDictionary(kvp => kvp.Key, kvp => kvp.Value.AsEnumerable());
return document.Placements.ToFrozenDictionary(kvp => kvp.Key, kvp => kvp.Value.AsEnumerable());
}

public async Task<IEnumerable<PlacementNode>> GetShapePlacementsAsync(string shapeType)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Threading;
Expand Down Expand Up @@ -187,14 +186,6 @@ public void Read(string indexName, Action<IndexReader> reader)
}
}

/// <summary>
/// Returns a list of open indices and the last time they were accessed.
/// </summary>
public IReadOnlyDictionary<string, DateTime> GetTimestamps()
{
return new ReadOnlyDictionary<string, DateTime>(_timestamps);
}

private Document CreateLuceneDocument(DocumentIndex documentIndex, LuceneIndexSettings indexSettings)
{
var doc = new Document
Expand All @@ -203,14 +194,14 @@ private Document CreateLuceneDocument(DocumentIndex documentIndex, LuceneIndexSe
// These fields need to be indexed as a StringField because it needs to be searchable for the writer.DeleteDocuments method.
// Else it won't be able to prune oldest draft from the indexes.
// Maybe eventually find a way to remove a document from a StoredDocument.
new StringField("ContentItemId", documentIndex.ContentItemId.ToString(), Field.Store.YES),
new StringField("ContentItemVersionId", documentIndex.ContentItemVersionId.ToString(), Field.Store.YES),
new StringField(IndexingConstants.ContentItemIdKey, documentIndex.ContentItemId.ToString(), Field.Store.YES),
new StringField(IndexingConstants.ContentItemVersionIdKey, documentIndex.ContentItemVersionId.ToString(), Field.Store.YES),
};

if (indexSettings.StoreSourceData)
{
doc.Add(new StoredField(IndexingConstants.SourceKey + "ContentItemId", documentIndex.ContentItemId.ToString()));
doc.Add(new StoredField(IndexingConstants.SourceKey + "ContentItemVersionId", documentIndex.ContentItemVersionId.ToString()));
doc.Add(new StoredField(IndexingConstants.SourceKey + IndexingConstants.ContentItemIdKey, documentIndex.ContentItemId.ToString()));
doc.Add(new StoredField(IndexingConstants.SourceKey + IndexingConstants.ContentItemVersionIdKey, documentIndex.ContentItemVersionId.ToString()));
}

foreach (var entry in documentIndex.Entries)
Expand All @@ -222,7 +213,7 @@ private Document CreateLuceneDocument(DocumentIndex documentIndex, LuceneIndexSe
switch (entry.Type)
{
case DocumentIndex.Types.Boolean:
// Store "true"/"false" for booleans.
// Store "true"/"false" for boolean.
doc.Add(new StringField(entry.Name, Convert.ToString(entry.Value).ToLowerInvariant(), store));

if (indexSettings.StoreSourceData)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;

namespace OrchardCore.AuditTrail.Services.Models
{
public class AuditTrailAdminListOptions
{
internal Dictionary<string, AuditTrailAdminListOptionBuilder> SortOptionBuilders { get; set; } = new Dictionary<string, AuditTrailAdminListOptionBuilder>();
internal Dictionary<string, AuditTrailAdminListOptionBuilder> SortOptionBuilders { get; set; } = [];

private Dictionary<string, AuditTrailAdminListOption> _sortOptions;
public IReadOnlyDictionary<string, AuditTrailAdminListOption> SortOptions => _sortOptions ??= BuildSortOptions();
private FrozenDictionary<string, AuditTrailAdminListOption> _sortOptions;

public IReadOnlyDictionary<string, AuditTrailAdminListOption> SortOptions
=> _sortOptions ??= BuildSortOptions();

private AuditTrailAdminListOption _defaultSortOption;
public AuditTrailAdminListOption DefaultSortOption => _defaultSortOption ??= SortOptions.Values.FirstOrDefault(x => x.IsDefault);
public AuditTrailAdminListOption DefaultSortOption
=> _defaultSortOption ??= SortOptions.Values.FirstOrDefault(x => x.IsDefault);

private Dictionary<string, AuditTrailAdminListOption> BuildSortOptions()
private FrozenDictionary<string, AuditTrailAdminListOption> BuildSortOptions()
{
var sortOptions = SortOptionBuilders.ToDictionary(k => k.Key, v => v.Value.Build());
var sortOptions = SortOptionBuilders.ToFrozenDictionary(k => k.Key, v => v.Value.Build());
SortOptionBuilders = null;

return sortOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Localization;

namespace OrchardCore.AuditTrail.Services.Models
{
public class AuditTrailOptions
{
internal Dictionary<string, AuditTrailCategoryDescriptorBuilder> CategoryDescriptorBuilders { get; set; } = new Dictionary<string, AuditTrailCategoryDescriptorBuilder>();
internal Dictionary<string, AuditTrailCategoryDescriptorBuilder> CategoryDescriptorBuilders { get; set; } = [];

private Dictionary<string, AuditTrailCategoryDescriptor> _categoryDescriptors;
public IReadOnlyDictionary<string, AuditTrailCategoryDescriptor> CategoryDescriptors => _categoryDescriptors ??= BuildCategoryDescriptors();
private FrozenDictionary<string, AuditTrailCategoryDescriptor> _categoryDescriptors;
public IReadOnlyDictionary<string, AuditTrailCategoryDescriptor> CategoryDescriptors
=> _categoryDescriptors ??= BuildCategoryDescriptors();

private Dictionary<string, AuditTrailCategoryDescriptor> BuildCategoryDescriptors()
private FrozenDictionary<string, AuditTrailCategoryDescriptor> BuildCategoryDescriptors()
{
var categoryDescriptors = CategoryDescriptorBuilders.ToDictionary(k => k.Key, v => v.Value.Build());
var categoryDescriptors = CategoryDescriptorBuilders.ToFrozenDictionary(k => k.Key, v => v.Value.Build());
CategoryDescriptorBuilders = null;

return categoryDescriptors;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;

Expand All @@ -9,14 +10,16 @@ namespace OrchardCore.ContentManagement
/// </summary>
public class ContentOptions
{
private readonly List<ContentPartOption> _contentParts = new();
private readonly List<ContentFieldOption> _contentFields = new();
private readonly List<ContentPartOption> _contentParts = [];
private readonly List<ContentFieldOption> _contentFields = [];

private IReadOnlyDictionary<string, ContentPartOption> _contentPartOptionsLookup;
public IReadOnlyDictionary<string, ContentPartOption> ContentPartOptionsLookup => _contentPartOptionsLookup ??= ContentPartOptions.ToDictionary(k => k.Type.Name);
public IReadOnlyDictionary<string, ContentPartOption> ContentPartOptionsLookup
=> _contentPartOptionsLookup ??= ContentPartOptions.ToFrozenDictionary(k => k.Type.Name);

private IReadOnlyDictionary<string, ContentFieldOption> _contentFieldOptionsLookup;
public IReadOnlyDictionary<string, ContentFieldOption> ContentFieldOptionsLookup => _contentFieldOptionsLookup ??= ContentFieldOptions.ToDictionary(k => k.Type.Name);
public IReadOnlyDictionary<string, ContentFieldOption> ContentFieldOptionsLookup
=> _contentFieldOptionsLookup ??= ContentFieldOptions.ToFrozenDictionary(k => k.Type.Name);

public IReadOnlyList<ContentPartOption> ContentPartOptions => _contentParts;
public IReadOnlyList<ContentFieldOption> ContentFieldOptions => _contentFields;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;

namespace OrchardCore.ContentManagement.Display.ContentDisplay
{
public class ContentDisplayOptions
{
private readonly List<ContentPartDisplayOption> _contentParts = new();
private readonly List<ContentFieldDisplayOption> _contentFields = new();
private readonly List<ContentPartDisplayOption> _contentParts = [];
private readonly List<ContentFieldDisplayOption> _contentFields = [];

private Dictionary<string, ContentPartDisplayOption> _contentPartOptions;
public IReadOnlyDictionary<string, ContentPartDisplayOption> ContentPartOptions => _contentPartOptions ??= _contentParts.ToDictionary(k => k.Type.Name);
private FrozenDictionary<string, ContentPartDisplayOption> _contentPartOptions;
private FrozenDictionary<string, ContentFieldDisplayOption> _contentFieldOptions;

private Dictionary<string, ContentFieldDisplayOption> _contentFieldOptions;
public IReadOnlyDictionary<string, ContentFieldDisplayOption> ContentFieldOptions => _contentFieldOptions ??= _contentFields.ToDictionary(k => k.Type.Name);
public IReadOnlyDictionary<string, ContentPartDisplayOption> ContentPartOptions
=> _contentPartOptions ??= _contentParts.ToFrozenDictionary(k => k.Type.Name);

public IReadOnlyDictionary<string, ContentFieldDisplayOption> ContentFieldOptions
=> _contentFieldOptions ??= _contentFields.ToFrozenDictionary(k => k.Type.Name);

internal void ForContentPartDisplayMode(Type contentPartType, Type displayDriverType, Func<string, bool> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ namespace OrchardCore.ContentManagement.Display.ContentDisplay
{
public class ContentPartDisplayOption : ContentPartOptionBase
{
private readonly List<ContentPartDisplayDriverOption> _partDisplayDrivers = new();
private readonly List<ContentPartDisplayDriverOption> _partDisplayDrivers = [];

public ContentPartDisplayOption(Type contentPartType) : base(contentPartType)
{
}

private List<ContentPartDisplayDriverOption> _displayDrivers;
public IReadOnlyList<ContentPartDisplayDriverOption> DisplayDrivers => _displayDrivers ??= _partDisplayDrivers.Where(d => d.DisplayMode != null).ToList();
private IReadOnlyList<ContentPartDisplayDriverOption> _displayDrivers;
private IReadOnlyList<ContentPartDisplayDriverOption> _editorDrivers;

private List<ContentPartDisplayDriverOption> _editorDrivers;
public IReadOnlyList<ContentPartDisplayDriverOption> EditorDrivers => _editorDrivers ??= _partDisplayDrivers.Where(d => d.Editor != null).ToList();
public IReadOnlyList<ContentPartDisplayDriverOption> DisplayDrivers
=> _displayDrivers ??= _partDisplayDrivers.Where(d => d.DisplayMode != null).ToList();

public IReadOnlyList<ContentPartDisplayDriverOption> EditorDrivers
=> _editorDrivers ??= _partDisplayDrivers.Where(d => d.Editor != null).ToList();

internal void ForDisplayMode(Type displayDriverType, Func<string, bool> predicate)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Extensions.Localization;

namespace OrchardCore.Rules
{
public class ConditionOperatorOptions
{
private FrozenDictionary<string, IConditionOperatorFactory> _factories;
private FrozenDictionary<Type, ConditionOperatorOption> _conditionOperatorOptionByType;

private Dictionary<string, IConditionOperatorFactory> _factories;
public IReadOnlyDictionary<string, IConditionOperatorFactory> Factories => _factories ??= Operators.ToDictionary(x => x.Factory.Name, x => x.Factory);
public IReadOnlyDictionary<string, IConditionOperatorFactory> Factories
=> _factories ??= Operators.ToFrozenDictionary(x => x.Factory.Name, x => x.Factory);

private Dictionary<Type, ConditionOperatorOption> _conditionOperatorOptionByType;
public IReadOnlyDictionary<Type, ConditionOperatorOption> ConditionOperatorOptionByType => _conditionOperatorOptionByType ??= Operators.ToDictionary(x => x.Operator, x => x);
public IReadOnlyDictionary<Type, ConditionOperatorOption> ConditionOperatorOptionByType
=> _conditionOperatorOptionByType ??= Operators.ToFrozenDictionary(x => x.Operator, x => x);

public List<ConditionOperatorOption> Operators { get; set; } = new();
public List<ConditionOperatorOption> Operators { get; set; } = [];
}

public class ConditionOperatorOption<TLocalizer> : ConditionOperatorOption where TLocalizer : class
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
using System;
using System.Collections.Frozen;
using System.Collections.Generic;
using System.Collections.Immutable;

namespace OrchardCore.Sms;

public class SmsProviderOptions
{
private Dictionary<string, Type> _providers { get; } = new();
private Dictionary<string, Type> _providers { get; } = [];

private IReadOnlyDictionary<string, Type> _readonlyProviders;
private FrozenDictionary<string, Type> _readonlyProviders;

/// <summary>
/// This read-only collections contains all registered SMS providers.
/// The 'Key' is the technical name of the provider.
/// The 'Value' is the type of the SMS provider. The type will awalys be an implementation of <see cref="ISmsProvider"></see> interface.
/// The 'Value' is the type of the SMS provider. The type will always be an implementation of <see cref="ISmsProvider"></see> interface.
/// </summary>
public IReadOnlyDictionary<string, Type> Providers => _readonlyProviders ??= _providers.ToImmutableDictionary(x => x.Key, x => x.Value);
public IReadOnlyDictionary<string, Type> Providers
=> _readonlyProviders ??= _providers.ToFrozenDictionary(x => x.Key, x => x.Value);

/// <summary>
/// Adds a provider if one does not exist.
Expand Down

0 comments on commit 2877187

Please sign in to comment.