@iobroker/db-states-jsonl 4.0.21 → 4.0.23

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.
@@ -42,6 +42,68 @@ const { tools } = require('@iobroker/js-controller-common');
42
42
  // };
43
43
  //
44
44
 
45
+ /**
46
+ * Normalizes options for the JsonlDB
47
+ * @param {Record<string, any> | undefined} conf The jsonlOptions options from iobroker.json
48
+ * @returns {import("@alcalzone/jsonl-db").JsonlDBOptions<any>}
49
+ */
50
+ function normalizeJsonlOptions(conf = {}) {
51
+ /** @type {import("@alcalzone/jsonl-db").JsonlDBOptions<any>} */
52
+ const ret = {
53
+ autoCompress: {
54
+ sizeFactor: 10,
55
+ sizeFactorMinimumSize: 50000
56
+ },
57
+ ignoreReadErrors: true,
58
+ throttleFS: {
59
+ intervalMs: 60000,
60
+ maxBufferedCommands: 2000
61
+ },
62
+ lockfile: {
63
+ // 5 retries starting at 250ms add up to just above 2s,
64
+ // so the DB has 3 more seconds to load all data if it wants to stay within the 5s connectionTimeout
65
+ retries: 5,
66
+ retryMinTimeoutMs: 250,
67
+ // This makes sure the DB stays locked for maximum 2s even if the process crashes
68
+ staleMs: 2000
69
+ }
70
+ };
71
+
72
+ // Be really careful what we allow here. Incorrect settings may cause problems in production.
73
+ if (tools.isObject(conf.autoCompress)) {
74
+ const ac = conf.autoCompress;
75
+ // Letting the DB grow more than 100x is risky
76
+ if (typeof ac.sizeFactor === 'number' && ac.sizeFactor >= 2 && ac.sizeFactor <= 100) {
77
+ ret.autoCompress.sizeFactor = ac.sizeFactor;
78
+ }
79
+ // Also we should definitely compress once the DB has reached 200k lines or it might grow too big
80
+ if (
81
+ typeof ac.sizeFactorMinimumSize === 'number' &&
82
+ ac.sizeFactorMinimumSize >= 0 &&
83
+ ac.sizeFactorMinimumSize <= 200000
84
+ ) {
85
+ ret.autoCompress.sizeFactorMinimumSize = ac.sizeFactorMinimumSize;
86
+ }
87
+ }
88
+ if (tools.isObject(conf.throttleFS)) {
89
+ const thr = conf.throttleFS;
90
+ // Don't write more often than every second and write at least once every hour
91
+ if (typeof thr.intervalMs === 'number' && thr.intervalMs >= 1000 && thr.intervalMs <= 3600000) {
92
+ ret.throttleFS.intervalMs = thr.intervalMs;
93
+ }
94
+ // Don't keep too much in memory - 100k changes are more than enough
95
+ if (
96
+ typeof thr.maxBufferedCommands === 'number' &&
97
+ thr.maxBufferedCommands >= 0 &&
98
+ thr.maxBufferedCommands <= 100000
99
+ ) {
100
+ ret.throttleFS.maxBufferedCommands = thr.maxBufferedCommands;
101
+ }
102
+ }
103
+
104
+ return ret;
105
+ }
106
+
45
107
  /**
46
108
  * This class inherits InMemoryFileDB class and adds all relevant logic for states
47
109
  * including the available methods for use by js-controller directly
@@ -55,26 +117,7 @@ class StatesInMemoryJsonlDB extends StatesInMemoryFileDB {
55
117
  backupDirName: 'backup-objects'
56
118
  };
57
119
 
58
- /** @type {import("@alcalzone/jsonl-db").JsonlDBOptions<any>} */
59
- const jsonlOptions = settings.connection.jsonlOptions || {
60
- autoCompress: {
61
- sizeFactor: 10,
62
- sizeFactorMinimumSize: 50000
63
- },
64
- ignoreReadErrors: true,
65
- throttleFS: {
66
- intervalMs: 60000,
67
- maxBufferedCommands: 2000
68
- },
69
- lockfile: {
70
- // 5 retries starting at 250ms add up to just above 2s,
71
- // so the DB has 3 more seconds to load all data if it wants to stay within the 5s connectionTimeout
72
- retries: 5,
73
- retryMinTimeoutMs: 250,
74
- // This makes sure the DB stays locked for maximum 2s even if the process crashes
75
- staleMs: 2000
76
- }
77
- };
120
+ const jsonlOptions = normalizeJsonlOptions(settings.connection.jsonlOptions);
78
121
  settings.jsonlDB = {
79
122
  fileName: 'states.jsonl'
80
123
  };
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@iobroker/db-states-jsonl",
3
- "version": "4.0.21",
3
+ "version": "4.0.23",
4
4
  "engines": {
5
5
  "node": ">=12.0.0"
6
6
  },
7
7
  "dependencies": {
8
8
  "@alcalzone/jsonl-db": "~2.5.1",
9
- "@iobroker/db-base": "4.0.21",
10
- "@iobroker/db-states-file": "4.0.21",
11
- "@iobroker/db-states-redis": "4.0.21"
9
+ "@iobroker/db-base": "4.0.23",
10
+ "@iobroker/db-states-file": "4.0.23",
11
+ "@iobroker/db-states-redis": "4.0.23"
12
12
  },
13
13
  "keywords": [
14
14
  "ioBroker",
@@ -35,5 +35,5 @@
35
35
  "lib/",
36
36
  "index.js"
37
37
  ],
38
- "gitHead": "4749d256d312623ae7621e7ae47d44580b3aa8c1"
38
+ "gitHead": "ea1a72ac2d74317ad16234f7aeac90632374614c"
39
39
  }