All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.couchbase.client.java.manager.query.QueryIndex Maven / Gradle / Ivy

There is a newer version: 3.7.2
Show newest version
/*
 * 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.java.manager.query;

import com.couchbase.client.java.json.JsonArray;
import com.couchbase.client.java.json.JsonObject;

import java.util.Objects;
import java.util.Optional;

import static com.couchbase.client.core.logging.RedactableArgument.redactMeta;
import static com.couchbase.client.core.util.CbObjects.defaultIfNull;
import static com.couchbase.client.core.util.CbStrings.emptyToNull;
import static java.util.Objects.requireNonNull;

/**
 * Describes a N1QL index.
 */
public class QueryIndex {

  private final String name;
  private final boolean primary;
  private final String state;
  private final String keyspace;
  private final String namespace;
  private final JsonArray indexKey;
  private final String type;
  private final Optional condition;
  private final Optional partition;
  private final JsonObject raw;

  public QueryIndex(JsonObject raw) {
    this.raw = requireNonNull(raw);

    this.name = raw.getString("name");
    this.state = raw.getString("state");
    this.keyspace = raw.getString("keyspace_id");
    this.namespace = raw.getString("namespace_id");
    this.indexKey = raw.getArray("index_key");
    this.condition = Optional.ofNullable(emptyToNull(raw.getString("condition")));
    this.primary = Boolean.TRUE.equals(raw.getBoolean("is_primary"));
    this.type = defaultIfNull(raw.getString("using"), "gsi");
    this.partition = Optional.ofNullable(emptyToNull(raw.getString("partition")));
  }

  public boolean primary() {
    return this.primary;
  }

  public String name() {
    return this.name;
  }

  public String type() {
    return type;
  }

  public String state() {
    return state;
  }

  /**
   * @return the keyspace for the index, typically the bucket name.
   */
  public String keyspace() {
    return keyspace;
  }

  /**
   * @return the namespace for the index. A namespace is a resource pool that contains multiple keyspaces.
   * @see #keyspace()
   */
  public String namespace() {
    return namespace;
  }

  /**
   * Return an {@link JsonArray array} of Strings that represent the index key(s).
   * The array is empty in the case of a PRIMARY INDEX.
   * Note that the query service can present the key in a slightly different manner from when you declared the index:
   * for instance, it will show the indexed fields in an escaped format (surrounded by backticks).
   *
   * @return an array of Strings that represent the index key(s), or an empty array in the case of a PRIMARY index.
   */
  public JsonArray indexKey() {
    return this.indexKey;
  }

  /**
   * Return the {@link String} representation of the index's condition (the WHERE clause of the index), or an empty
   * Optional if no condition was set.
   * 

* Note that the query service can present the condition in a slightly different manner from when you declared the index: * for instance it will wrap expressions with parentheses and show the fields in an escaped format (surrounded by * backticks). * * @return the condition/WHERE clause of the index or empty string if none. */ public Optional condition() { return this.condition; } /** * If present, returns the configured partition for the index. * * @return the partition if set, empty if none. */ public Optional partition() { return partition; } /** * @return the raw JSON representation of the index information, as returned by the query service. */ public JsonObject raw() { return raw; } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } QueryIndex that = (QueryIndex) o; return raw.equals(that.raw); } @Override public int hashCode() { return Objects.hash(raw); } @Override public String toString() { return redactMeta(raw).toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy