| 1 | // Copyright Jerome Benoit. 2021. All Rights Reserved. |
| 2 | |
| 3 | import fs from 'fs'; |
| 4 | |
| 5 | import lockfile from 'proper-lockfile'; |
| 6 | |
| 7 | import { FileType } from '../../types/FileType'; |
| 8 | import Statistics from '../../types/Statistics'; |
| 9 | import FileUtils from '../../utils/FileUtils'; |
| 10 | import { Storage } from './Storage'; |
| 11 | |
| 12 | export class JsonFileStorage extends Storage { |
| 13 | private fd: number | null = null; |
| 14 | |
| 15 | constructor(storageUri: string, logPrefix: string) { |
| 16 | super(storageUri, logPrefix); |
| 17 | this.dbName = this.storageUri.pathname; |
| 18 | } |
| 19 | |
| 20 | public storePerformanceStatistics(performanceStatistics: Statistics): void { |
| 21 | this.checkPerformanceRecordsFile(); |
| 22 | lockfile |
| 23 | .lock(this.dbName, { stale: 5000, retries: 3 }) |
| 24 | .then(async (release) => { |
| 25 | try { |
| 26 | const fileData = fs.readFileSync(this.dbName, 'utf8'); |
| 27 | const performanceRecords: Statistics[] = fileData |
| 28 | ? (JSON.parse(fileData) as Statistics[]) |
| 29 | : []; |
| 30 | performanceRecords.push(performanceStatistics); |
| 31 | fs.writeFileSync( |
| 32 | this.dbName, |
| 33 | JSON.stringify( |
| 34 | performanceRecords, |
| 35 | (key, value) => { |
| 36 | if (value instanceof Map) { |
| 37 | return { |
| 38 | dataType: 'Map', |
| 39 | value: [...value], |
| 40 | }; |
| 41 | } |
| 42 | return value as Statistics; |
| 43 | }, |
| 44 | 2 |
| 45 | ), |
| 46 | 'utf8' |
| 47 | ); |
| 48 | } catch (error) { |
| 49 | FileUtils.handleFileException( |
| 50 | this.logPrefix, |
| 51 | FileType.PerformanceRecords, |
| 52 | this.dbName, |
| 53 | error as NodeJS.ErrnoException |
| 54 | ); |
| 55 | } |
| 56 | await release(); |
| 57 | }) |
| 58 | .catch(() => { |
| 59 | /* This is intentional */ |
| 60 | }); |
| 61 | } |
| 62 | |
| 63 | public open(): void { |
| 64 | try { |
| 65 | if (!this?.fd) { |
| 66 | this.fd = fs.openSync(this.dbName, 'a+'); |
| 67 | } |
| 68 | } catch (error) { |
| 69 | FileUtils.handleFileException( |
| 70 | this.logPrefix, |
| 71 | FileType.PerformanceRecords, |
| 72 | this.dbName, |
| 73 | error as NodeJS.ErrnoException |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | public close(): void { |
| 79 | try { |
| 80 | if (this?.fd) { |
| 81 | fs.closeSync(this.fd); |
| 82 | this.fd = null; |
| 83 | } |
| 84 | } catch (error) { |
| 85 | FileUtils.handleFileException( |
| 86 | this.logPrefix, |
| 87 | FileType.PerformanceRecords, |
| 88 | this.dbName, |
| 89 | error as NodeJS.ErrnoException |
| 90 | ); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | private checkPerformanceRecordsFile(): void { |
| 95 | if (!this?.fd) { |
| 96 | throw new Error( |
| 97 | `${this.logPrefix} Performance records '${this.dbName}' file descriptor not found` |
| 98 | ); |
| 99 | } |
| 100 | } |
| 101 | } |