| | | 1 | | namespace Anichron.Worker.Ingestion.Proxy; |
| | | 2 | | |
| | | 3 | | internal 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 | | |
| | | 16 | | internal 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) |
| | 55 | 25 | | => $"{contentHash[..2]}/{storageConfigId:N}-{contentHash}"; |
| | | 26 | | |
| | | 27 | | public Guid? StorageConfigOf(string relativePath) |
| | 36 | 28 | | => relativePath.Split('/') is [_, var directory, ..] |
| | 36 | 29 | | && directory.Split('-', 2) is [var config, _] |
| | 36 | 30 | | && Guid.TryParseExact(config, "N", out var id) |
| | 36 | 31 | | ? id |
| | 36 | 32 | | : null; |
| | | 33 | | |
| | | 34 | | public string? ContentDirectoryOf(string relativeFilePath) |
| | 19 | 35 | | => relativeFilePath.Split('/') is [var shard, var directory, _] && StorageConfigOf(relativeFilePath) is not null |
| | 19 | 36 | | ? $"{shard}/{directory}" |
| | 19 | 37 | | : null; |
| | | 38 | | } |