class Mongo::Protocol::Serializers::BitVector

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

Defines serialization strategy upon initialization.

@api private

Public Class Methods

new(layout) click to toggle source

Initializes a BitVector with a layout

@param layout [ Array<Symbol> ] the array of fields in the bit vector

# File lib/mongo/protocol/bit_vector.rb, line 28
def initialize(layout)
  @masks = {}
  layout.each_with_index do |field, index|
    @masks[field] = 2**index if field
  end
end

Public Instance Methods

deserialize(buffer) click to toggle source

Deserializes vector by decoding the symbol according to its mask

@param [ String ] buffer Buffer containing the vector to be deserialized.

@return [ Array<Symbol> ] Flags contained in the vector

# File lib/mongo/protocol/bit_vector.rb, line 52
def deserialize(buffer)
  vector = buffer.get_int32
  flags = []
  @masks.each do |flag, mask|
    flags << flag if mask & vector != 0
  end
  flags
end
serialize(buffer, value, validating_keys = BSON::Config.validating_keys?) click to toggle source

Serializes vector by encoding each symbol according to its mask

@param buffer [ String ] Buffer to receive the serialized vector @param value [ Array<Symbol> ] Array of flags to encode

@return [ String ] Buffer that received the serialized vector

# File lib/mongo/protocol/bit_vector.rb, line 41
def serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
  bits = 0
  value.each { |flag| bits |= (@masks[flag] || 0) }
  buffer.put_int32(bits)
end