Class: Couchbase::Cluster

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/cluster.rb,
lib/couchbase/query_options.rb,
lib/couchbase/search_options.rb,
lib/couchbase/analytics_options.rb
more...

Defined Under Namespace

Classes: AnalyticsMetaData, AnalyticsMetrics, AnalyticsOptions, AnalyticsResult, AnalyticsWarning, ClusterOptions, DiagnosticsOptions, QueryMetaData, QueryMetrics, QueryOptions, QueryResult, QueryWarning, SearchFacet, SearchFacetResult, SearchMetaData, SearchMetrics, SearchOptions, SearchQuery, SearchResult, SearchRow, SearchRowLocation, SearchRowLocations, SearchSort

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.connect(connection_string, options) ⇒ Cluster

Connect to the Couchbase cluster

Parameters:

  • connection_string (String)

    connection string used to locate the Couchbase Cluster

  • options (ClusterOptions)

    custom options when creating the cluster connection

Returns:

[View source]

38
39
40
# File 'lib/couchbase/cluster.rb', line 38

def self.connect(connection_string, options)
  Cluster.new(connection_string, options)
end

Instance Method Details

#analytics_indexesManagement::AnalyticsIndexManager

[View source]

278
279
280
# File 'lib/couchbase/cluster.rb', line 278

def analytics_indexes
  Management::AnalyticsIndexManager.new(@backend)
end

#analytics_query(statement, options = AnalyticsOptions.new) ⇒ AnalyticsResult

Performs an analytics query

Parameters:

  • statement (String)

    the N1QL query statement

  • options (AnalyticsOptions) (defaults to: AnalyticsOptions.new)

    the custom options for this query

Returns:

[View source]

115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/couchbase/cluster.rb', line 115

def analytics_query(statement, options = AnalyticsOptions.new)
  resp = @backend.document_analytics(statement, {
      timeout: options.timeout,
      client_context_id: options.client_context_id,
      scan_consistency: options.scan_consistency,
      readonly: options.readonly,
      priority: options.priority,
      positional_parameters: options.export_positional_parameters,
      named_parameters: options.export_named_parameters,
      raw_parameters: options.raw_parameters,
  })

  AnalyticsResult.new do |res|
    res.transcoder = options.transcoder
    res. = AnalyticsMetaData.new do |meta|
      meta.status = resp[:meta][:status]
      meta.request_id = resp[:meta][:request_id]
      meta.client_context_id = resp[:meta][:client_context_id]
      meta.signature = JSON.parse(resp[:meta][:signature]) if resp[:meta][:signature]
      meta.profile = JSON.parse(resp[:meta][:profile]) if resp[:meta][:profile]
      meta.metrics = AnalyticsMetrics.new do |metrics|
        if resp[:meta][:metrics]
          metrics.elapsed_time = resp[:meta][:metrics][:elapsed_time]
          metrics.execution_time = resp[:meta][:metrics][:execution_time]
          metrics.result_count = resp[:meta][:metrics][:result_count]
          metrics.result_size = resp[:meta][:metrics][:result_size]
          metrics.error_count = resp[:meta][:metrics][:error_count]
          metrics.warning_count = resp[:meta][:metrics][:warning_count]
          metrics.processed_objects = resp[:meta][:metrics][:processed_objects]
        end
      end
      res[:warnings] = resp[:warnings].map { |warn| QueryWarning.new(warn[:code], warn[:message]) } if resp[:warnings]
    end
    res.instance_variable_set("@rows", resp[:rows])
  end
end

#bucket(name) ⇒ Bucket

Returns an instance of the Bucket

Parameters:

  • name (String)

    name of the bucket

Returns:

[View source]

47
48
49
# File 'lib/couchbase/cluster.rb', line 47

def bucket(name)
  Bucket.new(@backend, name)
end

#bucketsManagement::BucketManager

[View source]

268
269
270
# File 'lib/couchbase/cluster.rb', line 268

