@iobroker/db-objects-jsonl 4.0.0-alpha.45-20220114-88aaebdb → 4.0.0-alpha.46-20220119-9e487e55

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -54,7 +54,9 @@ class ObjectsInMemoryJsonlDB extends ObjectsInMemoryFileDB {
54
54
  }
55
55
 
56
56
  async open() {
57
- await this._db.open();
57
+ if (!(await this._maybeMigrateFileDB())) {
58
+ await this._db.open();
59
+ }
58
60
 
59
61
  // Create an object-like wrapper around the internal Map
60
62
  this.dataset = new Proxy(this._db, {
@@ -99,6 +101,82 @@ class ObjectsInMemoryJsonlDB extends ObjectsInMemoryFileDB {
99
101
  }
100
102
  }
101
103
 
104
+ /**
105
+ * Checks if an existing file DB should be migrated to JSONL
106
+ * @returns {Promise<boolean>} true if the file DB was migrated. false if not.
107
+ * If this returns true, the jsonl DB was opened and doesn't need to be opened again.
108
+ */
109
+ async _maybeMigrateFileDB() {
110
+ const jsonlFileName = path.join(this.dataDir, this.settings.jsonlDB.fileName);
111
+ const jsonFileName = path.join(this.dataDir, this.settings.fileDB.fileName);
112
+ const bakFileName = path.join(this.dataDir, this.settings.fileDB.fileName + '.bak');
113
+
114
+ // Check the timestamps of each file, defaulting to 0 if they don't exist
115
+ let jsonlTimeStamp = 0;
116
+ let jsonTimeStamp = 0;
117
+ let bakTimeStamp = 0;
118
+ try {
119
+ const stat = fs.statSync(jsonlFileName);
120
+ if (stat.isFile()) {
121
+ jsonlTimeStamp = stat.mtime;
122
+ }
123
+ } catch {
124
+ // ignore
125
+ }
126
+ try {
127
+ const stat = fs.statSync(jsonFileName);
128
+ if (stat.isFile()) {
129
+ jsonTimeStamp = stat.mtime;
130
+ }
131
+ } catch {
132
+ // ignore
133
+ }
134
+ try {
135
+ const stat = fs.statSync(bakFileName);
136
+ if (stat.isFile()) {
137
+ bakTimeStamp = stat.mtime;
138
+ }
139
+ } catch {
140
+ // ignore
141
+ }
142
+
143
+ // Figure out which file needs to be imported
144
+ /** @type {string} */
145
+ let importFilename;
146
+ if (jsonTimeStamp > 0 && jsonTimeStamp >= bakTimeStamp && jsonTimeStamp >= jsonlTimeStamp) {
147
+ importFilename = jsonFileName;
148
+ } else if (bakTimeStamp > 0 && bakTimeStamp >= jsonTimeStamp && bakTimeStamp >= jsonlTimeStamp) {
149
+ importFilename = bakFileName;
150
+ } else {
151
+ // None of the File DB files are newer than the JSONL file
152
+ // There is nothing to restore, we're done
153
+ return false;
154
+ }
155
+
156
+ await this._db.open();
157
+ this._db.clear();
158
+ await this._db.importJson(importFilename);
159
+
160
+ // And rename the existing files to avoid redoing the work next time
161
+ if (fs.existsSync(jsonFileName)) {
162
+ try {
163
+ fs.renameSync(jsonFileName, `${jsonFileName}.migrated`);
164
+ } catch {
165
+ // ignore
166
+ }
167
+ }
168
+ if (fs.existsSync(bakFileName)) {
169
+ try {
170
+ fs.renameSync(bakFileName, `${bakFileName}.migrated`);
171
+ } catch {
172
+ // ignore
173
+ }
174
+ }
175
+
176
+ // Signal to the caller that the DB is already open
177
+ return true;
178
+ }
179
+
102
180
  async saveState() {
103
181
  // Nothing to do, the DB saves behind the scenes
104
182
  }
@@ -106,10 +184,10 @@ class ObjectsInMemoryJsonlDB extends ObjectsInMemoryFileDB {
106
184
  // Is regularly called and stores a compressed backup of the DB
107
185
  async saveBackup() {
108
186
  const now = Date.now();
109
- const tmpBackupFileName = path.join(os.tmpdir(), this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName);
187
+ const tmpBackupFileName = path.join(os.tmpdir(), `${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}`);
110
188
  const backupFileName = path.join(
111
189
  this.backupDir,
112
- this.getTimeStr(now) + '_' + this.settings.jsonlDB.fileName + '.gz'
190
+ `${this.getTimeStr(now)}_${this.settings.jsonlDB.fileName}.gz`
113
191
  );
114
192
  try {
115
193
  if (fs.existsSync(backupFileName)) {
@@ -710,15 +710,6 @@ class ObjectsInMemoryServer extends ObjectsInMemoryJsonlDB {
710
710
  return this._handleScanOrKeys(handler, data[0], responseId);
711
711
  });
712
712
 
713
- // MULTI/EXEC is never used with return values, thus we just answer with syntactic correct responses
714
- handler.on('multi', (data, responseId) => {
715
- return void handler.sendString(responseId, 'OK');
716
- });
717
-
718
- handler.on('exec', (data, reponseId) => {
719
- return void handler.sendArray(reponseId, []);
720
- });
721
-
722
713
  // commands for redis SETS, just dummies
723
714
  handler.on('sadd', (data, responseId) => {
724
715
  return void handler.sendInteger(responseId, 1);
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@iobroker/db-objects-jsonl",
3
- "version": "4.0.0-alpha.45-20220114-88aaebdb",
3
+ "version": "4.0.0-alpha.46-20220119-9e487e55",
4
4
  "engines": {
5
5
  "node": ">=12.0.0"
6
6
  },
7
7
  "dependencies": {
8
8
  "@alcalzone/jsonl-db": "~2.4.1",
9
- "@iobroker/db-base": "4.0.0-alpha.45-20220114-88aaebdb",
10
- "@iobroker/db-objects-file": "4.0.0-alpha.45-20220114-88aaebdb",
11
- "@iobroker/db-objects-redis": "4.0.0-alpha.45-20220114-88aaebdb",
9
+ "@iobroker/db-base": "4.0.0-alpha.46-20220119-9e487e55",
10
+ "@iobroker/db-objects-file": "4.0.0-alpha.46-20220119-9e487e55",
11
+ "@iobroker/db-objects-redis": "4.0.0-alpha.46-20220119-9e487e55",
12
12
  "deep-clone": "^3.0.3",
13
13
  "fs-extra": "^10.0.0"
14
14
  },
@@ -37,5 +37,5 @@
37
37
  "lib/",
38
38
  "index.js"
39
39
  ],
40
- "gitHead": "e2fd3e7af91eb83c30f8ae45f71d6e178edc7b28"
40
+ "gitHead": "e081e38bf6d24e767e16a0bb5a8d8202f65a9302"
41
41
  }