<commit_before>import { Class, isArray, isObject, isNone } from 'orbit/lib/objects';
import OperationEncoder from 'orbit-common/operation-encoder';

export default Class.extend({
  init: function(schema, cache){
    this._schema = schema;
    this._cache = cache;
    this._operationEncoder = new OperationEncoder(schema);
  },

  process: function(operation, condemnedPaths) {
    var _this = this;
    condemnedPaths = condemnedPaths || [];
    var path = operation.path;
    var value = operation.value;
    var type = path[0];
    var schema = this._schema;
    var operationType = this._operationEncoder.identify(operation);

    function relatedLinkOps(linkValue, linkDef){
      linkDef = linkDef || schema.linkDefinition(type, path[3]);
      var op = operation.op;
      var ignoredPaths = op === 'remove' ? condemnedPaths : [];
      return _this._relatedLinkOps(linkDef, linkValue, path[1], operation, ignoredPaths);
    }

    function addRelatedLinksOpsForRecord(record){
      if (!record.__rel) return;
      var linkDef;
      var ops = [];

      Object.keys(record.__rel).forEach(function(link) {
        linkDef = schema.linkDefinition(type, link);

        if (linkDef.inverse) {
          var linkValue = linkDef.type === 'hasMany' ? Object.keys(record.__rel[link]) : record.__rel[link];
          var linkOps = relatedLinkOps(linkValue, linkDef);

          for(var i = 0; i < linkOps.length; i++){
            ops.push(linkOps[i]);
          }
        }
      });

      return ops;
    }

    function removeRelatedLinksOpsForRecord(record){
      if (!record.__rel) return [];
      var linkDef;
      var ops = [];

      Object.keys(record.__rel).forEach(function(link) {
        linkDef = schema.linkDefinition(type, link);

        if (linkDef.inverse) {
          var linkValue = linkDef.type === 'hasMany' ? Object.keys(record.__rel[link]) : record.__rel[link];
          var linkOps = relatedLinkOps(Object.keys(record.__rel[link]), linkDef);

          for(var i = 0; i < linkOps.length; i++){
            ops.push(linkOps[i]);
          }
        }
      });

      return ops;
    }

    switch (operationType) {
      case 'addHasOne': return relatedLinkOps(value);
      case 'replaceHasOne': return relatedLinkOps(value);
      case 'removeHasOne': return relatedLinkOps(this._retrieve(path));

      case 'addHasMany': return relatedLinkOps(Object.keys(operation.value));
      case 'replaceHasMany': return relatedLinkOps(Object.keys(operation.value));
      case 'removeHasMany': return relatedLinkOps(Object.keys(this._retrieve(path)));
      case 'addToHasMany': return relatedLinkOps(path[4]);<commit_msg>Fix removeRelatedLinksOpsForRecord<commit_after>import { Class, isArray, isObject, isNone } from 'orbit/lib/objects';
import OperationEncoder from 'orbit-common/operation-encoder';

export default Class.extend({
  init: function(schema, cache){
    this._schema = schema;
    this._cache = cache;
    this._operationEncoder = new OperationEncoder(schema);
  },

  process: function(operation, condemnedPaths) {
    var _this = this;
    condemnedPaths = condemnedPaths || [];
    var path = operation.path;
    var value = operation.value;
    var type = path[0];
    var schema = this._schema;
    var operationType = this._operationEncoder.identify(operation);

    function relatedLinkOps(linkValue, linkDef){
      linkDef = linkDef || schema.linkDefinition(type, path[3]);
      var op = operation.op;
      var ignoredPaths = op === 'remove' ? condemnedPaths : [];
      return _this._relatedLinkOps(linkDef, linkValue, path[1], operation, ignoredPaths);
    }

    function addRelatedLinksOpsForRecord(record){
      if (!record.__rel) return;
      var linkDef;
      var ops = [];

      Object.keys(record.__rel).forEach(function(link) {
        linkDef = schema.linkDefinition(type, link);

        if (linkDef.inverse) {
          var linkValue = linkDef.type === 'hasMany' ? Object.keys(record.__rel[link]) : record.__rel[link];
          var linkOps = relatedLinkOps(linkValue, linkDef);

          for(var i = 0; i < linkOps.length; i++){
            ops.push(linkOps[i]);
          }
        }
      });

      return ops;
    }

    function removeRelatedLinksOpsForRecord(record){
      if (!record.__rel) return [];
      var linkDef;
      var ops = [];

      Object.keys(record.__rel).forEach(function(link) {
        linkDef = schema.linkDefinition(type, link);

        if (linkDef.inverse) {
          var linkValue = linkDef.type === 'hasMany' ? Object.keys(record.__rel[link]) : record.__rel[link];
          var linkOps = relatedLinkOps(linkValue, linkDef);

          for(var i = 0; i < linkOps.length; i++){
            ops.push(linkOps[i]);
          }
        }
      });

      return ops;
    }

    switch (operationType) {
      case 'addHasOne': return relatedLinkOps(value);
      case 'replaceHasOne': return relatedLinkOps(value);
      case 'removeHasOne': return relatedLinkOps(this._retrieve(path));

      case 'addHasMany': return relatedLinkOps(Object.keys(operation.value));
      case 'replaceHasMany': return relatedLinkOps(Object.keys(operation.value));
      case 'removeHasMany': return relatedLinkOps(Object.keys(this._retrieve(path)));
      case 'addToHasMany': return relatedLinkOps(path[4]);
{'fraction_non_alphanumeric': 0.12761940721162923, 'fraction_numerical': 0.0022654332641117614, 'mean_word_length': 3.0566615620214397, 'pattern_counts': {'":': 0, '<': 7, '<?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': '6986304', 'n_tokens_mistral': 1549, 'n_tokens_neox': 1499, 'n_words': 332}