Commit f087f0e4 by James Cropcho

it's all called 'variety' now

parent e0d3bac4
/* MongoDB Schema Analyzer /* Variety: A MongoDB Schema Analyzer
This tool helps you get a sense of your application's schema, as well as any This tool helps you get a sense of your application's schema, as well as any
outliers to that schema. Particularly useful when you inherit a codebase with outliers to that schema. Particularly useful when you inherit a codebase with
data dump and want to quickly learn how the data's structured. Also useful for data dump and want to quickly learn how the data's structured. Also useful for
finding rare keys. finding rare keys.
Please see https://github.com/JamesCropcho/mongodb-schema-analyzer for details. Please see https://github.com/JamesCropcho/variety for details.
Released by Maypop Inc, © 2012, under the MIT License. */ Released by Maypop Inc, © 2012, under the MIT License. */
print("Variety: A MongoDB Schema Analyzer")
print("Version 1.0, released 10 April 2012")
if (typeof collection === "undefined") { if (typeof collection === "undefined") {
throw "You have to supply a 'collection' variable, à la --eval 'var collection = \"animals\"'"; throw "You have to supply a 'collection' variable, à la --eval 'var collection = \"animals\"'";
} }
...@@ -16,7 +19,7 @@ if (typeof collection === "undefined") { ...@@ -16,7 +19,7 @@ if (typeof collection === "undefined") {
if (typeof limit === "undefined") { var limit = db[collection].count(); } if (typeof limit === "undefined") { var limit = db[collection].count(); }
print("Using limit of " + limit); print("Using limit of " + limit);
schemaAnalyzerCanHaveChildren = function (v) { varietyCanHaveChildren = function (v) {
var isArray = v && var isArray = v &&
typeof v === 'object' && typeof v === 'object' &&
typeof v.length === 'number' && typeof v.length === 'number' &&
...@@ -24,25 +27,25 @@ schemaAnalyzerCanHaveChildren = function (v) { ...@@ -24,25 +27,25 @@ schemaAnalyzerCanHaveChildren = function (v) {
var isObject = typeof v === 'object'; var isObject = typeof v === 'object';
return isArray || isObject; return isArray || isObject;
} }
db.system.js.save( { _id : "schemaAnalyzerCanHaveChildren", value : schemaAnalyzerCanHaveChildren } ); db.system.js.save( { _id : "varietyCanHaveChildren", value : varietyCanHaveChildren } );
schemaAnalyzerMapRecursive = function(parentKey, keys) { varietyMapRecursive = function(parentKey, keys) {
for (var key in keys) { for (var key in keys) {
var value = keys[key]; var value = keys[key];
key = (parentKey + "." + key).replace(/\.\d+/g,'.XX'); key = (parentKey + "." + key).replace(/\.\d+/g,'.XX');
emit({key : key}, {type: schemaAnalyzerTypeOf(value)}); emit({key : key}, {type: varietyTypeOf(value)});
if (schemaAnalyzerCanHaveChildren(value)) { if (varietyCanHaveChildren(value)) {
schemaAnalyzerMapRecursive(key, value); varietyMapRecursive(key, value);
} }
} }
} }
db.system.js.save({_id: "schemaAnalyzerMapRecursive", value: schemaAnalyzerMapRecursive}); db.system.js.save({_id: "varietyMapRecursive", value: varietyMapRecursive});
schemaAnalyzerTypeOf = function(thing) { varietyTypeOf = function(thing) {
if (typeof thing === "undefined") { throw "schemaAnalyzerTypeOf() requires an argument"; } if (typeof thing === "undefined") { throw "varietyTypeOf() requires an argument"; }
if (typeof thing !== "object") { if (typeof thing !== "object") {
return typeof thing; return typeof thing;
...@@ -59,7 +62,7 @@ schemaAnalyzerTypeOf = function(thing) { ...@@ -59,7 +62,7 @@ schemaAnalyzerTypeOf = function(thing) {
} }
} }
} }
db.system.js.save({_id: "schemaAnalyzerTypeOf", value: schemaAnalyzerTypeOf}); db.system.js.save({_id: "varietyTypeOf", value: varietyTypeOf});
map = function() { map = function() {
var keys = this; var keys = this;
...@@ -71,10 +74,10 @@ map = function() { ...@@ -71,10 +74,10 @@ map = function() {
// items in an array. -JC // items in an array. -JC
key = key.replace(/\.\d+/g,'.XX'); key = key.replace(/\.\d+/g,'.XX');
emit({key : key}, {type: schemaAnalyzerTypeOf(value)}); emit({key : key}, {type: varietyTypeOf(value)});
if (schemaAnalyzerCanHaveChildren(value)) { if (varietyCanHaveChildren(value)) {
schemaAnalyzerMapRecursive(key, value); varietyMapRecursive(key, value);
} }
} }
} }
...@@ -97,12 +100,12 @@ var resultsCollectionName = collection + "Keys"; ...@@ -97,12 +100,12 @@ var resultsCollectionName = collection + "Keys";
db[collection].mapReduce(map, reduce, { db[collection].mapReduce(map, reduce, {
out: { out: {
replace : resultsCollectionName, replace : resultsCollectionName,
db : "schemaAnalyzerResults"}, db : "varietyResults"},
limit : limit, limit : limit,
sort : {_id: -1}, sort : {_id: -1},
scope : { limit : limit }}); scope : { limit : limit }});
var resultsDB = db.getMongo().getDB("schemaAnalyzerResults"); var resultsDB = db.getMongo().getDB("varietyResults");
var numDocuments = db[collection].count(); var numDocuments = db[collection].count();
......
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