<commit_before>var methods = {
  save: function(callback) {
    var doc = this;
    var Class = doc.constructor;

    // Trigger "beforeSave" event handlers on the current and parent classes.
    var event = new Astro.Event('beforeSave');
    event.target = doc;
    Class.emitEvent(event);
    // If user prevented default operation, then we have to stop here.
    if (event.defaultPrevented) {
      return;
    }

    // Set the flag indicating whether we are updating a document.
    var isUpdating = !doc._isNew;

    // Trigger "beforeInsert" or "beforeUpdate" event handlers on the current
    // and parent classes.
    event = new Astro.Event(isUpdating ? 'beforeUpdate' : 'beforeInsert');
    event.target = doc;
    Class.emitEvent(event);
    // If user prevented default operation, then we have to stop here.
    if (event.defaultPrevented) {
      return;
    }

    // Get values to update or insert.
    var values;
    if (doc._isNew) {
      values = Astro.utils.fields.getAllValues(doc, {
        cast: false
      });
    } else {
      values = Astro.utils.fields.getModifiedValues(doc, false, {
        cast: false
      });
    }

    // Get the "_id" field's value.
    var id = Astro.utils.fields.getValue(doc, '_id');
    // Remove the "_id" field, if its value is empty (null) or a document is
    // already stored in the collection (we can't change id).
    if (!values._id || !doc._isNew) {
      values = _.omit(values, '_id');
    }

    // Check if there are any values to update or insert. If there are no modified
    // fields, we shouldn't do anything.
    if (_.size(values) === 0) {
      return;
    }

    // Add callback to arguments list if provided.
    var args = [];
    if (callback) {
      args.push(callback);
    }

    // Get collection for given class or parent class.
    var Collection = Class.getCollection();

    var result;
    if (doc._isNew) {
      // Add values to insert into the list of arguments passed to the "insert"
      // method.
      args.unshift(values);
      // Insert document.
      result = Collection.insert.apply(Collection, args);
      Astro.utils.fields.setValue(doc, '_id', result);
    } else {
      // Add selector and modifier at the beginning of the arguments list. Right
      // now in the array is a callback function (if provided).
      args.unshift({ // Selector.
        _id: id
      }, { // Modifier.
        $set: values
      });
      // Update document.
      result = Collection.update.apply(Collection, args);
    }

    // Change the "_isNew" flag to "false". Now the document is not new, it has
    // just been saved.
    doc._isNew = false;

    // Trigger "afterInsert" or "afterUpdate" event handlers on the current and
    // parent classes.
    var event = new Astro.Event(isUpdating ? 'afterUpdate' : 'afterInsert');
    event.target = doc;
    Class.emitEvent(event);
    // Trigger "afterSave" event handlers on the current and parent classes.
    var event = new Astro.Event('afterSave');
    event.target = doc;
    Class.emitEvent(event);

    // Copy all values to the "_original" property so that we are starting with
    // the clean object without modifications (there is no diff between current
    // values and "_original").
    doc._original = EJSON.clone(Astro.utils.fields.getAllValues(doc));

    // Return result of executing Mongo query.
    return result;
  },

  remove: function(callback) {
    var doc = this;
    var Class = doc.constructor;

    // Remove only when document has the "_id" field (it's persisted).
    var id = Astro.utils.fields.getValue(doc, '_id');
    if (!id) {
      return;
    }

    // Trigger "beforeRemove" event handlers on the current and parent classes.
    var event = new Astro.Event('beforeRemove');
    event.target = doc;
    Class.emitEvent(event);

    // If user prevented default operation, then we have to stop here.
    if (event.defaultPrevented) {
      return;
    }

    // Add selector to arguments list.
    var args = [];
    args.push({
      _id: id
    });
    // Add callback to arguments list if provided.<commit_msg>Possibility to save certain fields of document<commit_after>var methods = {
  save: function(fieldsNames, callback) {
    var doc = this;
    var Class = doc.constructor;

    // Handle arguments' types detection.
    if (arguments.length === 1) {
      if (_.isFunction(fieldsNames)) {
        callback = fieldsNames;
      }
    }
    if (_.isString(fieldsNames)) {
      fieldsNames = [fieldsNames];
    } else if (!_.isArray(fieldsNames)) {
      fieldsNames = Class.getFieldsNames();
    }

    // Trigger "beforeSave" event handlers on the current and parent classes.
    var event = new Astro.Event('beforeSave');
    event.target = doc;
    Class.emitEvent(event);
    // If user prevented default operation, then we have to stop here.
    if (event.defaultPrevented) {
      return;
    }

    // Set the flag indicating whether we are updating a document.
    var isUpdating = !doc._isNew;

    // Trigger "beforeInsert" or "beforeUpdate" event handlers on the current
    // and parent classes.
    event = new Astro.Event(isUpdating ? 'beforeUpdate' : 'beforeInsert');
    event.target = doc;
    Class.emitEvent(event);
    // If user prevented default operation, then we have to stop here.
    if (event.defaultPrevented) {
      return;
    }

    // Get values to update or insert.
    var values;
    if (doc._isNew) {
      values = Astro.utils.fields.getAllValues(doc, {
        cast: false
      });
    } else {
      values = Astro.utils.fields.getModifiedValues(doc, false, {
        cast: false
      });
    }

    // Get a value of the "_id" field.
    var id = Astro.utils.fields.getValue(doc, '_id');

    // Remove the "_id" field from the fields names list. We can only allow
    // saving the "_id" field, if it's a new doc with provided id. It means that
    // the developer wants that.
    if (!(values._id && doc._isNew)) {
      fieldsNames = _.without(fieldsNames, '_id');
    }

    // Pick only the values that the developer wants to save.
    values = _.pick(values, fieldsNames);

    // Modify fields list to only those that are present in the values.
    fieldsNames = _.keys(values);

    // Check if there are any values to update or insert. If there are no
    // modified fields, we shouldn't do anything.
    if (_.size(values) === 0) {
      return;
    }

    // Add callback to arguments list if provided.
    var args = [];
    if (_.isFunction(callback)) {
      args.push(callback);
    }

    // Get collection for given class or parent class.
    var Collection = Class.getCollection();

    var result;
    if (isUpdating) {
      // Add selector and modifier at the beginning of the arguments list. Right
      // now in the array is a callback function (if provided).
      args.unshift({ // Selector.
        _id: id
      }, { // Modifier.
        $set: values
      });
      // Update document.
      result = Collection.update.apply(Collection, args);
    } else {
      // Add values to insert into the list of arguments passed to the "insert"
      // method.
      args.unshift(values);
      // Insert document.
      result = Collection.insert.apply(Collection, args);
      Astro.utils.fields.setValue(doc, '_id', result);
    }

    // Change the "_isNew" flag to "false". Now the document is not new, it has
    // just been saved.
    doc._isNew = false;

    // Trigger "afterInsert" or "afterUpdate" event handlers on the current and
    // parent classes.
    var event = new Astro.Event(isUpdating ? 'afterUpdate' : 'afterInsert');
    event.target = doc;
    Class.emitEvent(event);
    // Trigger "afterSave" event handlers on the current and parent classes.
    var event = new Astro.Event('afterSave');
    event.target = doc;
    Class.emitEvent(event);

    // Copy all values to the "_original" property so that we are starting with
    // the clean object without modifications (there is no diff between current
    // values and "_original").
    doc._original = _.extend(doc._original, EJSON.clone(
      Astro.utils.fields.getValues(doc, fieldsNames)
    ));

    // Return result of executing Mongo query.
    return result;
  },

  remove: function(callback) {
    var doc = this;
    var Class = doc.constructor;

    // Remove only when document has the "_id" field (it's persisted).
    var id = Astro.utils.fields.getValue(doc, '_id');
    if (!id) {
      return;
    }

    // Trigger "beforeRemove" event handlers on the current and parent classes.
    var event = new Astro.Event('beforeRemove');
    event.target = doc;
    Class.emitEvent(event);

    // If user prevented default operation, then we have to stop here.
    if (event.defaultPrevented) {
      return;
    }

    // Add selector to arguments list.
    var args = [];
    args.push({
      _id: id
    });
    // Add callback to arguments list if provided.
{'fraction_non_alphanumeric': 0.10987903225806452, 'fraction_numerical': 0.0003360215053763441, 'mean_word_length': 2.4028201219512195, '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': '1536285', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2506, 'n_words': 911}