
com.azure.cosmos.IndexingPolicy Maven / Gradle / Ivy
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.cosmos;
import com.azure.cosmos.implementation.Constants;
import com.fasterxml.jackson.databind.node.ArrayNode;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* Represents the indexing policy configuration for a collection in the Azure Cosmos DB database service.
*/
public final class IndexingPolicy extends JsonSerializable {
private static final String DEFAULT_PATH = "/*";
private List includedPaths;
private List excludedPaths;
private List> compositeIndexes;
private List spatialIndexes;
/**
* Constructor.
*/
public IndexingPolicy() {
this.setAutomatic(true);
this.setIndexingMode(IndexingMode.CONSISTENT);
}
/**
* Initializes a new instance of the IndexingPolicy class with the specified set of indexes as
* default index specifications for the root path.
*
* The following example shows how to override the default indexingPolicy for root path:
*
* {@codesnippet com.azure.cosmos.indexingPolicy.defaultOverride)}
*
* @param defaultIndexOverrides comma separated set of indexes that serve as default index specifications for the
* root path.
* @throws IllegalArgumentException thrown if an error occurs
*/
public IndexingPolicy(Index[] defaultIndexOverrides) {
this();
if (defaultIndexOverrides == null) {
throw new IllegalArgumentException("defaultIndexOverrides is null.");
}
IncludedPath includedPath = new IncludedPath();
includedPath.setPath(IndexingPolicy.DEFAULT_PATH);
includedPath.setIndexes(new ArrayList(Arrays.asList(defaultIndexOverrides)));
this.getIncludedPaths().add(includedPath);
}
/**
* Constructor.
*
* @param jsonString the json string that represents the indexing policy.
*/
IndexingPolicy(String jsonString) {
super(jsonString);
}
/**
* Gets whether automatic indexing is enabled for a collection.
*
* In automatic indexing, documents can be explicitly excluded from indexing using RequestOptions. In manual
* indexing, documents can be explicitly included.
*
* @return the automatic
*/
public Boolean getAutomatic() {
return super.getBoolean(Constants.Properties.AUTOMATIC);
}
/**
* Sets whether automatic indexing is enabled for a collection.
*
* In automatic indexing, documents can be explicitly excluded from indexing using RequestOptions. In manual
* indexing, documents can be explicitly included.
*
* @param automatic the automatic
* @return the Indexing Policy.
*/
public IndexingPolicy setAutomatic(boolean automatic) {
super.set(Constants.Properties.AUTOMATIC, automatic);
return this;
}
/**
* Gets the indexing mode (consistent or lazy).
*
* @return the indexing mode.
*/
public IndexingMode getIndexingMode() {
IndexingMode result = IndexingMode.LAZY;
try {
result = IndexingMode.valueOf(StringUtils.upperCase(super.getString(Constants.Properties.INDEXING_MODE)));
} catch (IllegalArgumentException e) {
this.getLogger().warn("INVALID indexingMode value {}.",
super.getString(Constants.Properties.INDEXING_MODE));
}
return result;
}
/**
* Sets the indexing mode (consistent or lazy).
*
* @param indexingMode the indexing mode.
* @return the Indexing Policy.
*/
public IndexingPolicy setIndexingMode(IndexingMode indexingMode) {
super.set(Constants.Properties.INDEXING_MODE, indexingMode.toString());
return this;
}
/**
* Gets the paths that are chosen to be indexed by the user.
*
* @return the included paths.
*/
public List getIncludedPaths() {
if (this.includedPaths == null) {
this.includedPaths = super.getList(Constants.Properties.INCLUDED_PATHS, IncludedPath.class);
if (this.includedPaths == null) {
this.includedPaths = new ArrayList();
}
}
return this.includedPaths;
}
public void setIncludedPaths(List includedPaths) {
this.includedPaths = includedPaths;
}
/**
* Gets the paths that are not indexed.
*
* @return the excluded paths.
*/
public List getExcludedPaths() {
if (this.excludedPaths == null) {
this.excludedPaths = super.getList(Constants.Properties.EXCLUDED_PATHS, ExcludedPath.class);
if (this.excludedPaths == null) {
this.excludedPaths = new ArrayList();
}
}
return this.excludedPaths;
}
public IndexingPolicy setExcludedPaths(List excludedPaths) {
this.excludedPaths = excludedPaths;
return this;
}
/**
* Gets the composite indexes for additional indexes.
*
* @return the composite indexes.
*/
public List> getCompositeIndexes() {
if (this.compositeIndexes == null) {
this.compositeIndexes = new ArrayList<>();
ArrayNode compositeIndexes = (ArrayNode) super.get(Constants.Properties.COMPOSITE_INDEXES);
for (int i = 0; i < compositeIndexes.size(); i++) {
ArrayNode compositeIndex = (ArrayNode) compositeIndexes.get(i);
ArrayList compositePaths = new ArrayList();
for (int j = 0; j < compositeIndex.size(); j++) {
CompositePath candidateCompositePath = new CompositePath(compositeIndex.get(j).toString());
compositePaths.add(candidateCompositePath);
}
this.compositeIndexes.add(compositePaths);
}
}
return this.compositeIndexes;
}
/**
* Sets the composite indexes for additional indexes.
*
* @param compositeIndexes the composite indexes.
* @return the Indexing Policy.
*/
public IndexingPolicy setCompositeIndexes(List> compositeIndexes) {
this.compositeIndexes = compositeIndexes;
super.set(Constants.Properties.COMPOSITE_INDEXES, this.compositeIndexes);
return this;
}
/**
* Sets the spatial indexes for additional indexes.
*
* @return the spatial indexes.
*/
public List getSpatialIndexes() {
if (this.spatialIndexes == null) {
this.spatialIndexes = super.getList(Constants.Properties.SPATIAL_INDEXES, SpatialSpec.class);
if (this.spatialIndexes == null) {
this.spatialIndexes = new ArrayList();
}
}
return this.spatialIndexes;
}
/**
* Sets the spatial indexes for additional indexes.
*
* @param spatialIndexes the spatial indexes.
* @return the Indexing Policy.
*/
public IndexingPolicy setSpatialIndexes(List spatialIndexes) {
this.spatialIndexes = spatialIndexes;
super.set(Constants.Properties.SPATIAL_INDEXES, this.spatialIndexes);
return this;
}
@Override
void populatePropertyBag() {
// If indexing mode is not 'none' and not paths are set, set them to the defaults
if (this.getIndexingMode() != IndexingMode.NONE && this.getIncludedPaths().size() == 0
&& this.getExcludedPaths().size() == 0) {
IncludedPath includedPath = new IncludedPath();
includedPath.setPath(IndexingPolicy.DEFAULT_PATH);
this.getIncludedPaths().add(includedPath);
}
if (this.includedPaths != null) {
for (IncludedPath includedPath : this.includedPaths) {
includedPath.populatePropertyBag();
}
super.set(Constants.Properties.INCLUDED_PATHS, this.includedPaths);
}
if (this.excludedPaths != null) {
for (ExcludedPath excludedPath : this.excludedPaths) {
excludedPath.populatePropertyBag();
}
super.set(Constants.Properties.EXCLUDED_PATHS, this.excludedPaths);
}
}
}