Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var async = require("async");
  2.  
  3. var subjectsAgents = require('../modules/subjects');
  4. var values = require('../modules/values');
  5. var mongodb = require('../modules/mongodb');
  6. var config = require('../../api/config.json');
  7.  
  8. //screen -dmSL mfroot node --stack-size=100000 migrations/00001.js
  9.  
  10. mongodb.openDatabase(config.database, config.mongodb.host, config.mongodb.port, config.mongodb.user, config.mongodb.password,  function (err, newdb) {
  11.     if (err) {
  12.         console.log(err);
  13.         return;
  14.     };
  15.     console.log("migrating identities to expected strucutre of {name: <key>, value: <value>}...");
  16.     // Services use Database
  17.     values.useDatabase(newdb);
  18.  
  19.     var repository = mongodb.createRepository(newdb, 'values');
  20.  
  21.     var startTime = new Date().getTime();
  22.  
  23.     var query = {
  24.             "identity.value":""
  25.         },
  26.         options = {};
  27.  
  28.     repository.findGetCursor(query, options, function(err, cursor) {
  29.  
  30.         function processItem(index, cb) {
  31.             cursor.nextObject(function(err, item) {
  32.                 console.log("item", item._id.toHexString());
  33.                 if(item){
  34.                     //TODOhere perform the actual change in structure
  35.                     //console.log("PROCESSING " + index + " BEFORE: " + JSON.stringify(item.identity));
  36.                     //item.identity = normalizeIdentity(item.identity);
  37.                     //item.identity[0].name = "username";
  38.                     //console.log("PROCESSING " + index + " AFTER : " + JSON.stringify(item.identity));
  39.                     repository.update(item._id.toHexString(),
  40.                         {
  41.                             $set: {
  42.                                 "identity.value": item.provider_context["X-Mofiler-InstallID"]
  43.                             }
  44.                         }
  45.                         , function(err){
  46.                             if(index % 1000 == 0){
  47.                                 var tmpend = new Date().getTime();
  48.                                 var timeelapsed = tmpend - startTime;
  49.                                 console.log("Processed " + index + " items - elapsed time: " + timeelapsed);
  50.                             }
  51.                             if (cb)
  52.                                 cb();
  53.                     });
  54.  
  55.                 }
  56.  
  57.             });
  58.  
  59.         }
  60.  
  61.         var queue = async.queue(processItem, 1000); // Run ten simultaneous process
  62.  
  63.         queue.drain = function() {
  64.             console.log("Finished!");
  65.             var end = new Date().getTime();
  66.             var time = end - startTime;
  67.             console.log("FINISHEDbb in xxx seconds: " + time);
  68.         };
  69.  
  70.         cursor.count(function(err, count) {
  71.             console.log(count);
  72.             if(err) {
  73.                 console.log(err);
  74.                 return;
  75.             }
  76.             for (var i = 0; i < count; i++) {
  77.                 queue.push(i, function(err) {});
  78.             };
  79.         });
  80.  
  81.  
  82.     });
  83. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement