node-red-contrib-ccu 4.7.2 → 4.9.0

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.
@@ -0,0 +1,333 @@
1
+ /*
2
+ * The datapoint subscriptions of a connection and their dispatch.
3
+ *
4
+ * Until 4.7.2 every subscription was tested against every event, and the outcome was remembered
5
+ * per datapoint in two Sets of subscription ids (a whitelist and a blacklist per datapoint name):
6
+ * a subscription that did not match a datapoint landed in that datapoint's blacklist, so the
7
+ * memory grew with datapoints x subscriptions (12,000 datapoints x 1,000 subscriptions was some
8
+ * 300 MB of live heap on a CCU3), and a `cache: true` subscription walked every cached value at
9
+ * subscribe time to fill them all at once. Worse, the replay added the id as a number while the
10
+ * event path added it as a string, so an unsubscribe (by number) left the string entries behind:
11
+ * every deploy grew the sets again.
12
+ *
13
+ * Now the subscriptions are indexed by one exact key: the most selective filter attribute given
14
+ * as a plain non-empty string (`datapointName`, then `channel`, `device`, ...). An event looks up
15
+ * the buckets of its own attribute values plus the small list of subscriptions without an exact
16
+ * key (rooms, functions, regular expressions, ...) and tests only those candidates - with the
17
+ * filter logic as it was. Memory is O(subscriptions): one entry per subscription, and unsubscribe
18
+ * removes exactly that entry.
19
+ */
20
+
21
+ /**
22
+ * The filter attributes an event can be looked up by, most selective first. A subscription is
23
+ * indexed by the first of these its filter names with a plain, non-empty string; the others
24
+ * (and anything that is a RegExp, a number, an array or empty) go into the generic list.
25
+ */
26
+ const INDEXED_ATTRIBUTES = [
27
+ 'datapointName',
28
+ 'channel',
29
+ 'device',
30
+ 'channelName',
31
+ 'deviceName',
32
+ 'datapoint',
33
+ 'channelType',
34
+ 'deviceType',
35
+ 'iface',
36
+ ];
37
+
38
+ /**
39
+ * The exact key a filter is indexed by.
40
+ * @param {object} filter
41
+ * @returns {{attr: string, value: string}|null} null for a subscription that goes into the generic list
42
+ */
43
+ function keyOf(filter) {
44
+ for (const attr of INDEXED_ATTRIBUTES) {
45
+ const value = filter[attr];
46
+ if (typeof value === 'string' && value !== '') {
47
+ return {attr, value};
48
+ }
49
+ }
50
+
51
+ return null;
52
+ }
53
+
54
+ /**
55
+ * Whether a message passes a subscription's filter. This is the attribute loop of the old
56
+ * callCallback(), unchanged in what it compares; only the per-datapoint whitelist shortcut is
57
+ * gone (it skipped the comparison for a datapoint that had matched once).
58
+ * @param {object} filter
59
+ * @param {object} message
60
+ * @param {number|string} [id] for the trace lines
61
+ * @param {{trace: Function}} [logger]
62
+ * @returns {boolean}
63
+ */
64
+ function matches(filter, message, id, logger) {
65
+ if (!filter) {
66
+ return true;
67
+ }
68
+
69
+ // Checked before the attribute loop: only present in the filter when a node opted in, so
70
+ // existing subscriptions keep receiving values the ReGa could not timestamp (#96)
71
+ if (filter.uncertain === false && message.uncertain) {
72
+ return false;
73
+ }
74
+
75
+ const attributes = Object.keys(filter);
76
+
77
+ for (let i = 0, {length} = attributes; i < length; i++) {
78
+ const attr = attributes[i];
79
+
80
+ if (attr === 'cache') {
81
+ // if filter.cache==false - Drop messages with msg.cache==true
82
+ if (!filter.cache && message.cache) {
83
+ return false;
84
+ }
85
+
86
+ continue;
87
+ }
88
+
89
+ if (attr === 'change') {
90
+ // if filter.change==true - Drop messages with msg.change==false - except msg.cache==true && filter.cache==true
91
+ if (filter.change && !message.change && !(filter.cache && message.cache)) {
92
+ return false;
93
+ }
94
+
95
+ continue;
96
+ }
97
+
98
+ if (attr === 'stable') {
99
+ // if filter.stable==true - Drop messages with msg.stable==false
100
+ if (filter.stable && !message.stable) {
101
+ return false;
102
+ }
103
+
104
+ continue;
105
+ }
106
+
107
+ if (attr === 'uncertain') {
108
+ // already decided above, and not a value to compare
109
+ continue;
110
+ }
111
+
112
+ if (filter[attr] === '') {
113
+ // an empty value filters nothing
114
+ continue;
115
+ }
116
+
117
+ if (attr === 'channelIndex' && typeof filter[attr] !== 'undefined') {
118
+ filter[attr] = Number.parseInt(filter[attr], 10);
119
+ }
120
+
121
+ if (Array.isArray(message[attr])) {
122
+ if (filter[attr] instanceof RegExp) {
123
+ if (logger) {
124
+ logger.trace('cb test regex array', id, attr, filter[attr], message[attr]);
125
+ }
126
+
127
+ if (!message[attr].some((item) => filter[attr].test(item))) {
128
+ return false;
129
+ }
130
+ } else if (!message[attr].includes(filter[attr])) {
131
+ if (logger) {
132
+ logger.trace('cb mismatch array', id, attr, filter[attr], message[attr]);
133
+ }
134
+
135
+ return false;
136
+ }
137
+ } else if (filter[attr] instanceof RegExp) {
138
+ if (!filter[attr].test(message[attr])) {
139
+ if (logger) {
140
+ logger.trace('cb mismatch regex', id, attr, filter[attr], message[attr]);
141
+ }
142
+
143
+ return false;
144
+ }
145
+ } else if (filter[attr] !== message[attr]) {
146
+ if (logger) {
147
+ logger.trace('cb mismatch misc ' + id + ' ' + attr + ' ' + filter[attr] + ' ' + message[attr]);
148
+ }
149
+
150
+ return false;
151
+ }
152
+ }
153
+
154
+ return true;
155
+ }
156
+
157
+ class SubscriptionIndex {
158
+ constructor() {
159
+ /** @type {Map<number, {filter: object, callback: Function, key: {attr: string, value: string}|null}>} */
160
+ this.subscriptions = new Map();
161
+ /** @type {Map<string, Map<string, Set<number>>>} attribute -> value -> ids */
162
+ this.buckets = new Map();
163
+ /** @type {Set<number>} the subscriptions without an exact key */
164
+ this.generic = new Set();
165
+ this.nextId = 0;
166
+ }
167
+
168
+ get size() {
169
+ return this.subscriptions.size;
170
+ }
171
+
172
+ /**
173
+ * @param {object} filter
174
+ * @param {Function} callback
175
+ * @returns {number} the subscription id
176
+ */
177
+ add(filter, callback) {
178
+ const id = this.nextId;
179
+ this.nextId += 1;
180
+ const key = keyOf(filter);
181
+ this.subscriptions.set(id, {filter, callback, key});
182
+
183
+ if (key) {
184
+ let byValue = this.buckets.get(key.attr);
185
+ if (!byValue) {
186
+ byValue = new Map();
187
+ this.buckets.set(key.attr, byValue);
188
+ }
189
+
190
+ let ids = byValue.get(key.value);
191
+ if (!ids) {
192
+ ids = new Set();
193
+ byValue.set(key.value, ids);
194
+ }
195
+
196
+ ids.add(id);
197
+ } else {
198
+ this.generic.add(id);
199
+ }
200
+
201
+ return id;
202
+ }
203
+
204
+ /**
205
+ * @param {number|string} id
206
+ * @returns {boolean} whether the subscription existed
207
+ */
208
+ remove(id) {
209
+ const subscription = this.subscriptions.get(Number(id));
210
+ if (!subscription) {
211
+ return false;
212
+ }
213
+
214
+ id = Number(id);
215
+ this.subscriptions.delete(id);
216
+ const {key} = subscription;
217
+ if (key) {
218
+ const byValue = this.buckets.get(key.attr);
219
+ const ids = byValue && byValue.get(key.value);
220
+ if (ids) {
221
+ ids.delete(id);
222
+ if (ids.size === 0) {
223
+ byValue.delete(key.value);
224
+ if (byValue.size === 0) {
225
+ this.buckets.delete(key.attr);
226
+ }
227
+ }
228
+ }
229
+ } else {
230
+ this.generic.delete(id);
231
+ }
232
+
233
+ return true;
234
+ }
235
+
236
+ /**
237
+ * @param {number|string} id
238
+ * @returns {{filter: object, callback: Function, key: object|null}|undefined}
239
+ */
240
+ get(id) {
241
+ return this.subscriptions.get(Number(id));
242
+ }
243
+
244
+ /**
245
+ * The ids whose filter can match the message - those indexed by one of the message's own
246
+ * attribute values, and the generic ones - in subscription order, as every subscription was
247
+ * called in before.
248
+ * @param {object} message
249
+ * @returns {number[]}
250
+ */
251
+ candidates(message) {
252
+ const ids = [];
253
+ for (const [attr, byValue] of this.buckets) {
254
+ const value = message[attr];
255
+ if (typeof value === 'string') {
256
+ const bucket = byValue.get(value);
257
+ if (bucket) {
258
+ for (const id of bucket) {
259
+ ids.push(id);
260
+ }
261
+ }
262
+ }
263
+ }
264
+
265
+ for (const id of this.generic) {
266
+ ids.push(id);
267
+ }
268
+
269
+ if (ids.length > 1) {
270
+ ids.sort((a, b) => a - b);
271
+ }
272
+
273
+ return ids;
274
+ }
275
+
276
+ /**
277
+ * The number of index entries - what the memory grows with. Equals size: one entry per
278
+ * subscription, in a bucket or in the generic list.
279
+ * @returns {number}
280
+ */
281
+ entries() {
282
+ let count = this.generic.size;
283
+ for (const byValue of this.buckets.values()) {
284
+ for (const ids of byValue.values()) {
285
+ count += ids.size;
286
+ }
287
+ }
288
+
289
+ return count;
290
+ }
291
+
292
+ /**
293
+ * For a `cache: true` subscription: the cached values it has to be replayed with. A filter that
294
+ * names the datapoint exactly (`datapointName`, or `iface` + `channel` + `datapoint`) is one
295
+ * lookup; any other walks the values, testing the exact key before the full filter.
296
+ * @param {object} filter
297
+ * @param {object} values datapointName -> cached message
298
+ * @returns {object[]} the cached messages that can match, in the values' order
299
+ */
300
+ static replayCandidates(filter, values) {
301
+ const key = keyOf(filter);
302
+ let datapointName;
303
+ if (key && key.attr === 'datapointName') {
304
+ datapointName = key.value;
305
+ } else if (
306
+ typeof filter.iface === 'string' &&
307
+ filter.iface !== '' &&
308
+ typeof filter.channel === 'string' &&
309
+ filter.channel !== '' &&
310
+ typeof filter.datapoint === 'string' &&
311
+ filter.datapoint !== ''
312
+ ) {
313
+ datapointName = filter.iface + '.' + filter.channel + '.' + filter.datapoint;
314
+ }
315
+
316
+ if (datapointName !== undefined) {
317
+ const value = values[datapointName];
318
+ return value ? [value] : [];
319
+ }
320
+
321
+ const result = [];
322
+ for (const name of Object.keys(values)) {
323
+ const value = values[name];
324
+ if (!key || value[key.attr] === key.value) {
325
+ result.push(value);
326
+ }
327
+ }
328
+
329
+ return result;
330
+ }
331
+ }
332
+
333
+ module.exports = {SubscriptionIndex, keyOf, matches, INDEXED_ATTRIBUTES};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-ccu",
3
- "version": "4.7.2",
3
+ "version": "4.9.0",
4
4
  "license": "MIT",
5
5
  "main": "none",
6
6
  "keywords": [