Sha256: ef6f9116a41970ee934904bf18758ccd64359e61e858c71120be9e1f069f765b

Contents?: true

Size: 1.82 KB

Versions: 18

Compression:

Stored size: 1.82 KB

Contents

require 'couchbase'
include Couchbase

def measure(msg)
  start = Time.now
  yield
  printf "%s in %.2f seconds\n", msg, Time.now - start
end

bucket_name = "travel-sample"
scope_name = "myapp"

options = Cluster::ClusterOptions.new
options.authenticate("Administrator", "password")
cluster = Cluster.connect("couchbase://localhost", options)
bucket = cluster.bucket(bucket_name)


scopes = bucket.collections.get_all_scopes
puts "There are #{scopes.size} on the bucket \"#{bucket_name}\""

scopes.each do |scope|
  puts "  * \"#{scope.name}\" (#{scope.collections.size})"
  scope.collections.each do |collection|
    puts "    * \"#{collection.name}\""
  end
end

if scopes.any? {|scope| scope.name == scope_name }
  measure("Scope \"#{scope_name}\" has been removed") { bucket.collections.drop_scope(scope_name) }
end
measure("Scope \"#{scope_name}\" has been created") { bucket.collections.create_scope(scope_name) }

scope = bucket.collections.get_scope(scope_name)
puts "Scope \"#{scope_name}\" has #{scope.collections.size} collections"

collection = Management::CollectionSpec.new
collection.scope_name = scope_name
collection.name = "users"
measure("Collection \"#{collection.name}\" on scope \"#{collection.scope_name}\" has been created") do
  bucket.collections.create_collection(collection)
end

scope = bucket.collections.get_scope(scope_name)
puts "Scope \"#{scope_name}\" has #{scope.collections.size} collections"
scope.collections.each do |collection|
  puts "  * \"#{collection.name}\""
end

measure("Collection \"#{collection.name}\" on scope \"#{collection.scope_name}\" has been dropped") do
  bucket.collections.drop_collection(collection)
end

scope = bucket.collections.get_scope(scope_name)
puts "Scope \"#{scope_name}\" has #{scope.collections.size} collections"
scope.collections.each do |collection|
  puts "  * \"#{collection.name}\""
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
couchbase-3.0.0.beta.1-universal-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.beta.1 examples/managing_collections.rb
couchbase-3.0.0.alpha.5-x86_64-linux examples/managing_collections.rb
couchbase-3.0.0.alpha.5-universal-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.5-x86_64-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.5 examples/managing_collections.rb
couchbase-3.0.0.alpha.4-x86_64-linux examples/managing_collections.rb
couchbase-3.0.0.alpha.4-x86_64-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.4-universal-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.4 examples/managing_collections.rb
couchbase-3.0.0.alpha.3-x86_64-linux examples/managing_collections.rb
couchbase-3.0.0.alpha.3-x86_64-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.3-universal-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.3 examples/managing_collections.rb
couchbase-3.0.0.alpha.2-x86_64-linux examples/managing_collections.rb
couchbase-3.0.0.alpha.2-x86_64-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.2-universal-darwin-19 examples/managing_collections.rb
couchbase-3.0.0.alpha.2 examples/managing_collections.rb