<commit_before>'use strict';

// version ID for original injection
var pluginSpec = 'mongooseHookPlugin_2015_03';

var pluginSpec_mongoose = pluginSpec + '_mongoose';
var pluginSpec_plugins = pluginSpec + '_plugins';

var hooks = {};

// async hook injector for mongoose database API wrappers
function hookAsync(namespace, methodName, hookFunction, result) {
  var hooked = namespace[methodName];

  // replace original method with a hook
  namespace[methodName] = function() {
    var _this = this;
    var outer = _this;
    var mongoose = outer[pluginSpec_mongoose];
    var hookPlugin = mongoose && mongoose[pluginSpec];
    var outerArgs = arguments;
    var T;

    // conditions to skip all the 'pre' and 'post' hooks
    if (
      !hookPlugin ||
      !(outer.name in hookPlugin.collectionOpts) ||
      typeof outerArgs[0] === 'function'
    ) {
      return hooked.apply(outer, outerArgs);
    }

    // declare database operation object; add request arguments
    T = {
      mongoose: mongoose,
      collection: outer,
      req: outerArgs,
      method: methodName,
      collectionOpts: hookPlugin.collectionOpts[outer.name]
    };

    // add named links to request arguments
    switch (T.method) {
      case 'insert':
        T.insert = T.req[0];
        T.opts = T.req[1];
        break;

      case 'find':
      case 'findOne':
      case 'remove':
        T.query = T.req[0];
        T.opts = T.req[1];
        break;

      case 'aggregate':
        T.pipe = T.req[0];
        T.opts = T.req[1];
        var match = T.pipe && T.pipe[0] && T.pipe[0].match;
        if (match) T.query = match;
        break;

      case 'mapReduce':
        T.opts = T.req[2];
        T.query = T.opts.query;
        T.map = T.opts.map;
        T.reduce = T.opts.reduce;
        break;

      case 'update':
        T.query = T.req[0];
        T.update = T.req[1];
        T.opts = T.req[2];
        break;

      case 'findAndModify':
        T.query = T.req[0];
        T.update = T.req[2];
        T.opts = T.req[3];
        break;
    }

    // async call pre hook
    hookFunction.call(T, 'pre', function(after) {
      if (after) {
        var upstream = outerArgs[outerArgs.length - 1];

        // prepare 'post' hook
        if (typeof after === 'function') {
          outerArgs[outerArgs.length - 1] = function() {
            var
              _this = this,
              inner = _this,
              innerArgs = arguments;

            // add response arguments
            T.resScope = inner;
            T.res = innerArgs;

            // add named links to response arguments
            T.error = innerArgs[0];
            T.data = innerArgs[1];<commit_msg>upsert and setOnInsert sets p.insert<commit_after>'use strict';

// version ID for original injection
var pluginSpec = 'mongooseHookPlugin_2015_03_20';

var pluginSpec_mongoose = pluginSpec + '_mongoose';
var pluginSpec_plugins = pluginSpec + '_plugins';

var hooks = {};

// async hook injector for mongoose database API wrappers
function hookAsync(namespace, methodName, hookFunction, result) {
  var hooked = namespace[methodName];

  // replace original method with a hook
  namespace[methodName] = function() {
    var _this = this;
    var outer = _this;
    var mongoose = outer[pluginSpec_mongoose];
    var hookPlugin = mongoose && mongoose[pluginSpec];
    var outerArgs = arguments;
    var T;

    // conditions to skip all the 'pre' and 'post' hooks
    if (
      !hookPlugin ||
      !(outer.name in hookPlugin.collectionOpts) ||
      typeof outerArgs[0] === 'function'
    ) {
      return hooked.apply(outer, outerArgs);
    }

    // declare database operation object; add request arguments
    T = {
      mongoose: mongoose,
      collection: outer,
      req: outerArgs,
      method: methodName,
      collectionOpts: hookPlugin.collectionOpts[outer.name]
    };

    // add named links to request arguments
    switch (T.method) {
      case 'insert':
        T.insert = T.req[0];
        T.opts = T.req[1];
        break;

      case 'find':
      case 'findOne':
      case 'remove':
        T.query = T.req[0];
        T.opts = T.req[1];
        break;

      case 'aggregate':
        T.pipe = T.req[0];
        T.opts = T.req[1];
        var match = T.pipe && T.pipe[0] && T.pipe[0].match;
        if (match) T.query = match;
        break;

      case 'mapReduce':
        T.opts = T.req[2];
        T.query = T.opts.query;
        T.map = T.opts.map;
        T.reduce = T.opts.reduce;
        break;

      case 'update':
        T.query = T.req[0];
        T.update = T.req[1];
        T.opts = T.req[2];
        if (T.opts && T.opts.upsert && T.update) T.insert = T.update.$setOnInsert;
        break;

      case 'findAndModify':
        T.query = T.req[0];
        T.update = T.req[2];
        T.opts = T.req[3];
        if (T.opts && T.opts.upsert && T.update) T.insert = T.update.$setOnInsert;
        break;
    }

    // async call pre hook
    hookFunction.call(T, 'pre', function(after) {
      if (after) {
        var upstream = outerArgs[outerArgs.length - 1];

        // prepare 'post' hook
        if (typeof after === 'function') {
          outerArgs[outerArgs.length - 1] = function() {
            var
              _this = this,
              inner = _this,
              innerArgs = arguments;

            // add response arguments
            T.resScope = inner;
            T.res = innerArgs;

            // add named links to response arguments
            T.error = innerArgs[0];
            T.data = innerArgs[1];
{'fraction_non_alphanumeric': 0.12477493698235506, 'fraction_numerical': 0.009722722362261434, 'mean_word_length': 1.9267650158061116, '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': '3707439', 'n_tokens_mistral': 1737, 'n_tokens_neox': 1715, 'n_words': 451}