Commit | Line | Data |
---|---|---|
a19b897d | 1 | // Copyright Jerome Benoit. 2021-2024. All Rights Reserved. |
c27c3eee | 2 | |
66a7748d JB |
3 | import { closeSync, existsSync, mkdirSync, openSync, writeSync } from 'node:fs' |
4 | import { dirname } from 'node:path' | |
8114d10e | 5 | |
66a7748d | 6 | import { BaseError } from '../../exception/index.js' |
276e05ae JB |
7 | import { FileType, MapStringifyFormat, type Statistics } from '../../types/index.js' |
8 | import { AsyncLock, AsyncLockType, handleFileException, JSONStringify } from '../../utils/index.js' | |
4c3f6c20 | 9 | import { Storage } from './Storage.js' |
72f041bd | 10 | |
100a5301 | 11 | export class JsonFileStorage extends Storage { |
66a7748d | 12 | private fd?: number |
2a370053 | 13 | |
66a7748d JB |
14 | constructor (storageUri: string, logPrefix: string) { |
15 | super(storageUri, logPrefix) | |
16 | this.dbName = this.storageUri.pathname | |
72f041bd JB |
17 | } |
18 | ||
66a7748d | 19 | public storePerformanceStatistics (performanceStatistics: Statistics): void { |
a66bbcfe | 20 | this.setPerformanceStatistics(performanceStatistics) |
66a7748d | 21 | this.checkPerformanceRecordsFile() |
0ebf7c2e | 22 | AsyncLock.runExclusive(AsyncLockType.performance, () => { |
0ebf7c2e | 23 | writeSync( |
66a7748d | 24 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
0ebf7c2e | 25 | this.fd!, |
276e05ae | 26 | JSONStringify([...this.getPerformanceStatistics()], 2, MapStringifyFormat.object), |
0ebf7c2e | 27 | 0, |
66a7748d JB |
28 | 'utf8' |
29 | ) | |
ea32ea05 | 30 | }).catch((error: unknown) => { |
0ebf7c2e JB |
31 | handleFileException( |
32 | this.dbName, | |
33 | FileType.PerformanceRecords, | |
34 | error as NodeJS.ErrnoException, | |
66a7748d JB |
35 | this.logPrefix |
36 | ) | |
37 | }) | |
72f041bd | 38 | } |
b652b0c3 | 39 | |
66a7748d | 40 | public open (): void { |
b652b0c3 | 41 | try { |
5199f9fd | 42 | if (this.fd == null) { |
d972af76 | 43 | if (!existsSync(dirname(this.dbName))) { |
66a7748d | 44 | mkdirSync(dirname(this.dbName), { recursive: true }) |
f682b2dc | 45 | } |
66a7748d | 46 | this.fd = openSync(this.dbName, 'w') |
a6ceb16a | 47 | } |
b652b0c3 | 48 | } catch (error) { |
fa5995d6 | 49 | handleFileException( |
e7aeea18 | 50 | this.dbName, |
7164966d JB |
51 | FileType.PerformanceRecords, |
52 | error as NodeJS.ErrnoException, | |
66a7748d JB |
53 | this.logPrefix |
54 | ) | |
2a370053 JB |
55 | } |
56 | } | |
57 | ||
66a7748d | 58 | public close (): void { |
a66bbcfe | 59 | this.clearPerformanceStatistics() |
2a370053 | 60 | try { |
5199f9fd | 61 | if (this.fd != null) { |
66a7748d | 62 | closeSync(this.fd) |
5199f9fd | 63 | delete this.fd |
2a370053 JB |
64 | } |
65 | } catch (error) { | |
fa5995d6 | 66 | handleFileException( |
e7aeea18 | 67 | this.dbName, |
7164966d JB |
68 | FileType.PerformanceRecords, |
69 | error as NodeJS.ErrnoException, | |
66a7748d JB |
70 | this.logPrefix |
71 | ) | |
2a370053 JB |
72 | } |
73 | } | |
74 | ||
66a7748d | 75 | private checkPerformanceRecordsFile (): void { |
5199f9fd | 76 | if (this.fd == null) { |
7b5dbe91 | 77 | throw new BaseError( |
66a7748d JB |
78 | `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found` |
79 | ) | |
b652b0c3 JB |
80 | } |
81 | } | |
72f041bd | 82 | } |