Class: Mongo::Index::View
- Inherits:
-
Object
- Object
- Mongo::Index::View
- Extended by:
- Forwardable
- Includes:
- Enumerable, Retryable
- Defined in:
- build/ruby-driver-master/lib/mongo/index/view.rb
Overview
A class representing a view of indexes.
Constant Summary collapse
- 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, }.freeze
Instance Attribute Summary collapse
-
#batch_size ⇒ Integer
readonly
Batch_size The size of the batch of results when sending the listIndexes command.
-
#collection ⇒ Collection
readonly
Collection The indexes collection.
Instance Method Summary collapse
-
#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) ⇒ Object
Iterate over all indexes for the collection.
-
#get(keys_or_name) ⇒ Hash
Convenience method for getting index information by a specific name or spec.
-
#initialize(collection, options = {}) ⇒ View
constructor
Create the new index view.
Methods included from Retryable
#legacy_write_with_retry, #nro_write_with_retry, #read_with_one_retry, #read_with_retry, #read_with_retry_cursor, #write_with_retry
Constructor Details
#initialize(collection, options = {}) ⇒ View
Create the new index view.
292 293 294 295 296 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 292 def initialize(collection, = {}) @collection = collection @batch_size = [:batch_size] @options = end |
Instance Attribute Details
#batch_size ⇒ Integer (readonly)
Returns batch_size The size of the batch of results when sending the listIndexes command.
34 35 36 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 34 def batch_size @batch_size end |
#collection ⇒ Collection (readonly)
Returns collection The indexes collection.
30 31 32 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 30 def collection @collection end |
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.
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 205 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.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 156 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.
108 109 110 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 108 def drop_all( = {}) drop_by_name(Index::ALL, comment: [:comment]) end |
#drop_one(name, options = {}) ⇒ Result
Drop an index by its name.
90 91 92 93 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 90 def drop_one(name, = {}) raise Error::MultiIndexDrop.new if name == Index::ALL drop_by_name(name, comment: [:comment]) end |
#each(&block) ⇒ Object
Iterate over all indexes for the collection.
264 265 266 267 268 269 270 271 272 273 274 275 276 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 264 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.
250 251 252 253 254 |
# File 'build/ruby-driver-master/lib/mongo/index/view.rb', line 250 def get(keys_or_name) find do |index| (index[NAME] == keys_or_name) || (index[KEY] == normalize_keys(keys_or_name)) end end |