I added the "next" feature which calculates siblings in the tree. There is
still a problem in the result:
key, value
"*", {CountLineCode: 2256, SumCyclomatic: 534, next: ["accessible/",
"accessible/", "accessible/", "accessible/", "accessible/", "accessible/",
"accessible/", "accessible/", "accessible/", "accessible/"]}
"accessible/", {CountLineCode: 2256, SumCyclomatic: 534, next: ["src/",
"src/", "build/", "src/", "src/", "src/", "src/", "src/", "src/", "src/"]}
...
The desired result would look like this:
key, value
"*", {CountLineCode: 2256, SumCyclomatic: 534, next: ["accessible/"]}
"accessible/", {CountLineCode: 2256, SumCyclomatic: 534, next: ["src/",
"build/"]}
...
Does it ring a bell? Please let me know.
I think it has something to do with the rereduce part of the reduce
function. I could not figure out how to do it right. Maybe this is also a
cloning feature.
Here the current version of my functions:
map:
function(doc) {
if(doc["File"] !== undefined) {
var pathelements = doc["File"].split("/");
pathelements.push(null); // .next of the complete path is []
var path = "*"; // used for the root of the tree
for (var idx = 0; idx < pathelements.length; idx++) {
{ // block local variables to avoid shallow copy issues!!!
var output = {
"CountLineCode": doc.CountLineCode,
"SumCyclomatic": doc.SumCyclomatic,
"File": doc.File,
"next": ""
};
output.next = pathelements[idx];
if (idx < pathelements.length-1) {
output.next += "/";
}
emit(path, output);
} // end block
if (path === "*") {
path = "";
}
path = path + pathelements[idx];
if (idx < pathelements.length-2) {
path += "/";
}
}
}
}
reduce:
function(keys, values, rereduce){
if (!rereduce){
var output = {
"CountLineCode": 0,
"SumCyclomatic": 0,
"next": []
};
for(var idx in values) {
if (values[idx].CountLineCode !== undefined) {
output.CountLineCode += values[idx].CountLineCode;
}
if (values[idx].SumCyclomatic !== undefined) {
output.SumCyclomatic += values[idx].SumCyclomatic;
}
if (values[idx].next !== undefined ) {
var path;
if (keys[idx][0] === "*") {
path = values[idx].next;
} else {
path = keys[idx][0]+values[idx].next
}
// File startsWith path?
if (values[idx].File.substr(0, path.length)===path) {
if (!(values[idx].next in output.next)) {
output.next.push(values[idx].next);
}
}
}
}
return output;
} else {
// rereduce
var output = {
"CountLineCode": 0,
"SumCyclomatic": 0,
"next": []
};
for (val in values) {
output.CountLineCode += val.CountLineCode;
output.SumCyclomatic += val.SumCyclomatic;
// only append next if not contained already
for (next in val.next) {
if (!(next in output.next)) {
output.next.push(next);
}
}
}
return output;
}
}