Class: EventMachine::Protocols::Couchbase::ConfigurationListener

Inherits:
Object
  • Object
show all
Defined in:
lib/em-couchbase/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfigurationListener

Returns a new instance of ConfigurationListener.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/em-couchbase/configuration.rb', line 47

def initialize
  @parser = Yajl::Parser.new
  @parser.on_parse_complete = lambda do |object|
    if @on_upgrade
      config = build_config(object)
      if config
        @on_upgrade.call(config)
      end
    end
  end
end

Instance Attribute Details

#config_streamObject (readonly)

Returns the value of attribute config_stream.



44
45
46
# File 'lib/em-couchbase/configuration.rb', line 44

def config_stream
  @config_stream
end

#optionsObject (readonly)

Returns the value of attribute options.



45
46
47
# File 'lib/em-couchbase/configuration.rb', line 45

def options
  @options
end

Instance Method Details

#build_config(json) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/em-couchbase/configuration.rb', line 95

def build_config(json)
  return unless json.is_a?(Hash)

  bucket_name = json.fetch("name")
  bucket_type = json.fetch("bucketType")
  sasl_password = json.fetch("saslPassword")
  node_locator = json.fetch("nodeLocator")
  if node_locator == "vbucket"
    server_map = json.fetch("vBucketServerMap")
    num_replicas = server_map.fetch("numReplicas")
    hash_algorithm = server_map.fetch("hashAlgorithm")
    vbucket_map = server_map.fetch("vBucketMap")
    vbucket_map_forward = server_map["vBucketMapForward"]
  end
  nodes = json.fetch("nodes")
  if nodes.empty?
    raise ArgumentError, "empty list of nodes"
  end
  nodes = nodes.map do |node|
    admin = node.fetch("hostname")
    ports = node.fetch("ports")
    {
      :admin => admin,
      :proxy => admin.sub(/:\d+$/, ":#{ports.fetch("proxy")}"),
      :direct => admin.sub(/:\d+$/, ":#{ports.fetch("direct")}"),
      :couch => node["couchApiBase"], # nil for 1.8.x series
      :status => node.fetch("status"),
      :version => node.fetch("version")
    }
  end.sort_by{|node| node[:direct]}
  Configuration.new(:bucket_name => bucket_name,
                    :bucket_type => bucket_type,
                    :sasl_password => sasl_password,
                    :node_locator => node_locator,
                    :num_replicas => num_replicas,
                    :hash_algorithm => hash_algorithm,
                    :vbucket_map => vbucket_map,
                    :vbucket_map_forward => vbucket_map_forward,
                    :nodes => nodes)
end

#listen(options = {}) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/em-couchbase/configuration.rb', line 70

def listen(options = {})
  @options = {
    :hostname => "localhost",
    :port => 8091,
    :pool => "default",
    :bucket => "default"
  }.merge(options)
  params = {:head => {}}
  (username, password) = auth = @options.values_at(:username, :password)
  if username && password
    params[:head][:authorization] = auth
  end
  uri = sprintf("http://%s:%d/pools/%s/bucketsStreaming/%s/",
                *@options.values_at(:hostname, :port, :pool, :bucket))
  @config_stream = EM::HttpRequest.new(URI.parse(uri),
                                       :inactivity_timeout => 0).get params
  @config_stream.errback do |http|
    @on_error.call(self, http.error) if @on_error && http.error
  end
  @config_stream.stream do |chunk|
    @parser << chunk
  end
  @config_stream
end

#on_error(&callback) ⇒ Object



63
64
65
66
67
68
# File 'lib/em-couchbase/configuration.rb', line 63

def on_error(&callback)
  @on_error = callback
  if @config_stream
    @config_stream.errback(&callback)
  end
end

#on_upgrade(&callback) ⇒ Object



59
60
61
# File 'lib/em-couchbase/configuration.rb', line 59

def on_upgrade(&callback)
  @on_upgrade = callback
end