Work on the datta handler
This commit is contained in:
@@ -1,6 +1,85 @@
|
||||
using SzCore.DataObjects;
|
||||
|
||||
namespace SzCore;
|
||||
|
||||
public class SzDataHandler
|
||||
{
|
||||
private Dictionary<string, SzDataset> _loadedDatasets = [];
|
||||
|
||||
public SzDataHandler(){}
|
||||
|
||||
public bool SaveDataset(SzDataset dataset)
|
||||
{
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DatasetsPath, dataset.Id, "dataset.json");
|
||||
try
|
||||
{
|
||||
return SZ.LocalFileManager.SaveFile(datasetPath, SZ.SzParser.SerializeDatasetToJson(dataset));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
SZ.Logger.LogError("Error saving dataset: " + e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public SzDataset? LoadDataset(string datasetId)
|
||||
{
|
||||
if (_loadedDatasets.TryGetValue(datasetId, out SzDataset? value))
|
||||
{
|
||||
if (value is not null) return value;
|
||||
}
|
||||
|
||||
var datasetPath = Path.Combine(SZ.LocalFileManager.DataPath, "datasets", datasetId, "dataset.json");
|
||||
|
||||
var json = SZ.LocalFileManager.LoadFile(datasetPath);
|
||||
if (json is null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
var parsedDataset = SZ.SzParser.DeserializeDataset(json);
|
||||
if (parsedDataset is null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Could not load dataset with ID '{datasetId}'");
|
||||
return null;
|
||||
}
|
||||
|
||||
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
||||
return parsedDataset;
|
||||
}
|
||||
|
||||
// The client should periodically call this to free memory, probably
|
||||
private void ClearLoadedDatasets()
|
||||
{
|
||||
_loadedDatasets.Clear();
|
||||
}
|
||||
|
||||
// Only use this for test purposes, we probably don't want to load all datasets to memory in production
|
||||
private void LoadAllDatasets()
|
||||
{
|
||||
ClearLoadedDatasets();
|
||||
if (!Directory.Exists(SZ.LocalFileManager.DatasetsPath))
|
||||
{
|
||||
throw new Exception("Could not load datasets, the datase directory does not exist.");
|
||||
}
|
||||
|
||||
foreach (var dir in Directory.GetDirectories(SZ.LocalFileManager.DatasetsPath))
|
||||
{
|
||||
var datasetFilePath = Path.Combine(dir, "dataset.json");
|
||||
if (!File.Exists(datasetFilePath)) continue;
|
||||
|
||||
var datasetJson = SZ.LocalFileManager.LoadFile(datasetFilePath);
|
||||
if (datasetJson == null) continue;
|
||||
|
||||
var parsedDataset = SZ.SzParser.DeserializeDataset(datasetJson);
|
||||
if (parsedDataset == null)
|
||||
{
|
||||
SZ.Logger.LogWarning($"Parse error: Could not parse file {datasetFilePath} as an SzDataset");
|
||||
continue;
|
||||
}
|
||||
|
||||
_loadedDatasets.Add(parsedDataset.Id, parsedDataset);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user