Commit 3c0521d1 by James Cropcho

Version 1.2.6: @jamescropcho has made variety.js pass JSHint analysis.

parent 2c720f3e
(28 March 2014) Version 1.2.6: @jamescropcho has made variety.js pass JSHint analysis.
(21 February 2014) Version 1.2.5: Thanks to @nitindhar7: Adds a sort option, so you can be more choosy with your limit. (21 February 2014) Version 1.2.5: Thanks to @nitindhar7: Adds a sort option, so you can be more choosy with your limit.
(05 December 2013) Version 1.2.4: Thanks to @jmargeta: Bugfix for newer mongo versions that don't support the JSON object. (05 December 2013) Version 1.2.4: Thanks to @jmargeta: Bugfix for newer mongo versions that don't support the JSON object.
......
...@@ -9,11 +9,11 @@ Please see https://github.com/variety/variety for details. ...@@ -9,11 +9,11 @@ Please see https://github.com/variety/variety for details.
Released by Maypop Inc, © 2012-2014, under the MIT License. */ Released by Maypop Inc, © 2012-2014, under the MIT License. */
print("Variety: A MongoDB Schema Analyzer") print("Variety: A MongoDB Schema Analyzer");
print("Version 1.2.5, released 21 February 2014") print("Version 1.2.6, released 28 March 2014");
var dbs = new Array(); var dbs = [];
var emptyDbs = new Array(); var emptyDbs = [];
if (typeof db_name === "string") { if (typeof db_name === "string") {
db = db.getMongo().getDB( db_name ); db = db.getMongo().getDB( db_name );
...@@ -24,17 +24,17 @@ db.adminCommand('listDatabases').databases.forEach(function(d){ ...@@ -24,17 +24,17 @@ db.adminCommand('listDatabases').databases.forEach(function(d){
if(db.getSisterDB(d.name).getCollectionNames().length > 0) { if(db.getSisterDB(d.name).getCollectionNames().length > 0) {
dbs.push(d.name); dbs.push(d.name);
} }
if(db.getSisterDB(d.name).getCollectionNames().length == 0) { if(db.getSisterDB(d.name).getCollectionNames().length === 0) {
emptyDbs.push(d.name); emptyDbs.push(d.name);
} }
}); });
if (emptyDbs.indexOf(db.getName()) != -1) { if (emptyDbs.indexOf(db.getName()) !== -1) {
throw "The database specified ("+ db +") is empty.\n"+ throw "The database specified ("+ db +") is empty.\n"+
"Possible database options are: " + dbs.join(", ") + "."; "Possible database options are: " + dbs.join(", ") + ".";
} }
if (dbs.indexOf(db.getName()) == -1) { if (dbs.indexOf(db.getName()) === -1) {
throw "The database specified ("+ db +") does not exist.\n"+ throw "The database specified ("+ db +") does not exist.\n"+
"Possible database options are: " + dbs.join(", ") + "."; "Possible database options are: " + dbs.join(", ") + ".";
} }
...@@ -46,7 +46,7 @@ if (typeof collection === "undefined") { ...@@ -46,7 +46,7 @@ if (typeof collection === "undefined") {
"Please see https://github.com/JamesCropcho/variety for details."; "Please see https://github.com/JamesCropcho/variety for details.";
} }
if (db[collection].count() == 0) { if (db[collection].count() === 0) {
throw "The collection specified (" + collection + ") in the database specified ("+ db +") does not exist or is empty.\n"+ throw "The collection specified (" + collection + ") in the database specified ("+ db +") does not exist or is empty.\n"+
"Possible collection options for database specified: " + collNames + "."; "Possible collection options for database specified: " + collNames + ".";
} }
...@@ -73,7 +73,7 @@ varietyCanHaveChildren = function (v) { ...@@ -73,7 +73,7 @@ varietyCanHaveChildren = function (v) {
v instanceof ObjectId || v instanceof ObjectId ||
v instanceof BinData; v instanceof BinData;
return !specialObject && (isArray || isObject); return !specialObject && (isArray || isObject);
} };
varietyTypeOf = function(thing) { varietyTypeOf = function(thing) {
if (typeof thing === "undefined") { throw "varietyTypeOf() requires an argument"; } if (typeof thing === "undefined") { throw "varietyTypeOf() requires an argument"; }
...@@ -110,13 +110,13 @@ varietyTypeOf = function(thing) { ...@@ -110,13 +110,13 @@ varietyTypeOf = function(thing) {
return "Object"; return "Object";
} }
} }
} };
var addTypeToArray = function(arr, value) { var addTypeToArray = function(arr, value) {
var t = varietyTypeOf(value); var t = varietyTypeOf(value);
var found = false; var found = false;
for(var i=0; i< arr.length; i++) { for(var i=0; i< arr.length; i++) {
if(arr[i] == t) { if(arr[i] === t) {
found = true; found = true;
break; break;
} }
...@@ -124,24 +124,24 @@ var addTypeToArray = function(arr, value) { ...@@ -124,24 +124,24 @@ var addTypeToArray = function(arr, value) {
if(!found) { if(!found) {
arr.push(t); arr.push(t);
} }
} };
var addRecordResult = function(key, value, result) { var addRecordResult = function(key, value, result) {
cur = result[key]; cur = result[key];
if(cur == null) { if(!cur) {
result[key] = {"_id":{"key":key},"value": {"type": varietyTypeOf(value)}, totalOccurrences:1}; result[key] = {"_id":{"key":key},"value": {"type": varietyTypeOf(value)}, totalOccurrences:1};
} else { } else {
var type = varietyTypeOf(value); var type = varietyTypeOf(value);
if(cur.value.type != type) { if(cur.value.type !== type) {
cur.value.types = [cur.value.type]; cur.value.types = [cur.value.type];
delete cur.value["type"]; delete cur.value.type;
addTypeToArray(cur.value.types, type); addTypeToArray(cur.value.types, type);
} else if(!cur.value.type) { } else if(!cur.value.type) {
addTypeToArray(cur.value.types, type); addTypeToArray(cur.value.types, type);
} }
result[key] = cur; result[key] = cur;
} }
} };
var mapRecursive = function(parentKey, obj, level, result) { var mapRecursive = function(parentKey, obj, level, result) {
for (var key in obj) { for (var key in obj) {
...@@ -154,7 +154,7 @@ var mapRecursive = function(parentKey, obj, level, result) { ...@@ -154,7 +154,7 @@ var mapRecursive = function(parentKey, obj, level, result) {
} }
} }
} }
} };
// store results here (no map reduce limit!) // store results here (no map reduce limit!)
var varietyResults = {}; var varietyResults = {};
...@@ -164,16 +164,16 @@ var addVarietyResults = function(result) { ...@@ -164,16 +164,16 @@ var addVarietyResults = function(result) {
if(result.hasOwnProperty(key)) { if(result.hasOwnProperty(key)) {
cur = varietyResults[key]; cur = varietyResults[key];
var value = result[key]; var value = result[key];
if(cur == null) { if(!cur) {
varietyResults[key] = value; varietyResults[key] = value;
} else { } else {
if(value.type && value.type == cur.value.type) { if(value.type && value.type === cur.value.type) {
} else { } else {
for(type in value.types) { for(var type in value.types) {
if(cur.value.type != type) { if(cur.value.type !== type) {
cur.value.types = [cur.value.type]; cur.value.types = [cur.value.type];
delete cur.value["type"]; delete cur.value.type;
addTypeToArray(cur.value.types, type); addTypeToArray(cur.value.types, type);
} else if(!cur.value.type) { } else if(!cur.value.type) {
addTypeToArray(cur.value.types, type); addTypeToArray(cur.value.types, type);
...@@ -185,7 +185,7 @@ var addVarietyResults = function(result) { ...@@ -185,7 +185,7 @@ var addVarietyResults = function(result) {
} }
} }
} }
} };
// main cursor // main cursor
db[collection].find(query).sort(sort).limit(limit).forEach(function(obj) { db[collection].find(query).sort(sort).limit(limit).forEach(function(obj) {
...@@ -208,7 +208,7 @@ var resultsCollectionName = collection + "Keys"; ...@@ -208,7 +208,7 @@ var resultsCollectionName = collection + "Keys";
// replace results collection // replace results collection
print("creating results collection: "+resultsCollectionName); print("creating results collection: "+resultsCollectionName);
resultsDB[resultsCollectionName].drop(); resultsDB[resultsCollectionName].drop();
for(result in varietyResults) { for(var result in varietyResults) {
resultsDB[resultsCollectionName].insert(varietyResults[result]); resultsDB[resultsCollectionName].insert(varietyResults[result]);
} }
...@@ -216,12 +216,12 @@ var numDocuments = db[collection].count(); ...@@ -216,12 +216,12 @@ var numDocuments = db[collection].count();
print("removing leaf arrays in results collection, and getting percentages"); print("removing leaf arrays in results collection, and getting percentages");
resultsDB[resultsCollectionName].find({}).forEach(function(key) { resultsDB[resultsCollectionName].find({}).forEach(function(key) {
var keyName = key["_id"].key; var keyName = key._id.key;
// 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
if(keyName.match(/\.XX$/)) { if(keyName.match(/\.XX$/)) {
resultsDB[resultsCollectionName].remove({ "_id" : key["_id"]}); resultsDB[resultsCollectionName].remove({ "_id" : key._id});
return; return;
} }
......
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