Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createProfileHandler3(conf) {
  2.   return createCollectionHandler(conf, function(collectionCallbackConf){
  3.     var db = collectionCallbackConf.db;
  4.     var callback = collectionCallbackConf.callback;
  5.     var collection = collectionCallbackConf.collection;
  6.     try {
  7.       var m, r, o, s;
  8.       s = {
  9.         getProfilePropertyDescriptor: function (profile, name){
  10.           var property;
  11.           //get the property descriptor
  12.           property = profile.properties[name];
  13.           if (!property) {
  14.             property = {
  15.               count: 0,
  16.               typeCount: 0,
  17.               types: {}
  18.             };
  19.             profile.properties[name] = property;
  20.           }
  21.           //mark an occurrence of this property
  22.           property.count++;
  23.           return property;
  24.         },
  25.         getTypeName: function (value){
  26.           var typeName;
  27.           if (value === null) {
  28.             typeName = "null";
  29.           }
  30.           else {
  31.             var cons = String(value.constructor);
  32.             typeName = cons.substring("function ".length, cons.indexOf("("));
  33.           }
  34.           return typeName;
  35.         },
  36.         getProfilePropertyTypeDescriptor: function (property, typeName, value){
  37.           var propertyType = property.types[typeName];
  38.           if (!propertyType) {
  39.             property.typeCount++;
  40.             propertyType = {
  41.               count: 0
  42.             };
  43.             property.types[typeName] = propertyType;
  44.  
  45.             switch (typeName) {
  46.               case "Object":
  47.                 propertyType.hasMinMax = false;
  48.                 propertyType.profile = createProfile();
  49.                 break;
  50.               case "Array":
  51.                 propertyType.hasMinMax = false;
  52.                 propertyType.types = {};
  53.                 propertyType.typeCount = 0;
  54.                 break;
  55.               case "Number":
  56.                 propertyType.isInt = true;
  57.               default:
  58.                 propertyType.hasMinMax = true;
  59.                 propertyType.minValue = value;
  60.                 propertyType.maxValue = value;
  61.             }
  62.  
  63.             switch (typeName){
  64.               case "Array":
  65.               case "String":
  66.                 propertyType.hasValueLength = true;
  67.                 break;
  68.               default:
  69.                 propertyType.hasValueLength = false;
  70.             }
  71.  
  72.             if (propertyType.hasValueLength) {
  73.               var valueLength = value.length;
  74.               propertyType.minLength = valueLength;
  75.               propertyType.maxLength = valueLength;
  76.             }
  77.           }
  78.           propertyType.count++;
  79.           return propertyType;
  80.         },
  81.         updatePropertyTypeMinMax: function (propertyType, value) {
  82.           if (propertyType.hasMinMax) {
  83.             if (propertyType.minValue > value) {
  84.               propertyType.minValue = value;
  85.             }
  86.             if (propertyType.maxValue < value) {
  87.               propertyType.maxValue = value;
  88.             }
  89.           }
  90.         },
  91.         updatePropertyTypeValueLength: function (propertyType, value) {
  92.           if (propertyType.hasValueLength) {
  93.             valueLength = value.length;
  94.             if (propertyType.minLength > valueLength) {
  95.               propertyType.minLength = valueLength;
  96.             }
  97.             if (propertyType.maxLength < valueLength) {
  98.               propertyType.maxLength = valueLength;
  99.             }
  100.           }
  101.         },
  102.         updateArrayElementProfile: function (propertyType, value){
  103.           var i, n = value.length, elementValue, typeName, typeDescriptor;
  104.           for (i = 0; i < n; i++) {
  105.             elementValue = value[i];
  106.             typeName = getTypeName(elementValue);
  107.             typeDescriptor = getProfilePropertyTypeDescriptor(propertyType, typeName, value);
  108.             updatePropertyType(typeDescriptor, typeName, elementValue);
  109.           }
  110.         },
  111.         updateSubDocumentProfile: function (propertyType, value){
  112.           updateProfile(propertyType.profile, value);
  113.         },
  114.         updateNumberSubtype: function (propertyType, value){
  115.           if (propertyType.isInt === true) {
  116.             propertyType.isInt = parseInt(value, 10) === parseFloat(value);
  117.           }
  118.         },
  119.         updatePropertyType: function (propertyType, typeName, value){
  120.           updatePropertyTypeMinMax(propertyType, value);
  121.           updatePropertyTypeValueLength(propertyType, value);
  122.           switch (typeName) {
  123.             case "Number":
  124.               updateNumberSubtype(propertyType, value);
  125.               break
  126.             case "Array":
  127.               updateArrayElementProfile(propertyType, value);
  128.               break;
  129.             case "Object":
  130.               updateSubDocumentProfile(propertyType, value);
  131.               break;
  132.           }
  133.         },
  134.         updateProfileProperty: function (profile, document, name){
  135.           var property, value, typeName, propertyType;
  136.  
  137.           property = getProfilePropertyDescriptor(profile, name);
  138.           value = document[name];
  139.           typeName = getTypeName(value);
  140.           propertyType = getProfilePropertyTypeDescriptor(property, typeName, value);
  141.           updatePropertyType(propertyType, typeName, value);
  142.         },
  143.         updateProfileProperties: function (profile, document) {
  144.           var name;
  145.           for (name in document) {
  146.             updateProfileProperty(profile, document, name);
  147.           }
  148.         },
  149.         createProfile: function (){
  150.           return {
  151.             count: 0,
  152.             properties: {}
  153.           };
  154.         },
  155.         updateProfile: function (profile, document) {
  156.           profile.count++;
  157.           updateProfileProperties(profile, document);
  158.         },
  159.         profile: {
  160.           count: 0,
  161.           properties: {}
  162.         }
  163.       };
  164.  
  165.       m = function(){
  166.         updateProfile(profile, this);
  167.         if (profile.count === 1){
  168.           emit(0, profile);
  169.         }
  170.       };
  171.       r = function(key, emits){
  172.         return emits[0];
  173.       };
  174.       o = {out : {inline: 1}, scope: s, jsMode: true};
  175.       collection.mapReduce(m, r, o, function(err, result, stats){
  176.       console.log(err);
  177.       console.log(result);
  178.       console.log(stats);
  179.         if (err){
  180.           callback(err, 500, null);
  181.           return;
  182.         }
  183.         callback(0, 200, result);
  184.       });
  185.     }
  186.     catch (err){
  187.       callback(err, 500, null);
  188.       db.close();
  189.     }
  190.   });
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement