Commit 512f0fdc by James Cropcho

well, that's pretty inefficient, but I have a better idea...

parent 7b6fc8c5
use popplet;
if (typeof collection == "undefined") {
throw "You have to supply a 'collection' variable, a la \"--eval 'var limit = 10'\"";
}
if (typeof limit == "undefined") { limit = db[collection].count(); }
print("Using limit of " + limit);
canHaveChildren = function (v) {
var isArray = ( v &&
typeof v === 'object' &&
typeof v.length === 'number' &&
!(v.propertyIsEnumerable('length')));
var alreadyEmitted = new Array(); // global
schemaAnalyzerKeyIsPresent = function(toBeEmitted, alreadyEmitted) {
for(key in alreadyEmitted) {
if(key["key"] === toBeEmitted["key"] && key["type"] === toBeEmitted["type"]) {
return true;
}
}
return false;
}
db.system.js.save( { _id : "schemaAnalyzerKeyIsPresent", value : schemaAnalyzerKeyIsPresent } );
schemaAnalyzerCanHaveChildren = function (v) {
var isArray = v &&
typeof v === 'object' &&
typeof v.length === 'number' &&
!(v.propertyIsEnumerable('length'));
var isObject = typeof v === 'object';
return isArray || isObject;
}
db.system.js.save( { _id : "canHaveChildren", value : canHaveChildren } );
mapRecursive = function(parentKey, keys){
db.system.js.save( { _id : "schemaAnalyzerCanHaveChildren", value : schemaAnalyzerCanHaveChildren } );
schemaAnalyzerMapRecursive = function(parentKey, keys) {
for (var key in keys) {
var value = keys[key];
key = key.replace(/\d+/g,'XX');
key = (parentKey + "." + key).replace(/\.\d+/g,'.XX');
emit({"key": parentKey + "." + key, "typee": typeof value}, {occurrences: 1});
toBeEmitted = {key: key, type: typeof value};
if(!schemaAnalyzerKeyIsPresent(toBeEmitted,alreadyEmitted)) {
emit(toBeEmitted, {occurrences: 1});
alreadyEmitted.push(toBeEmitted);
}
if (canHaveChildren(value)) {
mapRecursive(parentKey + "." + key, value);
if (schemaAnalyzerCanHaveChildren(value)) {
schemaAnalyzerMapRecursive(key, value);
}
}
}
db.system.js.save({_id: "mapRecursive", value: mapRecursive});
db.system.js.save({_id: "schemaAnalyzerMapRecursive", value: schemaAnalyzerMapRecursive});
map = function() {
var keys = this;
var alreadyEmitted = new Array(); // reset global variable
for (var key in keys) {
var value = keys[key];
key = key.replace(/\d+/g,'XX');
emit({"key": key, "typee": typeof value}, {occurrences: 1});
key = key.replace(/\.\d+/g,'.XX');
toBeEmitted = {key : key, type : typeof value};
if(!schemaAnalyzerKeyIsPresent(toBeEmitted,alreadyEmitted)) {
emit(toBeEmitted, {occurrences: 1});
alreadyEmitted.push(toBeEmitted);
}
if (canHaveChildren(value)) {
mapRecursive(key, value);
if (schemaAnalyzerCanHaveChildren(value)) {
schemaAnalyzerMapRecursive(key, value);
}
}
}
......@@ -52,16 +79,25 @@ reduce = function(key, values){
}
finalize = function(key, value) {
value.percentage = value.occurrences / 1000 * 100.0;
value.percentage = value.occurrences / limit * 100.0;
return value;
}
db.usersKeyNames.drop();
db.users.mapReduce(map, reduce, { finalize: finalize,
out: 'usersKeyNames',
verbose: true,
limit: 1000,
sort: {_id: -1}});
db.usersKeyNames.find()
var resultsCollectionName = collection + "KeyNames";
db[collection].mapReduce(map, reduce, { finalize: finalize,
out: {
replace : resultsCollectionName,
db : "schemaAnalyzerResults"},
limit : limit,
sort : {_id: -1},
scope : { limit : limit }});
var resultsDB = db.getMongo().getDB("schemaAnalyzerResults");
var keyNames = resultsDB[resultsCollectionName].find().sort({ "value.percentage": -1});
keyNames.forEach(function(keyName) {
print(tojson(keyName, '', true));
});
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