|
|
|
@ -13,6 +13,8 @@ using GBase.Attributes; |
|
|
|
using GBase.Factories; |
|
|
|
using GBase.Factories; |
|
|
|
using GBase.FileHandling.Factories; |
|
|
|
using GBase.FileHandling.Factories; |
|
|
|
using GBase.Interfaces; |
|
|
|
using GBase.Interfaces; |
|
|
|
|
|
|
|
using GBase.Interfaces.DataHandling; |
|
|
|
|
|
|
|
using GBase.Interfaces.DataHandling.Pool; |
|
|
|
using GBase.Interfaces.FileHandling; |
|
|
|
using GBase.Interfaces.FileHandling; |
|
|
|
|
|
|
|
|
|
|
|
namespace GBase |
|
|
|
namespace GBase |
|
|
|
@ -20,17 +22,22 @@ namespace GBase |
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// A <see cref="IGBase"/> table |
|
|
|
/// A <see cref="IGBase"/> table |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public class GBaseTable<T> : IGBaseTable<T> where T : INotifyGBaseEntryChanged |
|
|
|
public class GBaseTable<T> : IGBaseTable<T> |
|
|
|
{ |
|
|
|
{ |
|
|
|
private readonly IFileHandler _fileHandler; |
|
|
|
private readonly IFileHandler _fileHandler; |
|
|
|
|
|
|
|
private readonly IDataHandlerPool _dataHandlerPool; |
|
|
|
private readonly IGBaseColumnFactory _gBaseColumnFactory; |
|
|
|
private readonly IGBaseColumnFactory _gBaseColumnFactory; |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
/// A <see cref="IGBase"/> table |
|
|
|
/// A <see cref="IGBase"/> table |
|
|
|
/// </summary> |
|
|
|
/// </summary> |
|
|
|
public GBaseTable(IFileHandlerFactory fileHandlerFactory, IGBaseColumnFactory gBaseColumnFactory) |
|
|
|
/// <param name="fileHandlerFactory"></param> |
|
|
|
|
|
|
|
/// <param name="dataHandlerPool">The <see cref="IDataHandlerPool"/></param> |
|
|
|
|
|
|
|
/// <param name="gBaseColumnFactory"></param> |
|
|
|
|
|
|
|
public GBaseTable(IFileHandlerFactory fileHandlerFactory, IDataHandlerPool dataHandlerPool, IGBaseColumnFactory gBaseColumnFactory) |
|
|
|
{ |
|
|
|
{ |
|
|
|
_fileHandler = fileHandlerFactory.Create(); |
|
|
|
_fileHandler = fileHandlerFactory.Create(); |
|
|
|
|
|
|
|
_dataHandlerPool = dataHandlerPool; |
|
|
|
_gBaseColumnFactory = gBaseColumnFactory; |
|
|
|
_gBaseColumnFactory = gBaseColumnFactory; |
|
|
|
|
|
|
|
|
|
|
|
Columns = new List<IGBaseColumn>(); |
|
|
|
Columns = new List<IGBaseColumn>(); |
|
|
|
@ -123,9 +130,10 @@ namespace GBase |
|
|
|
public async Task<bool> AddEntry(T entry, CancellationToken cancellationToken) |
|
|
|
public async Task<bool> AddEntry(T entry, CancellationToken cancellationToken) |
|
|
|
{ |
|
|
|
{ |
|
|
|
Entries.Add(entry); |
|
|
|
Entries.Add(entry); |
|
|
|
entry.GBaseEntryChanged += OnGBaseEntryChanged; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await _fileHandler.AddEntry(entry, this, cancellationToken); |
|
|
|
using IGBaseFile file = _fileHandler.CreateEntryFile(entry, this); |
|
|
|
|
|
|
|
IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); |
|
|
|
|
|
|
|
await dataHandler.AddEntry(entry, this, file.File, cancellationToken); |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
@ -140,38 +148,30 @@ namespace GBase |
|
|
|
if (!Entries.Contains(entry)) |
|
|
|
if (!Entries.Contains(entry)) |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
|
|
|
|
entry.GBaseEntryChanged -= OnGBaseEntryChanged; |
|
|
|
await _fileHandler.DeleteEntryFile(entry); |
|
|
|
|
|
|
|
|
|
|
|
await _fileHandler.RemoveEntry(entry); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return Entries.Remove(entry); |
|
|
|
return Entries.Remove(entry); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
public async Task SetValue<TProperty>(T entry, string propertyName, TProperty value, CancellationToken cancellationToken) |
|
|
|
/// Modify the property of a given entry with the given value |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="entry">The entry</param> |
|
|
|
|
|
|
|
/// <param name="propertyName">The name of the property</param> |
|
|
|
|
|
|
|
/// <param name="value">The new value to set</param> |
|
|
|
|
|
|
|
/// <returns>True if successful, false if not</returns> |
|
|
|
|
|
|
|
private bool ModifyEntry(object entry, string propertyName, object value) //TODO: Write to file |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
//don't need to change value of property in `Entries` list, the instance is saved there, any property change is already changed there as well |
|
|
|
using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); |
|
|
|
return true; |
|
|
|
IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); |
|
|
|
|
|
|
|
await dataHandler.SetValue<T, TProperty>(file.File, propertyName, value, cancellationToken); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
public async Task<TProperty> GetValue<TProperty>(T entry, string propertyName, CancellationToken cancellationToken) |
|
|
|
/// A <see cref="IGBase"/> entry changed |
|
|
|
|
|
|
|
/// </summary> |
|
|
|
|
|
|
|
/// <param name="entry">The entry (sender)</param> |
|
|
|
|
|
|
|
/// <param name="args">The <see cref="GBaseEntryChangedEventArgs"/></param> |
|
|
|
|
|
|
|
private void OnGBaseEntryChanged(object entry, GBaseEntryChangedEventArgs args) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
//TODO: Might change, depending on #23 |
|
|
|
using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); |
|
|
|
bool success = ModifyEntry(entry, args.ColumnName, args.Value); |
|
|
|
IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); |
|
|
|
|
|
|
|
return await dataHandler.GetValue<T, TProperty>(file.File, propertyName, cancellationToken); |
|
|
|
if (!success) |
|
|
|
} |
|
|
|
throw new Exception("Failed to handle EntryChanged"); //TODO: Decide what to do here |
|
|
|
|
|
|
|
|
|
|
|
public async Task<IEnumerable<TProperty>> GetValues<TProperty>(T entry, string propertyName, CancellationToken cancellationToken) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
using IGBaseFile file = await _fileHandler.RequestEntryFile(entry); |
|
|
|
|
|
|
|
IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, CancellationToken.None); |
|
|
|
|
|
|
|
return await dataHandler.GetValues<T, TProperty>(file.File, propertyName, cancellationToken); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
/// <summary> |
|
|
|
@ -181,6 +181,7 @@ namespace GBase |
|
|
|
public async ValueTask DisposeAsync() |
|
|
|
public async ValueTask DisposeAsync() |
|
|
|
{ |
|
|
|
{ |
|
|
|
await _fileHandler.DisposeAsync(); |
|
|
|
await _fileHandler.DisposeAsync(); |
|
|
|
|
|
|
|
await _dataHandlerPool.DisposeAsync(); |
|
|
|
|
|
|
|
|
|
|
|
foreach (var column in Columns) |
|
|
|
foreach (var column in Columns) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|