Class: Couchbase::Management::QueryIndexManager

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/management/query_index_manager.rb

Constant Summary collapse

GetAllIndexOptions =

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 after 3.2

::Couchbase::Management::Options::Query::GetAllIndexes
CreateIndexOptions =

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 after 3.2

::Couchbase::Management::Options::Query::CreateIndex
CreatePrimaryIndexOptions =

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 after 3.2

::Couchbase::Management::Options::Query::CreatePrimaryIndex
DropIndexOptions =

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 after 3.2

::Couchbase::Management::Options::Query::DropIndex
DropPrimaryIndexOptions =

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 after 3.2

::Couchbase::Management::Options::Query::DropPrimaryIndex
BuildDeferredIndexOptions =

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 after 3.2

::Couchbase::Management::Options::Query::BuildDeferredIndexes
WatchIndexesOptions =

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 after 3.2

::Couchbase::Management::Options::Query::WatchIndexes

Instance Method Summary collapse

Constructor Details

#initialize(backend) ⇒ QueryIndexManager

Returns a new instance of QueryIndexManager.

Parameters:



386
387
388
# File 'lib/couchbase/management/query_index_manager.rb', line 386

def initialize(backend)
  @backend = backend
end

Instance Method Details

#build_deferred_indexes(bucket_name, options = Options::Query::BuildDeferredIndexes.new) ⇒ Object

Build all indexes which are currently in deferred state

Parameters:

Returns:

Raises:

  • (ArgumentError)


499
500
501
502
503
504
505
# File 'lib/couchbase/management/query_index_manager.rb', line 499

def build_deferred_indexes(bucket_name, options = Options::Query::BuildDeferredIndexes.new)
  unless options.scope_name.nil? && options.collection_name.nil?
    warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
  end

  @backend.query_index_build_deferred(bucket_name, options.to_backend)
end

#create_index(bucket_name, index_name, fields, options = Options::Query::CreateIndex.new) ⇒ Object

Creates a new index

Parameters:

  • bucket_name (String)

    name of the bucket

  • index_name (String)

    name of the index

  • fields (Array<String>)

    the lists of fields to create th index over

  • options (Options::Query::CreateIndex) (defaults to: Options::Query::CreateIndex.new)

Returns:

  • void

Raises:



431
432
433
434
435
436
437
# File 'lib/couchbase/management/query_index_manager.rb', line 431

def create_index(bucket_name, index_name, fields, options = Options::Query::CreateIndex.new)
  unless options.scope_name.nil? && options.collection_name.nil?
    warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
  end

  @backend.query_index_create(bucket_name, index_name, fields, options.to_backend)
end

#create_primary_index(bucket_name, options = Options::Query::CreatePrimaryIndex.new) ⇒ Object

Creates new primary index

Parameters:

Returns:

  • void

Raises:



448
449
450
451
452
453
454
# File 'lib/couchbase/management/query_index_manager.rb', line 448

def create_primary_index(bucket_name, options = Options::Query::CreatePrimaryIndex.new)
  unless options.scope_name.nil? && options.collection_name.nil?
    warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
  end

  @backend.query_index_create_primary(bucket_name, options.to_backend)
end

#drop_index(bucket_name, index_name, options = Options::Query::DropIndex.new) ⇒ Object

Drops the index

Parameters:

  • bucket_name (String)

    name of the bucket

  • index_name (String)

    name of the index

  • options (Options::Query::DropIndex) (defaults to: Options::Query::DropIndex.new)

Returns:

  • void

Raises:



466
467
468
469
470
471
472
# File 'lib/couchbase/management/query_index_manager.rb', line 466

def drop_index(bucket_name, index_name, options = Options::Query::DropIndex.new)
  unless options.scope_name.nil? && options.collection_name.nil?
    warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
  end

  @backend.query_index_drop(bucket_name, index_name, options.to_backend)
end

#drop_primary_index(bucket_name, options = Options::Query::DropPrimaryIndex.new) ⇒ Object

Drops the primary index

Parameters:

Returns:

  • void

Raises:



483
484
485
486
487
488
489
# File 'lib/couchbase/management/query_index_manager.rb', line 483

def drop_primary_index(bucket_name, options = Options::Query::DropPrimaryIndex.new)
  unless options.scope_name.nil? && options.collection_name.nil?
    warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
  end

  @backend.query_index_drop_primary(bucket_name, options.to_backend)
end

#get_all_indexes(bucket_name, options = GetAllIndexOptions.new) ⇒ Array<QueryIndex>

Fetches all indexes from the server

Parameters:

Returns:

Raises:

  • (ArgumentError)


398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/couchbase/management/query_index_manager.rb', line 398

def get_all_indexes(bucket_name, options = GetAllIndexOptions.new)
  unless options.scope_name.nil? && options.collection_name.nil?
    warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
  end

  res = @backend.query_index_get_all(bucket_name, options.to_backend)
  res[:indexes].map do |idx|
    QueryIndex.new do |index|
      index.name = idx[:name]
      index.is_primary = idx[:is_primary]
      index.type = idx[:type]
      index.state = idx[:state]
      index.bucket = idx[:bucket_name]
      index.scope = idx[:scope_name]
      index.collection = idx[:collection_name]
      index.index_key = idx[:index_key]
      index.condition = idx[:condition]
      index.partition = idx[:partition]
    end
  end
end

#watch_indexes(bucket_name, index_names, timeout, options = Options::Query::WatchIndexes.new) ⇒ Object

Polls indexes until they are online

Parameters:

  • bucket_name (String)

    name of the bucket

  • index_names (Array<String>)

    names of the indexes to watch

  • timeout (Integer, #in_milliseconds)

    the time in milliseconds allowed for the operation to complete

  • options (Options::Query::WatchIndexes) (defaults to: Options::Query::WatchIndexes.new)

Raises:



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/couchbase/management/query_index_manager.rb', line 516

def watch_indexes(bucket_name, index_names, timeout, options = Options::Query::WatchIndexes.new)
  unless options.scope_name.nil? && options.collection_name.nil?
    warn "The attributes 'scope_name' and 'collection_name' have been deprecated. Use 'collection.query_indexes' instead"
  end

  index_names.append("#primary") if options.watch_primary

  interval_millis = 50
  deadline = Time.now + (Utils::Time.extract_duration(timeout) * 0.001)
  while Time.now <= deadline
    get_all_opts = Options::Query::GetAllIndexes.new(timeout: ((deadline - Time.now) * 1000).round)
    indexes = get_all_indexes(bucket_name, get_all_opts).select { |idx| index_names.include? idx.name }
    indexes_not_found = index_names - indexes.map(&:name)
    raise Error::IndexNotFound, "Failed to find the indexes: #{indexes_not_found.join(', ')}" unless indexes_not_found.empty?

    all_online = indexes.all? { |idx| idx.state == :online }
    return if all_online

    sleep(interval_millis / 1000)
    interval_millis += 500
    interval_millis = 1000 if interval_millis > 1000
  end
  raise Error::UnambiguousTimeout, "Failed to find all indexes online within the allotted time"
end