Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function createProfileHandler3(conf) {
- return createCollectionHandler(conf, function(collectionCallbackConf){
- var db = collectionCallbackConf.db;
- var callback = collectionCallbackConf.callback;
- var collection = collectionCallbackConf.collection;
- try {
- var m, r, o, s;
- s = {
- getProfilePropertyDescriptor: function (profile, name){
- var property;
- //get the property descriptor
- property = profile.properties[name];
- if (!property) {
- property = {
- count: 0,
- typeCount: 0,
- types: {}
- };
- profile.properties[name] = property;
- }
- //mark an occurrence of this property
- property.count++;
- return property;
- },
- getTypeName: function (value){
- var typeName;
- if (value === null) {
- typeName = "null";
- }
- else {
- var cons = String(value.constructor);
- typeName = cons.substring("function ".length, cons.indexOf("("));
- }
- return typeName;
- },
- getProfilePropertyTypeDescriptor: function (property, typeName, value){
- var propertyType = property.types[typeName];
- if (!propertyType) {
- property.typeCount++;
- propertyType = {
- count: 0
- };
- property.types[typeName] = propertyType;
- switch (typeName) {
- case "Object":
- propertyType.hasMinMax = false;
- propertyType.profile = createProfile();
- break;
- case "Array":
- propertyType.hasMinMax = false;
- propertyType.types = {};
- propertyType.typeCount = 0;
- break;
- case "Number":
- propertyType.isInt = true;
- default:
- propertyType.hasMinMax = true;
- propertyType.minValue = value;
- propertyType.maxValue = value;
- }
- switch (typeName){
- case "Array":
- case "String":
- propertyType.hasValueLength = true;
- break;
- default:
- propertyType.hasValueLength = false;
- }
- if (propertyType.hasValueLength) {
- var valueLength = value.length;
- propertyType.minLength = valueLength;
- propertyType.maxLength = valueLength;
- }
- }
- propertyType.count++;
- return propertyType;
- },
- updatePropertyTypeMinMax: function (propertyType, value) {
- if (propertyType.hasMinMax) {
- if (propertyType.minValue > value) {
- propertyType.minValue = value;
- }
- if (propertyType.maxValue < value) {
- propertyType.maxValue = value;
- }
- }
- },
- updatePropertyTypeValueLength: function (propertyType, value) {
- if (propertyType.hasValueLength) {
- valueLength = value.length;
- if (propertyType.minLength > valueLength) {
- propertyType.minLength = valueLength;
- }
- if (propertyType.maxLength < valueLength) {
- propertyType.maxLength = valueLength;
- }
- }
- },
- updateArrayElementProfile: function (propertyType, value){
- var i, n = value.length, elementValue, typeName, typeDescriptor;
- for (i = 0; i < n; i++) {
- elementValue = value[i];
- typeName = getTypeName(elementValue);
- typeDescriptor = getProfilePropertyTypeDescriptor(propertyType, typeName, value);
- updatePropertyType(typeDescriptor, typeName, elementValue);
- }
- },
- updateSubDocumentProfile: function (propertyType, value){
- updateProfile(propertyType.profile, value);
- },
- updateNumberSubtype: function (propertyType, value){
- if (propertyType.isInt === true) {
- propertyType.isInt = parseInt(value, 10) === parseFloat(value);
- }
- },
- updatePropertyType: function (propertyType, typeName, value){
- updatePropertyTypeMinMax(propertyType, value);
- updatePropertyTypeValueLength(propertyType, value);
- switch (typeName) {
- case "Number":
- updateNumberSubtype(propertyType, value);
- break
- case "Array":
- updateArrayElementProfile(propertyType, value);
- break;
- case "Object":
- updateSubDocumentProfile(propertyType, value);
- break;
- }
- },
- updateProfileProperty: function (profile, document, name){
- var property, value, typeName, propertyType;
- property = getProfilePropertyDescriptor(profile, name);
- value = document[name];
- typeName = getTypeName(value);
- propertyType = getProfilePropertyTypeDescriptor(property, typeName, value);
- updatePropertyType(propertyType, typeName, value);
- },
- updateProfileProperties: function (profile, document) {
- var name;
- for (name in document) {
- updateProfileProperty(profile, document, name);
- }
- },
- createProfile: function (){
- return {
- count: 0,
- properties: {}
- };
- },
- updateProfile: function (profile, document) {
- profile.count++;
- updateProfileProperties(profile, document);
- },
- profile: {
- count: 0,
- properties: {}
- }
- };
- m = function(){
- updateProfile(profile, this);
- if (profile.count === 1){
- emit(0, profile);
- }
- };
- r = function(key, emits){
- return emits[0];
- };
- o = {out : {inline: 1}, scope: s, jsMode: true};
- collection.mapReduce(m, r, o, function(err, result, stats){
- console.log(err);
- console.log(result);
- console.log(stats);
- if (err){
- callback(err, 500, null);
- return;
- }
- callback(0, 200, result);
- });
- }
- catch (err){
- callback(err, 500, null);
- db.close();
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement