- initialize existing files

ImproveDataHandling_#25
Simon G 5 years ago
parent a949f786f8
commit d4ab37de84
  1. 27
      GBase/FileHandling/FileHandler.cs
  2. 4
      GBase/Interfaces/FileHandling/IFileHandler.cs

@ -38,12 +38,26 @@ namespace GBase.FileHandling
/// Initialize this <see cref="IFileHandler"/> /// Initialize this <see cref="IFileHandler"/>
/// </summary> /// </summary>
/// <param name="path">The path of the database</param> /// <param name="path">The path of the database</param>
/// <param name="folderName"></param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param>
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
public async Task<bool> Init(string path, CancellationToken cancellationToken) public List<IGBaseFile> Init(string path, string folderName, CancellationToken cancellationToken)
{ {
_path = path; _path = path;
return true; //TODO: initialize existing files
string directoryPath = Path.Combine(_path, folderName);
if (!Directory.Exists(directoryPath))
return null;
string[] files = Directory.GetFiles(directoryPath, $"*.{GBASE_TABLE_FILE_EXTENSION}");
foreach (var file in files)
{
FileStream entryFile = File.Open(file, FileMode.OpenOrCreate, FileAccess.ReadWrite);
_files.Add(new GBaseFile(null, entryFile, file));
}
return _files;
} }
public IGBaseFile CreateEntryFile<T>(T entry, IGBaseTable table) public IGBaseFile CreateEntryFile<T>(T entry, IGBaseTable table)
@ -55,9 +69,14 @@ namespace GBase.FileHandling
//create new entry file //create new entry file
string filePath = $"{Path.Combine(directoryPath, entry.ToString())}.{GBASE_TABLE_FILE_EXTENSION}"; string filePath = $"{Path.Combine(directoryPath, entry.ToString())}.{GBASE_TABLE_FILE_EXTENSION}";
FileStream entryFile = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); //TODO: Stream has to be disposed
IGBaseFile file = new GBaseFile(entry, entryFile, filePath); //check if file is already opened
IGBaseFile file = _files.FirstOrDefault(f => f.FilePath.Equals(filePath));
if (file != null)
return file.UseFile();
FileStream entryFile = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
file = new GBaseFile(entry, entryFile, filePath);
_files.Add(file); _files.Add(file);
return file.UseFile(); return file.UseFile();

@ -3,6 +3,7 @@
// Copyright(c) 2020 SimonG. All Rights Reserved. // Copyright(c) 2020 SimonG. All Rights Reserved.
using System; using System;
using System.Collections.Generic;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -17,9 +18,10 @@ namespace GBase.Interfaces.FileHandling
/// Initialize this <see cref="IFileHandler"/> /// Initialize this <see cref="IFileHandler"/>
/// </summary> /// </summary>
/// <param name="path">The path of the database</param> /// <param name="path">The path of the database</param>
/// <param name="folderName"></param>
/// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param> /// <param name="cancellationToken">A <see cref="CancellationToken"/> to cancel the asynchronous operation</param>
/// <returns>True if successful, false if not</returns> /// <returns>True if successful, false if not</returns>
Task<bool> Init(string path, CancellationToken cancellationToken); List<IGBaseFile> Init(string path, string folderName, CancellationToken cancellationToken);
IGBaseFile CreateEntryFile<T>(T entry, IGBaseTable table); IGBaseFile CreateEntryFile<T>(T entry, IGBaseTable table);

Loading…
Cancel
Save