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

com.microsoft.azure.documentdb.IndexingPolicy Maven / Gradle / Ivy

package com.microsoft.azure.documentdb;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

import org.apache.commons.lang3.text.WordUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import com.microsoft.azure.documentdb.internal.Constants;

/**
 * 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 Collection includedPaths;
    private Collection excludedPaths;
    private Collection> compositeIndexes;
    private Collection 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: *

     * {@code
     * HashIndex hashIndexOverride = Index.Hash(DataType.String, 5);
     * RangeIndex rangeIndexOverride = Index.Range(DataType.Number, 2);
     * SpatialIndex spatialIndexOverride = Index.Spatial(DataType.Point);
     *
     * IndexingPolicy indexingPolicy = new IndexingPolicy(hashIndexOverride, rangeIndexOverride, spatialIndexOverride);
     * }
     * 
*

* If you would like to just override the indexingPolicy for Numbers you can specify just that: *

     * {@code
     * RangeIndex rangeIndexOverride = Index.Range(DataType.Number, 2);
     *
     * IndexingPolicy indexingPolicy = new IndexingPolicy(rangeIndexOverride);
     * }
     * 
* * @param defaultIndexOverrides comma separated set of indexes that serve as default index specifications for the root path. */ 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. */ public IndexingPolicy(String jsonString) { super(jsonString); } /** * Constructor. * * @param jsonObject the json object that represents the indexing policy. */ public IndexingPolicy(JSONObject jsonObject) { super(jsonObject); } /** * 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 */ public void setAutomatic(boolean automatic) { super.set(Constants.Properties.AUTOMATIC, automatic); } /** * Gets the indexing mode (consistent or lazy). * * @return the indexing mode. */ public IndexingMode getIndexingMode() { IndexingMode result = IndexingMode.Lazy; try { result = IndexingMode.valueOf(WordUtils.capitalize(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. */ public void setIndexingMode(IndexingMode indexingMode) { super.set(Constants.Properties.INDEXING_MODE, indexingMode.name()); } /** * Gets the paths that are chosen to be indexed by the user. * * @return the included paths. */ public Collection getIncludedPaths() { if (this.includedPaths == null) { this.includedPaths = super.getCollection(Constants.Properties.INCLUDED_PATHS, IncludedPath.class); if (this.includedPaths == null) { this.includedPaths = new ArrayList(); } } return this.includedPaths; } public void setIncludedPaths(Collection includedPaths) { this.includedPaths = includedPaths; } /** * Gets the paths that are not indexed. * * @return the excluded paths. */ public Collection getExcludedPaths() { if (this.excludedPaths == null) { this.excludedPaths = super.getCollection(Constants.Properties.EXCLUDED_PATHS, ExcludedPath.class); if (this.excludedPaths == null) { this.excludedPaths = new ArrayList(); } } return this.excludedPaths; } public void setExcludedPaths(Collection excludedPaths) { this.excludedPaths = excludedPaths; } /** * Gets the composite indexes for additional indexes. * * @return the composite indexes. */ public Collection> getCompositeIndexes() { if (this.compositeIndexes == null) { this.compositeIndexes = new ArrayList>(); JSONArray compositeIndexes = (JSONArray) super.get(Constants.Properties.COMPOSITE_INDEXES); for (int i = 0; i < compositeIndexes.length(); i ++) { JSONArray compositeIndex = (JSONArray) compositeIndexes.get(i); ArrayList compositePaths = new ArrayList(); for (int j = 0; j < compositeIndex.length(); 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. */ public void setCompositeIndexes(Collection> compositeIndexes) { this.compositeIndexes = compositeIndexes; super.set(Constants.Properties.COMPOSITE_INDEXES, this.compositeIndexes); } /** * Sets the spatial indexes for additional indexes. * * @return the spatial indexes. */ public Collection getSpatialIndexes() { if (this.spatialIndexes == null) { this.spatialIndexes = super.getCollection(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. */ public void setSpatialIndexes(Collection spatialIndexes) { this.spatialIndexes = spatialIndexes; super.set(Constants.Properties.SPATIAL_INDEXES, this.spatialIndexes); } @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); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy