<commit_before>require "mongoid_retry/version"

module Mongoid
  module MongoidRetry

    DUPLICATE_KEY_ERROR_CODES = [11000,11001]

    if Mongoid::VERSION < '3'
      def self.error_message(exception)
        exception.message
      end

      def self.error_code(exception)
        exception.error_code
      end
    else
      def self.error_message(exception)
        exception.details["err"]
      end

      def self.error_code(exception)
        exception.details["code"]
      end
    end

    def self.is_a_duplicate_key_error?(exception)
      DUPLICATE_KEY_ERROR_CODES.include?(error_code(exception))
    end

    # Catch a duplicate key error
    def save_and_retry
      if ::Mongoid::VERSION < '3'
        begin
          save!
        rescue Mongo::OperationFailure => e
          retry_if_duplicate_key_error(e)
        end
      else
        begin
          with(safe: true).save!
        rescue Moped::Errors::OperationFailure => e
          retry_if_duplicate_key_error(e)
        end
      end
    end

    def retry_if_duplicate_key_error(e)
      if ::Mongoid::MongoidRetry.is_a_duplicate_key_error?(e)
        keys = duplicate_key(e)
        if (duplicate = find_duplicate(keys))
          update_document!(duplicate)
        end
      else
        raise e
      end<commit_msg>Fix failing tests with mongoid 2<commit_after>require "mongoid_retry/version"

module Mongoid
  module MongoidRetry

    DUPLICATE_KEY_ERROR_CODES = [11000,11001]

    if Mongoid::VERSION < '3'
      def self.error_message(exception)
        exception.message
      end

      def self.error_code(exception)
        exception.error_code
      end
    else
      def self.error_message(exception)
        exception.details["err"]
      end

      def self.error_code(exception)
        exception.details["code"]
      end
    end

    def self.is_a_duplicate_key_error?(exception)
      DUPLICATE_KEY_ERROR_CODES.include?(error_code(exception))
    end

    # Catch a duplicate key error
    def save_and_retry
      if ::Mongoid::VERSION < '3'
        begin
          safely.save!
        rescue Mongo::OperationFailure => e
          retry_if_duplicate_key_error(e)
        end
      else
        begin
          with(safe: true).save!
        rescue Moped::Errors::OperationFailure => e
          retry_if_duplicate_key_error(e)
        end
      end
    end

    def retry_if_duplicate_key_error(e)
      if ::Mongoid::MongoidRetry.is_a_duplicate_key_error?(e)
        keys = duplicate_key(e)
        if (duplicate = find_duplicate(keys))
          update_document!(duplicate)
        end
      else
        raise e
      end
{'fraction_non_alphanumeric': 0.07490494296577947, 'fraction_numerical': 0.009505703422053232, 'mean_word_length': 2.276463262764633, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '7530061', 'n_tokens_mistral': 832, 'n_tokens_neox': 830, 'n_words': 156}