Skip to content

Commit

Permalink
Added support for accessing non partitioned collections using V3 SDK (A…
Browse files Browse the repository at this point in the history
…zure#107)

* NP->P changes
  • Loading branch information
Srinath Narayanan authored and Christopher Anderson committed Jun 9, 2019
1 parent ed98066 commit cdd68cf
Show file tree
Hide file tree
Showing 119 changed files with 5,298 additions and 4,096 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ hs_err_pid*

doc
docs
target
bin
test-output
4 changes: 4 additions & 0 deletions commons-test-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ SOFTWARE.
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.stream.Collectors;

import com.microsoft.azure.cosmos.CosmosItemSettings;
import com.microsoft.azure.cosmosdb.BridgeInternal;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.microsoft.azure.cosmosdb.CompositePath;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void validate(List<FeedResponse<T>> feedList) {
return this;
}

public Builder<T> containsExactly(List<String> expectedIds) {
public Builder<T> containsExactly(List<String> expectedRids) {
validators.add(new FeedResponseListValidator<T>() {
@Override
public void validate(List<FeedResponse<T>> feedList) {
Expand All @@ -83,6 +84,23 @@ public void validate(List<FeedResponse<T>> feedList) {
.collect(Collectors.toList());
assertThat(actualIds)
.describedAs("Resource IDs of results")
.containsExactlyElementsOf(expectedRids);
}
});
return this;
}

public Builder<T> containsExactlyIds(List<String> expectedIds) {
validators.add(new FeedResponseListValidator<T>() {
@Override
public void validate(List<FeedResponse<T>> feedList) {
List<String> actualIds = feedList
.stream()
.flatMap(f -> f.getResults().stream())
.map(r -> r.getId())
.collect(Collectors.toList());
assertThat(actualIds)
.describedAs("IDs of results")
.containsExactlyElementsOf(expectedIds);
}
});
Expand Down Expand Up @@ -186,11 +204,11 @@ public void validate(List<FeedResponse<T>> feedList) {
}

public Builder<T> withAggregateValue(Object value) {
validators.add(new FeedResponseListValidator<Document>() {
validators.add(new FeedResponseListValidator<CosmosItemSettings>() {
@Override
public void validate(List<FeedResponse<Document>> feedList) {
List<Document> list = feedList.get(0).getResults();
Document result = list.size() > 0 ? list.get(0) : null;
public void validate(List<FeedResponse<CosmosItemSettings>> feedList) {
List<CosmosItemSettings> list = feedList.get(0).getResults();
CosmosItemSettings result = list.size() > 0 ? list.get(0) : null;

if (result != null) {
if (value instanceof Double) {
Expand Down Expand Up @@ -223,13 +241,13 @@ public void validate(List<FeedResponse<Document>> feedList) {
return this;
}

public Builder<T> withOrderedResults(ArrayList<Document> expectedOrderedList,
public Builder<T> withOrderedResults(ArrayList<CosmosItemSettings> expectedOrderedList,
ArrayList<CompositePath> compositeIndex) {
validators.add(new FeedResponseListValidator<Document>() {
validators.add(new FeedResponseListValidator<CosmosItemSettings>() {
@Override
public void validate(List<FeedResponse<Document>> feedList) {
public void validate(List<FeedResponse<CosmosItemSettings>> feedList) {

List<Document> resultOrderedList = feedList.stream()
List<CosmosItemSettings> resultOrderedList = feedList.stream()
.flatMap(f -> f.getResults().stream())
.collect(Collectors.toList());
assertThat(expectedOrderedList.size()).isEqualTo(resultOrderedList.size());
Expand Down
25 changes: 25 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,31 @@ SOFTWARE.
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
</dependency>
<dependency>
<groupId>io.projectreactor.addons</groupId>
<artifactId>reactor-adapter</artifactId>
<version>${reactor-addons.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.akarnokd</groupId>
<artifactId>rxjava2-interop</artifactId>
<version>${rxjava2interop.verison}</version>
</dependency>
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>${rxjava2.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,4 @@ protected RequestOptions toRequestOptions() {
requestOptions.setConsistencyLevel(consistencyLevel);
return requestOptions;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
package com.microsoft.azure.cosmos;

import com.microsoft.azure.cosmosdb.ConflictResolutionPolicy;
import com.microsoft.azure.cosmosdb.DocumentCollection;
import com.microsoft.azure.cosmosdb.IndexingPolicy;
import com.microsoft.azure.cosmosdb.PartitionKeyDefinition;
Expand Down Expand Up @@ -179,6 +180,30 @@ public void setPartitionKey(PartitionKeyDefinition partitionKeyDefinition) {
this.partitionKeyDefinition = partitionKeyDefinition;
}

/**
* 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);
}

DocumentCollection getV2Collection(){
DocumentCollection collection = new DocumentCollection(this.toJson());
collection.setPartitionKey(this.getPartitionKey());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ protected RequestOptions toRequestOptions() {
requestOptions.setOfferThroughput(offerThroughput);
return requestOptions;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,4 @@ public CosmosDatabaseSettings(String id) {
static List<CosmosDatabaseSettings> getFromV2Results(List<Database> results){
return results.stream().map(CosmosDatabaseSettings::new).collect(Collectors.toList());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,4 @@ protected RequestOptions toRequestOptions() {
requestOptions.setPartitionKey(partitionKey);
return requestOptions;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,17 @@ public CosmosItemSettings(String jsonString) {
super(jsonString);
}


/**
* Initialize an CosmosItemSettings object from json string.
*
* @param jsonString the json string that represents the item object.
* @param objectMapper the custom object mapper
*/
public CosmosItemSettings(String jsonString, ObjectMapper objectMapper) {
super(jsonString, objectMapper);
}

/**
* fromObject returns Document for compatibility with V2 sdk
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ protected RequestOptions toRequestOptions(){
requestOptions.setAccessCondition(accessCondition);
return requestOptions;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@
*/
package com.microsoft.azure.cosmos;

import com.microsoft.azure.cosmosdb.ClientSideRequestStatistics;
import com.microsoft.azure.cosmosdb.Resource;
import com.microsoft.azure.cosmosdb.ResourceResponse;
import com.microsoft.azure.cosmosdb.StoredProcedureResponse;

import java.time.Duration;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

public class CosmosResponse<T extends Resource> {
private T resourceSettings;
protected ResourceResponse resourceResponseWrapper;
Expand Down Expand Up @@ -134,5 +138,21 @@ public Map<String, String> getResponseHeaders() {
return resourceResponseWrapper.getResponseHeaders();
}


/**
* Gets the diagnostics information for the current request to Azure Cosmos DB service.
*
* @return diagnostics information for the current request to Azure Cosmos DB service.
*/
public String getRequestDiagnosticsString() {
return resourceResponseWrapper.getRequestDiagnosticsString();
}

/**
* Gets the end-to-end request latency for the current request to Azure Cosmos DB service.
*
* @return end-to-end request latency for the current request to Azure Cosmos DB service.
*/
public Duration getRequestLatency() {
return resourceResponseWrapper.getRequestLatency();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.microsoft.azure.cosmos;

import java.util.List;
import java.util.stream.Collectors;

import com.microsoft.azure.cosmosdb.DocumentCollection;
import com.microsoft.azure.cosmosdb.Resource;
import com.microsoft.azure.cosmosdb.ResourceResponse;
import com.microsoft.azure.cosmosdb.User;
import com.microsoft.azure.cosmosdb.internal.Constants;

public class CosmosUserSettings extends Resource {
/**
* Initialize a user object.
*/
public CosmosUserSettings() {
super();
}

/**
* Initialize a user object from json string.
*
* @param jsonString the json string that represents the database user.
*/
public CosmosUserSettings(String jsonString) {
super(jsonString);
}

CosmosUserSettings(ResourceResponse<User> response) {
super(response.getResource().toJson());
}

// Converting document collection to CosmosContainerSettings
CosmosUserSettings(User user){
super(user.toJson());
}

/**
* Gets the self-link of the permissions associated with the user.
*
* @return the permissions link.
*/
public String getPermissionsLink() {
String selfLink = this.getSelfLink();
if (selfLink.endsWith("/")) {
return selfLink + super.getString(Constants.Properties.PERMISSIONS_LINK);
} else {
return selfLink + "/" + super.getString(Constants.Properties.PERMISSIONS_LINK);
}
}

public User getV2User() {
return new User(this.toJson());
}

static List<CosmosUserSettings> getFromV2Results(List<User> results) {
return results.stream().map(CosmosUserSettings::new).collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.microsoft.azure.cosmosdb.internal.HttpConstants;
import com.microsoft.azure.cosmosdb.internal.query.metrics.ClientSideMetrics;
import com.microsoft.azure.cosmosdb.internal.routing.PartitionKeyInternal;
import com.microsoft.azure.cosmosdb.rx.internal.RxDocumentServiceResponse;
import com.microsoft.azure.cosmosdb.rx.internal.Strings;

Expand Down Expand Up @@ -282,4 +283,12 @@ public static String getInnerErrorMessage(DocumentClientException documentClient
}
return documentClientException.getInnerErrorMessage();
}

public static PartitionKeyInternal getNonePartitionKey(PartitionKeyDefinition partitionKeyDefinition) {
return partitionKeyDefinition.getNonePartitionKeyValue();
}

public static PartitionKey getPartitionKey(PartitionKeyInternal partitionKeyInternal) {
return new PartitionKey(partitionKeyInternal);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ public static PartitionKey FromJsonString(String jsonString) {
return new PartitionKey(PartitionKeyInternal.fromJsonString(jsonString));
}

public static PartitionKey None = new PartitionKey(PartitionKeyInternal.None);

/**
* Serialize the PartitionKey object to a JSON string.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package com.microsoft.azure.cosmosdb;

import com.microsoft.azure.cosmosdb.internal.Constants;
import com.microsoft.azure.cosmosdb.internal.routing.PartitionKeyInternal;
import com.microsoft.azure.cosmosdb.rx.internal.Strings;

import java.util.ArrayList;
Expand All @@ -40,6 +41,7 @@ public final class PartitionKeyDefinition extends JsonSerializable {
private List<String> paths;
private PartitionKind kind;
private PartitionKeyDefinitionVersion version;
private Boolean systemKey;

/**
* Constructor. Creates a new instance of the PartitionKeyDefinition object.
Expand Down Expand Up @@ -134,6 +136,31 @@ public void setPaths(List<String> paths) {
this.paths = paths;
}

/**
* Indicates if the partition key is generated by the system.
*
* @return the boolean indicating is it is a system key.
*/
Boolean isSystemKey() {
if (this.systemKey == null) {
if (super.has(Constants.Properties.SYSTEM_KEY)) {
this.systemKey = super.getBoolean(Constants.Properties.SYSTEM_KEY);
} else {
this.systemKey = false;
}
}

return this.systemKey;
}

PartitionKeyInternal getNonePartitionKeyValue() {
if (this.getPaths().size() == 0 || this.isSystemKey()) {
return PartitionKeyInternal.Empty;
} else {
return PartitionKeyInternal.UndefinedPartitionKey;
}
}

@Override
void populatePropertyBag() {
if (this.kind != null) {
Expand Down
Loading

0 comments on commit cdd68cf

Please sign in to comment.