Class: Couchbase::Management::CollectionQueryIndexManager

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

Instance Method Summary collapse

Constructor Details

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

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 a new instance of CollectionQueryIndexManager.



33
34
35
36
37
38
39
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 33

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

Instance Method Details

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

Build all indexes which are currently in deferred state



185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 185

def build_deferred_indexes(options = Options::Query::BuildDeferredIndexes.new)
  unless options.scope_name.nil?
    raise Error::InvalidArgument,
          "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 Error::InvalidArgument,
          "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @observability.record_operation(Observability::OP_QM_BUILD_DEFERRED_INDEXES, options.parent_span, self, :query) do |obs_handler|
    @backend.collection_query_index_build_deferred(@bucket_name, @scope_name, @collection_name, options.to_backend, obs_handler)
  end
end

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

Creates a new index



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

def create_index(index_name, fields, options = Options::Query::CreateIndex.new)
  unless options.scope_name.nil?
    raise Error::InvalidArgument,
          "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 Error::InvalidArgument,
          "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @observability.record_operation(Observability::OP_QM_CREATE_INDEX, options.parent_span, self, :query) do |obs_handler|
    @backend.collection_query_index_create(@bucket_name, @scope_name, @collection_name, index_name, fields, options.to_backend,
                                           obs_handler)
  end
end

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

Creates new primary index



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 113

def create_primary_index(options = Options::Query::CreatePrimaryIndex.new)
  unless options.scope_name.nil?
    raise Error::InvalidArgument,
          "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 Error::InvalidArgument,
          "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @observability.record_operation(Observability::OP_QM_CREATE_PRIMARY_INDEX, options.parent_span, self, :query) do |obs_handler|
    @backend.collection_query_index_create_primary(@bucket_name, @scope_name, @collection_name, options.to_backend, obs_handler)
  end
end

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

Drops the index



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 138

def drop_index(index_name, options = Options::Query::DropIndex.new)
  unless options.scope_name.nil?
    raise Error::InvalidArgument,
          "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 Error::InvalidArgument,
          "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @observability.record_operation(Observability::OP_QM_DROP_INDEX, options.parent_span, self, :query) do |obs_handler|
    @backend.collection_query_index_drop(@bucket_name, @scope_name, @collection_name, index_name, options.to_backend, obs_handler)
  end
end

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

Drops the primary index



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

def drop_primary_index(options = Options::Query::DropPrimaryIndex.new)
  unless options.scope_name.nil?
    raise Error::InvalidArgument,
          "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 Error::InvalidArgument,
          "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @observability.record_operation(Observability::OP_QM_DROP_PRIMARY_INDEX, options.parent_span, self, :query) do |obs_handler|
    @backend.collection_query_index_drop_primary(@bucket_name, @scope_name, @collection_name, options.to_backend, obs_handler)
  end
end

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

Fetches all indexes from the server



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 48

def get_all_indexes(options = Options::Query::GetAllIndexes.new)
  unless options.scope_name.nil?
    raise Error::InvalidArgument,
          "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 Error::InvalidArgument,
          "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @observability.record_operation(Observability::OP_QM_GET_ALL_INDEXES, options.parent_span, self, :query) do |obs_handler|
    res = @backend.collection_query_index_get_all(@bucket_name, @scope_name, @collection_name, options.to_backend, obs_handler)
    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
end

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

Polls indexes until they are online



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
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 209

def watch_indexes(index_names, timeout, options = Options::Query::WatchIndexes.new)
  unless options.scope_name.nil?
    raise Error::InvalidArgument,
          "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 Error::InvalidArgument,
          "Collection name cannot be set in the options when using the Query Index manager at the collection level"
  end

  @observability.record_operation(Observability::OP_QM_WATCH_INDEXES, options.parent_span, self, :query) do |obs_handler|
    index_names.append("#primary") if options.watch_primary

    interval_millis = 50
    deadline = Time.now + (Utils::Time.extract_duration(timeout) * 0.001)
    all_online = false
    while Time.now <= deadline
      get_all_opts = Options::Query::GetAllIndexes.new(
        timeout: ((deadline - Time.now) * 1000).round,
        parent_span: obs_handler.op_span,
      )
      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 }
      break 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" unless all_online
  end
end