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.
323 324 325 326 327 328 329 330 331 |
# File 'lib/mongo/index/view.rb', line 323 def initialize(collection, = {}) @collection = collection @operation_timeout_ms = .delete(:timeout_ms) validate_timeout_mode!() @batch_size = [:batch_size] = 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.
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 250 251 252 |
# File 'lib/mongo/index/view.rb', line 217 def create_many(*models) models = models.flatten = {} if models && !models.last.key?(:key) = models.pop end client.with_session(.merge()) do |session| indexes = normalize_models(models) 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 = Operation::CreateIndex.new(spec) tracer.trace_operation(operation, context, op_name: 'createIndexes') do server = next_primary(nil, session) operation.execute(server, context: context) end 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.
168 169 170 171 172 173 174 175 176 177 178 179 180 181 |
# File 'lib/mongo/index/view.rb', line 168 def create_one(keys, = {}) = .dup = {} if session = [: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.
120 121 122 |
# File 'lib/mongo/index/view.rb', line 120 def drop_all( = {}) drop_by_name(Index::ALL, ) end |
#drop_one(name, options = {}) ⇒ Result
Drop an index by its name.
102 103 104 105 |
# File 'lib/mongo/index/view.rb', line 102 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.
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/mongo/index/view.rb', line 282 def each(&block) session = client.get_session() context = Operation::Context.new( client: client, session: session, operation_timeouts: operation_timeouts() ) op = initial_query_op(session) tracer.trace_operation(op, context, op_name: 'listIndexes') do cursor = read_with_retry_cursor(session, ServerSelector.primary, self, context: context) do |server| send_initial_query(op, server, session, context) end if block_given? cursor.each do |doc| yield doc end else cursor.to_enum end end end |
#get(keys_or_name) ⇒ Hash
Convenience method for getting index information by a specific name or spec.
268 269 270 271 272 |
# File 'lib/mongo/index/view.rb', line 268 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).
345 346 347 348 349 350 351 352 353 |
# File 'lib/mongo/index/view.rb', line 345 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.
337 338 339 |
# File 'lib/mongo/index/view.rb', line 337 def timeout_ms operation_timeout_ms || collection.timeout_ms end |