Class: Neo4j::Core::CypherSession::Adaptors::Bolt::Message

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

Overview

Represents messages sent to or received from the server

Constant Summary collapse

TYPE_CODES =
{
  # client message types
  init: 0x01,             # 0000 0001 // INIT <user_agent>
  ack_failure: 0x0E,      # 0000 1110 // ACK_FAILURE
  reset: 0x0F,            # 0000 1111 // RESET
  run: 0x10,              # 0001 0000 // RUN <statement> <parameters>
  discard_all: 0x2F,      # 0010 1111 // DISCARD *
  pull_all: 0x3F,         # 0011 1111 // PULL *

  # server message types
  success: 0x70,          # 0111 0000 // SUCCESS <metadata>
  record: 0x71,           # 0111 0001 // RECORD <value>
  ignored: 0x7E,          # 0111 1110 // IGNORED <metadata>
  failure: 0x7F           # 0111 1111 // FAILURE <metadata>
}.freeze
CODE_TYPES =
TYPE_CODES.invert

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_or_code, *args) ⇒ Message

Returns a new instance of Message.



229
230
231
232
233
234
235
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 229

def initialize(type_or_code, *args)
  @type_code = Message.type_code_for(type_or_code)
  fail "Invalid message type: #{@type_code.inspect}" if !@type_code
  @type = CODE_TYPES[@type_code]

  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



263
264
265
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 263

def args
  @args
end

#typeObject (readonly)

Returns the value of attribute type.



263
264
265
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 263

def type
  @type
end

Class Method Details

.interpret_field_value(value) ⇒ Object



269
270
271
272
273
274
275
276
277
278
279
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 269

def self.interpret_field_value(value)
  if value.is_a?(Array) && (1..3).cover?(value[0])
    case value[0]
    when 1
      {type: :node, identity: value[1],
       labels: value[2], properties: value[3]}
    end
  else
    value
  end
end

.type_code_for(type_or_code) ⇒ Object



265
266
267
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 265

def self.type_code_for(type_or_code)
  type_or_code.is_a?(Integer) ? type_or_code : TYPE_CODES[type_or_code]
end

Instance Method Details

#packed_streamObject



245
246
247
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 245

def packed_stream
  PackStream::Packer.new(struct).packed_stream
end

#structObject



237
238
239
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 237

def struct
  PackStream::Structure.new(@type_code, @args)
end

#to_sObject



241
242
243
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 241

def to_s
  "#{ANSI::GREEN}#{@type.to_s.upcase}#{ANSI::CLEAR} #{@args.inspect if !@args.empty?}"
end

#valueObject



249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/neo4j/core/cypher_session/adaptors/bolt.rb', line 249

def value
  return if @type != :record
  @args.map do |arg|
    # Assuming result is Record
    field_names = arg[1]

    field_values = arg[2].map do |field_value|
      Message.interpret_field_value(field_value)
    end

    Hash[field_names.zip(field_values)]
  end
end