< Summary

Information
Class: Anichron.Worker.Ingestion.Proxy.TwoLevelHexShardStrategy
Assembly: Anichron.Worker
File(s): /home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Proxy/ProxyShardStrategy.cs
Tag: 19_45211875487
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 38
Line coverage: 100%
Branch coverage
81%
Covered branches: 13
Total branches: 16
Branch coverage: 81.2%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetDirectory(...)100%11100%
StorageConfigOf(...)80%1010100%
ContentDirectoryOf(...)83.33%66100%

File(s)

/home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Proxy/ProxyShardStrategy.cs

#LineLine coverage
 1namespace Anichron.Worker.Ingestion.Proxy;
 2
 3internal interface IProxyDirectoryStrategy
 4{
 5    string GetDirectory(Guid storageConfigId, string contentHash);
 6
 7    /// <summary>The storage config whose directory holds a path relative to the proxy root, or null when it names none.
 8    /// <param name="relativePath">A directory <see cref="GetDirectory"/> built, or a path inside one.</param>
 9    Guid? StorageConfigOf(string relativePath);
 10
 11    /// <summary>The directory <see cref="GetDirectory"/> built that directly holds a file, or null when none does.</sum
 12    /// <param name="relativeFilePath">A file's path relative to the proxy root.</param>
 13    string? ContentDirectoryOf(string relativeFilePath);
 14}
 15
 16internal sealed class TwoLevelHexShardStrategy : IProxyDirectoryStrategy
 17{
 18    // Derived from content rather than from the ingestion attempt, so a retry of a failed
 19    // file lands in the same directory and overwrites its own debris instead of stranding it.
 20    // Two hex chars of the content hash → 256 top-level buckets; prevents filesystem
 21    // directory-count blowup at large library sizes. The storage config id keeps byte-identical
 22    // files of different users apart — without it, removing one user's config would cascade
 23    // away the other user's proxies.
 24    public string GetDirectory(Guid storageConfigId, string contentHash)
 5525        => $"{contentHash[..2]}/{storageConfigId:N}-{contentHash}";
 26
 27    public Guid? StorageConfigOf(string relativePath)
 3628        => relativePath.Split('/') is [_, var directory, ..]
 3629            && directory.Split('-', 2) is [var config, _]
 3630            && Guid.TryParseExact(config, "N", out var id)
 3631                ? id
 3632                : null;
 33
 34    public string? ContentDirectoryOf(string relativeFilePath)
 1935        => relativeFilePath.Split('/') is [var shard, var directory, _] && StorageConfigOf(relativeFilePath) is not null
 1936            ? $"{shard}/{directory}"
 1937            : null;
 38}