From 1498a914df7421f3711f6ab947400f9a6c709a59 Mon Sep 17 00:00:00 2001 From: Simon G Date: Mon, 21 Sep 2020 03:46:16 +0200 Subject: [PATCH] #25: start implementing addEntry --- GBase/Attributes/GBaseTableAttribute.cs | 5 + GBase/FileHandling/FileHandler.cs | 20 ++- GBase/GBase.cs | 9 +- GBase/GBase.xml | 129 +++++++++++++----- GBase/GBaseTable.cs | 20 ++- GBase/Interfaces/FileHandling/IFileHandler.cs | 2 +- GBase/Interfaces/IGBaseTable.cs | 9 +- Test.GBase/GBaseTableIntegrationTest.cs | 2 +- 8 files changed, 135 insertions(+), 61 deletions(-) diff --git a/GBase/Attributes/GBaseTableAttribute.cs b/GBase/Attributes/GBaseTableAttribute.cs index 078e9f6..df332f8 100644 --- a/GBase/Attributes/GBaseTableAttribute.cs +++ b/GBase/Attributes/GBaseTableAttribute.cs @@ -13,6 +13,11 @@ namespace GBase.Attributes [AttributeUsage(AttributeTargets.Class)] public class GBaseTableAttribute : Attribute //TODO: Decide how to handle enums (as table or their own type) { + public GBaseTableAttribute(string folderName) + { + FolderName = folderName; + } + public string FolderName { get; } } } \ No newline at end of file diff --git a/GBase/FileHandling/FileHandler.cs b/GBase/FileHandling/FileHandler.cs index dcc9103..88ce20d 100644 --- a/GBase/FileHandling/FileHandler.cs +++ b/GBase/FileHandling/FileHandler.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; +using System.IO; using System.Threading; using System.Threading.Tasks; using GBase.Interfaces; @@ -18,7 +19,13 @@ namespace GBase.FileHandling /// public class FileHandler : IFileHandler { + /// + /// The file extension for all GBase tables + /// + public const string GBASE_TABLE_FILE_EXTENSION = "gb"; + private readonly IDataHandlerPool _dataHandlerPool; + private string _path; /// /// Internal file handler @@ -37,13 +44,22 @@ namespace GBase.FileHandling /// True if successful, false if not public async Task Init(string path, CancellationToken cancellationToken) { + _path = path; return true; //TODO: is there anything that needs to be initialized here? } - public Task AddEntry(T entry, IGBaseTable table) + public async Task AddEntry(T entry, IGBaseTable table, CancellationToken cancellationToken) { + string directoryPath = Path.Combine(_path, table.FolderName); + + //create directory if it doesn't exist + Directory.CreateDirectory(directoryPath); + //create new entry file - throw new NotImplementedException(); + string filePath = $"{Path.Combine(directoryPath, entry.ToString())}.{GBASE_TABLE_FILE_EXTENSION}"; + FileStream entryFile = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); + + IDataHandler dataHandler = await _dataHandlerPool.RequestDataHandler(this, false, cancellationToken); } public Task RemoveEntry(T entry) diff --git a/GBase/GBase.cs b/GBase/GBase.cs index 4a789a3..91ff813 100644 --- a/GBase/GBase.cs +++ b/GBase/GBase.cs @@ -21,11 +21,6 @@ namespace GBase /// public class GBase : IGBase { - /// - /// The file extension for all GBase tables - /// - public const string GBASE_TABLE_FILE_EXTENSION = "gb"; //TODO: Find correct place for this const - private readonly IGBaseTableFactory _gBaseTableFactory; /// @@ -76,7 +71,7 @@ namespace GBase continue; IGBaseTable gBaseTable = _gBaseTableFactory.Create(type); - await gBaseTable.Init(type, type.Name, Settings.DatabasePath, cancellationToken); + await gBaseTable.Init(type, Settings.DatabasePath, gBaseTableAttribute.FolderName, cancellationToken); AddTable(gBaseTable); } @@ -94,7 +89,7 @@ namespace GBase if (Tables.Contains(table)) return false; - if (Tables.Any(t => t.Name.Equals(table.Name))) + if (Tables.Any(t => t.FolderName.Equals(table.FolderName))) return false; Tables.Add(table); diff --git a/GBase/GBase.xml b/GBase/GBase.xml index 2945813..cf649c2 100644 --- a/GBase/GBase.xml +++ b/GBase/GBase.xml @@ -361,6 +361,50 @@ The of the passed interface + + + that the passed table type doesn't implement "/> + + + + + that the passed table type doesn't implement "/> + + The table type + + + + that the table for the given is missing + + + + + that the table for the given is missing + + + + + The that has no table + + + + + Factory for the + + + + + Factory for the + + Factory for the + Factory for the + + + + Creates an + + A newly created instance of the implementation for + Factory for the @@ -389,7 +433,7 @@ Factory for the - + Creates an @@ -551,37 +595,54 @@ A to await - + + + GBase object that supplies inheriting classes with methods to get data from a + + + + + GBase object that allows conversion from + + No table for is existing + + + + Initialize this from a given + + The given + + A table - + A table - + The of the class that this represents - + The name of this - + The s of this - + The entries of this - + Initialize this @@ -591,35 +652,35 @@ A to cancel the asynchronous operation True if successful, false if not - + Add a given to this The given True if successful, false if not - + Remove a given from this The given True if successful, false if not - + Add an entry that implements to this The entry implementing True if successful, false if not - + Remove an entry that implements from this The entry implementing True if successful, false if not - + Modify the property of a given entry with the given value @@ -628,14 +689,14 @@ The new value to set True if successful, false if not - + A entry changed The entry (sender) The - + The method @@ -985,6 +1046,25 @@ A column of a + + + The entries of this + + + + + Add an entry that implements to this + + The entry implementing + True if successful, false if not + + + + Remove an entry that implements from this + + The entry implementing + True if successful, false if not + A table @@ -1005,11 +1085,6 @@ The s of this - - - The entries of this - - Initialize this @@ -1034,20 +1109,6 @@ The given True if successful, false if not - - - Add an entry that implements to this - - The entry implementing - True if successful, false if not - - - - Remove an entry that implements from this - - The entry implementing - True if successful, false if not - Settings of a instance diff --git a/GBase/GBaseTable.cs b/GBase/GBaseTable.cs index 5076bac..10bd705 100644 --- a/GBase/GBaseTable.cs +++ b/GBase/GBaseTable.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; -using System.IO; using System.Linq; using System.Reflection; using System.Threading; @@ -47,7 +46,7 @@ namespace GBase /// /// The name of this /// - public string Name { get; private set; } + public string FolderName { get; private set; } /// /// The s of this @@ -64,18 +63,15 @@ namespace GBase /// Initialize this /// /// The of the class that this represents - /// The name of this /// The path to the database files + /// /// A to cancel the asynchronous operation /// True if successful, false if not - public async Task Init(Type type, string name, string databasePath, CancellationToken cancellationToken) + public async Task Init(Type type, string databasePath, string folderName, CancellationToken cancellationToken) { Type = type; - Name = name; - - string fileName = $"{name}.{GBase.GBASE_TABLE_FILE_EXTENSION}"; - string path = Path.Combine(databasePath, fileName); - await _fileHandler.Init(path, cancellationToken); + FolderName = folderName; + await _fileHandler.Init(databasePath, cancellationToken); //TODO: Init columns list depending on GBaseColumnAttributes set for this GBaseTable foreach (var property in type.GetProperties()) @@ -123,12 +119,12 @@ namespace GBase /// /// The entry implementing /// True if successful, false if not - public async Task AddEntry(T entry) //TODO: Write to file + public async Task AddEntry(T entry) { Entries.Add(entry); entry.GBaseEntryChanged += OnGBaseEntryChanged; - await _fileHandler.AddEntry(entry, this); + await _fileHandler.AddEntry(entry, this, TODO); return true; } @@ -138,7 +134,7 @@ namespace GBase /// /// The entry implementing /// True if successful, false if not - public async Task RemoveEntry(T entry) //TODO: remove from file + public async Task RemoveEntry(T entry) { if (!Entries.Contains(entry)) return false; diff --git a/GBase/Interfaces/FileHandling/IFileHandler.cs b/GBase/Interfaces/FileHandling/IFileHandler.cs index 31a8b86..3e3e715 100644 --- a/GBase/Interfaces/FileHandling/IFileHandler.cs +++ b/GBase/Interfaces/FileHandling/IFileHandler.cs @@ -22,7 +22,7 @@ namespace GBase.Interfaces.FileHandling /// True if successful, false if not Task Init(string path, CancellationToken cancellationToken); - Task AddEntry(T entry, IGBaseTable table); + Task AddEntry(T entry, IGBaseTable table, CancellationToken cancellationToken); Task RemoveEntry(T entry); diff --git a/GBase/Interfaces/IGBaseTable.cs b/GBase/Interfaces/IGBaseTable.cs index c240e37..e72bb9e 100644 --- a/GBase/Interfaces/IGBaseTable.cs +++ b/GBase/Interfaces/IGBaseTable.cs @@ -48,7 +48,7 @@ namespace GBase.Interfaces /// /// The name of this /// - string Name { get; } + string FolderName { get; } /// /// The s of this @@ -59,11 +59,12 @@ namespace GBase.Interfaces /// Initialize this /// /// The of the class that this represents - /// The name of this - /// /// The path to the database files + /// The path to the database files + /// /// A to cancel the asynchronous operation + /// /// /// True if successful, false if not - Task Init(Type type, string name, string databasePath, CancellationToken cancellationToken); + Task Init(Type type, string databasePath, string folderName, CancellationToken cancellationToken); /// /// Add a given to this diff --git a/Test.GBase/GBaseTableIntegrationTest.cs b/Test.GBase/GBaseTableIntegrationTest.cs index 4e5a753..8ec77c1 100644 --- a/Test.GBase/GBaseTableIntegrationTest.cs +++ b/Test.GBase/GBaseTableIntegrationTest.cs @@ -27,7 +27,7 @@ namespace Test.GBase gBaseColumnFactoryMock.Setup(c => c.Create()).Returns(new GBaseColumn()); IGBaseTable table = new GBaseTable(fileHandlerFactoryMock.Object, gBaseColumnFactoryMock.Object); - table.Init(typeof(Foo), nameof(Foo), "", CancellationToken.None); + table.Init(typeof(Foo), "", TODO, CancellationToken.None); Foo foo = new Foo("Test"); table.AddEntry(foo);