<commit_before>// Manage a connection to the Rackspace cloud. Retain and export handles to created resources.

var
  async = require('async'),
  pkgcloud = require('pkgcloud'),
  mongo = require('mongodb'),
  config = require('./config'),
  logger = require('./logging').getLogger();

/**
 * @description Create a function that asynchronously creates a Rackspace container if it
 *   doesn't already exist.
 */
function makeContainerCreator(client, containerName, logicalName, cdn) {
  return function (callback) {
    var reportBack = function (err, container) {
      if (err) return callback(err);

      exports[logicalName] = container;

      logger.debug("Container [" + container.name + "] now exists.");

      callback(null, container);
    };

    var cdnEnable = function (err, container) {
      if (err) return callback(err);

      logger.debug("Enabling CDN on container [" + container.name + "].");

      client.setCdnEnabled(container, { ttl: 31536000, enabled: true }, reportBack);
    };

    var handleCreation = cdn ? cdnEnable : reportBack;

    logger.info("Ensuring that container [" + containerName + "] exists.");

    // Instead of checking if the container exists first, we try to create it, and
    // if it already exists, we get a no-op (202) and move on.
    client.createContainer({ name: containerName }, handleCreation);
  };
}

/**
 * @description Utility function to ensure that the exported Container model includes CDN URIs when
 *   it's supposed to.
 */
function refresh(client, containerName, logicalName, callback) {
  client.getContainer(containerName, function (err, container) {
    if (err) {
      callback(err);
      return;
    }

    exports[logicalName] = container;

    callback(null);
  });
}

/**
 * @description Authenticate to MongoDB, export the active MongoDB connection as "db", and
 *   perform any necessary one-time initialization.
 */
function mongoInit(callback) {
  mongo.MongoClient.connect(config.mongodbURL(), function (err, db) {
    if (err) return callback(err);

    logger.debug("Connected to MongoDB database at [" + config.mongodbURL() + "].");

    exports.db = db;

    var envelopes = db.collection("envelopes");

    // Create indices on collections as necessary.
    async.parallel([
      function (callback) { envelopes.createIndex("tags", { sparse: true }, callback); },
      function (callback) { envelopes.createIndex("categories", { sparse: true }, callback); },
      function (callback) { envelopes.createIndex("contentID", { unique: true }, callback); }
    ], function (err, db) {
      if (err) return callback(err);

      logger.debug("All indices created.");

      callback(null);
    });
  });
}

exports.setup = function (callback) {
  var client = pkgcloud.providers.rackspace.storage.createClient({
    username: config.rackspaceUsername(),
    apiKey: config.rackspaceAPIKey(),
    region: config.rackspaceRegion(),
    useInternal: config.rackspaceServiceNet()
  });

  client.on('log::*', function (message, object) {
    if (object) {
      logger.log(this.event.split('::')[1], message, object);
    } else {
      logger.log(this.event.split('::')[1], message);
    }
  });

  client.auth(function (err) {
    if (err) throw err;

    exports.client = client;

    async.parallel([
      makeContainerCreator(client, config.contentContainer(), "contentContainer", false),
      makeContainerCreator(client, config.assetContainer(), "assetContainer", true),
      mongoInit
    ], callback);
  });
};
<commit_msg>Fix requires in connection.js.<commit_after>// Manage a connection to the Rackspace cloud. Retain and export handles to created resources.

var async = require('async');
var pkgcloud = require('pkgcloud');
var mongo = require('mongodb');
var config = require('../config');
var logger = require('../logging').getLogger();

/**
 * @description Create a function that asynchronously creates a Rackspace container if it
 *   doesn't already exist.
 */
function makeContainerCreator(client, containerName, logicalName, cdn) {
  return function(callback) {
    var reportBack = function(err, container) {
      if (err) return callback(err);

      exports[logicalName] = container;

      logger.debug("Container [" + container.name + "] now exists.");

      callback(null, container);
    };

    var cdnEnable = function(err, container) {
      if (err) return callback(err);

      logger.debug("Enabling CDN on container [" + container.name + "].");

      client.setCdnEnabled(container, {
        ttl: 31536000,
        enabled: true
      }, reportBack);
    };

    var handleCreation = cdn ? cdnEnable : reportBack;

    logger.info("Ensuring that container [" + containerName + "] exists.");

    // Instead of checking if the container exists first, we try to create it, and
    // if it already exists, we get a no-op (202) and move on.
    client.createContainer({
      name: containerName
    }, handleCreation);
  };
}

/**
 * @description Utility function to ensure that the exported Container model includes CDN URIs when
 *   it's supposed to.
 */
function refresh(client, containerName, logicalName, callback) {
  client.getContainer(containerName, function(err, container) {
    if (err) {
      callback(err);
      return;
    }

    exports[logicalName] = container;

    callback(null);
  });
}

/**
 * @description Authenticate to MongoDB, export the active MongoDB connection as "db", and
 *   perform any necessary one-time initialization.
 */
function mongoInit(callback) {
  mongo.MongoClient.connect(config.mongodbURL(), function(err, db) {
    if (err) return callback(err);

    logger.debug("Connected to MongoDB database at [" + config.mongodbURL() + "].");

    exports.db = db;

    var envelopes = db.collection("envelopes");

    // Create indices on collections as necessary.
    async.parallel([
      function(callback) {
        envelopes.createIndex("tags", {
          sparse: true
        }, callback);
      },
      function(callback) {
        envelopes.createIndex("categories", {
          sparse: true
        }, callback);
      },
      function(callback) {
        envelopes.createIndex("contentID", {
          unique: true
        }, callback);
      }
    ], function(err, db) {
      if (err) return callback(err);

      logger.debug("All indices created.");

      callback(null);
    });
  });
}

exports.setup = function(callback) {
  var client = pkgcloud.providers.rackspace.storage.createClient({
    username: config.rackspaceUsername(),
    apiKey: config.rackspaceAPIKey(),
    region: config.rackspaceRegion(),
    useInternal: config.rackspaceServiceNet()
  });

  client.on('log::*', function(message, object) {
    if (object) {
      logger.log(this.event.split('::')[1], message, object);
    } else {
      logger.log(this.event.split('::')[1], message);
    }
  });

  client.auth(function(err) {
    if (err) throw err;

    exports.client = client;

    async.parallel([
      makeContainerCreator(client, config.contentContainer(), "contentContainer", false),
      makeContainerCreator(client, config.assetContainer(), "assetContainer", true),
      mongoInit
    ], callback);
  });
};

{'fraction_non_alphanumeric': 0.13580075135661612, 'fraction_numerical': 0.0036176429664672325, 'mean_word_length': 3.330120481927711, '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': '208871', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1999, 'n_words': 551}