Skip to content

Commit

Permalink
feat(digital-twins): Add query API implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vinagesh committed Sep 8, 2020
1 parent 0471806 commit 9102fc0
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 53 deletions.
53 changes: 0 additions & 53 deletions sdk/digitaltwins/azure-digitaltwins-core/API design.md
Original file line number Diff line number Diff line change
Expand Up @@ -770,59 +770,6 @@ public DigitalTwinsResponse<Void> updateComponentWithResponse(String digitalTwin
```
</details>

## Query
<details><summary><b>Async APIs</b></summary>

```java
/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @return A {@link PagedFlux} of application/json for the query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux<String> query(String query)

/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @param clazz The model class to convert the query response to.
* @param <T> The generic type to convert the query response to.
* @return A {@link PagedFlux} of application/json for the query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public <T> PagedFlux<T> query(String query, Class<T> clazz)
```
</details>

<details><summary><b>Sync APIs</b></summary>

```java
/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return A {@link PagedIterable} of application/json for the query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<String> query(String query, Context context)

/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @param clazz The model class to convert the query response to.
* @param <T> The generic type to convert the query response to.
* @return A {@link PagedIterable} of application/json for the query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public <T> PagedIterable<T> query(String query, Class<T> clazz, Context context)
```
</details>

## Models
<details><summary><b>Examples</b></summary>
A model defines the properties, components, and relationships of a given digital twin. A sample model can be seen below
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.azure.digitaltwins.core.implementation.AzureDigitalTwinsAPIImplBuilder;
import com.azure.digitaltwins.core.implementation.converters.ModelDataConverter;
import com.azure.digitaltwins.core.implementation.models.DigitalTwinModelsListOptions;
import com.azure.digitaltwins.core.implementation.models.QuerySpecification;
import com.azure.digitaltwins.core.implementation.serializer.DigitalTwinsStringSerializer;
import com.azure.digitaltwins.core.models.IncomingRelationship;
import com.azure.digitaltwins.core.models.ModelData;
Expand Down Expand Up @@ -1067,4 +1068,128 @@ Mono<DigitalTwinsResponse<Void>> updateComponentWithResponse(String digitalTwinI
return Mono.just(new DigitalTwinsResponse<>(response.getRequest(), response.getStatusCode(), response.getHeaders(), null, twinHeaders));
});
}

/**
* Query digital twins.
* @param query The query string, in SQL-like syntax.
* @return A {@link PagedFlux} of application/json query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedFlux<String> query(String query) {
return new PagedFlux<>(
() -> withContext(context -> queryFirstPage(query, context)),
nextLink -> withContext(context -> queryNextPage(nextLink, context)));
}

PagedFlux<String> query(String query, Context context) {
return new PagedFlux<>(
() -> queryFirstPage(query, context),
nextLink -> queryNextPage(nextLink, context));
}

/**
* Query digital twins.
* @param query The query string, in SQL-like syntax.
* @param clazz The model class to convert the query response to.
* @param <T> The generic type to convert the query response to.
* @return A {@link PagedFlux} of application/json of the specified type.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public <T> PagedFlux<T> query(String query, Class<T> clazz) {
return new PagedFlux<T>(
() -> withContext(context -> queryFirstPage(query, clazz, context)),
nextLink -> withContext(context -> queryNextPage(nextLink, clazz, context)));
}

<T> PagedFlux<T> query(String query, Class<T> clazz, Context context) {
return new PagedFlux<>(
() -> queryFirstPage(query, clazz, context),
nextLink -> queryNextPage(nextLink, clazz, context));
}

Mono<PagedResponse<String>> queryFirstPage(String query, Context context) {
QuerySpecification querySpecification = new QuerySpecification().setQuery(query);

return protocolLayer
.getQueries()
.queryTwinsWithResponseAsync(querySpecification, context)
.map(objectPagedResponse -> new PagedResponseBase<>(
objectPagedResponse.getRequest(),
objectPagedResponse.getStatusCode(),
objectPagedResponse.getHeaders(),
objectPagedResponse.getValue().getItems().stream()
.map(object -> {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
logger.error("JsonProcessingException occurred while retrieving query result items: ", e);
throw new RuntimeException("JsonProcessingException occurred while retrieving query result items", e);
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList()),
objectPagedResponse.getValue().getContinuationToken(),
objectPagedResponse.getDeserializedHeaders()));
}

<T> Mono<PagedResponse<T>> queryFirstPage(String query, Class<T> clazz, Context context) {
QuerySpecification querySpecification = new QuerySpecification().setQuery(query);

return protocolLayer
.getQueries()
.queryTwinsWithResponseAsync(querySpecification, context)
.map(objectPagedResponse -> new PagedResponseBase<>(
objectPagedResponse.getRequest(),
objectPagedResponse.getStatusCode(),
objectPagedResponse.getHeaders(),
objectPagedResponse.getValue().getItems().stream()
.map(object -> mapper.convertValue(object, clazz))
.filter(Objects::nonNull)
.collect(Collectors.toList()),
objectPagedResponse.getValue().getContinuationToken(),
objectPagedResponse.getDeserializedHeaders()));
}

Mono<PagedResponse<String>> queryNextPage(String nextLink, Context context) {
QuerySpecification querySpecification = new QuerySpecification().setContinuationToken(nextLink);

return protocolLayer
.getQueries()
.queryTwinsWithResponseAsync(querySpecification, context)
.map(objectPagedResponse -> new PagedResponseBase<>(
objectPagedResponse.getRequest(),
objectPagedResponse.getStatusCode(),
objectPagedResponse.getHeaders(),
objectPagedResponse.getValue().getItems().stream()
.map(object -> {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
logger.error("JsonProcessingException occurred while retrieving query result items: ", e);
throw new RuntimeException("JsonProcessingException occurred while retrieving query result items", e);
}
})
.filter(Objects::nonNull)
.collect(Collectors.toList()),
objectPagedResponse.getValue().getContinuationToken(),
objectPagedResponse.getDeserializedHeaders()));
}

<T> Mono<PagedResponse<T>> queryNextPage(String nextLink, Class<T> clazz, Context context) {
QuerySpecification querySpecification = new QuerySpecification().setContinuationToken(nextLink);

return protocolLayer
.getQueries()
.queryTwinsWithResponseAsync(querySpecification, context)
.map(objectPagedResponse -> new PagedResponseBase<>(
objectPagedResponse.getRequest(),
objectPagedResponse.getStatusCode(),
objectPagedResponse.getHeaders(),
objectPagedResponse.getValue().getItems().stream()
.map(object -> mapper.convertValue(object, clazz))
.filter(Objects::nonNull)
.collect(Collectors.toList()),
objectPagedResponse.getValue().getContinuationToken(),
objectPagedResponse.getDeserializedHeaders()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -636,4 +636,54 @@ public void updateComponent(String digitalTwinId, String componentPath, List<Obj
public DigitalTwinsResponse<Void> updateComponentWithResponse(String digitalTwinId, String componentPath, List<Object> componentUpdateOperations, UpdateComponentRequestOptions options, Context context) {
return digitalTwinsAsyncClient.updateComponentWithResponse(digitalTwinId, componentPath, componentUpdateOperations, options, context).block();
}

/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @return A {@link PagedIterable} of application/json query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<String> query(String query) {
return new PagedIterable<>(digitalTwinsAsyncClient.query(query, Context.NONE));
}

/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @return A {@link PagedIterable} of application/json query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public PagedIterable<String> query(String query, Context context) {
return new PagedIterable<>(digitalTwinsAsyncClient.query(query, context));
}

/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @param clazz The model class to convert the query response to.
* @param <T> The generic type to convert the query response to.
* @return A {@link PagedIterable} of application/json query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public <T> PagedIterable<T> query(String query, Class<T> clazz) {
return new PagedIterable<>(digitalTwinsAsyncClient.query(query, clazz, Context.NONE));
}

/**
* Query digital twins.
*
* @param query The query string, in SQL-like syntax.
* @param context Additional context that is passed through the Http pipeline during the service call.
* @param clazz The model class to convert the query response to.
* @param <T> The generic type to convert the query response to.
* @return A {@link PagedIterable} of application/json query result items.
*/
@ServiceMethod(returns = ReturnType.COLLECTION)
public <T> PagedIterable<T> query(String query, Class<T> clazz, Context context) {
return new PagedIterable<>(digitalTwinsAsyncClient.query(query, clazz, context));
}
}

0 comments on commit 9102fc0

Please sign in to comment.