Class: Couchbase::Bucket

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/bucket.rb,
lib/couchbase/view_options.rb

Overview

Provides access to a Couchbase bucket APIs

Defined Under Namespace

Classes: ViewMetaData, ViewResult, ViewRow

Constant Summary collapse

PingOptions =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TODO: deprecate in 3.1

::Couchbase::Options::Ping
ViewOptions =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

TODO: deprecate in 3.1

::Couchbase::Options::View

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(backend, name) ⇒ Bucket

Returns a new instance of Bucket.

Parameters:

  • backend (Couchbase::Backend)


31
32
33
34
35
# File 'lib/couchbase/bucket.rb', line 31

def initialize(backend, name)
  backend.open_bucket(name, true)
  @backend = backend
  @name = name
end

Instance Attribute Details

#nameString (readonly)

Returns name of the bucket.

Returns:

  • (String)

    name of the bucket



26
27
28
# File 'lib/couchbase/bucket.rb', line 26

def name
  @name
end

Instance Method Details

#collection(collection_name) ⇒ Collection

Opens the named collection in the default scope of the bucket

Parameters:

  • collection_name (String)

    name of the collection

Returns:



58
59
60
# File 'lib/couchbase/bucket.rb', line 58

def collection(collection_name)
  default_scope.collection(collection_name)
end

#collectionsManagement::CollectionManager



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

def collections
  Management::CollectionManager.new(@backend, @name)
end

#default_collectionCollection

Opens the default collection for this bucket

Returns:



65
66
67
# File 'lib/couchbase/bucket.rb', line 65

def default_collection
  Collection.new(@backend, @name, "_default", "_default")
end

#default_scopeScope

Get default scope

Returns:



40
41
42
# File 'lib/couchbase/bucket.rb', line 40

def default_scope
  Scope.new(@backend, @name, "_default")
end

#ping(options = Options::Ping::DEFAULT) ⇒ PingResult

Performs application-level ping requests against services in the couchbase cluster

Parameters:

Returns:



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/couchbase/bucket.rb', line 115

def ping(options = Options::Ping::DEFAULT)
  resp = @backend.ping(@name, options.to_backend)
  PingResult.new do |res|
    res.version = resp[:version]
    res.id = resp[:id]
    res.sdk = resp[:sdk]
    resp[:services].each do |type, svcs|
      res.services[type] = svcs.map do |svc|
        PingResult::ServiceInfo.new do |info|
          info.id = svc[:id]
          info.state = svc[:state]
          info.latency = svc[:latency]
          info.remote = svc[:remote]
          info.local = svc[:local]
          info.error = svc[:error]
        end
      end
    end
  end
end

#scope(scope_name) ⇒ Scope

Get a named scope

Parameters:

  • scope_name (String)

    name of the scope

Returns:



49
50
51
# File 'lib/couchbase/bucket.rb', line 49

def scope(scope_name)
  Scope.new(@backend, @name, scope_name)
end

#view_indexesManagement::ViewIndexManager



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

def view_indexes
  Management::ViewIndexManager.new(@backend, @name)
end

#view_query(design_document_name, view_name, options = Options::View::DEFAULT) ⇒ ViewResult

Performs query to view index.

Examples:

Make sure the view engine catch up with all mutations and return keys starting from [“random_brewery:”]

bucket.view_query("beer", "brewery_beers",
                  Options::View(
                    start_key: ["random_brewery:"],
                    scan_consistency: :request_plus
                  ))

Parameters:

  • design_document_name (String)

    name of the design document

  • view_name (String)

    name of the view to query

  • options (Options::View) (defaults to: Options::View::DEFAULT)

Returns:



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

def view_query(design_document_name, view_name, options = Options::View::DEFAULT)
  resp = @backend.document_view(@name, design_document_name, view_name, options.namespace, options.to_backend)
  ViewResult.new do |res|
    res. = ViewMetaData.new do |meta|
      meta.total_rows = resp[:meta][:total_rows]
      meta.debug_info = resp[:meta][:debug_info]
    end
    res.rows = resp[:rows].map do |entry|
      ViewRow.new do |row|
        row.id = entry[:id] if entry.key?(:id)
        row.key = JSON.parse(entry[:key])
        row.value = JSON.parse(entry[:value])
      end
    end
  end
end