Class: Couchbase::Options::Query

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

Overview

Options for Cluster#query

Constant Summary collapse

DEFAULT =

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.

Query.new.freeze

Instance Attribute Summary collapse

Attributes inherited from Base

#client_context, #parent_span, #retry_strategy, #timeout

Instance Method Summary collapse

Constructor Details

#initialize(adhoc: true, client_context_id: nil, max_parallelism: nil, readonly: false, scan_wait: nil, scan_cap: nil, pipeline_cap: nil, pipeline_batch: nil, metrics: nil, profile: :off, flex_index: nil, preserve_expiry: nil, use_replica: nil, scope_qualifier: nil, scan_consistency: :not_bounded, mutation_state: nil, transcoder: JsonTranscoder.new, positional_parameters: nil, named_parameters: nil, timeout: nil, retry_strategy: nil, client_context: nil, parent_span: nil) {|self| ... } ⇒ Query

Note:

Either positional_parameters or named_parameters may be specified.

Creates new instance of options for Cluster#query

Parameters:

  • adhoc (Boolean) (defaults to: true)

    allows turning this request into a prepared statement query

  • client_context_id (String, nil) (defaults to: nil)

    provides a custom client context ID for this query

  • max_parallelism (Integer, nil) (defaults to: nil)

    allows overriding the default maximum parallelism for the query execution on the server side.

  • readonly (Boolean, nil) (defaults to: false)

    allows explicitly marking a query as being readonly and not mutating any documents on the server side.

  • scan_wait (Integer, #in_milliseconds, nil) (defaults to: nil)

    The maximum duration (in milliseconds) the query engine is willing to wait before failing. Allows customizing how long (in milliseconds) the query engine is willing to wait until the index catches up to whatever scan consistency is asked for in this query. Note that if :not_bounded consistency level is used, this method doesn’t do anything at all. If no value is provided to this method, the server default is used.

  • scan_cap (Integer, nil) (defaults to: nil)

    customize the maximum buffered channel size between the indexer and the query service

  • pipeline_cap (Integer, nil) (defaults to: nil)

    customize the number of items execution operators can batch for fetch from the Key Value layer on the server.

  • pipeline_batch (Integer, nil) (defaults to: nil)

    customize the maximum number of items each execution operator can buffer between various operators on the server.

  • metrics (Boolean, nil) (defaults to: nil)

    enables per-request metrics in the trailing section of the query

  • profile (Symbol) (defaults to: :off)

    customize server profile level for this query

    :off

    No profiling information is added to the query response

    :phases

    The query response includes a profile section with stats and details about various phases of the query plan and execution. Three phase times will be included in the system:active_requests and system:completed_requests monitoring keyspaces.

    :timings

    Besides the phase times, the profile section of the query response document will include a full query plan with timing and information about the number of processed documents at each phase. This information will be included in the system:active_requests and system:completed_requests keyspaces.

  • scan_consistency (Symbol, nil) (defaults to: :not_bounded)

    Sets the mutation tokens this query should be consistent with. Overrides mutation_state.

    :not_bounded

    The indexer will return whatever state it has to the query engine at the time of query. This is the default (for single-statement requests).

    :request_plus

    The indexer will wait until all mutations have been processed at the time of request before returning to the query engine.

  • flex_index (Boolean, nil) (defaults to: nil)

    Tells the query engine to use a flex index (utilizing the search service)

  • preserve_expiry (Boolean, nil) (defaults to: nil)

    Tells the query engine to preserve expiration values set on any documents modified by this query.

  • use_replica (Boolean, nil) (defaults to: nil)

    Specifies that the query engine should use replica nodes for KV fetches if the active node is down. If not provided, the server default will be used

  • scope_qualifier (String, nil) (defaults to: nil)

    Associate scope qualifier (also known as query_context) with the query. The qualifier must be in form {bucket_name}.{scope_name} or default:{bucket_name}.{scope_name}.

  • transcoder (JsonTranscoder) (defaults to: JsonTranscoder.new)

    to decode rows

  • positional_parameters (Array<#to_json>, nil) (defaults to: nil)

    parameters to be used as substitution for numbered macros like $1, $2 in query string

  • named_parameters (Hash<String => #to_json>, nil) (defaults to: nil)

    parameters to be used as substitution for named macros like $name in query string.

  • mutation_state (MutationState, nil) (defaults to: nil)

    Sets the mutation tokens this query should be consistent with. Overrides scan_consistency.

  • timeout (Integer, #in_milliseconds, nil) (defaults to: nil)
  • retry_strategy (Proc, nil) (defaults to: nil)

    the custom retry strategy, if set

  • client_context (Hash, nil) (defaults to: nil)

    the client context data, if set

  • parent_span (Span, nil) (defaults to: nil)

    if set holds the parent span, that should be used for this request

Yield Parameters:

Raises:

  • (ArgumentError)


2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
# File 'lib/couchbase/options.rb', line 2167

def initialize(adhoc: true,
               client_context_id: nil,
               max_parallelism: nil,
               readonly: false,
               scan_wait: nil,
               scan_cap: nil,
               pipeline_cap: nil,
               pipeline_batch: nil,
               metrics: nil,
               profile: :off,
               flex_index: nil,
               preserve_expiry: nil,
               use_replica: nil,
               scope_qualifier: nil,
               scan_consistency: :not_bounded,
               mutation_state: nil,
               transcoder: JsonTranscoder.new,
               positional_parameters: nil,
               named_parameters: nil,
               timeout: nil,
               retry_strategy: nil,
               client_context: nil,
               parent_span: nil)
  raise ArgumentError, "Cannot pass positional and named parameters at the same time" if positional_parameters && named_parameters

  super(timeout: timeout, retry_strategy: retry_strategy, client_context: client_context, parent_span: parent_span)
  @adhoc = adhoc
  @client_context_id = client_context_id
  @max_parallelism = max_parallelism
  @readonly = readonly
  @scan_wait = scan_wait
  @scan_cap = scan_cap
  @pipeline_cap = pipeline_cap
  @pipeline_batch = pipeline_batch
  @metrics = metrics
  @profile = profile
  @flex_index = flex_index
  @preserve_expiry = preserve_expiry
  @use_replica = use_replica
  @scope_qualifier = scope_qualifier
  @scan_consistency = scan_consistency
  @mutation_state = mutation_state
  @transcoder = transcoder
  @positional_parameters = positional_parameters
  @named_parameters = named_parameters
  @raw_parameters = {}
  yield self if block_given?
end

Instance Attribute Details

#adhocBoolean

Returns:

  • (Boolean)


2088
2089
2090
# File 'lib/couchbase/options.rb', line 2088

def adhoc
  @adhoc
end

#client_context_idString

Returns:

  • (String)


2089
2090
2091
# File 'lib/couchbase/options.rb', line 2089

def client_context_id
  @client_context_id
end

#flex_indexBoolean

Returns:

  • (Boolean)


2098
2099
2100
# File 'lib/couchbase/options.rb', line 2098

def flex_index
  @flex_index
end

#max_parallelismInteger

Returns:

  • (Integer)


2090
2091
2092
# File 'lib/couchbase/options.rb', line 2090

def max_parallelism
  @max_parallelism
end

#metricsBoolean

Returns:

  • (Boolean)


2096
2097
2098
# File 'lib/couchbase/options.rb', line 2096

def metrics
  @metrics
end

#mutation_stateMutationState (readonly)

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

Returns:



2280
2281
2282
# File 'lib/couchbase/options.rb', line 2280

def mutation_state
  @mutation_state
end

#pipeline_batchInteger

Returns:

  • (Integer)


2094
2095
2096
# File 'lib/couchbase/options.rb', line 2094

def pipeline_batch
  @pipeline_batch
end

#pipeline_capInteger

Returns:

  • (Integer)


2095
2096
2097
# File 'lib/couchbase/options.rb', line 2095

def pipeline_cap
  @pipeline_cap
end

#preserve_expiryBoolean

Returns:

  • (Boolean)


2099
2100
2101
# File 'lib/couchbase/options.rb', line 2099

def preserve_expiry
  @preserve_expiry
end

#profileSymbol

Returns:

  • (Symbol)


2097
2098
2099
# File 'lib/couchbase/options.rb', line 2097

def profile
  @profile
end

#raw_parametersHash<String => #to_json> (readonly)

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

Returns:

  • (Hash<String => #to_json>)


2284
2285
2286
# File 'lib/couchbase/options.rb', line 2284

def raw_parameters
  @raw_parameters
end

#readonlyBoolean

Returns:

  • (Boolean)


2091
2092
2093
# File 'lib/couchbase/options.rb', line 2091

def readonly
  @readonly
end

#scan_capInteger

Returns:

  • (Integer)


2093
2094
2095
# File 'lib/couchbase/options.rb', line 2093

def scan_cap
  @scan_cap
end

#scan_waitInteger, #in_milliseconds

Returns:

  • (Integer, #in_milliseconds)


2092
2093
2094
# File 'lib/couchbase/options.rb', line 2092

def scan_wait
  @scan_wait
end

#scope_qualifierString

Returns:

  • (String)


2101
2102
2103
# File 'lib/couchbase/options.rb', line 2101

def scope_qualifier
  @scope_qualifier
end

#transcoderJsonTranscoder, #decode(String)

Returns:



2102
2103
2104
# File 'lib/couchbase/options.rb', line 2102

def transcoder
  @transcoder
end

#use_replicaBoolean?

Returns:

  • (Boolean, nil)


2100
2101
2102
# File 'lib/couchbase/options.rb', line 2100

def use_replica
  @use_replica
end

Instance Method Details

#consistent_with(mutation_state) ⇒ Object

Note:

overrides consistency level set by #scan_consistency=

Sets the mutation tokens this query should be consistent with

Parameters:

  • mutation_state (MutationState)

    the mutation state containing the mutation tokens



2245
2246
2247
2248
# File 'lib/couchbase/options.rb', line 2245

def consistent_with(mutation_state)
  @scan_consistency = nil if @scan_consistency
  @mutation_state = mutation_state
end

#export_named_parametersHash<String => String>?

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

Returns:

  • (Hash<String => String>, nil)


2274
2275
2276
# File 'lib/couchbase/options.rb', line 2274

def export_named_parameters
  @named_parameters&.each_with_object({}) { |(n, v), o| o[n.to_s] = JSON.dump(v) }
end

#export_positional_parametersArray<String>?

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

Returns:

  • (Array<String>, nil)


2260
2261
2262
# File 'lib/couchbase/options.rb', line 2260

def export_positional_parameters
  @positional_parameters&.map { |p| JSON.dump(p) }
end

#named_parameters(named) ⇒ Object

Sets named parameters for the query

Parameters:

  • named (Hash)

    the key/value map of the parameters to substitute in the statement



2267
2268
2269
2270
# File 'lib/couchbase/options.rb', line 2267

def named_parameters(named)
  @named_parameters = named
  @positional_parameters = nil
end

#positional_parameters(positional) ⇒ Object

Sets positional parameters for the query

Parameters:

  • positional (Array)

    the list of parameters that have to be substituted in the statement



2253
2254
2255
2256
# File 'lib/couchbase/options.rb', line 2253

def positional_parameters(positional)
  @positional_parameters = positional
  @named_parameters = nil
end

#raw(key, value) ⇒ Object

Allows providing custom JSON key/value pairs for advanced usage

Parameters:

  • key (String)

    the parameter name (key of the JSON property)

  • value (Object)

    the parameter value (value of the JSON property)



2220
2221
2222
# File 'lib/couchbase/options.rb', line 2220

def raw(key, value)
  @raw_parameters[key] = JSON.generate(value)
end

#scan_consistency=(level) ⇒ Object

Note:

overrides consistency level set by #consistent_with

Customizes the consistency guarantees for this query

:not_bounded

The indexer will return whatever state it has to the query engine at the time of query. This is the default (for

single-statement requests).
:request_plus

The indexer will wait until all mutations have been processed at the time of request before returning to the query

engine.

Parameters:

  • level (:not_bounded, :request_plus)

    the index scan consistency to be used for this query



2235
2236
2237
2238
# File 'lib/couchbase/options.rb', line 2235

def scan_consistency=(level)
  @mutation_state = nil if @mutation_state
  @scan_consistency = level
end

#to_backend(scope_name: nil, bucket_name: nil) ⇒ Object

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



2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
# File 'lib/couchbase/options.rb', line 2287

def to_backend(scope_name: nil, bucket_name: nil)
  if scope_name && bucket_name
    default_query_context = format("default:`%<bucket>s`.`%<scope>s`", bucket: bucket_name, scope: scope_name)
  end
  {
    timeout: Utils::Time.extract_duration(@timeout),
    adhoc: @adhoc,
    client_context_id: @client_context_id,
    max_parallelism: @max_parallelism,
    readonly: @readonly,
    flex_index: @flex_index,
    preserve_expiry: @preserve_expiry,
    use_replica: @use_replica,
    scan_wait: Utils::Time.extract_duration(@scan_wait),
    scan_cap: @scan_cap,
    pipeline_batch: @pipeline_batch,
    pipeline_cap: @pipeline_cap,
    metrics: @metrics,
    profile: @profile,
    positional_parameters: export_positional_parameters,
    named_parameters: export_named_parameters,
    raw_parameters: @raw_parameters,
    scan_consistency: @scan_consistency,
    mutation_state: @mutation_state&.to_a,
    query_context: @scope_qualifier || default_query_context,
  }
end