Class: Couchbase::Management::CollectionQueryIndexManager

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/management/collection_query_index_manager.rb,
/Users/sergey.auseyau/code/couchbase-ruby-client/lib/couchbase/management/collection_query_index_manager.rb
more...

Instance Method Summary collapse

Constructor Details

#initialize(backend, bucket_name, scope_name, collection_name) ⇒ CollectionQueryIndexManager

Returns a new instance of CollectionQueryIndexManager.

Parameters:

  • backend (Couchbase::Backend)
  • bucket_name (String)

    name of the bucket

  • scope_name (String)

    name of the scope

  • collection_name (String)

    name of the collection

[View source]

27
28
29
30
31
32
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 27

def initialize(backend, bucket_name, scope_name, collection_name)
  @backend = backend
  @bucket_name = bucket_name
  @scope_name = scope_name
  @collection_name = collection_name
end

Instance Method Details

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

Build all indexes which are currently in deferred state

Parameters:

Returns:

  • void

Raises:

  • (ArgumentError)
[View source]

149
150
151
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 149

def build_deferred_indexes(options = Options::Query::BuildDeferredIndexes.new)
  @backend.collection_query_index_build_deferred(@bucket_name, @scope_name, @collection_name, options.to_backend)
end

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

Creates a new index

Parameters:

  • 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:

[View source]

69
70
71
72
73
74
75
76
77
78
79
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 69

def create_index(index_name, fields, options = Options::Query::CreateIndex.new)
  unless options.scope_name.nil?
    raise ArgumentError, "Scope name cannot be set in the options when using the Query Index manager at the collection level"
  end

  unless options.collection_name.nil?
    raise ArgumentError, "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @backend.collection_query_index_create(@bucket_name, @scope_name, @collection_name, index_name, fields, options.to_backend)
end

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

Creates new primary index

Parameters:

Returns:

  • void

Raises:

[View source]

89
90
91
92
93
94
95
96
97
98
99
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 89

def create_primary_index(options = Options::Query::CreatePrimaryIndex.new)
  unless options.scope_name.nil?
    raise ArgumentError, "Scope name cannot be set in the options when using the Query Index manager at the collection level"
  end

  unless options.collection_name.nil?
    raise ArgumentError, "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @backend.collection_query_index_create_primary(@bucket_name, @scope_name, @collection_name, options.to_backend)
end

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

Drops the index

Parameters:

Returns:

  • void

Raises:

[View source]

110
111
112
113
114
115
116
117
118
119
120
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 110

def drop_index(index_name, options = Options::Query::DropIndex.new)
  unless options.scope_name.nil?
    raise ArgumentError, "Scope name cannot be set in the options when using the Query Index manager at the collection level"
  end

  unless options.collection_name.nil?
    raise ArgumentError, "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @backend.collection_query_index_drop(@bucket_name, @scope_name, @collection_name, index_name, options.to_backend)
end

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

Drops the primary index

Parameters:

Returns:

  • void

Raises:

[View source]

130
131
132
133
134
135
136
137
138
139
140
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 130

def drop_primary_index(options = Options::Query::DropPrimaryIndex.new)
  unless options.scope_name.nil?
    raise ArgumentError, "Scope name cannot be set in the options when using the Query Index manager at the collection level"
  end

  unless options.collection_name.nil?
    raise ArgumentError, "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @backend.collection_query_index_drop_primary(@bucket_name, @scope_name, @collection_name, options.to_backend)
end

#get_all_indexes(options = Options::Query::GetAllIndexes.new) ⇒ Array<QueryIndex>

Fetches all indexes from the server

Parameters:

Returns:

Raises:

  • (ArgumentError)
[View source]

41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 41

def get_all_indexes(options = Options::Query::GetAllIndexes.new)
  res = @backend.collection_query_index_get_all(@bucket_name, @scope_name, @collection_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(index_names, timeout, options = Options::Query::WatchIndexes.new) ⇒ Object

Polls indexes until they are online

Parameters:

  • 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:

[View source]

161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 161

def watch_indexes(index_names, timeout, options = Options::Query::WatchIndexes.new)
  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(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