FileDatabase Refactor and normalization
Test cleanup
This commit is contained in:
@@ -8,7 +8,6 @@ namespace DeepDrftContent.Controllers;
|
||||
[Route("api/[controller]")]
|
||||
public class TrackController : ControllerBase
|
||||
{
|
||||
private readonly EntryKey _vaultKey = new("tracks", MediaVaultType.Audio);
|
||||
private readonly FileDatabase.Services.FileDatabase _fileDatabase;
|
||||
|
||||
public TrackController(FileDatabase.Services.FileDatabase fileDatabase)
|
||||
@@ -19,8 +18,12 @@ public class TrackController : ControllerBase
|
||||
[HttpGet("{trackId}")]
|
||||
public async Task<ActionResult<AudioBinaryDto>> GetTrack([FromQuery] string trackId)
|
||||
{
|
||||
if (_fileDatabase.GetVault(_vaultKey) is not AudioVault vault) { return NotFound(); }
|
||||
var file = await vault.GetEntryAsync<AudioBinary>(MediaVaultType.Audio, new EntryKey(trackId, MediaVaultType.Audio));
|
||||
// BEFORE: Complex with EntryKey objects and redundant MediaVaultType
|
||||
// var entryKey = new EntryKey(trackId, MediaVaultTypeMap.GetVaultType<AudioBinary>());
|
||||
// var file = await _fileDatabase.LoadResourceAsync<AudioBinary>(_vaultKey, entryKey);
|
||||
|
||||
// AFTER: Ultra clean - just string identifiers, types inferred
|
||||
var file = await _fileDatabase.LoadResourceAsync<AudioBinary>("tracks", trackId);
|
||||
if (file == null) { return NotFound(); }
|
||||
return File(file.Buffer, MimeTypeExtensions.GetMimeType(file.Extension));
|
||||
}
|
||||
|
||||
@@ -8,20 +8,30 @@ namespace DeepDrftContent.FileDatabase.Abstractions;
|
||||
/// </summary>
|
||||
public interface IIndexFactory
|
||||
{
|
||||
/// <summary>
|
||||
/// Creates an index of the specified type
|
||||
/// </summary>
|
||||
Task<IIndex?> CreateIndexAsync(IndexType type, string rootPath);
|
||||
|
||||
/// <summary>
|
||||
/// Loads an existing index of the specified type
|
||||
/// </summary>
|
||||
Task<IIndex?> LoadIndexAsync(IndexType type, string rootPath);
|
||||
|
||||
/// <summary>
|
||||
/// Loads existing index or creates new one if loading fails
|
||||
/// Creates a directory index
|
||||
/// </summary>
|
||||
Task<IIndex?> LoadOrCreateIndexAsync(IndexType type, string rootPath);
|
||||
Task<IDirectoryIndex?> CreateDirectoryIndexAsync(string rootPath);
|
||||
|
||||
/// <summary>
|
||||
/// Loads existing directory index or creates new one if loading fails
|
||||
/// </summary>
|
||||
Task<IDirectoryIndex?> LoadOrCreateDirectoryIndexAsync(string rootPath);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a vault index with the specified vault type
|
||||
/// </summary>
|
||||
Task<IVaultIndex?> CreateVaultIndexAsync(string rootPath, MediaVaultType vaultType);
|
||||
|
||||
/// <summary>
|
||||
/// Loads existing vault index or creates new one with the specified vault type if loading fails
|
||||
/// </summary>
|
||||
Task<IVaultIndex?> LoadOrCreateVaultIndexAsync(string rootPath, MediaVaultType vaultType);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -66,4 +66,14 @@ public interface IMediaTypeRegistry
|
||||
/// Get the metadata type for a vault type
|
||||
/// </summary>
|
||||
Type GetMetaDataType(MediaVaultType vaultType);
|
||||
|
||||
/// <summary>
|
||||
/// Get the vault type for a binary type (reverse mapping)
|
||||
/// </summary>
|
||||
MediaVaultType GetVaultType(Type binaryType);
|
||||
|
||||
/// <summary>
|
||||
/// Get the vault type for a binary type using generics (reverse mapping)
|
||||
/// </summary>
|
||||
MediaVaultType GetVaultType<T>() where T : FileBinary;
|
||||
}
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
namespace DeepDrftContent.FileDatabase.Models;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a key for entries in the file database system.
|
||||
/// Combines a string key with a media vault type for type-safe operations.
|
||||
/// </summary>
|
||||
/// <param name="Key">The string identifier for the entry</param>
|
||||
/// <param name="Type">The media vault type this entry belongs to</param>
|
||||
public record EntryKey(string Key, MediaVaultType Type);
|
||||
@@ -17,9 +17,9 @@ public interface IIndex
|
||||
public interface IEntryQueryable : IIndex
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets all entry keys in this index
|
||||
/// Gets all entry IDs in this index
|
||||
/// </summary>
|
||||
IReadOnlyList<EntryKey> GetEntries();
|
||||
IReadOnlyList<string> GetEntries();
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of entries in this index
|
||||
@@ -27,9 +27,9 @@ public interface IEntryQueryable : IIndex
|
||||
int GetEntriesSize();
|
||||
|
||||
/// <summary>
|
||||
/// Checks if the index contains the specified entry key
|
||||
/// Checks if the index contains the specified entry ID
|
||||
/// </summary>
|
||||
bool HasEntry(EntryKey entryKey);
|
||||
bool HasEntry(string entryId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -40,7 +40,7 @@ public interface IDirectoryIndex : IEntryQueryable
|
||||
/// <summary>
|
||||
/// Adds an entry to the directory index
|
||||
/// </summary>
|
||||
void PutEntry(EntryKey entryKey);
|
||||
void PutEntry(string entryId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -51,10 +51,10 @@ public interface IVaultIndex : IEntryQueryable
|
||||
/// <summary>
|
||||
/// Gets metadata for a specific entry
|
||||
/// </summary>
|
||||
MetaData? GetEntry(EntryKey entryKey);
|
||||
MetaData? GetEntry(string entryId);
|
||||
|
||||
/// <summary>
|
||||
/// Adds an entry with metadata to the vault index
|
||||
/// </summary>
|
||||
void PutEntry(EntryKey entryKey, MetaData metaData);
|
||||
void PutEntry(string entryId, MetaData metaData);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
using DeepDrftContent.FileDatabase.Utils;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace DeepDrftContent.FileDatabase.Models;
|
||||
|
||||
@@ -20,7 +21,7 @@ public abstract class IndexData
|
||||
/// </summary>
|
||||
public class DirectoryIndexData : IndexData
|
||||
{
|
||||
public List<EntryKey> Entries { get; set; } = new();
|
||||
public List<string> Entries { get; set; } = new();
|
||||
|
||||
public DirectoryIndexData(string indexKey) : base(indexKey) { }
|
||||
|
||||
@@ -39,7 +40,7 @@ public class DirectoryIndexData : IndexData
|
||||
/// </summary>
|
||||
public class VaultEntryData
|
||||
{
|
||||
public EntryKey Key { get; set; } = null!;
|
||||
public string Key { get; set; } = null!;
|
||||
public MetaData Value { get; set; } = null!;
|
||||
}
|
||||
|
||||
@@ -49,12 +50,22 @@ public class VaultEntryData
|
||||
public class VaultIndexData : IndexData
|
||||
{
|
||||
public List<VaultEntryData> Entries { get; set; } = new();
|
||||
public MediaVaultType VaultType { get; set; }
|
||||
|
||||
public VaultIndexData(string indexKey) : base(indexKey) { }
|
||||
public VaultIndexData(string indexKey) : base(indexKey)
|
||||
{
|
||||
VaultType = MediaVaultType.Media; // Default vault type for legacy compatibility
|
||||
}
|
||||
|
||||
[JsonConstructor]
|
||||
public VaultIndexData(string indexKey, MediaVaultType vaultType) : base(indexKey)
|
||||
{
|
||||
VaultType = vaultType;
|
||||
}
|
||||
|
||||
public static VaultIndexData FromIndex(VaultIndex index)
|
||||
{
|
||||
var data = new VaultIndexData(index.GetKey())
|
||||
var data = new VaultIndexData(index.GetKey(), index.VaultType)
|
||||
{
|
||||
Entries = index.Entries.Select(kvp => new VaultEntryData { Key = kvp.Key, Value = kvp.Value }).ToList()
|
||||
};
|
||||
@@ -67,11 +78,11 @@ public class VaultIndexData : IndexData
|
||||
/// </summary>
|
||||
public class DirectoryIndex : IndexData, IDirectoryIndex
|
||||
{
|
||||
public StructuralSet<EntryKey> Entries { get; }
|
||||
public StructuralSet<string> Entries { get; }
|
||||
|
||||
public DirectoryIndex(DirectoryIndexData indexData) : base(indexData.IndexKey)
|
||||
{
|
||||
Entries = new StructuralSet<EntryKey>();
|
||||
Entries = new StructuralSet<string>();
|
||||
// Load entries from data
|
||||
foreach (var entry in indexData.Entries)
|
||||
{
|
||||
@@ -81,13 +92,13 @@ public class DirectoryIndex : IndexData, IDirectoryIndex
|
||||
|
||||
public string GetKey() => IndexKey;
|
||||
|
||||
public IReadOnlyList<EntryKey> GetEntries() => Entries.ToList().AsReadOnly();
|
||||
public IReadOnlyList<string> GetEntries() => Entries.ToList().AsReadOnly();
|
||||
|
||||
public int GetEntriesSize() => Entries.Size;
|
||||
|
||||
public bool HasEntry(EntryKey entryKey) => Entries.Has(entryKey);
|
||||
public bool HasEntry(string entryId) => Entries.Has(entryId);
|
||||
|
||||
public void PutEntry(EntryKey entryKey) => Entries.Add(entryKey);
|
||||
public void PutEntry(string entryId) => Entries.Add(entryId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -95,11 +106,13 @@ public class DirectoryIndex : IndexData, IDirectoryIndex
|
||||
/// </summary>
|
||||
public class VaultIndex : IndexData, IVaultIndex
|
||||
{
|
||||
public StructuralMap<EntryKey, MetaData> Entries { get; }
|
||||
public StructuralMap<string, MetaData> Entries { get; }
|
||||
public MediaVaultType VaultType { get; }
|
||||
|
||||
public VaultIndex(VaultIndexData indexData) : base(indexData.IndexKey)
|
||||
{
|
||||
Entries = new StructuralMap<EntryKey, MetaData>();
|
||||
Entries = new StructuralMap<string, MetaData>();
|
||||
VaultType = indexData.VaultType;
|
||||
// Load entries from data
|
||||
foreach (var entry in indexData.Entries)
|
||||
{
|
||||
@@ -109,13 +122,13 @@ public class VaultIndex : IndexData, IVaultIndex
|
||||
|
||||
public string GetKey() => IndexKey;
|
||||
|
||||
public IReadOnlyList<EntryKey> GetEntries() => Entries.Keys.ToList().AsReadOnly();
|
||||
public IReadOnlyList<string> GetEntries() => Entries.Keys.ToList().AsReadOnly();
|
||||
|
||||
public int GetEntriesSize() => Entries.Size;
|
||||
|
||||
public bool HasEntry(EntryKey entryKey) => Entries.Has(entryKey);
|
||||
public bool HasEntry(string entryId) => Entries.Has(entryId);
|
||||
|
||||
public MetaData? GetEntry(EntryKey entryKey) => Entries.Get(entryKey);
|
||||
public MetaData? GetEntry(string entryId) => Entries.Get(entryId);
|
||||
|
||||
public void PutEntry(EntryKey entryKey, MetaData metaData) => Entries.Set(entryKey, metaData);
|
||||
public void PutEntry(string entryId, MetaData metaData) => Entries.Set(entryId, metaData);
|
||||
}
|
||||
|
||||
@@ -17,6 +17,16 @@ public static class MediaVaultTypeMap
|
||||
public static Type GetParamsType(MediaVaultType vaultType) => _registry.GetParamsType(vaultType);
|
||||
|
||||
public static Type GetMetaDataType(MediaVaultType vaultType) => _registry.GetMetaDataType(vaultType);
|
||||
|
||||
/// <summary>
|
||||
/// Get the vault type for a binary type (reverse mapping)
|
||||
/// </summary>
|
||||
public static MediaVaultType GetVaultType(Type binaryType) => _registry.GetVaultType(binaryType);
|
||||
|
||||
/// <summary>
|
||||
/// Get the vault type for a binary type using generics (reverse mapping)
|
||||
/// </summary>
|
||||
public static MediaVaultType GetVaultType<T>() where T : FileBinary => _registry.GetVaultType<T>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
||||
@@ -10,7 +10,7 @@ The C# port preserves the original three-layer architecture:
|
||||
- **Location**: `Services/FileDatabase.cs`
|
||||
- **Purpose**: Root-level manager coordinating multiple media vaults
|
||||
- **Key Features**:
|
||||
- Manages collection of `MediaVault` instances using `StructuralMap<EntryKey, MediaVault>`
|
||||
- Manages collection of `MediaVault` instances using `StructuralMap<string, MediaVault>`
|
||||
- Provides async factory method `FromAsync()` for initialization
|
||||
- Handles vault creation, resource loading, and resource registration
|
||||
|
||||
@@ -38,7 +38,7 @@ The C# port preserves the original three-layer architecture:
|
||||
## Key Components
|
||||
|
||||
### Models (`Models/` directory)
|
||||
- **`EntryKey`**: Composite key structure with string key + MediaVaultType
|
||||
- **String-based keys**: Simple string identifiers for vault and entry management
|
||||
- **`MetaData` hierarchy**: Base metadata → `ImageMetaData` with aspect ratio
|
||||
- **Media Types**: `FileBinary` → `MediaBinary` → `ImageBinary`
|
||||
- **Factory Classes**: Type-safe creation of media objects and metadata
|
||||
@@ -58,7 +58,7 @@ The C# port preserves the original three-layer architecture:
|
||||
5. **Dependency Inversion**: Abstract base classes and interfaces
|
||||
|
||||
### C# Language Features
|
||||
- **Records**: Immutable data structures for `EntryKey`, `MetaData`
|
||||
- **Records**: Immutable data structures for `MetaData` hierarchy
|
||||
- **Pattern Matching**: Switch expressions for type-safe factory methods
|
||||
- **Nullable Reference Types**: Explicit nullability handling
|
||||
- **Async/Await**: Full async support with `Task<T>` and `ValueTask<T>`
|
||||
@@ -76,17 +76,15 @@ The C# port preserves the original three-layer architecture:
|
||||
var database = await FileDatabase.FromAsync("/path/to/database");
|
||||
|
||||
// Create a vault
|
||||
var vaultKey = new EntryKey("images", MediaVaultType.Image);
|
||||
await database.CreateVaultAsync(vaultKey);
|
||||
var vaultId = "images";
|
||||
await database.CreateVaultAsync(vaultId, MediaVaultType.Image);
|
||||
|
||||
// Store an image
|
||||
var entryKey = new EntryKey("photo1", MediaVaultType.Image);
|
||||
// Store an image (MediaVaultType inferred from ImageBinary)
|
||||
var imageData = new ImageBinary(new ImageBinaryParams(buffer, size, ".jpg", 1.5));
|
||||
await database.RegisterResourceAsync(MediaVaultType.Image, vaultKey, entryKey, imageData);
|
||||
await database.RegisterResourceAsync("gallery", "photo1", imageData);
|
||||
|
||||
// Load an image
|
||||
var loadedImage = await database.LoadResourceAsync<ImageBinary>(
|
||||
MediaVaultType.Image, vaultKey, entryKey);
|
||||
// Load an image (MediaVaultType inferred from ImageBinary generic type)
|
||||
var loadedImage = await database.LoadResourceAsync<ImageBinary>("gallery", "photo1");
|
||||
```
|
||||
|
||||
## Project Structure
|
||||
@@ -94,7 +92,7 @@ var loadedImage = await database.LoadResourceAsync<ImageBinary>(
|
||||
```
|
||||
FileDatabase/
|
||||
├── Models/
|
||||
│ ├── EntryKey.cs # Composite key structure
|
||||
│ ├── [EntryKey removed] # Now using simple string keys
|
||||
│ ├── MetaData.cs # Metadata hierarchy
|
||||
│ ├── MediaModels.cs # Binary data classes
|
||||
│ ├── MediaFactories.cs # Factory pattern implementations
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
@@ -21,10 +21,10 @@ namespace DeepDrftContent
|
||||
|
||||
private static async Task InitializeTrackVault(FileDatabase.Services.FileDatabase fileDatabase)
|
||||
{
|
||||
var vaultKey = new EntryKey("tracks", MediaVaultType.Audio);
|
||||
if (!fileDatabase.HasVault(vaultKey))
|
||||
const string vaultId = "tracks";
|
||||
if (!fileDatabase.HasVault(vaultId))
|
||||
{
|
||||
await fileDatabase.CreateVaultAsync(vaultKey);
|
||||
await fileDatabase.CreateVaultAsync(vaultId, MediaVaultType.Audio);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user