Class: Mongo::SearchIndex::View
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Instance Chain:
self,
::Mongo::Collection::Helpers ,
::Mongo::Retryable ,
Enumerable
|
|
Inherits: | Object |
Defined in: | lib/mongo/search_index/view.rb |
Overview
A class representing a view of search indexes.
Class Method Summary
-
.new(collection, options = {}) ⇒ View
constructor
Create the new search index view.
Instance Attribute Summary
- #aggregate_options ⇒ Hash readonly
- #collection ⇒ Mongo::Collection readonly
-
#empty? ⇒ true | false
readonly
Queries whether the search index enumerable is empty.
- #requested_index_id ⇒ nil | String readonly
- #requested_index_name ⇒ nil | String readonly
Instance Method Summary
-
#create_many(indexes) ⇒ Array<String>
Create multiple search indexes with a single command.
-
#create_one(definition, name: nil, type: 'search') ⇒ String
Create a single search index with the given definition.
-
#drop_one(id: nil, name: nil) ⇒ Mongo::Operation::Result | false
Drop the search index with the given id, or name.
-
#each(&block) ⇒ self | Enumerator
Iterate over the search indexes.
-
#update_one(definition, id: nil, name: nil) ⇒ Mongo::Operation::Result
Update the search index with the given id or name.
-
#execution_context ⇒ Mongo::Operation::Context
private
A helper method for constructing a new operation context for executing an operation.
-
#next_primary(ping = nil, session = nil) ⇒ Mongo::Server
private
A helper method for retrieving the primary server from the cluster.
-
#spec_with(extras) ⇒ Hash
private
A helper method for building the specification document with certain values pre-populated.
-
#validate_id_or_name!(id, name)
private
Validates the given id and name, ensuring that exactly one of them is non-nil.
-
#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.
-
#validate_search_index_definition!(definition)
private
Validates the definition of a search index.
-
#validate_search_index_keys!(keys)
private
Validates the keys of a search index document, ensuring that they are all valid.
-
#validate_search_index_name!(name)
private
Validates the name of a search index, ensuring that it is either a String or nil.
::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.
# File 'lib/mongo/search_index/view.rb', line 33
def initialize(collection, = {}) @collection = collection @requested_index_id = [:id] @requested_index_name = [:name] @aggregate_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_options ⇒ Hash
(readonly)
# File 'lib/mongo/search_index/view.rb', line 22
attr_reader :
#collection ⇒ Mongo::Collection (readonly)
# File 'lib/mongo/search_index/view.rb', line 12
attr_reader :collection
#empty? ⇒ true
| false
(readonly)
Queries whether the search index enumerable is empty.
# File 'lib/mongo/search_index/view.rb', line 136
def empty? count.zero? end
#requested_index_id ⇒ nil
| String
(readonly)
# File 'lib/mongo/search_index/view.rb', line 15
attr_reader :requested_index_id
#requested_index_name ⇒ nil
| String
(readonly)
# 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.
# 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, type: 'search') ⇒ String
Create a single search index with the given definition. If the name is provided, the new index will be given that name.
# File 'lib/mongo/search_index/view.rb', line 51
def create_one(definition, name: nil, type: 'search') create_many([ { name: name, definition: definition, type: type } ]).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.
# 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.
# 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.with(read_concern: {}).aggregate( [ { '$listSearchIndexes' => spec } ], ) end return @result.to_enum unless block @result.each(&block) self end
#execution_context ⇒ Mongo::Operation::Context (private)
A helper method for constructing a new operation context for executing an operation.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# 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.
# File 'lib/mongo/search_index/view.rb', line 202
def validate_search_index_keys!(keys) extras = keys - [ 'name', 'definition', 'type', :name, :definition, :type ] 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.
# 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