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 JB |
6 | import { Storage } from './Storage.js' |
7 | import { BaseError } from '../../exception/index.js' | |
8 | import { FileType, type Statistics } from '../../types/index.js' | |
9bf0ef23 JB |
9 | import { |
10 | AsyncLock, | |
11 | AsyncLockType, | |
9bf0ef23 | 12 | JSONStringifyWithMapSupport, |
66a7748d JB |
13 | handleFileException |
14 | } from '../../utils/index.js' | |
72f041bd | 15 | |
100a5301 | 16 | export class JsonFileStorage extends Storage { |
66a7748d | 17 | private static performanceRecords: Map<string, Statistics> |
f1c729e0 | 18 | |
66a7748d | 19 | private fd?: number |
2a370053 | 20 | |
66a7748d JB |
21 | constructor (storageUri: string, logPrefix: string) { |
22 | super(storageUri, logPrefix) | |
23 | this.dbName = this.storageUri.pathname | |
72f041bd JB |
24 | } |
25 | ||
66a7748d JB |
26 | public storePerformanceStatistics (performanceStatistics: Statistics): void { |
27 | this.checkPerformanceRecordsFile() | |
28 | JsonFileStorage.performanceRecords.set(performanceStatistics.id, performanceStatistics) | |
0ebf7c2e | 29 | AsyncLock.runExclusive(AsyncLockType.performance, () => { |
0ebf7c2e | 30 | writeSync( |
66a7748d | 31 | // eslint-disable-next-line @typescript-eslint/no-non-null-assertion |
0ebf7c2e JB |
32 | this.fd!, |
33 | JSONStringifyWithMapSupport([...JsonFileStorage.performanceRecords.values()], 2), | |
34 | 0, | |
66a7748d JB |
35 | 'utf8' |
36 | ) | |
a974c8e4 | 37 | }).catch(error => { |
0ebf7c2e JB |
38 | handleFileException( |
39 | this.dbName, | |
40 | FileType.PerformanceRecords, | |
41 | error as NodeJS.ErrnoException, | |
66a7748d JB |
42 | this.logPrefix |
43 | ) | |
44 | }) | |
72f041bd | 45 | } |
b652b0c3 | 46 | |
66a7748d JB |
47 | public open (): void { |
48 | JsonFileStorage.performanceRecords = new Map<string, Statistics>() | |
b652b0c3 | 49 | try { |
5199f9fd | 50 | if (this.fd == null) { |
d972af76 | 51 | if (!existsSync(dirname(this.dbName))) { |
66a7748d | 52 | mkdirSync(dirname(this.dbName), { recursive: true }) |
f682b2dc | 53 | } |
66a7748d | 54 | this.fd = openSync(this.dbName, 'w') |
a6ceb16a | 55 | } |
b652b0c3 | 56 | } catch (error) { |
fa5995d6 | 57 | handleFileException( |
e7aeea18 | 58 | this.dbName, |
7164966d JB |
59 | FileType.PerformanceRecords, |
60 | error as NodeJS.ErrnoException, | |
66a7748d JB |
61 | this.logPrefix |
62 | ) | |
2a370053 JB |
63 | } |
64 | } | |
65 | ||
66a7748d JB |
66 | public close (): void { |
67 | JsonFileStorage.performanceRecords.clear() | |
2a370053 | 68 | try { |
5199f9fd | 69 | if (this.fd != null) { |
66a7748d | 70 | closeSync(this.fd) |
5199f9fd | 71 | delete this.fd |
2a370053 JB |
72 | } |
73 | } catch (error) { | |
fa5995d6 | 74 | handleFileException( |
e7aeea18 | 75 | this.dbName, |
7164966d JB |
76 | FileType.PerformanceRecords, |
77 | error as NodeJS.ErrnoException, | |
66a7748d JB |
78 | this.logPrefix |
79 | ) | |
2a370053 JB |
80 | } |
81 | } | |
82 | ||
66a7748d | 83 | private checkPerformanceRecordsFile (): void { |
5199f9fd | 84 | if (this.fd == null) { |
7b5dbe91 | 85 | throw new BaseError( |
66a7748d JB |
86 | `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found` |
87 | ) | |
b652b0c3 JB |
88 | } |
89 | } | |
72f041bd | 90 | } |