-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Sync-Wrapper for V3 Async SDK #4757
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
76255d8
3fe7154
08adc4a
c0c638e
33d1dbe
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,230 @@ | ||||||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||||||
| // Licensed under the MIT License. | ||||||
|
|
||||||
| package com.azure.data.cosmos.sync; | ||||||
|
|
||||||
| import com.azure.data.cosmos.CosmosClient; | ||||||
| import com.azure.data.cosmos.CosmosClientBuilder; | ||||||
| import com.azure.data.cosmos.CosmosClientException; | ||||||
| import com.azure.data.cosmos.CosmosDatabaseProperties; | ||||||
| import com.azure.data.cosmos.CosmosDatabaseRequestOptions; | ||||||
| import com.azure.data.cosmos.CosmosDatabaseResponse; | ||||||
| import com.azure.data.cosmos.FeedOptions; | ||||||
| import com.azure.data.cosmos.FeedResponse; | ||||||
| import com.azure.data.cosmos.SqlQuerySpec; | ||||||
| import reactor.core.Exceptions; | ||||||
| import reactor.core.publisher.Mono; | ||||||
|
|
||||||
| import java.util.Iterator; | ||||||
|
|
||||||
| /** | ||||||
| * Provides a client-side logical representation of the Azure Cosmos database service. | ||||||
| * SyncClient is used to perform operations in a synchronous way | ||||||
| */ | ||||||
| public class CosmosSyncClient { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Based on guidelines, this should be final
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if we make this final, then the neither the end user nor us can mock this for testing.
mbhaskar marked this conversation as resolved.
Outdated
|
||||||
| private CosmosClient asyncClientWrapper; | ||||||
|
|
||||||
| public CosmosSyncClient(CosmosClientBuilder builder) { | ||||||
| this.asyncClientWrapper = builder.build(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Instantiate the cosmos client builder to build cosmos client | ||||||
| * @return {@link CosmosClientBuilder} | ||||||
| */ | ||||||
| public static CosmosClientBuilder builder(){ | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (note for ADP folks: This doesn't match the guidelines, but it matches our existing pattern which we've gotten an exception for.) |
||||||
| return new CosmosClientBuilder(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Create a Database if it does not already exist on the service | ||||||
| * | ||||||
| * @param databaseProperties {@link CosmosDatabaseProperties} the database properties | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception. | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabaseIfNotExists(CosmosDatabaseProperties databaseProperties) | ||||||
| throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabaseIfNotExists(databaseProperties)); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Create a Database if it does not already exist on the service | ||||||
| * | ||||||
| * @param id the id of the database | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception. | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabaseIfNotExists(String id) throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabaseIfNotExists(id)); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| /** | ||||||
| * Creates a database. | ||||||
| * | ||||||
| * @param databaseProperties {@link CosmosDatabaseProperties} the database properties. | ||||||
| * @param options the request options. | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception. | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, | ||||||
| CosmosDatabaseRequestOptions options) throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabase(databaseProperties, options)); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates a database. | ||||||
| * | ||||||
| * @param databaseProperties {@link CosmosDatabaseProperties} the database properties. | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception. | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties) throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabase(databaseProperties)); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates a database. | ||||||
| * | ||||||
| * @param id the id of the database | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception. | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabase(String id) throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabase(id)); | ||||||
|
|
||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates a database. | ||||||
| * | ||||||
| * @param databaseProperties {@link CosmosDatabaseProperties} the database properties. | ||||||
| * @param throughput the throughput | ||||||
| * @param options {@link CosmosDatabaseRequestOptions} the request options | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, | ||||||
| int throughput, | ||||||
| CosmosDatabaseRequestOptions options) throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabase(databaseProperties, throughput, options)); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Creates a database. | ||||||
| * | ||||||
| * @param databaseProperties {@link CosmosDatabaseProperties} the database properties. | ||||||
| * @param throughput the throughput | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabase(CosmosDatabaseProperties databaseProperties, | ||||||
| int throughput) throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabase(databaseProperties, throughput)); | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| /** | ||||||
| * Creates a database. | ||||||
| * | ||||||
| * @param id the id of the database | ||||||
| * @param throughput the throughput | ||||||
| * @return the {@link CosmosSyncDatabaseResponse} with the created database. | ||||||
| * @throws CosmosClientException the cosmos client exception | ||||||
| */ | ||||||
| public CosmosSyncDatabaseResponse createDatabase(String id, int throughput) throws CosmosClientException { | ||||||
| return mapDatabaseResponseAndBlock(asyncClientWrapper.createDatabase(id, throughput)); | ||||||
| } | ||||||
|
|
||||||
| CosmosSyncDatabaseResponse mapDatabaseResponseAndBlock(Mono<CosmosDatabaseResponse> databaseMono) | ||||||
| throws CosmosClientException { | ||||||
| try { | ||||||
| return databaseMono | ||||||
| .map(this::convertResponse) | ||||||
| .block(); | ||||||
| } catch (Exception ex) { | ||||||
| final Throwable throwable = Exceptions.unwrap(ex); | ||||||
| if (throwable instanceof CosmosClientException) { | ||||||
| throw (CosmosClientException) throwable; | ||||||
|
mbhaskar marked this conversation as resolved.
|
||||||
| } else { | ||||||
| throw ex; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Reads all databases. | ||||||
| * | ||||||
| * @param options {@link FeedOptions}the feed options. | ||||||
| * @return the iterator for feed response with the read databases. | ||||||
| */ | ||||||
| public Iterator<FeedResponse<CosmosDatabaseProperties>> readAllDatabases(FeedOptions options) { | ||||||
| return asyncClientWrapper.readAllDatabases(options) | ||||||
| .toIterable() | ||||||
| .iterator(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Reads all databases. | ||||||
| * | ||||||
| * @return the iterator for feed response with the read databases. | ||||||
| */ | ||||||
| public Iterator<FeedResponse<CosmosDatabaseProperties>> readAllDatabases() { | ||||||
| return asyncClientWrapper.readAllDatabases() | ||||||
| .toIterable() | ||||||
| .iterator(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Query a database | ||||||
| * | ||||||
| * @param query the query | ||||||
| * @param options {@link FeedOptions}the feed options. | ||||||
| * @return the iterator for feed response with the obtained databases. | ||||||
| */ | ||||||
| public Iterator<FeedResponse<CosmosDatabaseProperties>> queryDatabases(String query, FeedOptions options) { | ||||||
| return asyncClientWrapper.queryDatabases(query, options) | ||||||
| .toIterable() | ||||||
| .iterator(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Query a database | ||||||
| * | ||||||
| * @param querySpec {@link SqlQuerySpec} the query spec | ||||||
| * @param options the query | ||||||
| * @return the iterator for feed response with the obtained databases. | ||||||
| */ | ||||||
| public Iterator<FeedResponse<CosmosDatabaseProperties>> queryDatabases(SqlQuerySpec querySpec, FeedOptions options) { | ||||||
| return asyncClientWrapper.queryDatabases(querySpec, options) | ||||||
| .toIterable() | ||||||
| .iterator(); | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Gets the database client | ||||||
| * | ||||||
| * @param id the id of the database | ||||||
| * @return {@link CosmosSyncDatabase} the cosmos sync database | ||||||
| */ | ||||||
| public CosmosSyncDatabase getDatabase(String id) { | ||||||
| return new CosmosSyncDatabase(id, this, asyncClientWrapper.getDatabase(id)); | ||||||
| } | ||||||
|
|
||||||
| CosmosSyncDatabaseResponse convertResponse(CosmosDatabaseResponse response) { | ||||||
| return new CosmosSyncDatabaseResponse(response, this); | ||||||
| } | ||||||
|
|
||||||
| CosmosClient getAsyncClient() { | ||||||
|
mbhaskar marked this conversation as resolved.
Outdated
|
||||||
| return this.asyncClientWrapper; | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Close this {@link CosmosSyncClient} instance | ||||||
| */ | ||||||
| public void close() { | ||||||
| asyncClientWrapper.close(); | ||||||
| } | ||||||
|
|
||||||
| } | ||||||
Uh oh!
There was an error while loading. Please reload this page.