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

Inherits:
Object
  • Object
show all
Defined in:
build/ruby-driver-master/lib/mongo/protocol/bit_vector.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

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

Defines serialization strategy upon initialization.

Instance Method Summary collapse

Constructor Details

#initialize(layout) ⇒ BitVector

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Initializes a BitVector with a layout

Parameters:

  • layout (Array<Symbol>)

    the array of fields in the bit vector



31
32
33
34
35
36
# File 'build/ruby-driver-master/lib/mongo/protocol/bit_vector.rb', line 31

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>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Deserializes vector by decoding the symbol according to its mask

Parameters:

  • buffer (String)

    Buffer containing the vector to be deserialized.

  • options (Hash) (defaults to: {})

    This method does not currently accept any options.

Returns:

  • (Array<Symbol>)

    Flags contained in the vector



56
57
58
59
60
61
62
63
# File 'build/ruby-driver-master/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 = BSON::Config.validating_keys?) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

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

Returns:

  • (String)

    Buffer that received the serialized vector



44
45
46
47
48
# File 'build/ruby-driver-master/lib/mongo/protocol/bit_vector.rb', line 44

def serialize(buffer, value, validating_keys = BSON::Config.validating_keys?)
  bits = 0
  value.each { |flag| bits |= (@masks[flag] || 0) }
  buffer.put_int32(bits)
end