|
|
|
|
@ -38,12 +38,26 @@ namespace GBase.FileHandling |
|
|
|
|
/// Initialize this <see cref="IFileHandler"/> |
|
|
|
|
/// </summary> |
|
|
|
|
/// <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> |
|
|
|
|
/// <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; |
|
|
|
|
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) |
|
|
|
|
@ -55,9 +69,14 @@ namespace GBase.FileHandling |
|
|
|
|
|
|
|
|
|
//create new entry file |
|
|
|
|
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 |
|
|
|
|
|
|
|
|
|
//check if file is already opened |
|
|
|
|
IGBaseFile file = _files.FirstOrDefault(f => f.FilePath.Equals(filePath)); |
|
|
|
|
if (file != null) |
|
|
|
|
return file.UseFile(); |
|
|
|
|
|
|
|
|
|
IGBaseFile file = new GBaseFile(entry, entryFile, filePath); |
|
|
|
|
FileStream entryFile = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite); |
|
|
|
|
file = new GBaseFile(entry, entryFile, filePath); |
|
|
|
|
_files.Add(file); |
|
|
|
|
|
|
|
|
|
return file.UseFile(); |
|
|
|
|
|