Commit deb618ea by Tim Ludwinski

Fix failing UnnamedObjectsAnalysisTest

parent 88e5f3c6
...@@ -200,6 +200,11 @@ To persist to an alternate MongoDB database, you may specify the following param ...@@ -200,6 +200,11 @@ To persist to an alternate MongoDB database, you may specify the following param
$ mongo test --quiet --eval "var collection = 'users', persistResults=true, resultsDatabase='db.example.com/variety' variety.js $ mongo test --quiet --eval "var collection = 'users', persistResults=true, resultsDatabase='db.example.com/variety' variety.js
``` ```
### Reserved Keys ###
Variety expects keys to be well formed, not having any '.'s in them (mongo 2.4 allows dots in certain cases). Also mongo uses the psudo keys 'XX' and keys coresponding to the regex 'XX\d+XX.*' for use with arrays. You can change the string XX in these patterns to whatever you like if there is a conflict in your database using the `arrayEscape` parameter.
$ mongo test --quiet --eval "var collection = 'users', arrayEscape = 'YY'" variety.js
### Command Line Interface ### Command Line Interface
Variety itself is command line friendly, as shown on examples above. Variety itself is command line friendly, as shown on examples above.
But if you are a NPM and Node.js user, you could prefer the But if you are a NPM and Node.js user, you could prefer the
......
...@@ -84,6 +84,7 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -84,6 +84,7 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
read('resultsPass', null); read('resultsPass', null);
read('logKeysContinuously', false); read('logKeysContinuously', false);
read('excludeSubkeys', []); read('excludeSubkeys', []);
read('arrayEscape', 'XX');
//Translate excludeSubkeys to set like object... using an object for compatibility... //Translate excludeSubkeys to set like object... using an object for compatibility...
config.excludeSubkeys = config.excludeSubkeys.reduce(function (result, item) { result[item+'.'] = true; return result; }, {}); config.excludeSubkeys = config.excludeSubkeys.reduce(function (result, item) { result[item+'.'] = true; return result; }, {});
...@@ -185,9 +186,11 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -185,9 +186,11 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
v instanceof BinData; v instanceof BinData;
return !specialObject && (isArray || isObject); return !specialObject && (isArray || isObject);
} }
var arrayRegex = new RegExp('\\.' + config.arrayEscape + '\\d+' + config.arrayEscape + '\\.', 'g');
function serialize(document, parentKey, maxDepth) { function serialize(document, parentKey, maxDepth) {
if(Object.prototype.hasOwnProperty.call(excludeSubkeys, parentKey.replace('.XX.', '.'))) if(Object.prototype.hasOwnProperty.call(excludeSubkeys, parentKey.replace(arrayRegex, '.')))
return; return;
for(var key in document) { for(var key in document) {
//skip over inherited properties such as string, length, etch //skip over inherited properties such as string, length, etch
...@@ -196,7 +199,7 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -196,7 +199,7 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
} }
var value = document[key]; var value = document[key];
if(Array.isArray(document)) if(Array.isArray(document))
key = 'XX'; //translate unnamed object key from {_parent_name_}.{_index_} to {_parent_name_}.XX key = config.arrayEscape + key + config.arrayEscape; //translate unnamed object key from {_parent_name_}.{_index_} to {_parent_name_}.arrayEscape{_index_}arrayEscape.
result[parentKey+key] = value; result[parentKey+key] = value;
//it's an object, recurse...only if we haven't reached max depth //it's an object, recurse...only if we haven't reached max depth
if(isHash(value) && maxDepth > 1) { if(isHash(value) && maxDepth > 1) {
...@@ -211,8 +214,10 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -211,8 +214,10 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
// convert document to key-value map, where value is always an array with types as plain strings // convert document to key-value map, where value is always an array with types as plain strings
var analyseDocument = function(document) { var analyseDocument = function(document) {
var result = {}; var result = {};
var arrayRegex = new RegExp('\\.' + config.arrayEscape + '\\d+' + config.arrayEscape, 'g');
for (var key in document) { for (var key in document) {
var value = document[key]; var value = document[key];
key = key.replace(arrayRegex, '.' + config.arrayEscape);
if(typeof result[key] === 'undefined') { if(typeof result[key] === 'undefined') {
result[key] = {}; result[key] = {};
} }
...@@ -283,8 +288,9 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */ ...@@ -283,8 +288,9 @@ Released by Maypop Inc, © 2012-2016, under the MIT License. */
// We throw away keys which end in an array index, since they are not useful // We throw away keys which end in an array index, since they are not useful
// for our analysis. (We still keep the key of their parent array, though.) -JC // for our analysis. (We still keep the key of their parent array, though.) -JC
var arrayRegex = new RegExp('\\.' + config.arrayEscape + '$', 'g');
var filter = function(item) { var filter = function(item) {
return !item._id.key.match(/\.XX$/); return !item._id.key.match(arrayRegex);
}; };
// sort desc by totalOccurrences or by key asc if occurrences equal // sort desc by totalOccurrences or by key asc if occurrences equal
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment