com.couchbase.client.core.manager.CoreViewIndexManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of core-io Show documentation
Show all versions of core-io Show documentation
The official Couchbase JVM Core IO Library
/*
* Copyright 2019 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.couchbase.client.core.manager;
import com.couchbase.client.core.Core;
import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.cnc.CbTracing;
import com.couchbase.client.core.cnc.RequestSpan;
import com.couchbase.client.core.cnc.TracingIdentifiers;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ObjectNode;
import com.couchbase.client.core.endpoint.http.CoreCommonOptions;
import com.couchbase.client.core.endpoint.http.CoreHttpClient;
import com.couchbase.client.core.endpoint.http.CoreHttpPath;
import com.couchbase.client.core.endpoint.http.CoreHttpResponse;
import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.core.error.DesignDocumentNotFoundException;
import com.couchbase.client.core.error.HttpStatusCodeException;
import com.couchbase.client.core.error.InvalidArgumentException;
import com.couchbase.client.core.error.context.ReducedViewErrorContext;
import com.couchbase.client.core.json.Mapper;
import com.couchbase.client.core.msg.RequestTarget;
import com.couchbase.client.core.msg.ResponseStatus;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import static com.couchbase.client.core.endpoint.http.CoreHttpPath.path;
import static com.couchbase.client.core.logging.RedactableArgument.redactMeta;
import static com.couchbase.client.core.util.CbCollections.mapOf;
import static com.couchbase.client.core.util.CbStrings.removeStart;
import static com.couchbase.client.core.util.UrlQueryStringBuilder.urlEncode;
import static com.couchbase.client.core.util.Validators.notNull;
import static com.couchbase.client.core.util.Validators.notNullOrEmpty;
import static java.util.Objects.requireNonNull;
@Stability.Internal
public class CoreViewIndexManager {
private static final String DEV_PREFIX = "dev_";
public static String requireUnqualifiedName(String name) {
if (name.startsWith(DEV_PREFIX)) {
throw InvalidArgumentException.fromMessage(
"Design document name '" + redactMeta(name) + "' must not start with '" + DEV_PREFIX + "'" +
"; instead specify the " + namespaceToString(false) + " namespace when referring to the document.");
}
return name;
}
protected final Core core;
private final String bucket;
protected final CoreHttpClient viewService;
protected final CoreHttpClient managerService;
public CoreViewIndexManager(Core core, String bucket) {
this.core = requireNonNull(core);
this.bucket = requireNonNull(bucket);
this.viewService = core.httpClient(RequestTarget.views(bucket));
this.managerService = core.httpClient(RequestTarget.manager());
}
private static String adjustName(String name, boolean production) {
if (production) {
return removeStart(name, DEV_PREFIX);
}
return name.startsWith(DEV_PREFIX) ? name : DEV_PREFIX + name;
}
private CoreHttpPath pathForDesignDocument(String name, boolean production) {
return path("/{bucket}/_design/{ddoc}", mapOf(
"bucket", bucket,
"ddoc", adjustName(name, production)));
}
private String pathForAllDesignDocuments() {
return "/pools/default/buckets/" + urlEncode(bucket) + "/ddocs";
}
/**
* Returns map of design doc name to JSON.
*
* JSON structure is same as returned by {@link #getDesignDocument}.
*/
public CompletableFuture