Updated Syncer

Removed reading database from file when starting up.
Changed writting to hand-written write, instead of using WriteAllLines()
static function on File.
This commit is contained in:
Mario Steele 2025-07-30 14:50:55 -05:00
parent 73a54b91ff
commit 7128bfad65

View file

@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
@ -40,7 +41,6 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
_dbName = dbName;
_restEndpoint = restEndpoint;
FetchDatabase().Wait();
ReadDatabase().Wait();
}
public bool IsDirty() => _isDirty;
@ -84,11 +84,11 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
if (data.Equals(entry)) continue;
Console.WriteLine($"Updated Entry for {_dbName}");
Console.WriteLine($"Updated Entry for {_dbName} - {entry.Id()}");
_entries.RemoveAll(x => x.EqualId(entry.Id()));
}
else
Console.WriteLine($"New Entry for {_dbName}");
Console.WriteLine($"New Entry for {_dbName} - {entry.Id()}");
_entries.Add(entry);
_isDirty = true;
}
@ -98,6 +98,7 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
{
if (dbName != _dbName)
return;
if (_syncing)
return;
@ -137,7 +138,13 @@ public class Syncer<T> : ISyncer where T : class, IDataModel, new()
var json = new List<string>();
foreach (var entry in _entries)
json.Add(entry.JsonData());
File.WriteAllLines(_dbPath, json);
using (var fh = File.OpenWrite(_dbPath))
{
foreach (var line in json)
fh.Write(Encoding.UTF8.GetBytes(line + "\n"));
fh.Flush();
fh.Close();
}
Console.WriteLine($"Updated {_dbPath}.");
_isDirty = false;
_syncing = false;