<commit_before>require 'active_support/concern'

module Ooor
  module FinderMethods
    extend ActiveSupport::Concern

    module ClassMethods
      def find(*arguments)
        if arguments.size == 1 &&
            arguments[0].is_a?(Hash) ||
            (arguments[0].is_a?(Array) && !([arguments[0][1]] & Ooor::TypeCasting::OPERATORS).empty?)
          find_single(nil, {domain: arguments[0]})
        else
          find_dispatch(*arguments)
        end
      end

      private
        def find_dispatch(*arguments)
          scope   = arguments.slice!(0)
          options = arguments.slice!(0) || {}
          if (!scope.is_a?(Array) && !options.is_a?(Hash))
            scope = [scope] + [options] + arguments
            options = {}
          end
          case scope
          when :all   then find_single(nil, options)
          when :first then find_first_or_last(options)
          when :last  then find_first_or_last(options, "DESC")
          when :one   then find_one(options)
          else             find_single(scope, options)
          end
        end
      
        def find_first_or_last(options, ordering = "ASC")
          options[:order] ||= "id #{ordering}"
          options[:limit] = 1
          domain = options[:domain] || options[:conditions] || []
          context = options[:context] || {}
          ids = rpc_execute('search', to_openerp_domain(domain), options[:offset] || 0,
            options[:limit],  options[:order], context.dup)
          if ids.empty?
            return nil
          else
            find_single(ids.first, options)
          end
        end

        #actually finds many resources specified with scope = ids_array
        def find_single(scope, options)
          context = options[:context] || {}<commit_msg>major optimization: find_first_or_last now use search_read when using JSRPC transport<commit_after>require 'active_support/concern'

module Ooor
  module FinderMethods
    extend ActiveSupport::Concern

    module ClassMethods
      def find(*arguments)
        if arguments.size == 1 &&
            arguments[0].is_a?(Hash) ||
            (arguments[0].is_a?(Array) && !([arguments[0][1]] & Ooor::TypeCasting::OPERATORS).empty?)
          find_single(nil, {domain: arguments[0]})
        else
          find_dispatch(*arguments)
        end
      end

      private
        def find_dispatch(*arguments)
          scope   = arguments.slice!(0)
          options = arguments.slice!(0) || {}
          if (!scope.is_a?(Array) && !options.is_a?(Hash))
            scope = [scope] + [options] + arguments
            options = {}
          end
          case scope
          when :all   then find_single(nil, options)
          when :first then find_first_or_last(options)
          when :last  then find_first_or_last(options, "DESC")
          when :one   then find_one(options)
          else             find_single(scope, options)
          end
        end
      
        def find_first_or_last(options, ordering = "ASC")
          options[:order] ||= "id #{ordering}"
          options[:limit] = 1
          find_single(nil, options)[0]
        end

        #actually finds many resources specified with scope = ids_array
        def find_single(scope, options)
          context = options[:context] || {}
{'fraction_non_alphanumeric': 0.1117879987816022, 'fraction_numerical': 0.006091989034419738, 'mean_word_length': 2.0749063670411987, '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': '911537', 'n_tokens_mistral': 911, 'n_tokens_neox': 901, 'n_words': 212}