Class: Mongo::Index::View
Relationships & Source Files | |
Super Chains via Extension / Inclusion / Inheritance | |
Class Chain:
self,
Forwardable
|
|
Instance Chain:
self,
::Mongo::Retryable ,
Enumerable
|
|
Inherits: | Object |
Defined in: | lib/mongo/index/view.rb |
Overview
A class representing a view of indexes.
Constant Summary
-
KEY =
The index key field.
'key'.freeze
-
NAME =
The index name field.
'name'.freeze
-
OPTIONS =
The mappings of Ruby index options to server options.
{ :background => :background, :bits => :bits, :bucket_size => :bucketSize, :default_language => :default_language, :expire_after => :expireAfterSeconds, :expire_after_seconds => :expireAfterSeconds, :key => :key, :language_override => :language_override, :max => :max, :min => :min, :name => :name, :partial_filter_expression => :partialFilterExpression, :sparse => :sparse, :sphere_version => :'2dsphereIndexVersion', :storage_engine => :storageEngine, :text_version => :textIndexVersion, :unique => :unique, :version => :v, :weights => :weights, :collation => :collation, :comment => :comment, :wildcard_projection => :wildcardProjection, }.freeze
Class Method Summary
-
.new(collection, options = {}) ⇒ View
constructor
Create the new index view.
Instance Attribute Summary
- #batch_size ⇒ Integer readonly
- #collection ⇒ Collection readonly
Instance Method Summary
-
#create_many(*models) ⇒ Result
Creates multiple indexes on the collection.
-
#create_one(keys, options = {}) ⇒ Result
Creates an index on the collection.
-
#drop_all(options = {}) ⇒ Result
Drop all indexes on the collection.
-
#drop_one(name, options = {}) ⇒ Result
Drop an index by its name.
-
#each(&block)
Iterate over all indexes for the collection.
-
#get(keys_or_name) ⇒ Hash
Convenience method for getting index information by a specific name or spec.
- #drop_by_name(name, comment: nil) private
- #index_name(spec) private
- #indexes_spec(session) private
- #initial_query_op(session) private
- #limit private
- #normalize_keys(spec) private
- #normalize_models(models, server) private
- #send_initial_query(server, session) private
::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 index view.
# File 'lib/mongo/index/view.rb', line 293
def initialize(collection, = {}) @collection = collection @batch_size = [:batch_size] @options = end
Instance Attribute Details
#batch_size ⇒ Integer
(readonly)
# File 'lib/mongo/index/view.rb', line 34
attr_reader :batch_size
#collection ⇒ Collection (readonly)
# File 'lib/mongo/index/view.rb', line 30
attr_reader :collection
Instance Method Details
#create_many(*models) ⇒ Result
On MongoDB 3.0.0 and higher, the indexes will be created in parallel on the server.
Creates multiple indexes on the collection.
# File 'lib/mongo/index/view.rb', line 206
def create_many(*models) models = models.flatten = {} if models && !models.last.key?(:key) = models.pop end client.send(:with_session, @options.merge( )) do |session| server = next_primary(nil, session) indexes = normalize_models(models, server) indexes.each do |index| if index[:bucketSize] || index['bucketSize'] client.log_warn("Haystack indexes (bucketSize index option) are deprecated as of MongoDB 4.4") end end spec = { indexes: indexes, db_name: database.name, coll_name: collection.name, session: session, commit_quorum: [:commit_quorum], write_concern: write_concern, comment: [:comment], } Operation::CreateIndex.new(spec).execute(server, context: Operation::Context.new(client: client, session: session)) end end
#create_one(keys, options = {}) ⇒ Result
Note that the options listed may be subset of those available.
Creates an index on the collection.
See the MongoDB documentation for a full list of supported options by server version.
# File 'lib/mongo/index/view.rb', line 157
def create_one(keys, = {}) = .dup = {} if session = @options[:session] [:session] = session end %i(commit_quorum session comment).each do |key| if value = .delete(key) [key] = value end end create_many({ key: keys }.merge( ), ) end
#drop_all(options = {}) ⇒ Result
Drop all indexes on the collection.
# File 'lib/mongo/index/view.rb', line 109
def drop_all( = {}) drop_by_name(Index::ALL, comment: [:comment]) end
#drop_by_name(name, comment: nil) (private)
# File 'lib/mongo/index/view.rb', line 301
def drop_by_name(name, comment: nil) client.send(:with_session, @options) do |session| spec = { db_name: database.name, coll_name: collection.name, index_name: name, session: session, write_concern: write_concern, } spec[:comment] = comment unless comment.nil? server = next_primary(nil, session) Operation::DropIndex.new(spec).execute(server, context: Operation::Context.new(client: client, session: session)) end end
#drop_one(name, options = {}) ⇒ Result
Drop an index by its name.
# File 'lib/mongo/index/view.rb', line 91
def drop_one(name, = {}) raise Error::MultiIndexDrop.new if name == Index::ALL drop_by_name(name, comment: [:comment]) end
#each(&block)
Iterate over all indexes for the collection.
# File 'lib/mongo/index/view.rb', line 265
def each(&block) session = client.send(:get_session, @options) cursor = read_with_retry_cursor(session, ServerSelector.primary, self) do |server| send_initial_query(server, session) end if block_given? cursor.each do |doc| yield doc end else cursor.to_enum end end
#get(keys_or_name) ⇒ Hash
Convenience method for getting index information by a specific name or spec.
# File 'lib/mongo/index/view.rb', line 251
def get(keys_or_name) find do |index| (index[NAME] == keys_or_name) || (index[KEY] == normalize_keys(keys_or_name)) end end
#index_name(spec) (private)
# File 'lib/mongo/index/view.rb', line 316
def index_name(spec) spec.to_a.join('_') end
#indexes_spec(session) (private)
# File 'lib/mongo/index/view.rb', line 320
def indexes_spec(session) { selector: { listIndexes: collection.name, cursor: batch_size ? { batchSize: batch_size } : {} }, coll_name: collection.name, db_name: database.name, session: session } end
#initial_query_op(session) (private)
# File 'lib/mongo/index/view.rb', line 330
def initial_query_op(session) Operation::Indexes.new(indexes_spec(session)) end
#limit (private)
# File 'lib/mongo/index/view.rb', line 334
def limit; -1; end
#normalize_keys(spec) (private)
# File 'lib/mongo/index/view.rb', line 336
def normalize_keys(spec) return false if spec.is_a?(String) Options::Mapper.transform_keys_to_strings(spec) end
#normalize_models(models, server) (private)
# File 'lib/mongo/index/view.rb', line 341
def normalize_models(models, server) models.map do |model| # Transform options first which gives us a mutable hash Options::Mapper.transform(model, OPTIONS).tap do |model| model[:name] ||= index_name(model.fetch(:key)) end end end
#send_initial_query(server, session) (private)
# File 'lib/mongo/index/view.rb', line 350
def send_initial_query(server, session) initial_query_op(session).execute(server, context: Operation::Context.new(client: client, session: session)) end