Class: Mongo::Index::View
- Inherits:
-
Object
- Object
- Mongo::Index::View
- Extended by:
- Forwardable
- Includes:
- Enumerable, Cursor::NonTailable, CursorHost, Retryable
- Defined in:
- 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, :wildcard_projection => :wildcardProjection, }.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.
-
#operation_timeout_ms ⇒ Integer | nil | The timeout_ms value that was passed as an option to the view.
readonly
private
Integer | nil | The timeout_ms value that was passed as an option to the view.
Attributes included from CursorHost
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.
-
#operation_timeouts(opts = {}) ⇒ Hash
private
Timeout_ms value set on the operation level (if any), and/or timeout_ms that is set on collection/database/client level (if any).
-
#timeout_ms ⇒ Integer | nil
The timeout_ms value to use for this operation; either specified as an option to the view, or inherited from the collection.
Methods included from Cursor::NonTailable
Methods included from CursorHost
Methods included from Retryable
#read_worker, #select_server, #write_worker
Constructor Details
#initialize(collection, options = {}) ⇒ View
Create the new index view.
318 319 320 321 322 323 324 325 326 |
# File 'lib/mongo/index/view.rb', line 318 def initialize(collection, = {}) @collection = collection @operation_timeout_ms = .delete(:timeout_ms) validate_timeout_mode!() @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.
38 39 40 |
# File 'lib/mongo/index/view.rb', line 38 def batch_size @batch_size end |
#collection ⇒ Collection (readonly)
Returns collection The indexes collection.
34 35 36 |
# File 'lib/mongo/index/view.rb', line 34 def collection @collection end |
#operation_timeout_ms ⇒ Integer | nil | The timeout_ms value that was passed as an option to the view. (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Integer | nil | The timeout_ms value that was passed as an option to the view.
44 45 46 |
# File 'lib/mongo/index/view.rb', line 44 def operation_timeout_ms @operation_timeout_ms 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.
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/mongo/index/view.rb', line 216 def create_many(*models) models = models.flatten = {} if models && !models.last.key?(:key) = models.pop end client.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], } context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts() ) Operation::CreateIndex.new(spec).execute(server, context: context) 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.
167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/mongo/index/view.rb', line 167 def create_one(keys, = {}) = .dup = {} if session = @options[:session] [:session] = session end %i(commit_quorum session comment timeout_ms max_time_ms).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.
119 120 121 |
# File 'lib/mongo/index/view.rb', line 119 def drop_all( = {}) drop_by_name(Index::ALL, ) end |
#drop_one(name, options = {}) ⇒ Result
Drop an index by its name.
101 102 103 104 |
# File 'lib/mongo/index/view.rb', line 101 def drop_one(name, = {}) raise Error::MultiIndexDrop.new if name == Index::ALL drop_by_name(name, ) end |
#each(&block) ⇒ Object
Iterate over all indexes for the collection.
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/mongo/index/view.rb', line 279 def each(&block) session = client.get_session(@options) context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts(@options) ) cursor = read_with_retry_cursor(session, ServerSelector.primary, self, context: context) do |server| send_initial_query(server, session, context) 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.
265 266 267 268 269 |
# File 'lib/mongo/index/view.rb', line 265 def get(keys_or_name) find do |index| (index[NAME] == keys_or_name) || (index[KEY] == normalize_keys(keys_or_name)) end end |
#operation_timeouts(opts = {}) ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns timeout_ms value set on the operation level (if any), and/or timeout_ms that is set on collection/database/client level (if any).
340 341 342 343 344 345 346 347 348 |
# File 'lib/mongo/index/view.rb', line 340 def operation_timeouts(opts = {}) {}.tap do |result| if opts[:timeout_ms] || operation_timeout_ms result[:operation_timeout_ms] = opts.delete(:timeout_ms) || operation_timeout_ms else result[:inherited_timeout_ms] = collection.timeout_ms end end end |
#timeout_ms ⇒ Integer | nil
The timeout_ms value to use for this operation; either specified as an option to the view, or inherited from the collection.
332 333 334 |
# File 'lib/mongo/index/view.rb', line 332 def timeout_ms operation_timeout_ms || collection.timeout_ms end |