@iobroker/db-objects-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.
@@ -20,6 +20,71 @@ const fs = require('fs');
20
20
  const os = require('os');
21
21
  const { tools } = require('@iobroker/js-controller-common');
22
22
 
23
+ /**
24
+ * Normalizes options for the JsonlDB
25
+ * @param {Record<string, any> | undefined} conf The jsonlOptions options from iobroker.json
26
+ * @returns {import("@alcalzone/jsonl-db").JsonlDBOptions<any>}
27
+ */
28
+ function normalizeJsonlOptions(conf = {}) {
29
+ /** @type {import("@alcalzone/jsonl-db").JsonlDBOptions<any>} */
30
+ const ret = {
31
+ autoCompress: {
32
+ sizeFactor: 2,
33
+ sizeFactorMinimumSize: 25000,
34
+ // Compress at least daily to avoid a huge file when DBs have few objects
35
+ // but big objects are updated regularly (e.g. the repositories)
36
+ intervalMs: 1000 * 60 * 60 * 23
37
+ },
38
+ ignoreReadErrors: true,
39
+ throttleFS: {
40
+ intervalMs: 60000,
41
+ maxBufferedCommands: 1000
42
+ },
43
+ lockfile: {
44
+ // 5 retries starting at 250ms add up to just above 2s,
45
+ // so the DB has 3 more seconds to load all data if it wants to stay within the 5s connectionTimeout
46
+ retries: 5,
47
+ retryMinTimeoutMs: 250,
48
+ // This makes sure the DB stays locked for maximum 2s even if the process crashes
49
+ staleMs: 2000
50
+ }
51
+ };
52
+
53
+ // Be really careful what we allow here. Incorrect settings may cause problems in production.
54
+ if (tools.isObject(conf.autoCompress)) {
55
+ const ac = conf.autoCompress;
56
+ // Letting the DB grow more than 100x is risky
57
+ if (typeof ac.sizeFactor === 'number' && ac.sizeFactor >= 2 && ac.sizeFactor <= 100) {
58
+ ret.autoCompress.sizeFactor = ac.sizeFactor;
59
+ }
60
+ // Also we should definitely compress once the DB has reached 100k lines or it might grow too big
61
+ if (
62
+ typeof ac.sizeFactorMinimumSize === 'number' &&
63
+ ac.sizeFactorMinimumSize >= 0 &&
64
+ ac.sizeFactorMinimumSize <= 100000
65
+ ) {
66
+ ret.autoCompress.sizeFactorMinimumSize = ac.sizeFactorMinimumSize;
67
+ }
68
+ }
69
+ if (tools.isObject(conf.throttleFS)) {
70
+ const thr = conf.throttleFS;
71
+ // Don't write more often than every second and write at least once every hour
72
+ if (typeof thr.intervalMs === 'number' && thr.intervalMs >= 1000 && thr.intervalMs <= 3600000) {
73
+ ret.throttleFS.intervalMs = thr.intervalMs;
74
+ }
75
+ // Don't keep too much in memory - 100k changes are more than enough
76
+ if (
77
+ typeof thr.maxBufferedCommands === 'number' &&
78
+ thr.maxBufferedCommands >= 0 &&
79
+ thr.maxBufferedCommands <= 100000
80
+ ) {
81
+ ret.throttleFS.maxBufferedCommands = thr.maxBufferedCommands;
82
+ }
83
+ }
84
+
85
+ return ret;
86
+ }
87
+
23
88
  /**
24
89
  * This class inherits InMemoryFileDB class and adds all relevant logic for objects
25
90
  * including the available methods for use by js-controller directly
@@ -32,26 +97,7 @@ class ObjectsInMemoryJsonlDB extends ObjectsInMemoryFileDB {
32
97
  backupDirName: 'backup-objects'
33
98
  };
34
99
 
35
- /** @type {import("@alcalzone/jsonl-db").JsonlDBOptions<any>} */
36
- const jsonlOptions = settings.connection.jsonlOptions || {
37
- autoCompress: {
38
- sizeFactor: 2,
39
- sizeFactorMinimumSize: 25000
40
- },
41
- ignoreReadErrors: true,
42
- throttleFS: {
43
- intervalMs: 60000,
44
- maxBufferedCommands: 1000
45
- },
46
- lockfile: {
47
- // 5 retries starting at 250ms add up to just above 2s,
48
- // so the DB has 3 more seconds to load all data if it wants to stay within the 5s connectionTimeout
49
- retries: 5,
50
- retryMinTimeoutMs: 250,
51
- // This makes sure the DB stays locked for maximum 2s even if the process crashes
52
- staleMs: 2000
53
- }
54
- };
100
+ const jsonlOptions = normalizeJsonlOptions(settings.connection.jsonlOptions);
55
101
  settings.jsonlDB = {
56
102
  fileName: 'objects.jsonl'
57
103
  };
package/package.json CHANGED
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@iobroker/db-objects-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-objects-file": "4.0.21",
11
- "@iobroker/db-objects-redis": "4.0.21",
9
+ "@iobroker/db-base": "4.0.24",
10
+ "@iobroker/db-objects-file": "4.0.24",
11
+ "@iobroker/db-objects-redis": "4.0.24",
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": "4749d256d312623ae7621e7ae47d44580b3aa8c1"
40
+ "gitHead": "5645debf3394177abeef58917f872a7d8c43e320"
41
41
  }