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

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

package com.microsoft.azure.documentdb;

import java.util.Collection;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;

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

/**
 * Represents a document collection in the Azure Cosmos DB database service. A collection is a named logical container
 * for documents.
 * 

* A database may contain zero or more named collections and each collection consists of zero or more JSON documents. * Being schema-free, the documents in a collection do not need to share the same structure or fields. Since collections * are application resources, they can be authorized using either the master key or resource keys. */ public final class DocumentCollection extends Resource { private IndexingPolicy indexingPolicy; private UniqueKeyPolicy uniqueKeyPolicy; /** * Initialize a document collection object. */ public DocumentCollection() { super(); } /** * Initialize a document collection object from json string. * * @param jsonString the json string that represents the document collection. */ public DocumentCollection(String jsonString) { super(jsonString); } /** * Initialize a document collection object from json object. * * @param jsonObject the json object that represents the document collection. */ public DocumentCollection(JSONObject jsonObject) { super(jsonObject); } /** * Gets the indexing policy. * * @return the indexing policy. */ public IndexingPolicy getIndexingPolicy() { if (this.indexingPolicy == null) { if (super.has(Constants.Properties.INDEXING_POLICY)) { this.indexingPolicy = super.getObject(Constants.Properties.INDEXING_POLICY, IndexingPolicy.class); } else { this.indexingPolicy = new IndexingPolicy(); } } return this.indexingPolicy; } /** * Sets the indexing policy. * * @param indexingPolicy the indexing policy. */ public void setIndexingPolicy(IndexingPolicy indexingPolicy) { if (indexingPolicy == null) { throw new IllegalArgumentException("IndexingPolicy cannot be null."); } this.indexingPolicy = indexingPolicy; } /** * Gets the conflictResolutionPolicy that is used for resolving conflicting writes * on documents in different regions, in a collection in the Azure Cosmos DB service. * * @return ConflictResolutionPolicy */ public ConflictResolutionPolicy getConflictResolutionPolicy() { return super.getObject(Constants.Properties.CONFLICT_RESOLUTION_POLICY, ConflictResolutionPolicy.class); } /** * Sets the conflictResolutionPolicy that is used for resolving conflicting writes * on documents in different regions, in a collection in the Azure Cosmos DB service. * * @param value ConflictResolutionPolicy to be used. */ public void setConflictResolutionPolicy(ConflictResolutionPolicy value) { if (value == null) { throw new IllegalArgumentException("CONFLICT_RESOLUTION_POLICY cannot be null."); } super.set(Constants.Properties.CONFLICT_RESOLUTION_POLICY, value); } /** * Gets the collection's partition key definition. * * @return the partition key definition. */ public PartitionKeyDefinition getPartitionKey() { if (super.has(Constants.Properties.PARTITION_KEY)) { return super.getObject(Constants.Properties.PARTITION_KEY, PartitionKeyDefinition.class); } this.setPartitionKey(new PartitionKeyDefinition()); return this.getPartitionKey(); } /** * Sets the collection's partition key definition. * * @param partitionKey the partition key definition. */ public void setPartitionKey(PartitionKeyDefinition partitionKey) { if (partitionKey == null) { throw new IllegalArgumentException("partitionKey cannot be null."); } super.set(Constants.Properties.PARTITION_KEY, partitionKey); } /** * Gets the collection's partition key statistics for each physical partition in the collection. * * @return the partition key statistics for each physical partition in the collection. */ public Collection getCollectionPartitionStatistics() { if (super.has(Constants.Properties.PARTITION_KEY_RANGE_STATISTICS)) { return super.getCollection(Constants.Properties.PARTITION_KEY_RANGE_STATISTICS, PartitionKeyRangeStatistics.class); } return null; } /** * Gets the collection's default time-to-live value. * * @return the the default time-to-live value in seconds. */ public Integer getDefaultTimeToLive() { if (super.has(Constants.Properties.DEFAULT_TTL)) { return super.getInt(Constants.Properties.DEFAULT_TTL); } return null; } /** * Sets the collection's default time-to-live value. *

* The default time-to-live value on a collection is an optional property. If set, the documents within the collection * expires after the specified number of seconds since their last write time. The value of this property should be one of the following: *

* null - indicates evaluation of time-to-live is disabled and documents within the collection will never expire, regardless whether * individual documents have their time-to-live set. *

* nonzero positive integer - indicates the default time-to-live value for all documents within the collection. This value can be overridden * by individual documents' time-to-live value. *

* -1 - indicates by default all documents within the collection never expire. This value can be overridden by individual documents' * time-to-live value. * * @param timeToLive the default time-to-live value in seconds. */ public void setDefaultTimeToLive(Integer timeToLive) { // a "null" value is represented as a missing element on the wire. // setting timeToLive to null should remove the property from the property bag. if (timeToLive != null) { super.set(Constants.Properties.DEFAULT_TTL, timeToLive); } else if (super.has(Constants.Properties.DEFAULT_TTL)) { super.remove(Constants.Properties.DEFAULT_TTL); } } /** * Sets the Uni that guarantees uniqueness of documents in collection in the Azure Cosmos DB service. * @return UniqueKeyPolicy */ public UniqueKeyPolicy getUniqueKeyPolicy() { // Thread safe lazy initialization for case when collection is cached (and is basically readonly). if (this.uniqueKeyPolicy == null) { this.uniqueKeyPolicy = super.getObject(Constants.Properties.UNIQUE_KEY_POLICY, UniqueKeyPolicy.class); if (this.uniqueKeyPolicy == null) { this.uniqueKeyPolicy = new UniqueKeyPolicy(); } } return this.uniqueKeyPolicy; } public void setUniqueKeyPolicy(UniqueKeyPolicy uniqueKeyPolicy) { if (uniqueKeyPolicy == null) { throw new IllegalArgumentException("uniqueKeyPolicy cannot be null."); } this.uniqueKeyPolicy = uniqueKeyPolicy; super.set(Constants.Properties.UNIQUE_KEY_POLICY, uniqueKeyPolicy); } /** * Gets the self-link for documents in a collection. * * @return the document link. */ public String getDocumentsLink() { return String.format("%s/%s", StringUtils.stripEnd(super.getSelfLink(), "/"), super.getString(Constants.Properties.DOCUMENTS_LINK)); } /** * Gets the self-link for stored procedures in a collection. * * @return the stored procedures link. */ public String getStoredProceduresLink() { return String.format("%s/%s", StringUtils.stripEnd(super.getSelfLink(), "/"), super.getString(Constants.Properties.STORED_PROCEDURES_LINK)); } /** * Gets the self-link for triggers in a collection. * * @return the trigger link. */ public String getTriggersLink() { return StringUtils.removeEnd(this.getSelfLink(), "/") + "/" + super.getString(Constants.Properties.TRIGGERS_LINK); } /** * Gets the self-link for user defined functions in a collection. * * @return the user defined functions link. */ public String getUserDefinedFunctionsLink() { return StringUtils.removeEnd(this.getSelfLink(), "/") + "/" + super.getString(Constants.Properties.USER_DEFINED_FUNCTIONS_LINK); } /** * Gets the self-link for conflicts in a collection. * * @return the conflicts link. */ public String getConflictsLink() { return StringUtils.removeEnd(this.getSelfLink(), "/") + "/" + super.getString(Constants.Properties.CONFLICTS_LINK); } @Override void populatePropertyBag() { if (this.indexingPolicy == null) { this.getIndexingPolicy(); } this.indexingPolicy.populatePropertyBag(); super.set(Constants.Properties.INDEXING_POLICY, this.indexingPolicy); if (this.uniqueKeyPolicy == null) { this.getUniqueKeyPolicy(); } this.uniqueKeyPolicy.populatePropertyBag(); super.set(Constants.Properties.UNIQUE_KEY_POLICY, this.uniqueKeyPolicy); if(this.getCollectionPartitionStatistics() != null) { for (PartitionKeyRangeStatistics pkRangeStats : this.getCollectionPartitionStatistics()) { pkRangeStats.populatePropertyBag(); } super.set(Constants.Properties.PARTITION_KEY_RANGE_STATISTICS, this.getCollectionPartitionStatistics()); } } @Override public boolean equals(Object obj) { if (obj == null || !DocumentCollection.class.isAssignableFrom(obj.getClass())) { return false; } DocumentCollection typedObj = (DocumentCollection) obj; return typedObj.getResourceId().equals(this.getResourceId()); } @Override public int hashCode() { return this.getResourceId().hashCode(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy