FileDatabase refactor for normalization and consistency

This commit is contained in:
daniel-c-harvey
2025-09-04 16:26:10 -04:00
parent e82366e47f
commit 6fefcbcfb5
24 changed files with 3069 additions and 186 deletions
@@ -50,7 +50,7 @@ public class FileDatabase : DirectoryIndexDirectory
private async Task InitVaultAsync(EntryKey vaultKey)
{
var path = Path.Combine(RootPath, vaultKey.Key);
var directoryVault = await ImageDirectoryVault.FromAsync(path);
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type);
if (directoryVault != null)
{
@@ -82,7 +82,7 @@ public class FileDatabase : DirectoryIndexDirectory
try
{
var path = Path.Combine(RootPath, vaultKey.Key);
var directoryVault = await ImageDirectoryVault.FromAsync(path);
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type);
if (directoryVault != null)
{
@@ -92,7 +92,6 @@ public class FileDatabase : DirectoryIndexDirectory
}
catch
{
// Re-throw to maintain the same error behavior as TypeScript version
throw;
}
}
@@ -0,0 +1,111 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils;
using IndexType = DeepDrftContent.FileDatabase.Services.IndexType;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Factory service for creating and managing indexes
/// </summary>
public class IndexFactoryService : IIndexFactory, IIndexDataFactory
{
private readonly Dictionary<IndexType, Func<string, IIndex>> _indexCreators;
private readonly Dictionary<IndexType, Func<object, IIndex>> _indexFromDataCreators;
private readonly Dictionary<IndexType, Func<IIndex, object>> _indexDataCreators;
public IndexFactoryService()
{
_indexCreators = new Dictionary<IndexType, Func<string, IIndex>>
{
{ IndexType.Directory, rootPath => new DirectoryIndex(new DirectoryIndexData(Path.GetFileName(rootPath))) },
{ IndexType.Vault, rootPath => new VaultIndex(new VaultIndexData(Path.GetFileName(rootPath))) }
};
_indexFromDataCreators = new Dictionary<IndexType, Func<object, IIndex>>
{
{ IndexType.Directory, data => new DirectoryIndex((DirectoryIndexData)data) },
{ IndexType.Vault, data => new VaultIndex((VaultIndexData)data) }
};
_indexDataCreators = new Dictionary<IndexType, Func<IIndex, object>>
{
{ IndexType.Directory, index => DirectoryIndexData.FromIndex((DirectoryIndex)index) },
{ IndexType.Vault, index => VaultIndexData.FromIndex((VaultIndex)index) }
};
}
public async Task<IIndex?> CreateIndexAsync(IndexType type, string rootPath)
{
if (!_indexCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
var index = creator(rootPath);
// Ensure directory exists and save the index
await FileUtils.MakeVaultDirectoryAsync(rootPath);
await SaveIndexAsync(rootPath, type, index);
return index;
}
public async Task<IIndex?> LoadIndexAsync(IndexType type, string rootPath)
{
if (!_indexFromDataCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
var indexPath = Path.Combine(rootPath, "index");
object indexData = type switch
{
IndexType.Directory => await FileUtils.FetchObjectAsync<DirectoryIndexData>(indexPath),
IndexType.Vault => await FileUtils.FetchObjectAsync<VaultIndexData>(indexPath),
_ => throw new ArgumentException($"Unknown index type: {type}")
};
return creator(indexData);
}
public async Task<IIndex?> LoadOrCreateIndexAsync(IndexType type, string rootPath)
{
try
{
return await LoadIndexAsync(type, rootPath);
}
catch
{
return await CreateIndexAsync(type, rootPath);
}
}
public object CreateIndexData(IndexType type, IIndex index)
{
if (!_indexDataCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
return creator(index);
}
public IIndex CreateIndexFromData(IndexType type, object indexData)
{
if (!_indexFromDataCreators.TryGetValue(type, out var creator))
{
throw new ArgumentException($"Unknown index type: {type}");
}
return creator(indexData);
}
private async Task SaveIndexAsync(string rootPath, IndexType type, IIndex index)
{
var indexPath = Path.Combine(rootPath, "index");
var indexData = CreateIndexData(type, index);
await FileUtils.PutObjectAsync(indexPath, indexData);
}
}
@@ -1,3 +1,4 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
using DeepDrftContent.FileDatabase.Utils;
@@ -19,11 +20,13 @@ public abstract class AbstractIndexContainer
{
protected IndexType Type { get; }
public string RootPath { get; }
private readonly IIndexDataFactory _indexDataFactory;
protected AbstractIndexContainer(string path, IndexType type)
protected AbstractIndexContainer(string path, IndexType type, IIndexDataFactory? indexDataFactory = null)
{
RootPath = path;
Type = type;
_indexDataFactory = indexDataFactory ?? new IndexFactoryService();
}
public string GetKey() => Path.GetFileName(RootPath);
@@ -31,78 +34,30 @@ public abstract class AbstractIndexContainer
protected async Task SaveIndexAsync<T>(T index) where T : IIndex
{
var indexPath = Path.Combine(RootPath, "index");
object indexData = Type switch
{
IndexType.Directory when index is DirectoryIndex dirIndex => DirectoryIndexData.FromIndex(dirIndex),
IndexType.Vault when index is VaultIndex vaultIndex => VaultIndexData.FromIndex(vaultIndex),
_ => throw new ArgumentException($"Invalid index type {Type} for index {typeof(T)}")
};
var indexData = _indexDataFactory.CreateIndexData(Type, index);
await FileUtils.PutObjectAsync(indexPath, indexData);
}
}
/// <summary>
/// Factory for creating and loading indexes
/// Factory for creating and loading indexes - delegates to IIndexFactory
/// </summary>
public class IndexFactory : AbstractIndexContainer
{
public IndexFactory(string path, IndexType type) : base(path, type) { }
private readonly IIndexFactory _factoryService;
public IndexFactory(string path, IndexType type, IIndexFactory? factoryService = null, IIndexDataFactory? indexDataFactory = null)
: base(path, type, indexDataFactory)
{
_factoryService = factoryService ?? new IndexFactoryService();
}
/// <summary>
/// Builds an index by loading existing or creating new
/// </summary>
public async Task<IIndex?> BuildIndexAsync()
{
try
{
return await LoadOrCreateIndexAsync();
}
catch
{
return null;
}
}
private async Task<IIndex?> LoadOrCreateIndexAsync()
{
try
{
return await LoadIndexAsync();
}
catch
{
return await CreateIndexAsync();
}
}
private async Task<IIndex?> LoadIndexAsync()
{
var indexPath = Path.Combine(RootPath, "index");
IIndex result = Type switch
{
IndexType.Directory => new DirectoryIndex(await FileUtils.FetchObjectAsync<DirectoryIndexData>(indexPath)),
IndexType.Vault => new VaultIndex(await FileUtils.FetchObjectAsync<VaultIndexData>(indexPath)),
_ => throw new ArgumentException($"Unknown index type: {Type}")
};
return result;
}
private async Task<IIndex?> CreateIndexAsync()
{
IIndex index = Type switch
{
IndexType.Directory => new DirectoryIndex(new DirectoryIndexData(RootPath)),
IndexType.Vault => new VaultIndex(new VaultIndexData(RootPath)),
_ => throw new ArgumentException($"Unknown index type: {Type}")
};
await FileUtils.MakeVaultDirectoryAsync(RootPath);
await SaveIndexAsync(index);
return index;
return await _factoryService.LoadOrCreateIndexAsync(Type, RootPath);
}
}
@@ -111,9 +66,10 @@ public class IndexFactory : AbstractIndexContainer
/// </summary>
public abstract class IndexDirectory : AbstractIndexContainer
{
protected IIndex Index { get; }
protected IEntryQueryable Index { get; }
protected IndexDirectory(string rootPath, IndexType type, IIndex index) : base(rootPath, type)
protected IndexDirectory(string rootPath, IndexType type, IEntryQueryable index, IIndexDataFactory? indexDataFactory = null)
: base(rootPath, type, indexDataFactory)
{
Index = index;
}
@@ -130,16 +86,18 @@ public abstract class IndexDirectory : AbstractIndexContainer
/// </summary>
public class DirectoryIndexDirectory : IndexDirectory
{
public DirectoryIndexDirectory(string rootPath, DirectoryIndex index)
: base(rootPath, IndexType.Directory, index) { }
private readonly IDirectoryIndex _directoryIndex;
public DirectoryIndexDirectory(string rootPath, IDirectoryIndex index, IIndexDataFactory? indexDataFactory = null)
: base(rootPath, IndexType.Directory, index, indexDataFactory)
{
_directoryIndex = index;
}
protected async Task AddToIndexAsync(EntryKey entryKey)
{
if (Index is DirectoryIndex dirIndex)
{
dirIndex.PutEntry(entryKey);
await SaveIndexAsync(dirIndex);
}
_directoryIndex.PutEntry(entryKey);
await SaveIndexAsync(_directoryIndex);
}
}
@@ -148,15 +106,17 @@ public class DirectoryIndexDirectory : IndexDirectory
/// </summary>
public class VaultIndexDirectory : IndexDirectory
{
public VaultIndexDirectory(string rootPath, VaultIndex index)
: base(rootPath, IndexType.Vault, index) { }
private readonly IVaultIndex _vaultIndex;
public VaultIndexDirectory(string rootPath, IVaultIndex index, IIndexDataFactory? indexDataFactory = null)
: base(rootPath, IndexType.Vault, index, indexDataFactory)
{
_vaultIndex = index;
}
protected async Task AddToIndexAsync(EntryKey entryKey, MetaData metaData)
{
if (Index is VaultIndex vaultIndex)
{
vaultIndex.PutEntry(entryKey, metaData);
await SaveIndexAsync(vaultIndex);
}
_vaultIndex.PutEntry(entryKey, metaData);
await SaveIndexAsync(_vaultIndex);
}
}
@@ -45,7 +45,7 @@ public abstract class MediaVault : VaultIndexDirectory
var (buffer, extension) = ExtractMediaProperties(media);
var mediaPath = GetMediaPathFromEntryKey(entryKey.Key, extension);
var metaData = MetaDataFactory.Create(vaultType, entryKey.Key, extension, GetAspectRatio(media));
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryKey.Key, extension, media);
await AddToIndexAsync(entryKey, metaData);
await FileUtils.PutFileAsync(mediaPath, buffer);
@@ -86,42 +86,51 @@ public abstract class MediaVault : VaultIndexDirectory
return media switch
{
ImageBinary imageBinary => (imageBinary.Buffer, imageBinary.Extension),
AudioBinary audioBinary => (audioBinary.Buffer, audioBinary.Extension),
MediaBinary mediaBinary => (mediaBinary.Buffer, mediaBinary.Extension),
_ => throw new ArgumentException($"Unsupported media type: {media.GetType()}")
};
}
/// <summary>
/// Extracts aspect ratio from media object if it's an image
/// </summary>
private static double GetAspectRatio(object media)
{
return media is ImageBinary imageBinary ? imageBinary.AspectRatio : 1.0;
}
}
/// <summary>
/// Concrete implementation of MediaVault for image storage
/// </summary>
public class ImageDirectoryVault : MediaVault
public class ImageVault : MediaVault
{
private ImageDirectoryVault(string rootPath, VaultIndex index) : base(rootPath, index) { }
private ImageVault(string rootPath, VaultIndex index) : base(rootPath, index) { }
/// <summary>
/// Factory method to create an ImageDirectoryVault instance
/// Factory method to create an ImageVault instance
/// </summary>
public static async Task<ImageDirectoryVault?> FromAsync(string rootPath)
public static async Task<ImageVault?> FromAsync(string rootPath)
{
var factory = new IndexFactory(rootPath, IndexType.Vault);
var index = await factory.BuildIndexAsync();
if (index is VaultIndex vaultIndex)
{
return new ImageDirectoryVault(rootPath, vaultIndex);
return new ImageVault(rootPath, vaultIndex);
}
return null;
}
}
public class AudioVault : MediaVault
{
private AudioVault(string rootPath, VaultIndex index) : base(rootPath, index) { }
public static async Task<AudioVault?> FromAsync(string rootPath)
{
var factory = new IndexFactory(rootPath, IndexType.Vault);
var index = await factory.BuildIndexAsync();
if (index is VaultIndex vaultIndex)
{
return new AudioVault(rootPath, vaultIndex);
}
return null;
}
}
@@ -0,0 +1,17 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Factory for creating media vaults - simple dictionary-based approach
/// </summary>
public static class MediaVaultFactory
{
private static readonly IMediaTypeRegistry _registry = new SimpleMediaTypeRegistry();
public static async Task<MediaVault?> From(string rootPath, MediaVaultType mediaType)
{
return await _registry.CreateVaultAsync(mediaType, rootPath);
}
}
@@ -0,0 +1,149 @@
using DeepDrftContent.FileDatabase.Abstractions;
using DeepDrftContent.FileDatabase.Models;
namespace DeepDrftContent.FileDatabase.Services;
/// <summary>
/// Simple dictionary-based registry for media type factories
/// </summary>
public class SimpleMediaTypeRegistry : IMediaTypeRegistry
{
private readonly Dictionary<MediaVaultType, Func<object, FileBinary>> _binaryFactories = new();
private readonly Dictionary<MediaVaultType, Func<object, FileBinary>> _binaryFromDtoFactories = new();
private readonly Dictionary<MediaVaultType, Func<FileBinary, FileBinaryDto>> _dtoFactories = new();
private readonly Dictionary<MediaVaultType, Func<string, string, object, MetaData>> _metaDataFromMediaFactories = new();
private readonly Dictionary<MediaVaultType, Func<FileBinary, MetaData, object>> _paramsFactories = new();
private readonly Dictionary<MediaVaultType, Func<string, Task<MediaVault?>>> _vaultFactories = new();
private readonly Dictionary<MediaVaultType, Type> _binaryTypes = new();
private readonly Dictionary<MediaVaultType, Type> _dtoTypes = new();
private readonly Dictionary<MediaVaultType, Type> _paramsTypes = new();
private readonly Dictionary<MediaVaultType, Type> _metaDataTypes = new();
public SimpleMediaTypeRegistry()
{
// Clean one-line registrations with generics - no reflection!
RegisterType<MediaBinary, MediaBinaryParams, MediaBinaryDto, MetaData>(
MediaVaultType.Media,
p => new MediaBinary(p),
dto => MediaBinary.From(dto),
binary => new MediaBinaryDto(binary),
(key, ext, _) => new MetaData(key, ext),
(binary, meta) => new MediaBinaryParams(binary.Buffer, binary.Size, meta.Extension));
RegisterType<ImageBinary, ImageBinaryParams, ImageBinaryDto, ImageMetaData>(
MediaVaultType.Image,
p => new ImageBinary(p),
dto => ImageBinary.From(dto),
binary => new ImageBinaryDto(binary),
(key, ext, media) => media is ImageBinary img ? new ImageMetaData(key, ext, img.AspectRatio) : new MetaData(key, ext),
(binary, meta) => meta is ImageMetaData imgMeta
? new ImageBinaryParams(binary.Buffer, binary.Size, meta.Extension, imgMeta.AspectRatio)
: throw new ArgumentException("ImageBinary requires ImageMetaData"),
async path => await ImageVault.FromAsync(path));
RegisterType<AudioBinary, AudioBinaryParams, AudioBinaryDto, AudioMetaData>(
MediaVaultType.Audio,
p => new AudioBinary(p),
dto => AudioBinary.From(dto),
binary => new AudioBinaryDto(binary),
(key, ext, media) => media is AudioBinary audio ? new AudioMetaData(key, ext, audio.Duration, audio.Bitrate) : new MetaData(key, ext),
(binary, meta) => meta is AudioMetaData audioMeta
? new AudioBinaryParams(binary.Buffer, binary.Size, meta.Extension, audioMeta.Duration, audioMeta.Bitrate)
: throw new ArgumentException("AudioBinary requires AudioMetaData"),
async path => await AudioVault.FromAsync(path));
}
private void RegisterType<TBinary, TParams, TDto, TMetaData>(
MediaVaultType vaultType,
Func<TParams, TBinary> binaryFactory,
Func<TDto, TBinary> binaryFromDtoFactory,
Func<TBinary, TDto> dtoFactory,
Func<string, string, object, MetaData> metaDataFactory,
Func<FileBinary, MetaData, object> paramsFactory,
Func<string, Task<MediaVault?>>? vaultFactory = null)
where TBinary : FileBinary
where TParams : FileBinaryParams
where TDto : FileBinaryDto
where TMetaData : MetaData
{
_binaryFactories[vaultType] = p => binaryFactory((TParams)p);
_binaryFromDtoFactories[vaultType] = dto => binaryFromDtoFactory((TDto)dto);
_dtoFactories[vaultType] = binary => dtoFactory((TBinary)binary);
_metaDataFromMediaFactories[vaultType] = metaDataFactory;
_paramsFactories[vaultType] = paramsFactory;
_binaryTypes[vaultType] = typeof(TBinary);
_dtoTypes[vaultType] = typeof(TDto);
_paramsTypes[vaultType] = typeof(TParams);
_metaDataTypes[vaultType] = typeof(TMetaData);
if (vaultFactory != null)
_vaultFactories[vaultType] = vaultFactory;
}
// Public interface implementation - allows external registration
public void RegisterMediaType<TBinary, TParams, TDto, TMetaData, TVault>(MediaVaultType vaultType)
where TBinary : FileBinary
where TParams : FileBinaryParams
where TDto : FileBinaryDto
where TMetaData : MetaData
{
// For now, we can't auto-generate the factories without reflection
// This would need to be implemented if external registration is needed
throw new NotImplementedException("Use RegisterType method for internal registration. External registration not yet implemented.");
}
public FileBinary CreateBinary(MediaVaultType vaultType, object parameters)
{
return _binaryFactories.TryGetValue(vaultType, out var factory)
? factory(parameters)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public FileBinary CreateBinaryFromDto(MediaVaultType vaultType, object dto)
{
return _binaryFromDtoFactories.TryGetValue(vaultType, out var factory)
? factory(dto)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public FileBinaryDto CreateDto(MediaVaultType vaultType, FileBinary binary)
{
return _dtoFactories.TryGetValue(vaultType, out var factory)
? factory(binary)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public MetaData CreateMetaDataFromMedia(MediaVaultType vaultType, string entryKey, string extension, object media)
{
return _metaDataFromMediaFactories.TryGetValue(vaultType, out var factory)
? factory(entryKey, extension, media)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public object CreateParams(MediaVaultType vaultType, FileBinary fileBinary, MetaData metaData)
{
return _paramsFactories.TryGetValue(vaultType, out var factory)
? factory(fileBinary, metaData)
: throw new ArgumentException($"Unknown vault type: {vaultType}");
}
public async Task<MediaVault?> CreateVaultAsync(MediaVaultType vaultType, string rootPath)
{
return _vaultFactories.TryGetValue(vaultType, out var factory)
? await factory(rootPath)
: null;
}
public Type GetBinaryType(MediaVaultType vaultType) =>
_binaryTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
public Type GetDtoType(MediaVaultType vaultType) =>
_dtoTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
public Type GetParamsType(MediaVaultType vaultType) =>
_paramsTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
public Type GetMetaDataType(MediaVaultType vaultType) =>
_metaDataTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
}