| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Common.Multitenancy; |
| | | 4 | | using Elsa.Common.Services; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Labels.Contracts; |
| | | 7 | | using Elsa.Labels.Entities; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Labels.Services; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// An in-memory store of labels. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Ambient tenant is applied here rather than in callers. |
| | | 16 | | /// EF owns that via <c>SetTenantIdFilter</c> / <c>ApplyTenantId</c>; Memory must compensate. |
| | | 17 | | /// Labels contracts have no TenantAgnostic flag, so isolation always applies (EF query filter). |
| | | 18 | | /// </remarks> |
| | | 19 | | public class InMemoryLabelStore : ILabelStore |
| | | 20 | | { |
| | | 21 | | private readonly MemoryStore<Label> _labelStore; |
| | | 22 | | private readonly MemoryStore<WorkflowDefinitionLabel> _workflowDefinitionLabelStore; |
| | | 23 | | private readonly ITenantAccessor? _tenantAccessor; |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Constructor. |
| | | 27 | | /// </summary> |
| | 31 | 28 | | public InMemoryLabelStore( |
| | 31 | 29 | | MemoryStore<Label> labelStore, |
| | 31 | 30 | | MemoryStore<WorkflowDefinitionLabel> workflowDefinitionLabelStore, |
| | 31 | 31 | | ITenantAccessor? tenantAccessor = null) |
| | | 32 | | { |
| | 31 | 33 | | _labelStore = labelStore; |
| | 31 | 34 | | _workflowDefinitionLabelStore = workflowDefinitionLabelStore; |
| | 31 | 35 | | _tenantAccessor = tenantAccessor; |
| | 31 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public Task SaveAsync(Label record, CancellationToken cancellationToken = default) |
| | | 40 | | { |
| | 64 | 41 | | lock (_labelStore.Sync) |
| | | 42 | | { |
| | 64 | 43 | | ApplyCurrentTenant(record); |
| | 64 | 44 | | SyncNormalizedName(record); |
| | 64 | 45 | | EnsureNormalizedNameAvailable(record, [record]); |
| | 114 | 46 | | _labelStore.Save(record, x => x.Id); |
| | 57 | 47 | | } |
| | | 48 | | |
| | 57 | 49 | | return Task.CompletedTask; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public Task SaveManyAsync(IEnumerable<Label> records, CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | 7 | 55 | | var list = records.ToList(); |
| | | 56 | | |
| | 7 | 57 | | lock (_labelStore.Sync) |
| | | 58 | | { |
| | 34 | 59 | | foreach (var record in list) |
| | | 60 | | { |
| | 10 | 61 | | ApplyCurrentTenant(record); |
| | 10 | 62 | | SyncNormalizedName(record); |
| | | 63 | | } |
| | | 64 | | |
| | 26 | 65 | | foreach (var record in list) |
| | 8 | 66 | | EnsureNormalizedNameAvailable(record, list); |
| | | 67 | | |
| | 7 | 68 | | _labelStore.SaveMany(list, x => x.Id); |
| | 3 | 69 | | } |
| | | 70 | | |
| | 3 | 71 | | return Task.CompletedTask; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | public Task<bool> DeleteAsync(string id, CancellationToken cancellationToken = default) |
| | | 76 | | { |
| | 4 | 77 | | lock (_labelStore.Sync) |
| | 4 | 78 | | lock (_workflowDefinitionLabelStore.Sync) |
| | | 79 | | { |
| | 14 | 80 | | var deleted = _labelStore.DeleteWhere(x => x.Id == id && IsVisible(x)); |
| | | 81 | | |
| | 4 | 82 | | if (deleted == 0) |
| | 3 | 83 | | return Task.FromResult(false); |
| | | 84 | | |
| | 5 | 85 | | _workflowDefinitionLabelStore.DeleteWhere(x => x.LabelId == id && IsVisible(x)); |
| | 1 | 86 | | return Task.FromResult(true); |
| | | 87 | | } |
| | 4 | 88 | | } |
| | | 89 | | |
| | | 90 | | /// <inheritdoc /> |
| | | 91 | | public Task<long> DeleteManyAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default) |
| | | 92 | | { |
| | 3 | 93 | | var idList = ids.ToList(); |
| | | 94 | | |
| | 3 | 95 | | lock (_labelStore.Sync) |
| | 3 | 96 | | lock (_workflowDefinitionLabelStore.Sync) |
| | | 97 | | { |
| | 13 | 98 | | var deleted = _labelStore.DeleteWhere(x => idList.Contains(x.Id) && IsVisible(x)); |
| | 10 | 99 | | _workflowDefinitionLabelStore.DeleteWhere(x => idList.Contains(x.LabelId) && IsVisible(x)); |
| | 3 | 100 | | return Task.FromResult(deleted); |
| | | 101 | | } |
| | 3 | 102 | | } |
| | | 103 | | |
| | | 104 | | /// <inheritdoc /> |
| | | 105 | | public Task<Label?> FindByIdAsync(string id, CancellationToken cancellationToken = default) |
| | | 106 | | { |
| | 38 | 107 | | var found = FindVisibleLabel(id); |
| | 38 | 108 | | return Task.FromResult(found is null ? null : Clone(found)); |
| | | 109 | | } |
| | | 110 | | |
| | | 111 | | /// <inheritdoc /> |
| | | 112 | | public Task<Page<Label>> ListAsync(PageArgs? pageArgs = default, CancellationToken cancellationToken = default) |
| | | 113 | | { |
| | 13 | 114 | | var query = _labelStore.List().AsQueryable().WhereVisibleToTenant(CurrentTenantId).OrderBy(x => x.Name); |
| | 13 | 115 | | var page = query.ToPage(pageArgs); |
| | 13 | 116 | | return Task.FromResult(Page.Of(page.Items.Select(Clone).ToList(), page.TotalCount)); |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | public Task<IEnumerable<Label>> FindManyByIdAsync(IEnumerable<string> ids, CancellationToken cancellationToken) |
| | | 121 | | { |
| | 2 | 122 | | var idList = ids.ToList(); |
| | 4 | 123 | | var records = _labelStore.Query(query => query.WhereVisibleToTenant(CurrentTenantId).Where(x => idList.Contains( |
| | 2 | 124 | | .Select(Clone) |
| | 2 | 125 | | .ToList(); |
| | 2 | 126 | | return Task.FromResult<IEnumerable<Label>>(records); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | private Label? FindVisibleLabel(string id) => |
| | 76 | 130 | | _labelStore.Query(query => query.WhereVisibleToTenant(CurrentTenantId).Where(x => x.Id == id)).FirstOrDefault(); |
| | | 131 | | |
| | | 132 | | /// <summary> |
| | | 133 | | /// Clone-on-read, matching Memory identity stores. Labels.Update does Find → mutate |
| | | 134 | | /// Name (which writes NormalizedName) → Save. Without a copy, a rejected rename |
| | | 135 | | /// would already have mutated the stored row. |
| | | 136 | | /// </summary> |
| | | 137 | | private static Label Clone(Label label) => |
| | 53 | 138 | | new() |
| | 53 | 139 | | { |
| | 53 | 140 | | Id = label.Id, |
| | 53 | 141 | | TenantId = label.TenantId, |
| | 53 | 142 | | Name = label.Name, |
| | 53 | 143 | | NormalizedName = label.NormalizedName, |
| | 53 | 144 | | Description = label.Description, |
| | 53 | 145 | | Color = label.Color |
| | 53 | 146 | | }; |
| | | 147 | | |
| | 16 | 148 | | private bool IsVisible(Entity entity) => TenantVisibility.IsVisible(entity.TenantId, CurrentTenantId); |
| | | 149 | | |
| | 69 | 150 | | private string CurrentTenantId => _tenantAccessor?.TenantId ?? Tenant.DefaultTenantId; |
| | | 151 | | |
| | | 152 | | private void ApplyCurrentTenant(Entity entity) |
| | | 153 | | { |
| | 74 | 154 | | if (entity.TenantId == Tenant.AgnosticTenantId || _tenantAccessor is null) |
| | 10 | 155 | | return; |
| | | 156 | | |
| | 64 | 157 | | entity.TenantId ??= _tenantAccessor.TenantId; |
| | 64 | 158 | | } |
| | | 159 | | |
| | | 160 | | private static void SyncNormalizedName(Label record) => |
| | 74 | 161 | | record.NormalizedName = record.Name.ToLowerInvariant(); |
| | | 162 | | |
| | | 163 | | /// <summary> |
| | | 164 | | /// Memory counterpart of the EF unique index on <c>(TenantId, NormalizedName)</c>. |
| | | 165 | | /// Same-Id upserts are allowed so a row can rename itself. Incoming batch rows |
| | | 166 | | /// replace same-Id store rows, so those store rows are ignored here. |
| | | 167 | | /// </summary> |
| | | 168 | | private void EnsureNormalizedNameAvailable(Label record, IReadOnlyCollection<Label> batch) |
| | | 169 | | { |
| | 148 | 170 | | var batchIds = batch.Select(x => x.Id).ToHashSet(); |
| | 72 | 171 | | var existing = _labelStore.Find(candidate => |
| | 166 | 172 | | candidate.TenantId == record.TenantId |
| | 166 | 173 | | && candidate.Id != record.Id |
| | 166 | 174 | | && !batchIds.Contains(candidate.Id) |
| | 166 | 175 | | && candidate.NormalizedName == record.NormalizedName); |
| | | 176 | | |
| | 72 | 177 | | if (existing is not null) |
| | 9 | 178 | | throw DuplicateNormalizedName(record); |
| | | 179 | | |
| | 63 | 180 | | if (batch.Any(other => |
| | 130 | 181 | | other.Id != record.Id |
| | 130 | 182 | | && other.TenantId == record.TenantId |
| | 130 | 183 | | && other.NormalizedName == record.NormalizedName)) |
| | | 184 | | { |
| | 2 | 185 | | throw DuplicateNormalizedName(record); |
| | | 186 | | } |
| | 61 | 187 | | } |
| | | 188 | | |
| | | 189 | | private static InvalidOperationException DuplicateNormalizedName(Label record) => |
| | 11 | 190 | | new($"A label already exists with normalized name '{record.NormalizedName}' in tenant '{record.TenantId}'."); |
| | | 191 | | } |