Module: Couchbase

Defined in:
lib/couchbase.rb,
lib/couchbase/view.rb,
lib/couchbase/async.rb,
lib/couchbase/error.rb,
lib/couchbase/query.rb,
lib/couchbase/utils.rb,
lib/couchbase/bucket.rb,
lib/couchbase/result.rb,
lib/couchbase/cluster.rb,
lib/couchbase/version.rb,
lib/couchbase/view_row.rb,
lib/couchbase/constants.rb,
lib/couchbase/design_doc.rb,
lib/couchbase/operations.rb,
lib/couchbase/transcoder.rb,
lib/couchbase/async/queue.rb,
lib/couchbase/async/callback.rb

Overview

Author

Mike Evans <[email protected]>

Copyright

2013 Urlgonomics LLC.

License

Apache License, Version 2.0

Licensed under the Apache License, Version 2.0 (the “License”); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an “AS IS” BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Defined Under Namespace

Modules: Async, Constants, Error, Operations, Transcoder Classes: Bucket, Cluster, ClusterError, DesignDoc, Query, Result, Utils, View, ViewRow

Constant Summary collapse

VERSION =
'0.2.2'
@@buckets =
ThreadSafe::Cache.new
@@connections =
ThreadSafe::Array.new

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.connection_optionsHash, String

Default connection options

Examples:

Using connection_options to change the bucket

Couchbase.connection_options = {:bucket => 'blog'}
Couchbase.bucket.name     #=> "blog"

Returns:

Since:

  • 1.1.0



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

def connection_options
  @connection_options
end

Class Method Details

.bucket(name = nil) ⇒ Bucket

The connection instance for current thread

Examples:

Couchbase.bucket.set("foo", "bar")

Set connection options using Hash

Couchbase.connection_options = {:node_list => ["example.com:8091"]}
Couchbase.bucket("slot1").set("foo", "bar")
Couchbase.bucket("slot1").bucket #=> "default"
Couchbase.connection_options[:bucket] = "test"
Couchbase.bucket("slot2").bucket #=> "test"

Set connection options using URI

Couchbase.connection_options = "http://example.com:8091/pools"
Couchbase.bucket("slot1").set("foo", "bar")
Couchbase.bucket("slot1").bucket #=> "default"
Couchbase.connection_options = "http://example.com:8091/pools/buckets/test"
Couchbase.bucket("slot2").bucket #=> "test"

Use named slots to keep a connection

Couchbase.connection_options = {
  :node_list => ["example.com", "example.org"],
  :bucket => "users"
}
Couchbase.bucket("users").set("john", {"balance" => 0})
Couchbase.connection_options[:bucket] = "orders"
Couchbase.bucket("other").set("john:1", {"products" => [42, 66]})

Returns:

See Also:

Since:

  • 1.1.0



144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/couchbase.rb', line 144

def bucket(name = nil)
  name ||= case @connection_options
           when Hash
             @connection_options[:bucket]
           when String
             path = URI.parse(@connection_options).path
             path[%r(^(/pools/([A-Za-z0-9_.-]+)(/buckets/([A-Za-z0-9_.-]+))?)?), 3] || 'default'
           else
             'default'
           end

  @@buckets[name] ||= connect(connection_options)
end

.bucket=(connection) ⇒ Bucket Also known as: set_bucket

Set a connection instance for current thread

Returns:

Since:

  • 1.1.0



163
164
165
166
# File 'lib/couchbase.rb', line 163

def bucket=(connection)
  name = @connection_options && @connection_options[:bucket] || "default"
  @@buckets[name] = connection
end

.connect(*options) ⇒ Bucket Also known as: new

The method connect initializes new Bucket instance with all arguments passed.

Examples:

Use default values for all options

Couchbase.connect

Establish connection with couchbase default pool and default bucket

Couchbase.connect("http://localhost:8091/pools/default")

Select custom bucket

Couchbase.connect("http://localhost:8091/pools/default", :bucket => 'blog')

Specify bucket credentials

Couchbase.connect("http://localhost:8091/pools/default", :bucket => 'blog', :username => 'bucket', :password => 'secret')

Use URL notation

Couchbase.connect("http://bucket:secret@localhost:8091/pools/default/buckets/blog")

Returns:

  • connection instance

See Also:

Since:

  • 1.0.0



85
86
87
88
89
# File 'lib/couchbase.rb', line 85

def connect(*options)
  bucket = Bucket.new(*(options.flatten))
  @@connections << bucket
  bucket
end

.connected?Boolean

Returns:



169
170
171
# File 'lib/couchbase.rb', line 169

def connected?
  !!@@buckets.empty?
end

.disconnectObject



173
174
175
176
177
178
179
180
181
182
# File 'lib/couchbase.rb', line 173

def disconnect
  @@buckets.each_pair do |bucket, connection|
    connection.disconnect if connection.connected?
  end
  @@connections.each do |connection|
    connection.disconnect if connection.connected?
  end
  @@buckets     = ThreadSafe::Cache.new
  @@connections = ThreadSafe::Array.new
end

.normalize_connection_options(options) ⇒ Object



107
108
109
# File 'lib/couchbase.rb', line 107

def normalize_connection_options(options)
  Hash[ options.map { |k, v| [k.to_sym, v] } ]
end