Commit | Line | Data |
---|---|---|
edd13439 | 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
c27c3eee | 2 | |
f1c729e0 | 3 | import { closeSync, existsSync, mkdirSync, openSync, writeSync } from 'node:fs'; |
d972af76 | 4 | import { dirname } from 'node:path'; |
8114d10e | 5 | |
c1565026 | 6 | import { Storage } from './Storage'; |
7b5dbe91 | 7 | import { BaseError } from '../../exception'; |
268a74bb | 8 | import { FileType, type Statistics } from '../../types'; |
9bf0ef23 JB |
9 | import { |
10 | AsyncLock, | |
11 | AsyncLockType, | |
9bf0ef23 JB |
12 | JSONStringifyWithMapSupport, |
13 | handleFileException, | |
14 | isNullOrUndefined, | |
15 | } from '../../utils'; | |
72f041bd | 16 | |
100a5301 | 17 | export class JsonFileStorage extends Storage { |
f1c729e0 JB |
18 | private static readonly performanceRecords: Map<string, Statistics> = new Map< |
19 | string, | |
20 | Statistics | |
21 | >(); | |
22 | ||
23 | private fd?: number; | |
2a370053 | 24 | |
1f5df42a JB |
25 | constructor(storageUri: string, logPrefix: string) { |
26 | super(storageUri, logPrefix); | |
27 | this.dbName = this.storageUri.pathname; | |
72f041bd JB |
28 | } |
29 | ||
30 | public storePerformanceStatistics(performanceStatistics: Statistics): void { | |
2a370053 | 31 | this.checkPerformanceRecordsFile(); |
061ff2b0 | 32 | JsonFileStorage.performanceRecords.set(performanceStatistics.id, performanceStatistics); |
0ebf7c2e | 33 | AsyncLock.runExclusive(AsyncLockType.performance, () => { |
0ebf7c2e JB |
34 | writeSync( |
35 | this.fd!, | |
36 | JSONStringifyWithMapSupport([...JsonFileStorage.performanceRecords.values()], 2), | |
37 | 0, | |
38 | 'utf8', | |
39 | ); | |
40 | }).catch((error) => { | |
41 | handleFileException( | |
42 | this.dbName, | |
43 | FileType.PerformanceRecords, | |
44 | error as NodeJS.ErrnoException, | |
45 | this.logPrefix, | |
46 | ); | |
47 | }); | |
72f041bd | 48 | } |
b652b0c3 | 49 | |
2a370053 | 50 | public open(): void { |
b652b0c3 | 51 | try { |
9bf0ef23 | 52 | if (isNullOrUndefined(this?.fd)) { |
d972af76 JB |
53 | if (!existsSync(dirname(this.dbName))) { |
54 | mkdirSync(dirname(this.dbName), { recursive: true }); | |
f682b2dc | 55 | } |
0ebf7c2e | 56 | this.fd = openSync(this.dbName, 'w'); |
a6ceb16a | 57 | } |
b652b0c3 | 58 | } catch (error) { |
fa5995d6 | 59 | handleFileException( |
e7aeea18 | 60 | this.dbName, |
7164966d JB |
61 | FileType.PerformanceRecords, |
62 | error as NodeJS.ErrnoException, | |
5edd8ba0 | 63 | this.logPrefix, |
e7aeea18 | 64 | ); |
2a370053 JB |
65 | } |
66 | } | |
67 | ||
68 | public close(): void { | |
69 | try { | |
a6ceb16a | 70 | if (this?.fd) { |
d972af76 | 71 | closeSync(this.fd); |
f1c729e0 | 72 | delete this?.fd; |
2a370053 JB |
73 | } |
74 | } catch (error) { | |
fa5995d6 | 75 | handleFileException( |
e7aeea18 | 76 | this.dbName, |
7164966d JB |
77 | FileType.PerformanceRecords, |
78 | error as NodeJS.ErrnoException, | |
5edd8ba0 | 79 | this.logPrefix, |
e7aeea18 | 80 | ); |
2a370053 JB |
81 | } |
82 | } | |
83 | ||
84 | private checkPerformanceRecordsFile(): void { | |
73d09045 | 85 | if (!this?.fd) { |
7b5dbe91 | 86 | throw new BaseError( |
5edd8ba0 | 87 | `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found`, |
e7aeea18 | 88 | ); |
b652b0c3 JB |
89 | } |
90 | } | |
72f041bd | 91 | } |