def buckets
  Management::BucketManager.new(@backend)
end

#diagnostics(options = DiagnosticsOptions.new) ⇒ Object

[View source]

291
# File 'lib/couchbase/cluster.rb', line 291

def diagnostics(options = DiagnosticsOptions.new) end

#disconnectObject

[View source]

287
288
289
# File 'lib/couchbase/cluster.rb', line 287

def disconnect
  @backend.close
end

#query(statement, options = QueryOptions.new) ⇒ QueryResult

Performs a query against the query (N1QL) services

Parameters:

  • statement (String)

    the N1QL query statement

  • options (QueryOptions) (defaults to: QueryOptions.new)

    the custom options for this query

Returns:

[View source]

57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/couchbase/cluster.rb', line 57

def query(statement, options = QueryOptions.new)
  resp = @backend.document_query(statement, {
      timeout: options.timeout,
      adhoc: options.adhoc,
      client_context_id: options.client_context_id,
      max_parallelism: options.max_parallelism,
      readonly: options.readonly,
      scan_wait: options.scan_wait,
      scan_cap: options.scan_cap,
      pipeline_batch: options.pipeline_batch,
      pipeline_cap: options.pipeline_cap,
      metrics: options.metrics,
      profile: options.profile,
      positional_parameters: options.export_positional_parameters,
      named_parameters: options.export_named_parameters,
      raw_parameters: options.raw_parameters,
      scan_consistency: options.scan_consistency,
      mutation_state: (options.mutation_state.tokens.map { |t|
        {
            bucket_name: t.bucket_name,
            partition_id: t.partition_id,
            partition_uuid: t.partition_uuid,
            sequence_number: t.sequence_number,
        }
      } if options.mutation_state),
  })

  QueryResult.new do |res|
    res. = QueryMetaData.new do |meta|
      meta.status = resp[:meta][:status]
      meta.request_id = resp[:meta][:request_id]
      meta.client_context_id = resp[:meta][:client_context_id]
      meta.signature = JSON.parse(resp[:meta][:signature]) if resp[:meta][:signature]
      meta.profile = JSON.parse(resp[:meta][:profile]) if resp[:meta][:profile]
      meta.metrics = QueryMetrics.new do |metrics|
        if resp[:meta][:metrics]
          metrics.elapsed_time = resp[:meta][:metrics][:elapsed_time]
          metrics.execution_time = resp[:meta][:metrics][:execution_time]
          metrics.sort_count = resp[:meta][:metrics][:sort_count]
          metrics.result_count = resp[:meta][:metrics][:result_count]
          metrics.result_size = resp[:meta][:metrics][:result_size]
          metrics.mutation_count = resp[:meta][:metrics][:mutation_count]
          metrics.error_count = resp[:meta][:metrics][:error_count]
          metrics.warning_count = resp[:meta][:metrics][:warning_count]
        end
      end
      res[:warnings] = resp[:warnings].map { |warn| QueryWarning.new(warn[:code], warn[:message]) } if resp[:warnings]
    end
    res.instance_variable_set("@rows", resp[:rows])
  end
end

#query_indexesManagement::QueryIndexManager

[View source]

273
274
275
# File 'lib/couchbase/cluster.rb', line 273

def query_indexes
  Management::QueryIndexManager.new(@backend)
end

#search_indexesManagement::SearchIndexManager

[View source]

283
284
285
# File 'lib/couchbase/cluster.rb', line 283

def search_indexes
  Management::SearchIndexManager.new(@backend)
end

#search_query(index_name, query, options = SearchOptions.new) ⇒ SearchResult

Performs a Full Text Search (FTS) query

Parameters:

  • index_name (String)

    the name of the search index

  • query (SearchQuery)

    the query tree

  • options (SearchOptions) (defaults to: SearchOptions.new)

    the query tree

Returns:

[View source]

159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'lib/couchbase/cluster.rb', line 159

