123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Server::AppMetadata::Truncator Private

Do not use. This class is for internal use only.
Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/server/app_metadata/truncator.rb

Overview

Implements the metadata truncation logic described in the handshake spec.

Since:

  • 2.0.0

Constant Summary

Class Method Summary

  • .new(document) ⇒ Truncator constructor Internal use only

    Creates a new Truncator instance and tries enforcing the maximum document size on the given document.

Instance Attribute Summary

Instance Method Summary

  • #size ⇒ Integer Internal use only

    The current size of the document, in bytes, as a serialized BSON document.

  • #excess ⇒ Integer private Internal use only

    How many extra bytes must be trimmed before the document may be considered #ok?.

  • #try_truncate! private Internal use only

    Attempt to truncate the document using the documented metadata priorities (see the handshake specification).

  • #try_truncate_env! private Internal use only

    Remove the {:env} key from the document.

  • #try_truncate_env_fields! private Internal use only

    Attempt to truncate the keys in the {:env} subdocument.

  • #try_truncate_hash(hash, reserved: []) private Internal use only

    A helper method for removing the keys of a Hash (in-place) until the document is the necessary size.

  • #try_truncate_os_fields! private Internal use only

    Attempt to truncate the keys in the {:os} subdocument.

  • #try_truncate_platform! private Internal use only

    Attempt to truncate or remove the {:platform} key from the document.

  • #try_truncate_string(string) private Internal use only

    A helper method for truncating a string (in-place) by whatever {#excess} is required.

Instance Attribute Details

#documentBSON::Document (readonly)

Returns:

  • (BSON::Document)

    the document being truncated.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 26

attr_reader :document

#ok?true | false (readonly)

Whether the document fits within the required maximum document size.

Returns:

  • (true | false)

    if the document is okay or not.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 56

def ok?
  size <= MAX_DOCUMENT_SIZE
end

Instance Method Details

#excessInteger (private)

How many extra bytes must be trimmed before the document may be considered #ok?.

Returns:

  • (Integer)

    how many bytes larger the document is than the maximum document size.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 67

def excess
  size - MAX_DOCUMENT_SIZE
end

#sizeInteger

The current size of the document, in bytes, as a serialized BSON document.

Returns:

  • (Integer)

    the size of the document

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 49

def size
  @document.to_bson.to_s.length
end

#try_truncate! (private)

Attempt to truncate the document using the documented metadata priorities (see the handshake specification).

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 73

def try_truncate!
  %i[ env_fields os_fields env platform ].each do |target|
    break if ok?

    send(:"try_truncate_#{target}!")
  end
end

#try_truncate_env! (private)

Remove the {:env} key from the document.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 98

def try_truncate_env!
  @document.delete(:env)
end

#try_truncate_env_fields! (private)

Attempt to truncate the keys in the {:env} subdocument.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 88

def try_truncate_env_fields!
  try_truncate_hash(@document[:env], reserved: %w[ name ])
end

#try_truncate_hash(hash, reserved: []) (private)

Note:

the hash parameter is modified in-place.

A helper method for removing the keys of a Hash (in-place) until the document is the necessary size. The keys are considered in order (using the Hash’s native key ordering), and each will be removed from the hash in turn, until the document is the necessary size.

Any keys in the {reserved} list will be ignored.

Parameters:

  • hash (Hash | nil)

    the Hash instance to consider.

  • reserved (Array)

    the list of keys to ignore in the hash.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 127

def try_truncate_hash(hash, reserved: [])
  return false unless hash

  keys = hash.keys - reserved
  keys.each do |key|
    hash.delete(key)

    return true if ok?
  end

  false
end

#try_truncate_os_fields! (private)

Attempt to truncate the keys in the {:os} subdocument.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 93

def try_truncate_os_fields!
  try_truncate_hash(@document[:os], reserved: %w[ type ])
end

#try_truncate_platform! (private)

Attempt to truncate or remove the {:platform} key from the document.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 83

def try_truncate_platform!
  @document.delete(:platform) unless try_truncate_string(@document[:platform])
end

#try_truncate_string(string) (private)

Note:

the parameter is modified in-place.

A helper method for truncating a string (in-place) by whatever {#excess} is required.

Parameters:

  • string (String)

    the string value to truncate.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/server/app_metadata/truncator.rb', line 108

def try_truncate_string(string)
  length = string&.length || 0

  return false if excess > length

  string[(length - excess)..-1] = ''
end