Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c27c3eee | 2 | |
130783a7 | 3 | import fs from 'node:fs'; |
f682b2dc | 4 | import path from 'node:path'; |
8114d10e | 5 | |
c1565026 | 6 | import { Storage } from './Storage'; |
268a74bb | 7 | import { FileType, type Statistics } from '../../types'; |
1227a6f1 | 8 | import { AsyncLock, AsyncLockType, Constants, FileUtils, Utils } from '../../utils'; |
72f041bd | 9 | |
100a5301 | 10 | export class JsonFileStorage extends Storage { |
2a370053 JB |
11 | private fd: number | null = null; |
12 | ||
1f5df42a JB |
13 | constructor(storageUri: string, logPrefix: string) { |
14 | super(storageUri, logPrefix); | |
15 | this.dbName = this.storageUri.pathname; | |
72f041bd JB |
16 | } |
17 | ||
18 | public storePerformanceStatistics(performanceStatistics: Statistics): void { | |
2a370053 | 19 | this.checkPerformanceRecordsFile(); |
dd485b56 | 20 | AsyncLock.acquire(AsyncLockType.performance) |
1227a6f1 JB |
21 | .then(() => { |
22 | const fileData = fs.readFileSync(this.dbName, 'utf8'); | |
23 | const performanceRecords: Statistics[] = fileData | |
24 | ? (JSON.parse(fileData) as Statistics[]) | |
25 | : []; | |
26 | performanceRecords.push(performanceStatistics); | |
27 | fs.writeFileSync( | |
28 | this.dbName, | |
29 | Utils.JSONStringifyWithMapSupport(performanceRecords, 2), | |
30 | 'utf8' | |
31 | ); | |
32 | }) | |
33 | .catch((error) => { | |
34 | FileUtils.handleFileException( | |
35 | this.dbName, | |
36 | FileType.PerformanceRecords, | |
37 | error as NodeJS.ErrnoException, | |
38 | this.logPrefix | |
39 | ); | |
c63c21bc | 40 | }) |
1227a6f1 | 41 | .finally(() => { |
dd485b56 | 42 | AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION); |
1227a6f1 | 43 | }); |
72f041bd | 44 | } |
b652b0c3 | 45 | |
2a370053 | 46 | public open(): void { |
b652b0c3 | 47 | try { |
4c5e87ae | 48 | if (Utils.isNullOrUndefined(this?.fd)) { |
f682b2dc JB |
49 | if (!fs.existsSync(path.dirname(this.dbName))) { |
50 | fs.mkdirSync(path.dirname(this.dbName), { recursive: true }); | |
51 | } | |
a6ceb16a JB |
52 | this.fd = fs.openSync(this.dbName, 'a+'); |
53 | } | |
b652b0c3 | 54 | } catch (error) { |
e7aeea18 | 55 | FileUtils.handleFileException( |
e7aeea18 | 56 | this.dbName, |
7164966d JB |
57 | FileType.PerformanceRecords, |
58 | error as NodeJS.ErrnoException, | |
59 | this.logPrefix | |
e7aeea18 | 60 | ); |
2a370053 JB |
61 | } |
62 | } | |
63 | ||
64 | public close(): void { | |
65 | try { | |
a6ceb16a | 66 | if (this?.fd) { |
2a370053 JB |
67 | fs.closeSync(this.fd); |
68 | this.fd = null; | |
69 | } | |
70 | } catch (error) { | |
e7aeea18 | 71 | FileUtils.handleFileException( |
e7aeea18 | 72 | this.dbName, |
7164966d JB |
73 | FileType.PerformanceRecords, |
74 | error as NodeJS.ErrnoException, | |
75 | this.logPrefix | |
e7aeea18 | 76 | ); |
2a370053 JB |
77 | } |
78 | } | |
79 | ||
80 | private checkPerformanceRecordsFile(): void { | |
73d09045 | 81 | if (!this?.fd) { |
e7aeea18 JB |
82 | throw new Error( |
83 | `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found` | |
84 | ); | |
b652b0c3 JB |
85 | } |
86 | } | |
72f041bd | 87 | } |