Class: Mongo::Server::AppMetadata::Truncator Private
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.
Constant Summary
-
MAX_DOCUMENT_SIZE =
The max application metadata document byte size.
512
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
- #document ⇒ BSON::Document readonly Internal use only
-
#ok? ⇒ true | false
readonly
Internal use only
Whether the document fits within the required maximum document size.
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
#document ⇒ BSON::Document
(readonly)
# 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.
# File 'lib/mongo/server/app_metadata/truncator.rb', line 56
def ok? size <= MAX_DOCUMENT_SIZE end
Instance Method Details
#excess ⇒ Integer
(private)
How many extra bytes must be trimmed before the document may be considered #ok?.
# File 'lib/mongo/server/app_metadata/truncator.rb', line 67
def excess size - MAX_DOCUMENT_SIZE end
#size ⇒ Integer
The current size of the document, in bytes, as a serialized BSON document.
# 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).
# 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.
# 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.
# 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)
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.
# 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.
# 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.
# 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)
the parameter is modified in-place.
A helper method for truncating a string (in-place) by whatever {#excess
} is required.