<commit_before>var mongodb = require('mongodb-core');
var each = require('each-series');

var oid = mongodb.BSON.ObjectID.createPk;

var Bulk = function(colName, ordered, onserver, dbname) {
  this._colname = colName;
  this._cmds = [];
  this._currCmd = null;
  this._ordered = ordered;
  this._onserver = onserver;
  this._dbname = dbname;

  var self = this;
  this.find = function(query) {
    var upsert = false;
    var findobj = {};
    var remove = function(lim) {
      if (!self._currCmd) {
        self._currCmd = {
          delete: self._colname,
          deletes: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      if (!self._currCmd.delete) {
        self._cmds.push(self._currCmd);
        self._currCmd = {
          delete: self._colname,
          deletes: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      self._currCmd.deletes.push({q: query, limit: lim});
    };

    var update = function(updObj, multi) {
      if (!self._currCmd) {
        self._currCmd = {
          update: self._colname,
          updates: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      if (!self._currCmd.update) {
        self._cmds.push(self._currCmd);
        self._currCmd = {
          update: self._colname,
          updates: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      self._currCmd.updates.push({q: query, u: updObj, multi: multi, upsert: upsert});
    };

    findobj.upsert = function(){
      return upsert = true, findobj;
    };

    findobj.remove = function() {
      remove(0);
    };

    findobj.removeOne = function() {
      remove(1);
    };

    findobj.update = function(updObj) {
      update(updObj, true);
    };

    findobj.updateOne = function(updObj) {
      update(updObj, false);
    };

    return findobj;
  };
};

Bulk.prototype.insert = function(doc) {
  if (!this._currCmd) {
    this._currCmd = {
      insert: this._colname,
      documents: [],
      ordered: this._ordered,
      writeConcern: {w: 1}
    };
  }
  if (!this._currCmd.insert) {
    this._cmds.push(this._currCmd);
    this._currCmd = {
      insert: this._colname,
      documents: [],
      ordered: this._ordered,
      writeConcern: {w: 1}
    };
  }
  if (!doc._id) doc._id = oid();
  this._currCmd.documents.push(doc);
};

var cmdkeys = {
  insert: 'nInserted',
  delete: 'nRemoved',
  update: 'nUpserted'
};

Bulk.prototype.tojson = function() {
  var obj = { 
    nInsertOps: 0,
    nUpdateOps: 0,
    nRemoveOps: 0,
    nBatches: this._cmds.length
  };

  this._cmds.forEach(function(cmd) {
    if (cmd.update) {
      obj.nUpdateOps += cmd.updates.length;
    } else if (cmd.insert) {
      obj.nInsertOps += cmd.documents.length;
    } else if (cmd.delete) {
      obj.nRemoveOps += cmd.deletes.length;
    }
  });

  return obj;
};

Bulk.prototype.execute = function(cb) {
  var self = this;
  var result = {
    writeErrors : [ ],
    writeConcernErrors : [ ],
    nInserted : 0,
    nUpserted : 0,
    nMatched : 0,
    nModified : 0,
    nRemoved : 0,
    upserted : [ ]
  };

  this._cmds.push(this._currCmd);
  this._onserver(function(err, server) {
    if (err) return cb(err);
    each(self._cmds, function(cmd, i, done) {
      server.command(self._dbname + '.$cmd', cmd, function(err, res) {
        if (err) return done(err);
        result[cmdkeys[Object.keys(cmd)[0]]] += res.result.n;
        done();
      });
    }, function(err) {
      if (err) return cb(err);
      result.ok = 1;
      cb(null, result);
    });
  });
};

module.exports = Bulk;
<commit_msg>Split set upsert to true and return this for readability<commit_after>var mongodb = require('mongodb-core');
var each = require('each-series');

var oid = mongodb.BSON.ObjectID.createPk;

var Bulk = function(colName, ordered, onserver, dbname) {
  this._colname = colName;
  this._cmds = [];
  this._currCmd = null;
  this._ordered = ordered;
  this._onserver = onserver;
  this._dbname = dbname;

  var self = this;
  this.find = function(query) {
    var upsert = false;
    var findobj = {};
    var remove = function(lim) {
      if (!self._currCmd) {
        self._currCmd = {
          delete: self._colname,
          deletes: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      if (!self._currCmd.delete) {
        self._cmds.push(self._currCmd);
        self._currCmd = {
          delete: self._colname,
          deletes: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      self._currCmd.deletes.push({q: query, limit: lim});
    };

    var update = function(updObj, multi) {
      if (!self._currCmd) {
        self._currCmd = {
          update: self._colname,
          updates: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      if (!self._currCmd.update) {
        self._cmds.push(self._currCmd);
        self._currCmd = {
          update: self._colname,
          updates: [],
          ordered: self._ordered,
          writeConcern: {w: 1}
        };
      }
      self._currCmd.updates.push({q: query, u: updObj, multi: multi, upsert: upsert});
    };

    findobj.upsert = function(){
      upsert = true;
      return findobj;
    };

    findobj.remove = function() {
      remove(0);
    };

    findobj.removeOne = function() {
      remove(1);
    };

    findobj.update = function(updObj) {
      update(updObj, true);
    };

    findobj.updateOne = function(updObj) {
      update(updObj, false);
    };

    return findobj;
  };
};

Bulk.prototype.insert = function(doc) {
  if (!this._currCmd) {
    this._currCmd = {
      insert: this._colname,
      documents: [],
      ordered: this._ordered,
      writeConcern: {w: 1}
    };
  }
  if (!this._currCmd.insert) {
    this._cmds.push(this._currCmd);
    this._currCmd = {
      insert: this._colname,
      documents: [],
      ordered: this._ordered,
      writeConcern: {w: 1}
    };
  }
  if (!doc._id) doc._id = oid();
  this._currCmd.documents.push(doc);
};

var cmdkeys = {
  insert: 'nInserted',
  delete: 'nRemoved',
  update: 'nUpserted'
};

Bulk.prototype.tojson = function() {
  var obj = { 
    nInsertOps: 0,
    nUpdateOps: 0,
    nRemoveOps: 0,
    nBatches: this._cmds.length
  };

  this._cmds.forEach(function(cmd) {
    if (cmd.update) {
      obj.nUpdateOps += cmd.updates.length;
    } else if (cmd.insert) {
      obj.nInsertOps += cmd.documents.length;
    } else if (cmd.delete) {
      obj.nRemoveOps += cmd.deletes.length;
    }
  });

  return obj;
};

Bulk.prototype.execute = function(cb) {
  var self = this;
  var result = {
    writeErrors : [ ],
    writeConcernErrors : [ ],
    nInserted : 0,
    nUpserted : 0,
    nMatched : 0,
    nModified : 0,
    nRemoved : 0,
    upserted : [ ]
  };

  this._cmds.push(this._currCmd);
  this._onserver(function(err, server) {
    if (err) return cb(err);
    each(self._cmds, function(cmd, i, done) {
      server.command(self._dbname + '.$cmd', cmd, function(err, res) {
        if (err) return done(err);
        result[cmdkeys[Object.keys(cmd)[0]]] += res.result.n;
        done();
      });
    }, function(err) {
      if (err) return cb(err);
      result.ok = 1;
      cb(null, result);
    });
  });
};

module.exports = Bulk;

{'fraction_non_alphanumeric': 0.15029057980808216, 'fraction_numerical': 0.004865522367887553, 'mean_word_length': 2.135593220338983, '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': '6044132', 'n_tokens_mistral': 2636, 'n_tokens_neox': 2557, 'n_words': 497}