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   |
MARKER_HEADERS[:int]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object) ⇒ Packer

Returns a new instance of Packer


60
61
62
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 60

def initialize(object)
  @object = object
end

Class Method Details

.pack_arguments(*objects) ⇒ Object


148
149
150
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 148

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

Instance Method Details

#array_streamObject Also known as: set_stream


125
126
127
128
129
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 125

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

#float_streamObject


106
107
108
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 106

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

#hash_streamObject


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

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


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

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


64
65
66
67
68
69
70
71
72
73
74
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 64

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, Set, 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

117
118
119
120
121
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 117

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


133
134
135
136
137
138
# File 'lib/neo4j/core/cypher_session/adaptors/bolt/pack_stream.rb', line 133

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