Search in sources :

Example 1 with CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions

use of com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions in project spring-data-couchbase by spring-projects.

the class JavaIntegrationTests method createPrimaryIndex.

public static CompletableFuture<Void> createPrimaryIndex(Cluster cluster, String bucketName, String scopeName, String collectionName) {
    CreatePrimaryQueryIndexOptions options = CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions();
    options.timeout(Duration.ofSeconds(300));
    options.ignoreIfExists(true);
    final CreatePrimaryQueryIndexOptions.Built builtOpts = options.build();
    final String indexName = builtOpts.indexName().orElse(null);
    String keyspace = "default:`" + bucketName + "`.`" + scopeName + "`.`" + collectionName + "`";
    String statement = "CREATE PRIMARY INDEX ";
    if (indexName != null) {
        statement += (indexName) + " ";
    }
    // do not quote, this might be "default:bucketName.scopeName.collectionName"
    statement += "ON " + (keyspace);
    return exec(cluster, false, statement, builtOpts.with(), builtOpts).exceptionally(t -> {
        if (builtOpts.ignoreIfExists() && hasCause(t, IndexExistsException.class)) {
            return null;
        }
        throwIfUnchecked(t);
        throw new RuntimeException(t);
    }).thenApply(result -> null);
}
Also used : UnambiguousTimeoutException(com.couchbase.client.core.error.UnambiguousTimeoutException) PingResult(com.couchbase.client.core.diagnostics.PingResult) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) PingState(com.couchbase.client.core.diagnostics.PingState) ScopeSpec(com.couchbase.client.java.manager.collection.ScopeSpec) PingOptions(com.couchbase.client.java.diagnostics.PingOptions) Collection(com.couchbase.client.java.Collection) SearchIndex(com.couchbase.client.java.manager.search.SearchIndex) QueryResult(com.couchbase.client.java.query.QueryResult) FormBody(okhttp3.FormBody) BeforeAll(org.junit.jupiter.api.BeforeAll) ServiceType(com.couchbase.client.core.service.ServiceType) Duration(java.time.Duration) Map(java.util.Map) CbThrowables.throwIfUnchecked(com.couchbase.client.core.util.CbThrowables.throwIfUnchecked) ClusterEnvironment(com.couchbase.client.java.env.ClusterEnvironment) UpsertSearchIndexOptions(com.couchbase.client.java.manager.search.UpsertSearchIndexOptions) COUCHBASE_TEMPLATE(org.springframework.data.couchbase.config.BeanNames.COUCHBASE_TEMPLATE) IndexExistsException(com.couchbase.client.core.error.IndexExistsException) ParsingFailureException(com.couchbase.client.core.error.ParsingFailureException) Request(okhttp3.Request) CbThrowables.hasCause(com.couchbase.client.core.util.CbThrowables.hasCause) Predicate(java.util.function.Predicate) Mapper(com.couchbase.client.core.json.Mapper) Util.waitUntilCondition(org.springframework.data.couchbase.util.Util.waitUntilCondition) UUID(java.util.UUID) Credentials(okhttp3.Credentials) Scope(com.couchbase.client.java.Scope) AnnotationConfigApplicationContext(org.springframework.context.annotation.AnnotationConfigApplicationContext) List(java.util.List) Bucket(com.couchbase.client.java.Bucket) QueryOptions(com.couchbase.client.java.query.QueryOptions) ClusterOptions(com.couchbase.client.java.ClusterOptions) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) REACTIVE_COUCHBASE_TEMPLATE(org.springframework.data.couchbase.config.BeanNames.REACTIVE_COUCHBASE_TEMPLATE) JsonObject(com.couchbase.client.java.json.JsonObject) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions.createPrimaryQueryIndexOptions) CollectionNotFoundException(com.couchbase.client.core.error.CollectionNotFoundException) SearchResult(com.couchbase.client.java.search.result.SearchResult) CouchbaseClientFactory(org.springframework.data.couchbase.CouchbaseClientFactory) AsyncUtils.block(com.couchbase.client.java.AsyncUtils.block) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) CompletableFuture(java.util.concurrent.CompletableFuture) QueryException(com.couchbase.client.core.error.QueryException) Function(java.util.function.Function) ScopeNotFoundException(com.couchbase.client.core.error.ScopeNotFoundException) LinkedHashMap(java.util.LinkedHashMap) CommonOptions(com.couchbase.client.java.CommonOptions) CreatePrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions) Response(okhttp3.Response) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) ReactiveCouchbaseTemplate(org.springframework.data.couchbase.core.ReactiveCouchbaseTemplate) IOException(java.io.IOException) CollectionSpec(com.couchbase.client.java.manager.collection.CollectionSpec) ApplicationContext(org.springframework.context.ApplicationContext) TimeUnit(java.util.concurrent.TimeUnit) CouchbaseTemplate(org.springframework.data.couchbase.core.CouchbaseTemplate) OkHttpClient(okhttp3.OkHttpClient) CollectionManager(com.couchbase.client.java.manager.collection.CollectionManager) Cluster(com.couchbase.client.java.Cluster) SimpleCouchbaseClientFactory(org.springframework.data.couchbase.SimpleCouchbaseClientFactory) Config(org.springframework.data.couchbase.domain.Config) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) SearchQuery(com.couchbase.client.java.search.SearchQuery) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) CreatePrimaryQueryIndexOptions(com.couchbase.client.java.manager.query.CreatePrimaryQueryIndexOptions)

Aggregations

PingResult (com.couchbase.client.core.diagnostics.PingResult)1 PingState (com.couchbase.client.core.diagnostics.PingState)1 CollectionNotFoundException (com.couchbase.client.core.error.CollectionNotFoundException)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)1 IndexExistsException (com.couchbase.client.core.error.IndexExistsException)1 ParsingFailureException (com.couchbase.client.core.error.ParsingFailureException)1 QueryException (com.couchbase.client.core.error.QueryException)1 ScopeNotFoundException (com.couchbase.client.core.error.ScopeNotFoundException)1 UnambiguousTimeoutException (com.couchbase.client.core.error.UnambiguousTimeoutException)1 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)1 Mapper (com.couchbase.client.core.json.Mapper)1 ServiceType (com.couchbase.client.core.service.ServiceType)1 CbThrowables.hasCause (com.couchbase.client.core.util.CbThrowables.hasCause)1 CbThrowables.throwIfUnchecked (com.couchbase.client.core.util.CbThrowables.throwIfUnchecked)1 AsyncUtils.block (com.couchbase.client.java.AsyncUtils.block)1 Bucket (com.couchbase.client.java.Bucket)1 Cluster (com.couchbase.client.java.Cluster)1 ClusterOptions (com.couchbase.client.java.ClusterOptions)1 Collection (com.couchbase.client.java.Collection)1