123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Shardable

Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Included In:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ActiveSupport::Concern
Defined in: lib/mongoid/shardable.rb

Overview

This module contains behavior for adding shard key fields to updates.

Instance Method Summary

DSL Calls

included

[ GitHub ]


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/mongoid/shardable.rb', line 10

included do
  # Returns the list of shard key fields, if shard key was declared on
  # this model. If no shard key was declared, returns an empty array.
  #
  # @return [ Array<Symbol> ] List of shard key fields.
  # @api public
  cattr_accessor :shard_key_fields
  self.shard_key_fields = []

  # Returns the shard configuration, which is a hash with the following
  # (symbol) keys:
  #
  # - keys: A hash mapping (symbol) field names to values, defining the
  #   shard key. Values can be either the integer 1 for ranged sharding
  #   or the string "hashed" for hashed sharding.
  # - options: A hash containing options for shardCollections command.
  #
  # If shard key was not declared via the +shard_key+ macro, +shard_config+
  # attribute is nil.
  #
  # @example Get the shard configuration.
  #   Model.shard_config
  #   # => {key: {foo: 1, bar: 1}, options: {unique: true}}
  #
  # @return [ Hash | nil ] Shard configuration.
  # @api public
  cattr_accessor :shard_config
end

Instance Method Details

#shard_key_field_value(field, prefer_persisted:) ⇒ Object

This method is for internal use only.

Returns the value for the named shard key. If the field identifies an embedded document, the key will be parsed and recursively evaluated. If ‘prefer_persisted` is true, the value last persisted to the database will be returned, regardless of what the current value of the attribute may be.

Parameters:

  • field (String)

    The name of the field to evaluate

  • prefer_persisted (true|false)

    Whether or not to prefer the persisted value over the current value.

Returns:

  • (Object)

    The value of the named field.

[ GitHub ]

  
# File 'lib/mongoid/shardable.rb', line 96

def shard_key_field_value(field, prefer_persisted:)
  if field.include?(".")
    relation, remaining = field.split(".", 2)
    send(relation)&.shard_key_field_value(remaining, prefer_persisted: prefer_persisted)
  elsif prefer_persisted && !new_record?
    attribute_was(field)
  else
    send(field)
  end
end

#shard_key_fieldsArray<String>

Note:

Refactored from using delegate for class load performance.

Get the shard key fields.

Examples:

Get the shard key fields.

model.shard_key_fields

Returns:

[ GitHub ]

  
# File 'lib/mongoid/shardable.rb', line 47

def shard_key_fields
  self.class.shard_key_fields
end

#shard_key_selector(prefer_persisted: false) ⇒ Hash

This method is for internal use only.

Returns the selector that would match the defined shard keys. If ‘prefer_persisted` is false (the default), it uses the current values of the specified shard keys, otherwise, it will try to use whatever value was most recently persisted.

Parameters:

  • prefer_persisted (true | false)

    Whether to use the current value of the shard key fields, or to use their most recently persisted values.

Returns:

  • (Hash)

    The shard key selector.

[ GitHub ]

  
# File 'lib/mongoid/shardable.rb', line 63

def shard_key_selector(prefer_persisted: false)
  shard_key_fields.each_with_object({}) do |field, selector|
    selector[field.to_s] = shard_key_field_value(field.to_s, prefer_persisted: prefer_persisted)
  end
end

#shard_key_selector_in_dbHash

This method is for internal use only.

Returns the selector that would match the existing version of this document in the database.

If the document is not persisted, this method uses the current values of the shard key fields. If the document is persisted, this method uses the values retrieved from the database.

Returns:

  • (Hash)

    The shard key selector.

[ GitHub ]

  
# File 'lib/mongoid/shardable.rb', line 79

def shard_key_selector_in_db
  shard_key_selector(prefer_persisted: true)
end