Class: Neo4j::Core::PackStream::Packer

Inherits:
Object
  • Object
show all
Defined in:
lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb

Overview

Object which holds a Ruby object and can pack it into a PackStream stream

Constant Summary collapse

INT_HEADERS =

Range Minimum | Range Maximum | Representation | Byte |

|============================|================|======|
-9 223 372 036 854 775 808 |             -2 147 483 649 | INT_64         | CB   |
            -2 147 483 648 |                    -32 769 | INT_32         | CA   |
                   -32 768 |                       -129 | INT_16         | C9   |
                      -128 |                        -17 | INT_8          | C8   |
                       -16 |                       +127 | TINY_INT       | N/A  |
                      +128 |                    +32 767 | INT_16         | C9   |
                   +32 768 |             +2 147 483 647 | INT_32         | CA   |
            +2 147 483 648 | +9 223 372 036 854 775 807 | INT_64         | CB   |

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Packer

Returns a new instance of Packer.



57
58
59
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 57

def initialize(object)
  @object = object
end

Class Method Details

.pack_arguments(*objects) ⇒ Object



143
144
145
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 143

def self.pack_arguments(*objects)
  objects.map { |o| new(o).packed_stream }.join
end

Instance Method Details

#array_streamObject



122
123
124
125
126
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 122

def array_stream
  marker_string(0x90, 0xD4, @object.size) + @object.map do |e|
    Packer.new(e).packed_stream
  end.join
end

#float_streamObject



103
104
105
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 103

def float_stream
  MARKER_HEADERS[:float][64] + [@object].pack('G').force_encoding(Encoding::BINARY)
end

#hash_streamObject



135
136
137
138
139
140
141
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 135

def hash_stream
  marker_string(0xA0, 0xD8, @object.size) +
    @object.map do |key, value|
      Packer.new(key).packed_stream +
        Packer.new(value).packed_stream
    end.join
end

#integer_streamObject Also known as: fixnum_stream, bignum_stream



85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 85

def integer_stream
  case @object
  when -0x10...0x80 # TINY_INT
    pack_integer_object_as_string
  when -0x80...-0x10 # INT_8
    INT_HEADERS[8] + pack_integer_object_as_string
  when -0x8000...0x8000 # INT_16
    INT_HEADERS[16] + pack_integer_object_as_string(2)
  when -0x80000000...0x80000000 # INT_32
    INT_HEADERS[32] + pack_integer_object_as_string(4)
  when -0x8000000000000000...0x8000000000000000 # INT_64
    INT_HEADERS[64] + pack_integer_object_as_string(8)
  end
end

#packed_streamObject



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 61

def packed_stream
  if byte = MARKER_BYTES[@object]
    pack_array_as_string([byte])
  else
    case @object
    when Date, Time, DateTime then string_stream
    when Integer, Float, String, Symbol, Array, Structure, Hash
      send(@object.class.name.split('::').last.downcase + '_stream')
    end
  end
end

#string_streamObject Also known as: symbol_stream

Marker | Size | Maximum size

|=============================================|=====================
80..8F | contained within low-order nibble of marker | 15 bytes
D0     | 8-bit big-endian unsigned integer           | 255 bytes
D1     | 16-bit big-endian unsigned integer          | 65 535 bytes
D2     | 32-bit big-endian unsigned integer          | 4 294 967 295 bytes


114
115
116
117
118
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 114

def string_stream
  s = @object.to_s
  s = s.dup if s.frozen?
  marker_string(0x80, 0xD0, @object.to_s.bytesize) + s.force_encoding(Encoding::BINARY)
end

#structure_streamObject



128
129
130
131
132
133
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 128

def structure_stream
  fail 'Structure too big' if @object.list.size > 65_535
  marker_string(0xB0, 0xDC, @object.list.size) + [@object.signature].pack('C') + @object.list.map do |e|
    Packer.new(e).packed_stream
  end.join
end