Module: Aerospike::Peers::Parse

Defined in:
lib/aerospike/peers/parse.rb

Overview

Parse the response from peers command

Defined Under Namespace

Classes: Object

Constant Summary collapse

BASE_REGEX =
/(\d+),(\d*),\[(.*)\]/.freeze

Class Method Summary collapse

Class Method Details

.call(response) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/aerospike/peers/parse.rb', line 31

def call(response)
  gen, port, peers = parse_base(response)

  ::Aerospike::Peers::Parse::Object.new.tap do |obj|
    obj.generation = gen.to_i
    obj.port_default = port.empty? ? nil : port.to_i
    obj.peers = parse_peers(peers, obj)
  end
end

.parse_base(response) ⇒ Object



41
42
43
44
45
46
# File 'lib/aerospike/peers/parse.rb', line 41

def parse_base(response)
 BASE_REGEX.match(response).to_a.last(3).tap do |parsed|
  # Expect three pieces parsed from the Regex
  raise ::Aerospike::Exceptions::Parse if parsed.size != 3
 end
end

.parse_host(parser, peer) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/aerospike/peers/parse.rb', line 87

def parse_host(parser, peer)
  if parser.current == '[' # IPv6
    parser.step
    host = parser.read_until(']')
    # skip one extra if port is detected, to match read behavior below
    parser.step if parser.current == ':'
  else
    host = parser.read_until(',', ':', ']')
  end

  port = parser.prev == ':' ? parser.read_until(',', ']').to_i : nil

  if parser.prev == ',' || parser.prev == ']'
    return ::Aerospike::Host.new(host, port, peer.tls_name)
  end

  raise ::Aerospike::Exceptions::Parse, "Unterminated host in response"
end

.parse_hosts(parser, peer) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/aerospike/peers/parse.rb', line 74

def parse_hosts(parser, peer)
  result = []
  parser.expect('[')
  return result if parser.current == ']'

  loop do
    result << parse_host(parser, peer)
    break if parser.current == ']'
  end

  result
end

.parse_peer(parser, obj) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/aerospike/peers/parse.rb', line 60

def parse_peer(parser, obj)
  ::Aerospike::Peer.new.tap do |peer|
    parser.expect('[')
    peer.node_name = parser.read_until(',')
    peer.tls_name = parser.read_until(',')
    peer.hosts = parse_hosts(parser, peer)
    # Assign default port if missing
    peer.hosts.each do |host|
      host.port ||= obj.port_default
    end
    parser.expect(']')
  end
end

.parse_peers(response, obj) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/aerospike/peers/parse.rb', line 48

def parse_peers(response, obj)
  return [] if response.empty?
  parser = ::Aerospike::Utils::StringParser.new(response)
  [].tap do |result|
    loop do
      result << parse_peer(parser, obj)
      break unless parser.current == ','
      parser.step
    end
  end
end