FileDatabase refactor for normalization and consistency
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user