123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Protocol::Serializers::BitVector Private

Relationships & Source Files
Inherits: Object
Defined in: lib/mongo/protocol/bit_vector.rb

Overview

Class used to define a bitvector for a MongoDB wire protocol message.

Defines serialization strategy upon initialization.

Class Method Summary

Instance Method Summary

Constructor Details

.new(layout) ⇒ BitVector

Initializes a BitVector with a layout

Parameters:

  • layout (Array<Symbol>)

    the array of fields in the bit vector

[ GitHub ]

  
# File 'lib/mongo/protocol/bit_vector.rb', line 29

def initialize(layout)
  @masks = {}
  layout.each_with_index do |field, index|
    @masks[field] = 2**index if field
  end
end

Instance Method Details

#deserialize(buffer, _options = {}) ⇒ Array<Symbol>

Deserializes vector by decoding the symbol according to its mask

Parameters:

  • buffer (String)

    Buffer containing the vector to be deserialized.

  • options (Hash)

    This method does not currently accept any options.

Returns:

  • (Array<Symbol>)

    Flags contained in the vector

[ GitHub ]

  
# File 'lib/mongo/protocol/bit_vector.rb', line 56

def deserialize(buffer, _options = {})
  vector = buffer.get_int32
  flags = []
  @masks.each do |flag, mask|
    flags << flag if mask & vector != 0
  end
  flags
end

#serialize(buffer, value, _validating_keys = nil) ⇒ String

Serializes vector by encoding each symbol according to its mask

Parameters:

  • buffer (String)

    Buffer to receive the serialized vector

  • value (Array<Symbol>)

    Array of flags to encode

  • validating_keys (true, false)

    Whether keys should be validated when serializing. This option is deprecated and will not be used. It will removed in version 3.0.

Returns:

  • (String)

    Buffer that received the serialized vector

[ GitHub ]

  
# File 'lib/mongo/protocol/bit_vector.rb', line 44

def serialize(buffer, value, _validating_keys = nil)
  bits = 0
  value.each { |flag| bits |= @masks[flag] || 0 }
  buffer.put_int32(bits)
end