@iobroker/db-objects-jsonl
Advanced tools
Comparing version 4.0.0-alpha.45-20220114-88aaebdb to 4.0.0-alpha.46-20220119-9e487e55
@@ -57,3 +57,5 @@ /** | ||
async open() { | ||
await this._db.open(); | ||
if (!(await this._maybeMigrateFileDB())) { | ||
await this._db.open(); | ||
} | ||
@@ -103,2 +105,78 @@ // Create an object-like wrapper around the internal Map | ||
/** | ||
* Checks if an existing file DB should be migrated to JSONL | ||
* @returns {Promise<boolean>} true if the file DB was migrated. false if not. | ||
* If this returns true, the jsonl DB was opened and doesn't need to be opened again. | ||
*/ | ||
async _maybeMigrateFileDB() { | ||
const jsonlFileName = path.join(this.dataDir, this.settings.jsonlDB.fileName); | ||
const jsonFileName = path.join(this.dataDir, this.settings.fileDB.fileName); | ||
const bakFileName = path.join(this.dataDir, this.settings.fileDB.fileName + '.bak'); | ||
// Check the timestamps of each file, defaulting to 0 if they don't exist | ||
let jsonlTimeStamp = 0; | ||
let jsonTimeStamp = 0; | ||
let bakTimeStamp = 0; | ||
try { | ||
const stat = fs.statSync(jsonlFileName); | ||
if (stat.isFile()) { | ||
jsonlTimeStamp = stat.mtime; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
try { | ||
const stat = fs.statSync(jsonFileName); | ||
if (stat.isFile()) { | ||
jsonTimeStamp = stat.mtime; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
try { | ||
const stat = fs.statSync(bakFileName); | ||
if (stat.isFile()) { | ||
bakTimeStamp = stat.mtime; | ||
} | ||
} catch { | ||
// ignore | ||
} | ||
// Figure out which file needs to be imported | ||
/** @type {string} */ | ||
let importFilename; | ||
if (jsonTimeStamp > 0 && jsonTimeStamp >= bakTimeStamp && jsonTimeStamp >= jsonlTimeStamp) { | ||
importFilename = jsonFileName; | ||
} else if (bakTimeStamp > 0 && bakTimeStamp >= jsonTimeStamp && bakTimeStamp >= jsonlTimeStamp) { | ||
importFilename = bakFileName; | ||
} else { | ||
// None of the File DB files are newer than the JSONL file | ||
// There is nothing to restore, we're done | ||
return false; | ||
} | ||
await this._db.open(); | ||
this._db.clear(); | ||
await this._db.importJson(importFilename); | ||
// And rename the existing files to avoid redoing the work next time | ||
if (fs.existsSync(jsonFileName)) { | ||
try { | ||
fs.renameSync(jsonFileName, `${jsonFileName}.migrated`); | ||
} catch { | ||
// ignore | ||
} | ||
} | ||
if (fs.existsSync(bakFileName)) { | ||
try { | ||
fs.renameSync(bakFileName, `${bakFileName}.migrated`); | ||
} catch { | ||
// ignore | ||
} | ||
} | ||
// Signal to the caller that the DB is already open | ||
return true; | ||
} | ||
async saveState() { | ||
@@ -111,6 +189,6 @@ // Nothing to do, the DB saves behind the scenes | ||
const now = Date.now(); | ||
const tmpBackupFileName = path.join(os.tmpdir(), this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName); | ||
const tmpBackupFileName = path.join(os.tmpdir(), `${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}`); | ||
const backupFileName = path.join( | ||
this.backupDir, | ||
this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName + '.gz' | ||
`${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}.gz` | ||
); | ||
@@ -117,0 +195,0 @@ try { |
@@ -713,11 +713,2 @@ /** | ||
// MULTI/EXEC is never used with return values, thus we just answer with syntactic correct responses | ||
handler.on('multi', (data, responseId) => { | ||
return void handler.sendString(responseId, 'OK'); | ||
}); | ||
handler.on('exec', (data, reponseId) => { | ||
return void handler.sendArray(reponseId, []); | ||
}); | ||
// commands for redis SETS, just dummies | ||
@@ -724,0 +715,0 @@ handler.on('sadd', (data, responseId) => { |
{ | ||
"name": "@iobroker/db-objects-jsonl", | ||
"version": "4.0.0-alpha.45-20220114-88aaebdb", | ||
"version": "4.0.0-alpha.46-20220119-9e487e55", | ||
"engines": { | ||
@@ -9,5 +9,5 @@ "node": ">=12.0.0" | ||
"@alcalzone/jsonl-db": "~2.4.1", | ||
"@iobroker/db-base": "4.0.0-alpha.45-20220114-88aaebdb", | ||
"@iobroker/db-objects-file": "4.0.0-alpha.45-20220114-88aaebdb", | ||
"@iobroker/db-objects-redis": "4.0.0-alpha.45-20220114-88aaebdb", | ||
"@iobroker/db-base": "4.0.0-alpha.46-20220119-9e487e55", | ||
"@iobroker/db-objects-file": "4.0.0-alpha.46-20220119-9e487e55", | ||
"@iobroker/db-objects-redis": "4.0.0-alpha.46-20220119-9e487e55", | ||
"deep-clone": "^3.0.3", | ||
@@ -41,3 +41,3 @@ "fs-extra": "^10.0.0" | ||
], | ||
"gitHead": "e2fd3e7af91eb83c30f8ae45f71d6e178edc7b28" | ||
"gitHead": "e081e38bf6d24e767e16a0bb5a8d8202f65a9302" | ||
} |
60670
1155
+ Added@iobroker/db-base@4.0.0-alpha.46-20220119-9e487e55(transitive)
+ Added@iobroker/db-objects-file@4.0.0-alpha.46-20220119-9e487e55(transitive)
+ Added@iobroker/db-objects-redis@4.0.0-alpha.46-20220119-9e487e55(transitive)
+ Added@iobroker/js-controller-common@4.0.0-alpha.46-20220119-9e487e55(transitive)
- Removed@iobroker/db-base@4.0.0-alpha.45-20220114-88aaebdb(transitive)
- Removed@iobroker/db-objects-file@4.0.0-alpha.45-20220114-88aaebdb(transitive)
- Removed@iobroker/db-objects-redis@4.0.0-alpha.45-20220114-88aaebdb(transitive)
- Removed@iobroker/js-controller-common@4.0.0-alpha.45-20220114-88aaebdb(transitive)
Updated@iobroker/db-objects-file@4.0.0-alpha.46-20220119-9e487e55
Updated@iobroker/db-objects-redis@4.0.0-alpha.46-20220119-9e487e55