<commit_before>module DataMapper
  module Adapters
    module Sphinx
      # == Synopsis
      #
      # DataMapper uses URIs or a connection has to connect to your data-stores. In this case the sphinx search daemon
      # <tt>searchd</tt>.
      #
      # On its own this adapter will only return an array of document hashes when queried. The DataMapper library dm-more
      # however provides dm-is-searchable, a common interface to search one adapter and load documents from another. My
      # preference is to use this adapter in tandem with dm-is-searchable.
      #
      # Like all DataMapper adapters you can connect with a Hash or URI.
      #
      # A URI:
      #   DataMapper.setup(:search, 'sphinx://localhost')
      #
      # The breakdown is:
      #   "#{adapter}://#{host}:#{port}/#{config}"
      #   - adapter Must be :sphinx
      #   - host    Hostname (default: localhost)
      #   - port    Optional port number (default: 3312)
      #
      # Alternatively supply a Hash:
      #   DataMapper.setup(:search, {
      #     :adapter  => 'sphinx',       # required
      #     :host     => 'localhost',    # optional. Default: localhost
      #     :port     => 3312            # optional. Default: 3312
      #   })
      class Adapter < AbstractAdapter

        # ==== See
        # * DataMapper::Adapters::AbstractAdapter
        #
        # ==== Parameters
        # uri_or_options<URI, DataObject::URI, Addressable::URI, String, Hash, Pathname>::
        #   DataMapper uri or options hash.
        def initialize(name, uri_or_options)
          super # Set up defaults.
          @options = normalize_options(uri_or_options)
        end

        def create(resources) #:nodoc:
          0
        end

        def delete(query) #:nodoc:
          0
        end

        # Query your Sphinx repository and return all matching documents.
        #
        # ==== Notes
        #
        # These methods are public but normally called indirectly through DataMapper::Resource#get,
        # DataMapper::Resource#first or DataMapper::Resource#all.
        #
        # The document hashes returned are those from Riddle::Client.
        #
        # ==== Parameters
        # query<DataMapper::Query>:: The query object.
        #
        # ==== Returns
        # Array<Hash>:: An array of document hashes. <tt>[{:id => 1, ...}, {:id => 2, ...}]</tt>
        # Array<>::     An empty array if no documents match.
        def read_many(query)
          read(query)
        end

        # Query your Sphinx repository and return the first document matched.
        #
        # ==== Notes
        #
        # These methods are public but normally called indirectly through DataMapper::Resource#get,
        # DataMapper::Resource#first or DataMapper::Resource#all.
        #
        # ==== Parameters
        # query<DataMapper::Query>:: The query object.
        #
        # ==== Returns
        # Hash:: An document hash of the first document matched. <tt>{:id => 1, ...}</tt>
        # Nil::  If no documents match.
        def read_one(query)
          read(query).first
        end

        protected
          # List sphinx indexes to search.
          #
          # If no indexes are explicitly declared using DataMapper::Adapters::Sphinx::Resource then the default storage
          # name is used.
          #
          # ==== See
          # * DataMapper::Adapters::Sphinx::Resource::ClassMethods#sphinx_indexes
          #
          # ==== Parameters
          # model<DataMapper::Model>:: The DataMapper::Model.
          #
          # ==== Returns
          # Array<DataMapper::Adapters::Sphinx::Index>:: Index objects from the model.
          def indexes(query)
            indexes = query.model.sphinx_indexes(query.repository.name) if query.model.respond_to?(:sphinx_indexes)
            if indexes.nil? or indexes.empty?
              indexes = [Index.new(query.model, query.model.storage_name(query.repository.name))]
            end
            indexes
          end

          # Query sphinx for a list of document IDs.
          #
          # ==== Parameters
          # query<DataMapper::Query>:: The query object.
          #
          # ==== Returns
          # Array<Hash>:: An array of document hashes. <tt>[{:id => 1, ...}, {:id => 2, ...}]</tt>
          # Array<>::     An empty array if no documents match.
          def read(query)
            from   = indexes(query).map{|index| index.name}.join(', ')<commit_msg>Dkubb - self.name should always be equal to query.repository.name, because query.repository is used to select the correct adapter.<commit_after>module DataMapper
  module Adapters
    module Sphinx
      # == Synopsis
      #
      # DataMapper uses URIs or a connection has to connect to your data-stores. In this case the sphinx search daemon
      # <tt>searchd</tt>.
      #
      # On its own this adapter will only return an array of document hashes when queried. The DataMapper library dm-more
      # however provides dm-is-searchable, a common interface to search one adapter and load documents from another. My
      # preference is to use this adapter in tandem with dm-is-searchable.
      #
      # Like all DataMapper adapters you can connect with a Hash or URI.
      #
      # A URI:
      #   DataMapper.setup(:search, 'sphinx://localhost')
      #
      # The breakdown is:
      #   "#{adapter}://#{host}:#{port}/#{config}"
      #   - adapter Must be :sphinx
      #   - host    Hostname (default: localhost)
      #   - port    Optional port number (default: 3312)
      #
      # Alternatively supply a Hash:
      #   DataMapper.setup(:search, {
      #     :adapter  => 'sphinx',       # required
      #     :host     => 'localhost',    # optional. Default: localhost
      #     :port     => 3312            # optional. Default: 3312
      #   })
      class Adapter < AbstractAdapter

        # ==== See
        # * DataMapper::Adapters::AbstractAdapter
        #
        # ==== Parameters
        # uri_or_options<URI, DataObject::URI, Addressable::URI, String, Hash, Pathname>::
        #   DataMapper uri or options hash.
        def initialize(name, uri_or_options)
          super # Set up defaults.
          @options = normalize_options(uri_or_options)
        end

        def create(resources) #:nodoc:
          0
        end

        def delete(query) #:nodoc:
          0
        end

        # Query your Sphinx repository and return all matching documents.
        #
        # ==== Notes
        #
        # These methods are public but normally called indirectly through DataMapper::Resource#get,
        # DataMapper::Resource#first or DataMapper::Resource#all.
        #
        # The document hashes returned are those from Riddle::Client.
        #
        # ==== Parameters
        # query<DataMapper::Query>:: The query object.
        #
        # ==== Returns
        # Array<Hash>:: An array of document hashes. <tt>[{:id => 1, ...}, {:id => 2, ...}]</tt>
        # Array<>::     An empty array if no documents match.
        def read_many(query)
          read(query)
        end

        # Query your Sphinx repository and return the first document matched.
        #
        # ==== Notes
        #
        # These methods are public but normally called indirectly through DataMapper::Resource#get,
        # DataMapper::Resource#first or DataMapper::Resource#all.
        #
        # ==== Parameters
        # query<DataMapper::Query>:: The query object.
        #
        # ==== Returns
        # Hash:: An document hash of the first document matched. <tt>{:id => 1, ...}</tt>
        # Nil::  If no documents match.
        def read_one(query)
          read(query).first
        end

        protected
          # List sphinx indexes to search.
          #
          # If no indexes are explicitly declared using DataMapper::Adapters::Sphinx::Resource then the default storage
          # name is used.
          #
          # ==== See
          # * DataMapper::Adapters::Sphinx::Resource::ClassMethods#sphinx_indexes
          #
          # ==== Parameters
          # model<DataMapper::Model>:: The DataMapper::Model.
          #
          # ==== Returns
          # Array<DataMapper::Adapters::Sphinx::Index>:: Index objects from the model.
          def indexes(query)
            indexes = query.model.sphinx_indexes(name) if query.model.respond_to?(:sphinx_indexes)
            if indexes.nil? or indexes.empty?
              indexes = [Index.new(query.model, query.model.storage_name(name))]
            end
            indexes
          end

          # Query sphinx for a list of document IDs.
          #
          # ==== Parameters
          # query<DataMapper::Query>:: The query object.
          #
          # ==== Returns
          # Array<Hash>:: An array of document hashes. <tt>[{:id => 1, ...}, {:id => 2, ...}]</tt>
          # Array<>::     An empty array if no documents match.
          def read(query)
            from   = indexes(query).map{|index| index.name}.join(', ')
{'fraction_non_alphanumeric': 0.11207183239108746, 'fraction_numerical': 0.0042123933045116945, 'mean_word_length': 1.8991002570694087, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 55, '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': '7535619', 'n_tokens_mistral': 2506, 'n_tokens_neox': 2405, 'n_words': 736}