<commit_before>/**
 * Used by the db-interface to actually communicate with CouchDB.
 */
var sys = require('sys'),
	connect = require('connect'),
	couchdb = require('./dep/couchdb'),
	simpleInheritance = require('./dep/simple-inheritance'),
	arrayHelpers = require('./array-helpers'),
	synchronousProcessor = new (require('./synchronous-processor').SynchronousProcessor)(),
	ErrorHandler = require('./error-handler').ErrorHandler;

exports.connector = Class.extend({
	
	defaults: {
		legacy: false,
		errorHandler: false
	},

	init: function(params) {
		arrayHelpers.extend(this, this.defaults, params);
		this.synchronousProcessor = synchronousProcessor;
		this.errorHandler = this.errorHandler || new ErrorHandler();
	},
	
	connect: function() {
		this.connection = couchdb
		    .createClient(this.port, this.host, this.user, this.password, this.errorHandler)
		    .db(this.database);
	},
	
	deleteDocument: function(params, callback) {
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'deleteDocument',
			params
		);
		
		this.connection.removeDoc(params._id, params._rev, callback);
	},
	
	saveDocument: function(params, callback) {
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'saveDocument',
			params
		);
		
		var defaults = {
			type: '',
			fields: {}
		};
		
		params = arrayHelpers.extend(defaults, {}, params);
		
		params.fields.type = params.type;
		
		if (params.fields && params.fields._id) {
			this.connection.saveDoc(params.fields._id, params.fields, callback);
		} else {
			this.connection.saveDoc(params.fields, callback);
		}
	},
	
	loadDocument: function(params, callback) {
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'loadDocument',
			params,
			'warn'
		);
		
		if (typeof params == 'object') {
			this.loadDocumentByParams(params, callback);
		} else {
			this.connection.request('/' + params, callback);
		}
	},
	
	loadDocumentByParams: function(params, callback) {
		
		this.fixCommonTypos(params);
		
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'loadDocumentByParams',
			params,
			'warn'
		);
		
		var defaults = {
			key: false,
			startkey: false,
			startkey_docid: false,
			endkey: false,
			endkey_docid: false,
			descending: false,
			limit: false,
			count: false,
			skip: 0,
			type: '',
			keys: []
		}
		params = arrayHelpers.extend(defaults, {}, params);

    // if there's a custom view specified, load by that instead
    var viewURL;
    if( params.custom_view ) {
      viewURL = this.buildCustomViewURL(params.custom_view_name,params.type);
    }
		else {		
      viewURL = this.buildViewURL(params.keys, params.type);
		}
		var viewParams = this.buildViewParams({
			key: params.key,
			startkey: params.startkey,
			startkey_docid: params.startkey_docid,
			endkey: params.endkey,
			endkey_docid: params.endkey_docid,
			descending: params.descending,
			limit: params.limit,
			count: params.count,
			skip: params.skip
		});
		this.connection.request(viewURL + viewParams, callback);
	},
	<commit_msg>view url generation should depend on the existence of the custom_view_name parameter<commit_after>/**
 * Used by the db-interface to actually communicate with CouchDB.
 */
var sys = require('sys'),
	connect = require('connect'),
	couchdb = require('./dep/couchdb'),
	simpleInheritance = require('./dep/simple-inheritance'),
	arrayHelpers = require('./array-helpers'),
	synchronousProcessor = new (require('./synchronous-processor').SynchronousProcessor)(),
	ErrorHandler = require('./error-handler').ErrorHandler;

exports.connector = Class.extend({
	
	defaults: {
		legacy: false,
		errorHandler: false
	},

	init: function(params) {
		arrayHelpers.extend(this, this.defaults, params);
		this.synchronousProcessor = synchronousProcessor;
		this.errorHandler = this.errorHandler || new ErrorHandler();
	},
	
	connect: function() {
		this.connection = couchdb
		    .createClient(this.port, this.host, this.user, this.password, this.errorHandler)
		    .db(this.database);
	},
	
	deleteDocument: function(params, callback) {
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'deleteDocument',
			params
		);
		
		this.connection.removeDoc(params._id, params._rev, callback);
	},
	
	saveDocument: function(params, callback) {
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'saveDocument',
			params
		);
		
		var defaults = {
			type: '',
			fields: {}
		};
		
		params = arrayHelpers.extend(defaults, {}, params);
		
		params.fields.type = params.type;
		
		if (params.fields && params.fields._id) {
			this.connection.saveDoc(params.fields._id, params.fields, callback);
		} else {
			this.connection.saveDoc(params.fields, callback);
		}
	},
	
	loadDocument: function(params, callback) {
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'loadDocument',
			params,
			'warn'
		);
		
		if (typeof params == 'object') {
			this.loadDocumentByParams(params, callback);
		} else {
			this.connection.request('/' + params, callback);
		}
	},
	
	loadDocumentByParams: function(params, callback) {
		
		this.fixCommonTypos(params);
		
		callback = this.errorHandler.wrapCallback(
			callback,
			'CouchConnector',
			'loadDocumentByParams',
			params,
			'warn'
		);
		
		var defaults = {
			key: false,
			startkey: false,
			startkey_docid: false,
			endkey: false,
			endkey_docid: false,
			descending: false,
			limit: false,
			count: false,
			skip: 0,
			type: '',
			keys: []
		}
		params = arrayHelpers.extend(defaults, {}, params);

    // if there's a custom view specified, load by that instead
    var viewURL;
    if( params.custom_view_name ) {
      viewURL = this.buildCustomViewURL(params.custom_view_name,params.type);
    }
		else {		
      viewURL = this.buildViewURL(params.keys, params.type);
		}
		var viewParams = this.buildViewParams({
			key: params.key,
			startkey: params.startkey,
			startkey_docid: params.startkey_docid,
			endkey: params.endkey,
			endkey_docid: params.endkey_docid,
			descending: params.descending,
			limit: params.limit,
			count: params.count,
			skip: params.skip
		});
		this.connection.request(viewURL + viewParams, callback);
	},
	
{'fraction_non_alphanumeric': 0.13282491261518906, 'fraction_numerical': 0.00031776294884016526, 'mean_word_length': 3.6907600596125185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6163472', 'n_tokens_mistral': 2296, 'n_tokens_neox': 1970, 'n_words': 397}