< Summary

Information
Class: TeleFlow.Storage.Memory.MemoryStateDataStore
Assembly: TeleFlow.Storage.Memory
File(s): /_/src/TeleFlow.Storage.Memory/MemoryStateDataStore.cs
Line coverage
100%
Covered lines: 21
Uncovered lines: 0
Coverable lines: 21
Total lines: 71
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor()100%11100%
GetDataAsync(...)100%11100%
SetDataAsync(...)100%11100%
RemoveDataAsync(...)100%11100%
ClearDataAsync(...)100%44100%
ValidateDataKey(...)100%11100%

File(s)

/_/src/TeleFlow.Storage.Memory/MemoryStateDataStore.cs

#LineLine coverage
 1using System.Collections.Concurrent;
 2using TeleFlow.Framework.States;
 3
 4namespace TeleFlow.Storage.Memory;
 5
 6public sealed class MemoryStateDataStore : IStateDataStore
 7{
 378    private readonly ConcurrentDictionary<StateDataKey, string> _data = new();
 9
 10    public ValueTask<string?> GetDataAsync(
 11        StateKey key,
 12        string dataKey,
 13        CancellationToken cancellationToken = default)
 14    {
 3315        cancellationToken.ThrowIfCancellationRequested();
 3216        ValidateDataKey(dataKey);
 17
 3018        _data.TryGetValue(new StateDataKey(key, dataKey), out var value);
 3019        return ValueTask.FromResult(value);
 20    }
 21
 22    public ValueTask SetDataAsync(
 23        StateKey key,
 24        string dataKey,
 25        string value,
 26        CancellationToken cancellationToken = default)
 27    {
 2028        cancellationToken.ThrowIfCancellationRequested();
 1929        ValidateDataKey(dataKey);
 1730        ArgumentNullException.ThrowIfNull(value);
 31
 1632        _data[new StateDataKey(key, dataKey)] = value;
 1633        return ValueTask.CompletedTask;
 34    }
 35
 36    public ValueTask RemoveDataAsync(
 37        StateKey key,
 38        string dataKey,
 39        CancellationToken cancellationToken = default)
 40    {
 541        cancellationToken.ThrowIfCancellationRequested();
 442        ValidateDataKey(dataKey);
 43
 244        _data.TryRemove(new StateDataKey(key, dataKey), out _);
 245        return ValueTask.CompletedTask;
 46    }
 47
 48    public ValueTask ClearDataAsync(
 49        StateKey key,
 50        CancellationToken cancellationToken = default)
 51    {
 1052        cancellationToken.ThrowIfCancellationRequested();
 53
 3654        foreach (var candidate in _data.Keys)
 55        {
 956            if (candidate.StateKey == key)
 57            {
 858                _data.TryRemove(candidate, out _);
 59            }
 60        }
 61
 962        return ValueTask.CompletedTask;
 63    }
 64
 65    private static void ValidateDataKey(string dataKey)
 66    {
 5567        ArgumentException.ThrowIfNullOrWhiteSpace(dataKey);
 4968    }
 69
 70    private readonly record struct StateDataKey(StateKey StateKey, string DataKey);
 71}