FileDatabase Refactor and normalization
Test cleanup
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using DeepDrftContent.FileDatabase.Models;
|
||||
using DeepDrftContent.FileDatabase.Utils;
|
||||
using IndexType = DeepDrftContent.FileDatabase.Services.IndexType;
|
||||
|
||||
namespace DeepDrftContent.FileDatabase.Services;
|
||||
|
||||
@@ -8,19 +9,19 @@ namespace DeepDrftContent.FileDatabase.Services;
|
||||
/// </summary>
|
||||
public class FileDatabase : DirectoryIndexDirectory
|
||||
{
|
||||
private readonly StructuralMap<EntryKey, MediaVault> _vaults;
|
||||
private readonly StructuralMap<string, MediaVault> _vaults;
|
||||
|
||||
/// <summary>
|
||||
/// Factory method to create a FileDatabase instance
|
||||
/// </summary>
|
||||
public static async Task<FileDatabase?> FromAsync(string rootPath)
|
||||
{
|
||||
var factory = new IndexFactory(rootPath, IndexType.Directory);
|
||||
var rootIndex = await factory.BuildIndexAsync();
|
||||
var factoryService = new IndexFactoryService();
|
||||
var rootIndex = await factoryService.LoadOrCreateDirectoryIndexAsync(rootPath);
|
||||
|
||||
if (rootIndex is DirectoryIndex directoryIndex)
|
||||
if (rootIndex != null)
|
||||
{
|
||||
var db = new FileDatabase(rootPath, directoryIndex);
|
||||
var db = new FileDatabase(rootPath, (DirectoryIndex)rootIndex);
|
||||
await db.InitVaultsAsync();
|
||||
return db;
|
||||
}
|
||||
@@ -30,7 +31,7 @@ public class FileDatabase : DirectoryIndexDirectory
|
||||
|
||||
private FileDatabase(string rootPath, DirectoryIndex index) : base(rootPath, index)
|
||||
{
|
||||
_vaults = new StructuralMap<EntryKey, MediaVault>();
|
||||
_vaults = new StructuralMap<string, MediaVault>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -38,56 +39,86 @@ public class FileDatabase : DirectoryIndexDirectory
|
||||
/// </summary>
|
||||
private async Task InitVaultsAsync()
|
||||
{
|
||||
foreach (var vaultKey in GetIndexEntries())
|
||||
foreach (var vaultId in GetIndexEntries())
|
||||
{
|
||||
await InitVaultAsync(vaultKey);
|
||||
var vaultType = await GetVaultTypeFromIndex(vaultId);
|
||||
if (vaultType.HasValue)
|
||||
{
|
||||
await InitVaultAsync(vaultId, vaultType.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a specific vault
|
||||
/// </summary>
|
||||
private async Task InitVaultAsync(EntryKey vaultKey)
|
||||
private async Task InitVaultAsync(string vaultId, MediaVaultType vaultType)
|
||||
{
|
||||
var path = Path.Combine(RootPath, vaultKey.Key);
|
||||
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type);
|
||||
var path = Path.Combine(RootPath, vaultId);
|
||||
var directoryVault = await MediaVaultFactory.From(path, vaultType);
|
||||
|
||||
if (directoryVault != null)
|
||||
{
|
||||
_vaults.Set(vaultKey, directoryVault);
|
||||
_vaults.Set(vaultId, directoryVault);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Checks if a vault exists for the given key
|
||||
/// Gets vault type from the vault's index file
|
||||
/// </summary>
|
||||
public bool HasVault(EntryKey vaultKey)
|
||||
private async Task<MediaVaultType?> GetVaultTypeFromIndex(string vaultId)
|
||||
{
|
||||
return _vaults.Has(vaultKey);
|
||||
try
|
||||
{
|
||||
var factoryService = new IndexFactoryService();
|
||||
var vaultPath = Path.Combine(RootPath, vaultId);
|
||||
var index = await factoryService.LoadIndexAsync(IndexType.Vault, vaultPath);
|
||||
|
||||
if (index is VaultIndex vaultIndex)
|
||||
{
|
||||
return vaultIndex.VaultType;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// If we can't load the index, we can't determine the vault type
|
||||
// This might happen for legacy vaults or corrupted indexes
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a vault by key
|
||||
/// Checks if a vault exists for the given vault ID
|
||||
/// </summary>
|
||||
public MediaVault? GetVault(EntryKey vaultKey)
|
||||
public bool HasVault(string vaultId)
|
||||
{
|
||||
return HasVault(vaultKey) ? _vaults.Get(vaultKey) : null;
|
||||
return _vaults.Has(vaultId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a vault by vault ID
|
||||
/// </summary>
|
||||
public MediaVault? GetVault(string vaultId)
|
||||
{
|
||||
return HasVault(vaultId) ? _vaults.Get(vaultId) : null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new vault
|
||||
/// </summary>
|
||||
public async Task CreateVaultAsync(EntryKey vaultKey)
|
||||
public async Task CreateVaultAsync(string vaultId, MediaVaultType vaultType)
|
||||
{
|
||||
try
|
||||
{
|
||||
var path = Path.Combine(RootPath, vaultKey.Key);
|
||||
var directoryVault = await MediaVaultFactory.From(path, vaultKey.Type);
|
||||
var path = Path.Combine(RootPath, vaultId);
|
||||
var directoryVault = await MediaVaultFactory.From(path, vaultType);
|
||||
|
||||
if (directoryVault != null)
|
||||
{
|
||||
_vaults.Set(vaultKey, directoryVault);
|
||||
await AddToIndexAsync(vaultKey);
|
||||
_vaults.Set(vaultId, directoryVault);
|
||||
// Now using string-based index
|
||||
await AddToIndexAsync(vaultId);
|
||||
}
|
||||
}
|
||||
catch
|
||||
@@ -97,17 +128,17 @@ public class FileDatabase : DirectoryIndexDirectory
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Loads a resource from a specific vault
|
||||
/// Loads a resource from a specific vault (MediaVaultType inferred from T)
|
||||
/// </summary>
|
||||
public async Task<T?> LoadResourceAsync<T>(MediaVaultType vaultType, EntryKey vaultKey, EntryKey entryKey)
|
||||
public async Task<T?> LoadResourceAsync<T>(string vaultId, string entryId)
|
||||
where T : FileBinary
|
||||
{
|
||||
try
|
||||
{
|
||||
var vault = _vaults.Get(vaultKey);
|
||||
var vault = _vaults.Get(vaultId);
|
||||
if (vault != null)
|
||||
{
|
||||
return await vault.GetEntryAsync<T>(vaultType, entryKey);
|
||||
return await vault.GetEntryAsync<T>(entryId);
|
||||
}
|
||||
}
|
||||
catch
|
||||
@@ -119,16 +150,16 @@ public class FileDatabase : DirectoryIndexDirectory
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Registers a resource in a specific vault
|
||||
/// Registers a resource in a specific vault (MediaVaultType inferred from media type)
|
||||
/// </summary>
|
||||
public async Task<bool> RegisterResourceAsync(MediaVaultType vaultType, EntryKey vaultKey, EntryKey entryKey, object media)
|
||||
public async Task<bool> RegisterResourceAsync(string vaultId, string entryId, FileBinary media)
|
||||
{
|
||||
try
|
||||
{
|
||||
var directoryVault = _vaults.Get(vaultKey);
|
||||
var directoryVault = _vaults.Get(vaultId);
|
||||
if (directoryVault != null)
|
||||
{
|
||||
await directoryVault.AddEntryAsync(vaultType, entryKey, media);
|
||||
await directoryVault.AddEntryAsync(entryId, media);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -141,9 +172,9 @@ public class FileDatabase : DirectoryIndexDirectory
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets all vault keys managed by this database
|
||||
/// Gets all vault IDs managed by this database
|
||||
/// </summary>
|
||||
public IReadOnlyList<EntryKey> GetVaultKeys()
|
||||
public IReadOnlyList<string> GetVaultIds()
|
||||
{
|
||||
return _vaults.Keys.ToList().AsReadOnly();
|
||||
}
|
||||
@@ -155,4 +186,5 @@ public class FileDatabase : DirectoryIndexDirectory
|
||||
{
|
||||
return _vaults.Size;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -10,18 +10,11 @@ namespace DeepDrftContent.FileDatabase.Services;
|
||||
/// </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) },
|
||||
@@ -35,18 +28,14 @@ public class IndexFactoryService : IIndexFactory, IIndexDataFactory
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<IIndex?> CreateIndexAsync(IndexType type, string rootPath)
|
||||
public async Task<IDirectoryIndex?> CreateDirectoryIndexAsync(string rootPath)
|
||||
{
|
||||
if (!_indexCreators.TryGetValue(type, out var creator))
|
||||
{
|
||||
throw new ArgumentException($"Unknown index type: {type}");
|
||||
}
|
||||
|
||||
var index = creator(rootPath);
|
||||
var indexData = new DirectoryIndexData(Path.GetFileName(rootPath));
|
||||
var index = new DirectoryIndex(indexData);
|
||||
|
||||
// Ensure directory exists and save the index
|
||||
await FileUtils.MakeVaultDirectoryAsync(rootPath);
|
||||
await SaveIndexAsync(rootPath, type, index);
|
||||
await SaveIndexAsync(rootPath, IndexType.Directory, index);
|
||||
|
||||
return index;
|
||||
}
|
||||
@@ -70,15 +59,41 @@ public class IndexFactoryService : IIndexFactory, IIndexDataFactory
|
||||
return creator(indexData);
|
||||
}
|
||||
|
||||
public async Task<IIndex?> LoadOrCreateIndexAsync(IndexType type, string rootPath)
|
||||
public async Task<IDirectoryIndex?> LoadOrCreateDirectoryIndexAsync(string rootPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
return await LoadIndexAsync(type, rootPath);
|
||||
var index = await LoadIndexAsync(IndexType.Directory, rootPath);
|
||||
return index as IDirectoryIndex;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return await CreateIndexAsync(type, rootPath);
|
||||
return await CreateDirectoryIndexAsync(rootPath);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IVaultIndex?> CreateVaultIndexAsync(string rootPath, MediaVaultType vaultType)
|
||||
{
|
||||
var vaultIndexData = new VaultIndexData(Path.GetFileName(rootPath), vaultType);
|
||||
var index = new VaultIndex(vaultIndexData);
|
||||
|
||||
// Ensure directory exists and save the index
|
||||
await FileUtils.MakeVaultDirectoryAsync(rootPath);
|
||||
await SaveIndexAsync(rootPath, IndexType.Vault, index);
|
||||
|
||||
return index;
|
||||
}
|
||||
|
||||
public async Task<IVaultIndex?> LoadOrCreateVaultIndexAsync(string rootPath, MediaVaultType vaultType)
|
||||
{
|
||||
try
|
||||
{
|
||||
var index = await LoadIndexAsync(IndexType.Vault, rootPath);
|
||||
return index as IVaultIndex;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return await CreateVaultIndexAsync(rootPath, vaultType);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -39,27 +39,6 @@ public abstract class AbstractIndexContainer
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Factory for creating and loading indexes - delegates to IIndexFactory
|
||||
/// </summary>
|
||||
public class IndexFactory : AbstractIndexContainer
|
||||
{
|
||||
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()
|
||||
{
|
||||
return await _factoryService.LoadOrCreateIndexAsync(Type, RootPath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Abstract base class for directory containers that manage indexes
|
||||
@@ -74,11 +53,11 @@ public abstract class IndexDirectory : AbstractIndexContainer
|
||||
Index = index;
|
||||
}
|
||||
|
||||
protected IReadOnlyList<EntryKey> GetIndexEntries() => Index.GetEntries();
|
||||
protected IReadOnlyList<string> GetIndexEntries() => Index.GetEntries();
|
||||
|
||||
public int GetIndexSize() => Index.GetEntriesSize();
|
||||
|
||||
public bool HasIndexEntry(EntryKey entryKey) => Index.HasEntry(entryKey);
|
||||
public bool HasIndexEntry(string entryId) => Index.HasEntry(entryId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -94,9 +73,9 @@ public class DirectoryIndexDirectory : IndexDirectory
|
||||
_directoryIndex = index;
|
||||
}
|
||||
|
||||
protected async Task AddToIndexAsync(EntryKey entryKey)
|
||||
protected async Task AddToIndexAsync(string entryId)
|
||||
{
|
||||
_directoryIndex.PutEntry(entryKey);
|
||||
_directoryIndex.PutEntry(entryId);
|
||||
await SaveIndexAsync(_directoryIndex);
|
||||
}
|
||||
}
|
||||
@@ -114,9 +93,9 @@ public class VaultIndexDirectory : IndexDirectory
|
||||
_vaultIndex = index;
|
||||
}
|
||||
|
||||
protected async Task AddToIndexAsync(EntryKey entryKey, MetaData metaData)
|
||||
protected async Task AddToIndexAsync(string entryId, MetaData metaData)
|
||||
{
|
||||
_vaultIndex.PutEntry(entryKey, metaData);
|
||||
_vaultIndex.PutEntry(entryId, metaData);
|
||||
await SaveIndexAsync(_vaultIndex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,32 +37,39 @@ public abstract class MediaVault : VaultIndexDirectory
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a new entry to the vault with the specified media data
|
||||
/// Adds a new entry to the vault with the specified media data (MediaVaultType inferred from media type)
|
||||
/// </summary>
|
||||
public async Task AddEntryAsync(MediaVaultType vaultType, EntryKey entryKey, object media)
|
||||
public async Task AddEntryAsync(string entryId, FileBinary media)
|
||||
{
|
||||
// Extract properties from media object based on type
|
||||
var (buffer, extension) = ExtractMediaProperties(media);
|
||||
|
||||
var mediaPath = GetMediaPathFromEntryKey(entryKey.Key, extension);
|
||||
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryKey.Key, extension, media);
|
||||
// Infer MediaVaultType from the media object type
|
||||
var vaultType = MediaVaultTypeMap.GetVaultType(media.GetType());
|
||||
|
||||
await AddToIndexAsync(entryKey, metaData);
|
||||
var mediaPath = GetMediaPathFromEntryKey(entryId, extension);
|
||||
var metaData = MetaDataFactory.CreateFromMedia(vaultType, entryId, extension, media);
|
||||
|
||||
// Use string-based index operations
|
||||
await AddToIndexAsync(entryId, metaData);
|
||||
await FileUtils.PutFileAsync(mediaPath, buffer);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Retrieves an entry from the vault
|
||||
/// Retrieves an entry from the vault (MediaVaultType inferred from T)
|
||||
/// </summary>
|
||||
public async Task<T?> GetEntryAsync<T>(MediaVaultType vaultType, EntryKey entryKey) where T : FileBinary
|
||||
public async Task<T?> GetEntryAsync<T>(string entryId) where T : FileBinary
|
||||
{
|
||||
if (!HasIndexEntry(entryKey))
|
||||
// Infer MediaVaultType from the generic type T
|
||||
var vaultType = MediaVaultTypeMap.GetVaultType<T>();
|
||||
|
||||
if (!HasIndexEntry(entryId))
|
||||
return null;
|
||||
|
||||
if (Index is not VaultIndex vaultIndex)
|
||||
return null;
|
||||
|
||||
var metaData = vaultIndex.GetEntry(entryKey);
|
||||
var metaData = vaultIndex.GetEntry(entryId);
|
||||
if (metaData == null)
|
||||
return null;
|
||||
|
||||
@@ -79,15 +86,16 @@ public abstract class MediaVault : VaultIndexDirectory
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Extracts buffer and extension from a media object
|
||||
/// Extracts buffer and extension from a media binary
|
||||
/// </summary>
|
||||
private static (byte[] buffer, string extension) ExtractMediaProperties(object media)
|
||||
private static (byte[] buffer, string extension) ExtractMediaProperties(FileBinary media)
|
||||
{
|
||||
return media switch
|
||||
{
|
||||
ImageBinary imageBinary => (imageBinary.Buffer, imageBinary.Extension),
|
||||
AudioBinary audioBinary => (audioBinary.Buffer, audioBinary.Extension),
|
||||
MediaBinary mediaBinary => (mediaBinary.Buffer, mediaBinary.Extension),
|
||||
FileBinary fileBinary => throw new ArgumentException($"FileBinary must be a specific media type (ImageBinary, AudioBinary, or MediaBinary), not base FileBinary"),
|
||||
_ => throw new ArgumentException($"Unsupported media type: {media.GetType()}")
|
||||
};
|
||||
}
|
||||
@@ -105,12 +113,12 @@ public class ImageVault : MediaVault
|
||||
/// </summary>
|
||||
public static async Task<ImageVault?> FromAsync(string rootPath)
|
||||
{
|
||||
var factory = new IndexFactory(rootPath, IndexType.Vault);
|
||||
var index = await factory.BuildIndexAsync();
|
||||
var factoryService = new IndexFactoryService();
|
||||
var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Image);
|
||||
|
||||
if (index is VaultIndex vaultIndex)
|
||||
if (index != null)
|
||||
{
|
||||
return new ImageVault(rootPath, vaultIndex);
|
||||
return new ImageVault(rootPath, (VaultIndex)index);
|
||||
}
|
||||
|
||||
return null;
|
||||
@@ -123,12 +131,12 @@ public class AudioVault : MediaVault
|
||||
|
||||
public static async Task<AudioVault?> FromAsync(string rootPath)
|
||||
{
|
||||
var factory = new IndexFactory(rootPath, IndexType.Vault);
|
||||
var index = await factory.BuildIndexAsync();
|
||||
var factoryService = new IndexFactoryService();
|
||||
var index = await factoryService.LoadOrCreateVaultIndexAsync(rootPath, MediaVaultType.Audio);
|
||||
|
||||
if (index is VaultIndex vaultIndex)
|
||||
if (index != null)
|
||||
{
|
||||
return new AudioVault(rootPath, vaultIndex);
|
||||
return new AudioVault(rootPath, (VaultIndex)index);
|
||||
}
|
||||
|
||||
return null;
|
||||
|
||||
@@ -18,6 +18,9 @@ public class SimpleMediaTypeRegistry : IMediaTypeRegistry
|
||||
private readonly Dictionary<MediaVaultType, Type> _dtoTypes = new();
|
||||
private readonly Dictionary<MediaVaultType, Type> _paramsTypes = new();
|
||||
private readonly Dictionary<MediaVaultType, Type> _metaDataTypes = new();
|
||||
|
||||
// Reverse mapping: Type -> MediaVaultType
|
||||
private readonly Dictionary<Type, MediaVaultType> _typeToVaultType = new();
|
||||
|
||||
public SimpleMediaTypeRegistry()
|
||||
{
|
||||
@@ -76,6 +79,9 @@ public class SimpleMediaTypeRegistry : IMediaTypeRegistry
|
||||
_paramsTypes[vaultType] = typeof(TParams);
|
||||
_metaDataTypes[vaultType] = typeof(TMetaData);
|
||||
|
||||
// Populate reverse mapping
|
||||
_typeToVaultType[typeof(TBinary)] = vaultType;
|
||||
|
||||
if (vaultFactory != null)
|
||||
_vaultFactories[vaultType] = vaultFactory;
|
||||
}
|
||||
@@ -146,4 +152,22 @@ public class SimpleMediaTypeRegistry : IMediaTypeRegistry
|
||||
|
||||
public Type GetMetaDataType(MediaVaultType vaultType) =>
|
||||
_metaDataTypes.TryGetValue(vaultType, out var type) ? type : throw new ArgumentException($"Unknown vault type: {vaultType}");
|
||||
|
||||
public MediaVaultType GetVaultType(Type binaryType)
|
||||
{
|
||||
if (_typeToVaultType.TryGetValue(binaryType, out var vaultType))
|
||||
return vaultType;
|
||||
|
||||
// Check inheritance hierarchy for derived types
|
||||
foreach (var kvp in _typeToVaultType)
|
||||
{
|
||||
if (kvp.Key.IsAssignableFrom(binaryType))
|
||||
return kvp.Value;
|
||||
}
|
||||
|
||||
throw new ArgumentException($"Cannot infer MediaVaultType for {binaryType.Name}. Type not registered.");
|
||||
}
|
||||
|
||||
public MediaVaultType GetVaultType<T>() where T : FileBinary
|
||||
=> GetVaultType(typeof(T));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user