using DeepDrftContent.FileDatabase.Abstractions; using DeepDrftContent.FileDatabase.Models; using DeepDrftContent.FileDatabase.Utils; using IndexType = DeepDrftContent.FileDatabase.Services.IndexType; namespace DeepDrftContent.FileDatabase.Services; /// /// Factory service for creating and managing indexes /// public class IndexFactoryService : IIndexFactory, IIndexDataFactory { private readonly Dictionary> _indexCreators; private readonly Dictionary> _indexFromDataCreators; private readonly Dictionary> _indexDataCreators; public IndexFactoryService() { _indexCreators = new Dictionary> { { IndexType.Directory, rootPath => new DirectoryIndex(new DirectoryIndexData(Path.GetFileName(rootPath))) }, { IndexType.Vault, rootPath => new VaultIndex(new VaultIndexData(Path.GetFileName(rootPath))) } }; _indexFromDataCreators = new Dictionary> { { IndexType.Directory, data => new DirectoryIndex((DirectoryIndexData)data) }, { IndexType.Vault, data => new VaultIndex((VaultIndexData)data) } }; _indexDataCreators = new Dictionary> { { IndexType.Directory, index => DirectoryIndexData.FromIndex((DirectoryIndex)index) }, { IndexType.Vault, index => VaultIndexData.FromIndex((VaultIndex)index) } }; } public async Task 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 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(indexPath), IndexType.Vault => await FileUtils.FetchObjectAsync(indexPath), _ => throw new ArgumentException($"Unknown index type: {type}") }; return creator(indexData); } public async Task 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); } }