def search_query(index_name, query, options = SearchOptions.new)
  resp = @backend.document_search(index_name, JSON.generate(query), {
      timeout: options.timeout,
      limit: options.limit,
      skip: options.skip,
      explain: options.explain,
      highlight_style: options.highlight_style,
      highlight_fields: options.highlight_fields,
      fields: options.fields,
      sort: (options.sort.map { |v| JSON.generate(v) } if options.sort),
      facets: (options.facets.map { |(k, v)| [k, JSON.generate(v)] } if options.facets),
      scan_consistency: options.scan_consistency,
      mutation_state: (options.mutation_state.tokens.map { |t|
        {
            bucket_name: t.bucket_name,
            partition_id: t.partition_id,
            partition_uuid: t.partition_uuid,
            sequence_number: t.sequence_number,
        }
      } if options.mutation_state),
  })

  SearchResult.new do |res|
    res. = SearchMetaData.new do |meta|
      meta.metrics.max_score = resp[:meta_data][:metrics][:max_score]
      meta.metrics.error_partition_count = resp[:meta_data][:metrics][:error_partition_count]
      meta.metrics.success_partition_count = resp[:meta_data][:metrics][:success_partition_count]
      meta.metrics.took = resp[:meta_data][:metrics][:took]
      meta.metrics.total_rows = resp[:meta_data][:metrics][:total_rows]
    end
    res.rows = resp[:rows].map do |r|
      SearchRow.new do |row|
        row.transcoder = options.transcoder
        row.index = r[:index]
        row.id = r[:id]
        row.score = r[:score]
        row.fragments = r[:fragments]
        row.locations = SearchRowLocations.new(
            r[:locations].map do |loc|
              SearchRowLocation.new do |location|
                location.field = loc[:field]
                location.term = loc[:term]
                location.position = loc[:position]
                location.start_offset = loc[:start_offset]
                location.end_offset = loc[:end_offset]
                location.array_positions = loc[:array_positions]
              end
            end
        )
        row.instance_variable_set("@fields", r[:fields])
        row.explanation = JSON.parse(r[:explanation]) if r[:explanation]
      end
    end
    if resp[:facets]
      res.facets = resp[:facets].each_with_object({}) do |(k, v), o|
        facet = case options.facets[k]
                when SearchFacet::SearchFacetTerm
                  SearchFacetResult::TermFacetResult.new do |f|
                    f.terms =
                        if v[:terms]
                          v[:terms].map do |t|
                            SearchFacetResult::TermFacetResult::TermFacet.new(t[:term], t[:count])
                          end
                        else
                          []
                        end
                  end
                when SearchFacet::SearchFacetDateRange
                  SearchFacetResult::DateRangeFacetResult.new do |f|
                    f.date_ranges =
                        if v[:date_ranges]
                          v[:date_ranges].map do |r|
                            SearchFacetResult::DateRangeFacetResult::DateRangeFacet.new(r[:name], r[:count], r[:start_time], r[:end_time])
                          end
                        else
                          []
                        end
                  end
                when SearchFacet::SearchFacetNumericRange
                  SearchFacetResult::NumericRangeFacetResult.new do |f|
                    f.numeric_ranges =
                        if v[:numeric_ranges]
                          v[:numeric_ranges].map do |r|
                            SearchFacetResult::NumericRangeFacetResult::NumericRangeFacet.new(r[:name], r[:count], r[:min], r[:max])
                          end
                        else
                          []
                        end
                  end
                else
                  next # ignore unknown facet result
                end
        facet.name = v[:name]
        facet.field = v[:field]
        facet.total = v[:total]
        facet.missing = v[:missing]
        facet.other = v[:other]
        o[k] = facet
      end
    end
  end
end

#usersManagement::UserManager

[View source]

263
264
265
# File 'lib/couchbase/cluster.rb', line 263

def users
  Management::UserManager.new(@backend)
end