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) ⇒ 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



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

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:



168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 168

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

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



80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 80

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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 102

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

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



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 125

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

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



147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 147

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

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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/couchbase/management/collection_query_index_manager.rb', line 42

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

  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:



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

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

  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