Class: Couchbase::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/cluster.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Cluster

Establish connection to the cluster for administration

Parameters:

  • options (Hash)

    The connection parameter

Options Hash (options):

  • :username (String)

    The username

  • :password (String)

    The password

  • :pool (String) — default: "default"

    The pool name

  • :hostname (String) — default: "localhost"

    The hostname

  • :port (String) — default: 8091

    The port



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/couchbase/cluster.rb', line 37

def initialize(options)
  if options[:username].nil? || options[:password].nil?
    raise ArgumentError, "username and password mandatory to connect to the cluster"
  end

  options = {
    hostname: 'localhost',
    port:     8091
  }.merge(options)

  cluster_uri = "http://#{options[:hostname]}:#{options[:port]}"

  uri_list = Array(URI.new(cluster_uri))
  @manager = Java::ComCouchbaseClient::ClusterManager.new(uri_list, options[:username], options[:password])
end

Class Method Details

.manage(cluster_uri, username, password, &block) ⇒ Object



108
109
110
111
112
113
# File 'lib/couchbase/cluster.rb', line 108

def self.manage(cluster_uri, username, password, &block)
  manager = new(cluster_uri, username, password)
  yield manager
ensure
  manager.shutdown
end

Instance Method Details

#create_bucket(name, options = {}) ⇒ Object

Create data bucket

Parameters:

  • name (String)

    The name of the bucket

  • options (Hash) (defaults to: {})

    The bucket parameters

Options Hash (options):

  • :bucket_type (String) — default: "couchbase"

    The type of the bucket. Possible values are “memcached” and “couchbase”.

  • :ram_quota (Fixnum) — default: 100

    The RAM quota in megabytes.

  • :replica_number (Fixnum) — default: 1

    The number of replicas of each document

  • :auth_type (String) — default: "sasl"

    The authentication type. Possible values are “sasl” and “none”. Note you should specify free port for “none”

  • :proxy_port (Fixnum)

    The port for moxi



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/couchbase/cluster.rb', line 79

def create_bucket(name, options = {})
  ram_quota   = options[:ram_quota] || 100
  replicas    = options[:replica_number] || 0
  flush       = options.fetch(:flush) { true }
  password    = options[:password]
  proxy_port  = options[:proxy_port]
  auth_type   = options[:auth_type] || 'sasl'
  bucket_type = options[:bucket_type] == 'memcached' ? BucketType::MEMCACHED : BucketType::COUCHBASE

  if name == 'default'
    @manager.createDefaultBucket(bucket_type, ram_quota, replicas, flush)
  elsif auth == 'sasl'
    @manager.createPortBucket(bucket_type, name, ram_quota, replicas, proxy_port, flush)
  else
    @manager.createNamedBucket(bucket_type, name, ram_quota, replicas, password, flush)
  end
  true
rescue Java::JavaLang::RuntimeException => e
  raise ClusterError, e
end

#delete_bucket(bucket) ⇒ Object

Delete the data bucket

Parameters:

  • name (String)

    The name of the bucket

  • options (Hash)


62
63
64
# File 'lib/couchbase/cluster.rb', line 62

def delete_bucket(bucket)
  @manager.deleteBucket(bucket)
end

#flush_bucket(bucket) ⇒ Object



100
101
102
# File 'lib/couchbase/cluster.rb', line 100

def flush_bucket(bucket)
  @manager.flushBucket(bucket) == FlushResponse::OK
end

#list_bucketsObject

List available buckets



54
55
56
# File 'lib/couchbase/cluster.rb', line 54

def list_buckets
  @manager.listBuckets
end

#update_bucket(name, options) ⇒ Object



104
105
106
# File 'lib/couchbase/cluster.rb', line 104

def update_bucket(name, options)
  # implement
end