Implement path alias unpacking

This commit is contained in:
Zaid Abdulla committed 2016-07-20 03:29:19 +03:00
1 parent 788bd98c8c
commit 1537603945
7 files changed
+691 -541

No files matched your search

+86 -17
View File
@@ -61,12 +61,6 @@ function sanitizeParameterValue(parameterValue) {
}
function getAliasDeclarations(path) {
// TODO Return declarations necessary for unpacking aliases
return [];
}
function inferTimestamps(deviceData, path) {
var res = {};
@@ -79,14 +73,14 @@ function inferTimestamps(deviceData, path) {
}
for (let i = path.length - 1; i > 0; -- i) {
if (path.wildcards & ((2 << i) - 1) === 0)
if (path.wildcard & ((2 << i) - 1) === 0)
break;
let param = null;
let ancestor = path.slice(0, i);
let ancestorTimestamp = 0;
for (let sup of deviceData.paths.superset(ancestor)) {
if (!sup.wildcards)
if (!sup.wildcard)
param = sup;
let t = deviceData.timestamps.exist.get(sup);
@@ -108,7 +102,7 @@ function inferTimestamps(deviceData, path) {
newt[attrName] = ancestorTimestamp;
for (let sub of deviceData.paths.subset(ancestor)) {
if (sub.wildcards)
if (sub.wildcard)
continue;
let subt = inferTimestamps(deviceData, sub.concat(path.slice(sub.length, path.length)));
for (let attrName in newt) {
@@ -128,9 +122,84 @@ function inferTimestamps(deviceData, path) {
}
function getAliasDeclarations(path, timestamp) {
var decs = [];
function recursive(pattern, prefix) {
var pattern = prefix.concat(pattern);
for (let i = prefix.length; i < pattern.length; ++ i) {
if (Array.isArray(pattern[i])) {
let pat = pattern[i];
pattern[i] = '*';
for (let j = 0; j < pat.length; j += 2)
recursive(pat[j], pattern.slice(0, i + 1));
}
}
if (prefix.length)
decs.push([pattern, {exist: timestamp, value: timestamp}]);
else
decs.push([pattern, {exist: timestamp}]);
}
recursive(path, []);
return decs;
}
function unpack(deviceData, path, revision) {
// TODO implement alias unpacking
return [path];
let allMatches = [];
if (path.alias == 0) {
for (let p of deviceData.paths.subset(path, 0))
if (deviceData.values.exist.get(p, revision))
allMatches.push(p);
return allMatches;
}
let wildcardPath = path.slice();
wildcardPath.wildcard = path.wildcard;
for (let i = 0; i < wildcardPath.length; ++ i)
if (Array.isArray(wildcardPath[i])) {
wildcardPath[i] = '*';
wildcardPath.wildcard |= 1 << i;
}
for (let p of deviceData.paths.subset(wildcardPath, 0))
if (deviceData.values.exist.get(p, revision))
allMatches.push(p);
for (let i = path.length - 1; i >= 0; -- i) {
if (Array.isArray(path[i])) {
for (let j = 0; j < path[i].length; j += 2) {
let p = wildcardPath.slice(0, i + 1).concat(path[i][j]);
p.alias = (wildcardPath.alias & ((1 << (i + 1)) - 1)) + path[i][j].alias << (i + 1);
p.wildcard = (wildcardPath.wildcard & ((1 << (i + 1)) - 1)) + path[i][j].wildcard << (i + 1);
let unpacked = unpack(deviceData, p, revision);
let filtered = [];
for (let up of unpacked) {
let v = deviceData.values.value.get(up, revision);
if (v && sanitizeParameterValue([path[i][j + 1], v[1]])[0] == v[0]) {
for (let m = 0; m < allMatches.length; ++ m) {
let k;
for (k = i; k >= 0; -- k)
if (allMatches[m][k] != up[k])
break
if (k < 0) {
filtered.push(allMatches[m]);
allMatches[m] = [];
}
}
}
}
allMatches = filtered;
}
}
}
return allMatches;
}
@@ -176,7 +245,7 @@ function set(deviceData, path, timestamps, values) {
// TODO Allow overwriting attribute value with same timestamp
path = deviceData.paths.add(path);
if (!path.wildcards) {
if (!path.wildcard) {
normalizeAttributes(timestamps, values);
if (values.exist && path.length > 1)
set(deviceData, path.slice(0, path.length - 1),
@@ -187,7 +256,7 @@ function set(deviceData, path, timestamps, values) {
var current = inferTimestamps(deviceData, path);
for (let c in current)
if (current[c] > timestamps[c] || (current[c] == timestamps[c] && path.wildcards > 0))//!deviceData.values[c].has(path)))
if (current[c] > timestamps[c] || (current[c] == timestamps[c] && path.wildcard > 0))
delete timestamps[c];
if (timestamps.exist != null) {
@@ -199,19 +268,19 @@ function set(deviceData, path, timestamps, values) {
current = inferTimestamps(deviceData, path);
for (let c in current)
if (current[c] > timestamps[c] || (current[c] == timestamps[c] && path.wildcards > 0))//!deviceData.values[c].has(path)))
if (current[c] > timestamps[c] || (current[c] == timestamps[c] && path.wildcard > 0))
delete timestamps[c];
let toInvalidate = new Map();
for (let sub of deviceData.paths.subset(path, 0)) {
if (path.wildcards == sub.wildcards)
if (path.wildcard == sub.wildcard)
continue;
for (let attrName in timestamps) {
if (deviceData.timestamps[attrName].get(sub) <= timestamps[attrName]) {
if (sub.wildcards || !deviceData.values[attrName].has(sub))
if (sub.wildcard || !deviceData.values[attrName].has(sub))
deviceData.timestamps[attrName].delete(sub);
else if (deviceData.timestamps[attrName].get(sub) < timestamps[attrName]) {
deviceData.timestamps[attrName].delete(sub);
@@ -263,7 +332,7 @@ function set(deviceData, path, timestamps, values) {
// depth = 99;
//
// for (let sup of deviceData.paths.superset(path, depth)) {
// if ((sup.wildcards && (3 << path.length) - 1) != path.wildcards) {
// if ((sup.wildcard && (3 << path.length) - 1) != path.wildcard) {
// throw new Error('Not implemented');
// }
// }