| 1 | // Copyright Jerome Benoit. 2021-2023. All Rights Reserved. |
| 2 | |
| 3 | import fs from 'node:fs'; |
| 4 | import path from 'node:path'; |
| 5 | |
| 6 | import { Storage } from './Storage'; |
| 7 | import { FileType, type Statistics } from '../../types'; |
| 8 | import { AsyncLock, AsyncLockType, Constants, FileUtils, Utils } from '../../utils'; |
| 9 | |
| 10 | export class JsonFileStorage extends Storage { |
| 11 | private fd: number | null = null; |
| 12 | |
| 13 | constructor(storageUri: string, logPrefix: string) { |
| 14 | super(storageUri, logPrefix); |
| 15 | this.dbName = this.storageUri.pathname; |
| 16 | } |
| 17 | |
| 18 | public storePerformanceStatistics(performanceStatistics: Statistics): void { |
| 19 | this.checkPerformanceRecordsFile(); |
| 20 | AsyncLock.acquire(AsyncLockType.performance) |
| 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 | ); |
| 40 | }) |
| 41 | .finally(() => { |
| 42 | AsyncLock.release(AsyncLockType.performance).catch(Constants.EMPTY_FUNCTION); |
| 43 | }); |
| 44 | } |
| 45 | |
| 46 | public open(): void { |
| 47 | try { |
| 48 | if (Utils.isNullOrUndefined(this?.fd)) { |
| 49 | if (!fs.existsSync(path.dirname(this.dbName))) { |
| 50 | fs.mkdirSync(path.dirname(this.dbName), { recursive: true }); |
| 51 | } |
| 52 | this.fd = fs.openSync(this.dbName, 'a+'); |
| 53 | } |
| 54 | } catch (error) { |
| 55 | FileUtils.handleFileException( |
| 56 | this.dbName, |
| 57 | FileType.PerformanceRecords, |
| 58 | error as NodeJS.ErrnoException, |
| 59 | this.logPrefix |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public close(): void { |
| 65 | try { |
| 66 | if (this?.fd) { |
| 67 | fs.closeSync(this.fd); |
| 68 | this.fd = null; |
| 69 | } |
| 70 | } catch (error) { |
| 71 | FileUtils.handleFileException( |
| 72 | this.dbName, |
| 73 | FileType.PerformanceRecords, |
| 74 | error as NodeJS.ErrnoException, |
| 75 | this.logPrefix |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | private checkPerformanceRecordsFile(): void { |
| 81 | if (!this?.fd) { |
| 82 | throw new Error( |
| 83 | `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found` |
| 84 | ); |
| 85 | } |
| 86 | } |
| 87 | } |