From ac759e1d04fad0e068bea4864ad88ac670032024 Mon Sep 17 00:00:00 2001 From: Zaid Abdulla Date: Tue, 16 Aug 2016 13:17:14 -0700 Subject: [PATCH] Clear descendants when a parameter is deleted --- lib/device.js | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) diff --git a/lib/device.js b/lib/device.js index ba2c2af..3e925a3 100644 --- a/lib/device.js +++ b/lib/device.js @@ -224,7 +224,22 @@ function unpack(deviceData, path, revision) { function invalidateSuperset(deviceData, path, attributes) { - throw new Error('Not implemented'); + let depth = 0; + if (attributes.exist != null) + depth = 99; + + for (let sup of deviceData.paths.superset(path, 99)) { + if (sup.wildcard == path.wildcard && sup.length == path.length) + continue; + + for (let attrName in deviceData.timestamps) { + let t = deviceData.timestamps[attrName].get(sup); + if (t <= attributes[attrName] || t <= attributes.exist) { + deviceData.timestamps[attrName].delete(sup); + deviceData.values[attrName].delete(sup); + } + } + } } @@ -263,6 +278,9 @@ function normalizeAttributes(timestamps, values) { function set(deviceData, path, timestamps, values) { // TODO Optimize // TODO Allow overwriting attribute value with same timestamp + if (values == null) + values = {}; + path = deviceData.paths.add(path); if (!path.wildcard) { @@ -281,7 +299,7 @@ function set(deviceData, path, timestamps, values) { if (timestamps.exist != null) { deviceData.timestamps.exist.set(path, timestamps.exist); - if (values != null && values.exist != null) + if (values.exist != null) deviceData.values.exist.set(path, values.exist); } @@ -327,22 +345,23 @@ function set(deviceData, path, timestamps, values) { deviceData.values[attrName].set(path, values[attrName]); } else { - if (deviceData.values[attrName].has(path)) + if (deviceData.values[attrName].has(path)) { invalidate = true; - deviceData.values[attrName].delete(path) + deviceData.values[attrName].delete(path); + } } if (invalidate) { let inv = toInvalidate.get(path) || {}; - if (!timestamps[attrName] <= inv[attrName]) { + if (!(timestamps[attrName] <= inv[attrName])) { inv[attrName] = timestamps[attrName]; - toInvalidate.set(sub, inv); + toInvalidate.set(path, inv); } } } } - toInvalidate.forEach(function(p, a) { + toInvalidate.forEach(function(a, p) { invalidateSuperset(deviceData, p, a); });