X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fperformance%2Fstorage%2FJsonFileStorage.ts;h=2f6e498749f5751f2364cc62716b3cfa8904872f;hb=f1c729e06d48e7821b732276343990946fad9f35;hp=8bbbd42cea06563d91d7e6d360bf38cc06f92563;hpb=c1120092b7dde7abc8f84cf529fbf12f46321857;p=e-mobility-charging-stations-simulator.git diff --git a/src/performance/storage/JsonFileStorage.ts b/src/performance/storage/JsonFileStorage.ts index 8bbbd42c..2f6e4987 100644 --- a/src/performance/storage/JsonFileStorage.ts +++ b/src/performance/storage/JsonFileStorage.ts @@ -1,6 +1,6 @@ // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. -import { closeSync, existsSync, mkdirSync, openSync, readFileSync, writeFileSync } from 'node:fs'; +import { closeSync, existsSync, mkdirSync, openSync, writeSync } from 'node:fs'; import { dirname } from 'node:path'; import { Storage } from './Storage'; @@ -16,7 +16,12 @@ import { } from '../../utils'; export class JsonFileStorage extends Storage { - private fd: number | null = null; + private static readonly performanceRecords: Map = new Map< + string, + Statistics + >(); + + private fd?: number; constructor(storageUri: string, logPrefix: string) { super(storageUri, logPrefix); @@ -27,12 +32,13 @@ export class JsonFileStorage extends Storage { this.checkPerformanceRecordsFile(); AsyncLock.acquire(AsyncLockType.performance) .then(() => { - const fileData = readFileSync(this.dbName, 'utf8'); - const performanceRecords: Statistics[] = fileData - ? (JSON.parse(fileData) as Statistics[]) - : []; - performanceRecords.push(performanceStatistics); - writeFileSync(this.dbName, JSONStringifyWithMapSupport(performanceRecords, 2), 'utf8'); + JsonFileStorage.performanceRecords.set(performanceStatistics.id, performanceStatistics); + writeSync( + this.fd!, + JSONStringifyWithMapSupport([...JsonFileStorage.performanceRecords.values()], 2), + 0, + 'utf8', + ); }) .catch((error) => { handleFileException( @@ -53,7 +59,7 @@ export class JsonFileStorage extends Storage { if (!existsSync(dirname(this.dbName))) { mkdirSync(dirname(this.dbName), { recursive: true }); } - this.fd = openSync(this.dbName, 'a+'); + this.fd = openSync(this.dbName, 'w+'); } } catch (error) { handleFileException( @@ -69,7 +75,7 @@ export class JsonFileStorage extends Storage { try { if (this?.fd) { closeSync(this.fd); - this.fd = null; + delete this?.fd; } } catch (error) { handleFileException(