@iobroker/db-states-jsonl 4.0.21 → 4.0.24

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,71 @@ 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
+ // Compress at least daily to avoid a huge file when DBs have few objects
57
+ // but big objects are updated regularly (e.g. the repositories)
58
+ intervalMs: 1000 * 60 * 60 * 23
59
+ },
60
+ ignoreReadErrors: true,
61
+ throttleFS: {
62
+ intervalMs: 60000,
63
+ maxBufferedCommands: 2000
64
+ },
65
+ lockfile: {
66
+ // 5 retries starting at 250ms add up to just above 2s,
67
+ // so the DB has 3 more seconds to load all data if it wants to stay within the 5s connectionTimeout
68
+ retries: 5,
69
+ retryMinTimeoutMs: 250,
70
+ // This makes sure the DB stays locked for maximum 2s even if the process crashes
71
+ staleMs: 2000
72
+ }
73
+ };
74
+
75
+ // Be really careful what we allow here. Incorrect settings may cause problems in production.
76
+ if (tools.isObject(conf.autoCompress)) {
77
+ const ac = conf.autoCompress;
78
+ // Letting the DB grow more than 100x is risky
79
+ if (typeof ac.sizeFactor === 'number' && ac.sizeFactor >= 2 && ac.sizeFactor <= 100) {
80
+ ret.autoCompress.sizeFactor = ac.sizeFactor;
81
+ }
82
+ // Also we should definitely compress once the DB has reached 200k lines or it might grow too big
83
+ if (
84
+ typeof ac.sizeFactorMinimumSize === 'number' &&
85
+ ac.sizeFactorMinimumSize >= 0 &&
86
+ ac.sizeFactorMinimumSize <= 200000
87
+ ) {
88
+ ret.autoCompress.sizeFactorMinimumSize = ac.sizeFactorMinimumSize;
89
+ }
90
+ }
91
+ if (tools.isObject(conf.throttleFS)) {
92
+ const thr = conf.throttleFS;
93
+ // Don't write more often than every second and write at least once every hour
94
+ if (typeof thr.intervalMs === 'number' && thr.intervalMs >= 1000 && thr.intervalMs <= 3600000) {
95
+ ret.throttleFS.intervalMs = thr.intervalMs;
96
+ }
97
+ // Don't keep too much in memory - 100k changes are more than enough
98
+ if (
99
+ typeof thr.maxBufferedCommands === 'number' &&
100
+ thr.maxBufferedCommands >= 0 &&
101
+ thr.maxBufferedCommands <= 100000
102
+ ) {
103
+ ret.throttleFS.maxBufferedCommands = thr.maxBufferedCommands;
104
+ }
105
+ }
106
+
107
+ return ret;
108
+ }
109
+
45
110
  /**
46
111
  * This class inherits InMemoryFileDB class and adds all relevant logic for states
47
112
  * including the available methods for use by js-controller directly
@@ -55,26 +120,7 @@ class StatesInMemoryJsonlDB extends StatesInMemoryFileDB {
55
120
  backupDirName: 'backup-objects'
56
121
  };
57
122
 
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
- };
123
+ const jsonlOptions = normalizeJsonlOptions(settings.connection.jsonlOptions);
78
124
  settings.jsonlDB = {
79
125
  fileName: 'states.jsonl'
80
126
  };
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.24",
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.24",
10
+ "@iobroker/db-states-file": "4.0.24",
11
+ "@iobroker/db-states-redis": "4.0.24"
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": "5645debf3394177abeef58917f872a7d8c43e320"
39
39
  }