def get_all_indexes( self, # type: QueryIndexManager bucket_name, # type: str *options, # type: GetAllQueryIndexOptions **kwargs # type: Any ): # type: (...) -> List[QueryIndex] """ Fetches all indexes from the server. :param str bucket_name: the name of the bucket. :param GetAllQueryIndexOptions options: Options to use for getting all indexes. :param Any kwargs: Override corresponding value in options. :return: A list of QueryIndex objects. :raises: InvalidArgumentsException """ # N1QL # SELECT idx.* FROM system:indexes AS idx # WHERE keyspace_id = "bucket_name" # ORDER BY is_primary DESC, name ASC info = N1qlIndex() info.keyspace = bucket_name response = IxmgmtRequest(self._admin_bucket, 'list', info, **forward_args(kwargs, *options)).execute() return list(map(QueryIndex.from_n1qlindex, response))
def n1ql_index_watch(self, indexes, timeout=30, interval=0.2, watch_primary=False): """ Await completion of index building This method will wait up to `timeout` seconds for every index in `indexes` to have been built. It will poll the cluster every `interval` seconds. :param list indexes: A list of indexes to check. This is returned by :meth:`build_deferred_indexes` :param float timeout: How long to wait for the indexes to become ready. :param float interval: How often to poll the cluster. :param bool watch_primary: Whether to also watch the primary index. This parameter should only be used when manually constructing a list of string indexes :raise: :exc:`~.TimeoutError` if the timeout was reached before all indexes were built :raise: :exc:`~.NotFoundError` if one of the indexes passed no longer exists. """ kwargs = { 'timeout_us': int(timeout * 1000000), 'interval_us': int(interval * 1000000) } ixlist = [N1qlIndex.from_any(x, self._cb.bucket) for x in indexes] if watch_primary: ixlist.append( N1qlIndex.from_any(N1QL_PRIMARY_INDEX, self._cb.bucket)) return IxmgmtRequest(self._cb, 'watch', ixlist, **kwargs).execute()
def _n1ql_index_create(self, bucket_name, ix, defer=False, ignore_exists=False, primary=False, fields=None, cond=None, **kwargs): """ Create an index for use with N1QL. :param str ix: The name of the index to create :param bool defer: Whether the building of indexes should be deferred. If creating multiple indexes on an existing dataset, using the `defer` option in conjunction with :meth:`build_deferred_indexes` and :meth:`watch_indexes` may result in substantially reduced build times. :param bool ignore_exists: Do not throw an exception if the index already exists. :param Iterable[str] fields: A list of fields that should be supplied as keys for the index. For non-primary indexes, this must be specified and must contain at least one field name. :param bool primary: Whether this is a primary index. If creating a primary index, the name may be an empty string and `fields` must be empty. :param str condition: Specify a condition for indexing. Using a condition reduces an index size :raise: :exc:`~.KeyExistsError` if the index already exists .. seealso:: :meth:`n1ql_index_create_primary` """ fields = fields or [] if kwargs: raise TypeError('Unknown keyword arguments', kwargs) info = self._mk_index_def(bucket_name, ix, primary) if primary and fields: raise TypeError('Cannot create primary index with explicit fields') elif not primary and not fields: raise ValueError('Fields required for non-primary index') if fields: info.fields = fields if primary and info.name is N1QL_PRIMARY_INDEX: del info.name if cond: if primary: raise ValueError('cannot specify condition for primary index') info.condition = cond options = {'ignore_exists': ignore_exists, 'defer': defer} # Now actually create the indexes return IxmgmtRequest(self._admin_bucket, 'create', info, **options).execute()
def n1ql_index_build_deferred(self, other_buckets=False): """ Instruct the server to begin building any previously deferred index definitions. This method will gather a list of all pending indexes in the cluster (including those created using the `defer` option with :meth:`create_index`) and start building them in an efficient manner. :param bool other_buckets: Whether to also build indexes found in other buckets, if possible :return: list[couchbase_core._ixmgmt.Index] objects. This list contains the indexes which are being built and may be passed to :meth:`n1ql_index_watch` to poll their build statuses. You can use the :meth:`n1ql_index_watch` method to wait until all indexes have been built:: mgr.n1ql_index_create('ix_fld1', fields=['field1'], defer=True) mgr.n1ql_index_create('ix_fld2', fields['field2'], defer=True) mgr.n1ql_index_create('ix_fld3', fields=['field3'], defer=True) indexes = mgr.n1ql_index_build_deferred() # [IndexInfo('field1'), IndexInfo('field2'), IndexInfo('field3')] mgr.n1ql_index_watch(indexes, timeout=30, interval=1) """ info = N1qlIndex() if not other_buckets: info.keyspace = self._cb.bucket return IxmgmtRequest(self._cb, 'build', info).execute()
def _drop_index(self, bucket_name, index_name, *options, **kwargs): info = BucketManager._mk_index_def(bucket_name, index_name, primary=kwargs.pop( 'primary', False)) final_args = { k.replace('ignore_if_not_exists', 'ignore_missing'): v for k, v in forward_args(kwargs, *options).items() } IxmgmtRequest(self._admin_bucket, 'drop', info, **final_args).execute()
def n1ql_index_list(self, other_buckets=False): """ List indexes in the cluster. :param bool other_buckets: Whether to also include indexes belonging to other buckets (i.e. buckets other than the current `Bucket` object) :return: list[couchbase_core._ixmgmt.Index] objects """ info = N1qlIndex() if not other_buckets: info.keyspace = self._cb.bucket return IxmgmtRequest(self._cb, 'list', info).execute()
def n1ql_index_drop(self, ix, primary=False, **kwargs): """ Delete an index from the cluster. :param str ix: the name of the index :param bool primary: if this index is a primary index :param bool ignore_missing: Do not raise an exception if the index does not exist :raise: :exc:`~.NotFoundError` if the index does not exist and `ignore_missing` was not specified """ info = self._mk_index_def(ix, primary) return IxmgmtRequest(self._cb, 'drop', info, **kwargs).execute()
def n1ql_index_drop(self, ix, primary=False, ignore_missing=False): """ Delete an index from the cluster. :param str ix: the name of the index :param bool primary: if this index is a primary index :param bool ignore_missing: Do not raise an exception if the index does not exist :raise: :exc:`~.QueryIndexNotFoundException` if the index does not exist and `ignore_missing` was not specified """ info = BucketManager._mk_index_def(self._bucketname, ix, primary) kwargs['ignore_missing'] = ignore_missing return IxmgmtRequest(self._cb, 'drop', info, **kwargs).execute()
def get_all_indexes( self, # type: QueryIndexManager bucket_name, # type: str *options, # type: GetAllQueryIndexOptions **kwargs): # type: (...)->List[QueryIndex] """ Fetches all indexes from the server. :param str bucket_name: the name of the bucket. :param Duration timeout: the time allowed for the operation to be terminated. This is controlled by the client. :return: A list of QueryIndex objects. :raises: InvalidArgumentsException """ # N1QL # SELECT idx.* FROM system:indexes AS idx # WHERE keyspace_id = "bucket_name" # ORDER BY is_primary DESC, name ASC info = N1qlIndex() info.keyspace = bucket_name response = IxmgmtRequest(self._admin_bucket, 'list', info).execute() return list(map(QueryIndex.from_n1qlindex, response))
def _n1ql_index_build_deferred(bucketname, bucket, other_buckets=False): info = N1qlIndex() if not other_buckets: info.keyspace = bucketname return IxmgmtRequest(bucket, 'build', info).execute()