123456789_123456789_123456789_123456789_123456789_

Class: Mongo::SearchIndex::View

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Instance Chain:
Inherits: Object
Defined in: lib/mongo/search_index/view.rb

Overview

A class representing a view of search indexes.

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::Mongo::Collection::Helpers - Included

#do_drop

Executes drop operation and and ignores NamespaceNotFound error.

::Mongo::Retryable - Included

#read_worker

Returns the read worker for handling retryable reads.

#select_server

This is a separate method to make it possible for the test suite to assert that server selection is performed during retry attempts.

#write_worker

Returns the write worker for handling retryable writes.

Constructor Details

.new(collection, options = {}) ⇒ View

Create the new search index view.

Parameters:

  • collection (Collection)

    The collection.

  • options (Hash) (defaults to: {})

    The options that configure the behavior of the view.

Options Hash (options):

  • :id (String)

    The specific index id to query (optional)

  • :name (String)

    The name of the specific index to query (optional)

  • :aggregate (Hash)

    The options hash to send to the aggregate command when querying the available indexes.

Raises:

  • (ArgumentError)
[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 33

def initialize(collection, options = {})
  @collection = collection
  @requested_index_id = options[:id]
  @requested_index_name = options[:name]
  @aggregate_options = options[:aggregate] || {}

  return if @aggregate_options.is_a?(Hash)

  raise ArgumentError, "The :aggregate option must be a Hash (got a #{@aggregate_options.class})"
end

Instance Attribute Details

#aggregate_optionsHash (readonly)

Returns:

  • (Hash)

    the options hash to use for the aggregate command when querying the available indexes.

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 22

attr_reader :aggregate_options

#collectionMongo::Collection (readonly)

Returns:

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 12

attr_reader :collection

#empty?true | false (readonly)

Queries whether the search index enumerable is empty.

Returns:

  • (true | false)

    whether the enumerable is empty or not.

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 136

def empty?
  count.zero?
end

#requested_index_idnil | String (readonly)

Returns:

  • (nil | String)

    the index id to query

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 15

attr_reader :requested_index_id

#requested_index_namenil | String (readonly)

Returns:

  • (nil | String)

    the index name to query

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 18

attr_reader :requested_index_name

Instance Method Details

#create_many(indexes) ⇒ Array<String>

Create multiple search indexes with a single command.

Parameters:

  • indexes (Array<Hash>)

    The description of the indexes to create. Each element of the list must be a hash with a definition key, and an optional name key.

Returns:

  • (Array<String>)

    the names of the new search indexes.

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 62

def create_many(indexes)
  spec = spec_with(indexes: indexes.map { |v| validate_search_index!(v) })
  result = Operation::CreateSearchIndexes.new(spec).execute(next_primary, context: execution_context)
  result.first['indexesCreated'].map { |idx| idx['name'] }
end

#create_one(definition, name: nil) ⇒ String

Create a single search index with the given definition. If the name is provided, the new index will be given that name.

Parameters:

  • definition (Hash)

    The definition of the search index.

  • name (nil | String)

    The name to give the new search index.

Returns:

  • (String)

    the name of the new search index.

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 51

def create_one(definition, name: nil)
  create_many([ { name: name, definition: definition } ]).first
end

#drop_one(id: nil, name: nil) ⇒ Mongo::Operation::Result | false

Drop the search index with the given id, or name. One or the other must be specified, but not both.

Parameters:

  • id (String)

    the id of the index to drop

  • name (String)

    the name of the index to drop

Returns:

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 76

def drop_one(id: nil, name: nil)
  validate_id_or_name!(id, name)

  spec = spec_with(index_id: id, index_name: name)
  op = Operation::DropSearchIndex.new(spec)

  # per the spec:
  # Drivers MUST suppress NamespaceNotFound errors for the
  # ``dropSearchIndex`` helper.  Drop operations should be idempotent.
  do_drop(op, nil, execution_context)
end

#each(&block) ⇒ self | Enumerator

Iterate over the search indexes.

Parameters:

  • block (Proc)

    if given, each search index will be yieleded to the block.

Returns:

  • (self | Enumerator)

    if a block is given, self is returned. Otherwise, an enumerator will be returned.

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 95

def each(&block)
  @result ||= begin
    spec = {}.tap do |s|
      s[:id] = requested_index_id if requested_index_id
      s[:name] = requested_index_name if requested_index_name
    end

    collection.aggregate(
      [ { '$listSearchIndexes' => spec } ],
      aggregate_options
    )
  end

  return @result.to_enum unless block

  @result.each(&block)
  self
end

#execution_contextMongo::Operation::Context (private)

A helper method for constructing a new operation context for executing an operation.

Returns:

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 166

def execution_context
  Operation::Context.new(client: collection.client)
end

#next_primary(ping = nil, session = nil) ⇒ Mongo::Server (private)

A helper method for retrieving the primary server from the cluster.

Returns:

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 158

def next_primary(ping = nil, session = nil)
  collection.cluster.next_primary(ping, session)
end

#spec_with(extras) ⇒ Hash (private)

A helper method for building the specification document with certain values pre-populated.

Parameters:

  • extras (Hash)

    the values to put into the specification

Returns:

  • (Hash)

    the specification document

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 148

def spec_with(extras)
  {
    coll_name: collection.name,
    db_name: collection.database.name,
  }.merge(extras)
end

#update_one(definition, id: nil, name: nil) ⇒ Mongo::Operation::Result

Update the search index with the given id or name. One or the other must be provided, but not both.

Parameters:

  • definition (Hash)

    the definition to replace the given search index with.

  • id (nil | String)

    the id of the search index to update

  • name (nil | String)

    the name of the search index to update

Returns:

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 123

def update_one(definition, id: nil, name: nil)
  validate_id_or_name!(id, name)

  spec = spec_with(index_id: id, index_name: name, index: definition)
  Operation::UpdateSearchIndex.new(spec).execute(next_primary, context: execution_context)
end

#validate_id_or_name!(id, name) (private)

Validates the given id and name, ensuring that exactly one of them is non-nil.

Parameters:

  • id (nil | String)

    the id to validate

  • name (nil | String)

    the name to validate

Raises:

  • (ArgumentError)

    if neither or both arguments are nil

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 177

def validate_id_or_name!(id, name)
  return unless (id.nil? && name.nil?) || (!id.nil? && !name.nil?)

  raise ArgumentError, 'exactly one of id or name must be specified'
end

#validate_search_index!(doc) (private)

Validates the given search index document, ensuring that it has no extra keys, and that the name and definition are valid.

Parameters:

  • doc (Hash)

    the document to validate

Raises:

  • (ArgumentError)

    if the document is invalid.

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 189

def validate_search_index!(doc)
  validate_search_index_keys!(doc.keys)
  validate_search_index_name!(doc[:name] || doc['name'])
  validate_search_index_definition!(doc[:definition] || doc['definition'])
  doc
end

#validate_search_index_definition!(definition) (private)

Validates the definition of a search index.

Parameters:

  • definition (Hash)

    the definition of a search index

Raises:

  • (ArgumentError)

    if the definition is not valid

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 225

def validate_search_index_definition!(definition)
  return if definition.is_a?(Hash)

  raise ArgumentError, "search index definition must be a Hash (got #{definition.inspect})"
end

#validate_search_index_keys!(keys) (private)

Validates the keys of a search index document, ensuring that they are all valid.

Parameters:

  • keys (Array<String | Hash>)

    the keys of a search index document

Raises:

  • (ArgumentError)

    if the list contains any invalid keys

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 202

def validate_search_index_keys!(keys)
  extras = keys - [ 'name', 'definition', :name, :definition ]

  raise ArgumentError, "invalid keys in search index creation: #{extras.inspect}" if extras.any?
end

#validate_search_index_name!(name) (private)

Validates the name of a search index, ensuring that it is either a String or nil.

Parameters:

  • name (nil | String)

    the name of a search index

Raises:

  • (ArgumentError)

    if the name is not valid

[ GitHub ]

  
# File 'lib/mongo/search_index/view.rb', line 214

def validate_search_index_name!(name)
  return if name.nil? || name.is_a?(String)

  raise ArgumentError, "search index name must be nil or a string (got #{name.inspect})"
end