Skip to content

Commit

Permalink
only print status updates every 10 seconds. adding indexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
skratchdot committed Oct 21, 2012
1 parent ca19883 commit 4688c41
Showing 1 changed file with 20 additions and 11 deletions.
31 changes: 20 additions & 11 deletions flatten.js
Expand Up @@ -3,8 +3,8 @@
/**
* MongoDB - flatten.js
*
* Version: 1.1
* Date: October 20, 2012
* Version: 1.2
* Date: October 21, 2012
* Project: http://skratchdot.com/projects/mongodb-flatten/
* Source Code: https://github.com/skratchdot/mongodb-flatten/
* Issues: https://github.com/skratchdot/mongodb-flatten/issues/
Expand All @@ -19,6 +19,7 @@
'use strict';

var currentDb, emitKeys, insertKey, flatten,
statusDelayInMs = 10000,
currentId = null, currentDate = null, currentTick = null, previousTick = null;

// Assumes that currentTick is a Date, and currentId is a number
Expand Down Expand Up @@ -62,44 +63,52 @@
* @param {string} collectionName - the name of the collection in which the results will be stored
*/
flatten = function (db, arr, collectionName) {
var collection, i;
var collection, i, numDocs = arr.length, currentDoc;

// If an invalid name is passed, create a temporary collection
if (typeof collectionName !== 'string' || collectionName.length === 0) {
collectionName = 'temp.flatten_' + new Date().getTime();
}

// Print some debug info
print('Flattening ' + arr.length + ' document(s) into the "' + collectionName + '" collection.');
print('Flattening ' + numDocs + ' document(s) into the "' + collectionName + '" collection.');

// Get our collection
collection = db.getCollection(collectionName);

// Empty our collection
collection.remove();
collection.drop();

// Index our collection to speed up lookups
collection.ensureIndex({i : 1});
collection.ensureIndex({k : 1});
collection.ensureIndex({v : 1});

// Initialize some global counters/variables
previousTick = new Date().getTime();
currentId = 0;

// Loop through all our objects, inserting records into our collection
for (i = 0; i < arr.length; i++) {
for (i = 0; i < numDocs; i++) {
// The current document we are processing
currentDoc = arr[i];

// Output some debugging info if needed
currentDate = new Date();
currentTick = currentDate.getTime();
if (currentTick - previousTick > 1000) {
print('Flattened ' + i + ' document(s) and ' + currentId + ' key(s) at ' + currentDate);
if (currentTick - previousTick > statusDelayInMs) {
print('Flattened ' + i + ' of ' + numDocs + ' document(s) and ' + currentId + ' key(s) at ' + currentDate);
previousTick = currentTick;
}

// There's a chance documents don't have
// _id values (capped collections, internal collections)
if (!arr[i].hasOwnProperty('_id')) {
arr[i]._id = 'unknown';
if (!currentDoc.hasOwnProperty('_id')) {
currentDoc._id = 'unknown';
}

// Insert key/value pairs into our new collection
emitKeys(collection, arr[i]._id, arr[i], '');
emitKeys(collection, currentDoc._id, currentDoc, '');
}

return collection;
Expand Down

0 comments on commit 4688c41

Please sign in to comment.