PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/sdk/cosmos/azure-cosmos/src/main/java/com/azure/cosmos/CosmosAsyncStoredProcedure.java

http://github.com/WindowsAzure/azure-sdk-for-java
Java | 246 lines | 128 code | 25 blank | 93 comment | 8 complexity | 615ffb08e8a2961f54dac980922d2161 MD5 | raw file
Possible License(s): MIT
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License.
  3. package com.azure.cosmos;
  4. import com.azure.core.util.Context;
  5. import com.azure.cosmos.implementation.Paths;
  6. import com.azure.cosmos.implementation.StoredProcedure;
  7. import com.azure.cosmos.models.CosmosStoredProcedureResponse;
  8. import com.azure.cosmos.models.CosmosStoredProcedureProperties;
  9. import com.azure.cosmos.models.CosmosStoredProcedureRequestOptions;
  10. import com.azure.cosmos.models.ModelBridgeInternal;
  11. import reactor.core.publisher.Mono;
  12. import static com.azure.core.util.FluxUtil.withContext;
  13. import java.util.List;
  14. /**
  15. * The type Cosmos async stored procedure.
  16. */
  17. public class CosmosAsyncStoredProcedure {
  18. @SuppressWarnings("EnforceFinalFields")
  19. private final CosmosAsyncContainer cosmosContainer;
  20. private String id;
  21. CosmosAsyncStoredProcedure(String id, CosmosAsyncContainer cosmosContainer) {
  22. this.id = id;
  23. this.cosmosContainer = cosmosContainer;
  24. }
  25. /**
  26. * Get the id of the {@link CosmosAsyncStoredProcedure}
  27. *
  28. * @return the id of the {@link CosmosAsyncStoredProcedure}
  29. */
  30. public String getId() {
  31. return id;
  32. }
  33. /**
  34. * Set the id of the {@link CosmosAsyncStoredProcedure}
  35. *
  36. * @param id the id of the {@link CosmosAsyncStoredProcedure}
  37. * @return the same {@link CosmosAsyncStoredProcedure} that had the id set
  38. */
  39. CosmosAsyncStoredProcedure setId(String id) {
  40. this.id = id;
  41. return this;
  42. }
  43. /**
  44. * Read a stored procedure
  45. * <p>
  46. * After subscription the operation will be performed.
  47. * The {@link Mono} upon successful completion will contain a single resource response with the read stored
  48. * procedure.
  49. * In case of failure the {@link Mono} will error.
  50. *
  51. * @return an {@link Mono} containing the single resource response with the read stored procedure or an error.
  52. */
  53. public Mono<CosmosStoredProcedureResponse> read() {
  54. return read(null);
  55. }
  56. /**
  57. * Read a stored procedure
  58. * <p>
  59. * After subscription the operation will be performed.
  60. * The {@link Mono} upon successful completion will contain a single resource response with the read stored
  61. * procedure.
  62. * In case of failure the {@link Mono} will error.
  63. *
  64. * @param options the request options.
  65. * @return an {@link Mono} containing the single resource response with the read stored procedure or an error.
  66. */
  67. public Mono<CosmosStoredProcedureResponse> read(CosmosStoredProcedureRequestOptions options) {
  68. return withContext(context -> readInternal(options, context));
  69. }
  70. /**
  71. * Deletes a stored procedure
  72. * <p>
  73. * After subscription the operation will be performed.
  74. * The {@link Mono} upon successful completion will contain a single resource response for the deleted stored
  75. * procedure.
  76. * In case of failure the {@link Mono} will error.
  77. *
  78. * @return an {@link Mono} containing the single resource response for the deleted stored procedure or an error.
  79. */
  80. public Mono<CosmosStoredProcedureResponse> delete() {
  81. return delete(null);
  82. }
  83. /**
  84. * Deletes a stored procedure
  85. * <p>
  86. * After subscription the operation will be performed.
  87. * The {@link Mono} upon successful completion will contain a single resource response for the deleted stored
  88. * procedure.
  89. * In case of failure the {@link Mono} will error.
  90. *
  91. * @param options the request options.
  92. * @return an {@link Mono} containing the single resource response for the deleted stored procedure or an error.
  93. */
  94. public Mono<CosmosStoredProcedureResponse> delete(CosmosStoredProcedureRequestOptions options) {
  95. return withContext(context -> deleteInternal(options, context));
  96. }
  97. /**
  98. * Executes a stored procedure
  99. * <p>
  100. * After subscription the operation will be performed.
  101. * The {@link Mono} upon successful completion will contain a single resource response with the stored procedure
  102. * response.
  103. * In case of failure the {@link Mono} will error.
  104. *
  105. * @param procedureParams the list of procedure parameter values.
  106. * @param options the request options.
  107. * @return an {@link Mono} containing the single resource response with the stored procedure response or an error.
  108. */
  109. public Mono<CosmosStoredProcedureResponse> execute(List<Object> procedureParams,
  110. CosmosStoredProcedureRequestOptions options) {
  111. return withContext(context -> executeInternal(procedureParams, options, context));
  112. }
  113. /**
  114. * Replaces a stored procedure.
  115. * <p>
  116. * After subscription the operation will be performed.
  117. * The {@link Mono} upon successful completion will contain a single resource response with the replaced stored
  118. * procedure.
  119. * In case of failure the {@link Mono} will error.
  120. *
  121. * @param storedProcedureProperties the stored procedure properties
  122. * @return an {@link Mono} containing the single resource response with the replaced stored procedure or an error.
  123. */
  124. public Mono<CosmosStoredProcedureResponse> replace(CosmosStoredProcedureProperties storedProcedureProperties) {
  125. return replace(storedProcedureProperties, null);
  126. }
  127. /**
  128. * Replaces a stored procedure.
  129. * <p>
  130. * After subscription the operation will be performed.
  131. * The {@link Mono} upon successful completion will contain a single resource response with the replaced stored
  132. * procedure.
  133. * In case of failure the {@link Mono} will error.
  134. *
  135. * @param storedProcedureProperties the stored procedure properties.
  136. * @param options the request options.
  137. * @return an {@link Mono} containing the single resource response with the replaced stored procedure or an error.
  138. */
  139. public Mono<CosmosStoredProcedureResponse> replace(CosmosStoredProcedureProperties storedProcedureProperties,
  140. CosmosStoredProcedureRequestOptions options) {
  141. return withContext(context -> replaceInternal(storedProcedureProperties, options,
  142. context));
  143. }
  144. String getURIPathSegment() {
  145. return Paths.STORED_PROCEDURES_PATH_SEGMENT;
  146. }
  147. String getParentLink() {
  148. return cosmosContainer.getLink();
  149. }
  150. String getLink() {
  151. StringBuilder builder = new StringBuilder();
  152. builder.append(getParentLink());
  153. builder.append("/");
  154. builder.append(getURIPathSegment());
  155. builder.append("/");
  156. builder.append(getId());
  157. return builder.toString();
  158. }
  159. private Mono<CosmosStoredProcedureResponse> readInternal(CosmosStoredProcedureRequestOptions options,
  160. Context context) {
  161. if (options == null) {
  162. options = new CosmosStoredProcedureRequestOptions();
  163. }
  164. String spanName = "readStoredProcedure." + cosmosContainer.getId();
  165. Mono<CosmosStoredProcedureResponse> responseMono = cosmosContainer.getDatabase().getDocClientWrapper().readStoredProcedure(getLink(),
  166. ModelBridgeInternal.toRequestOptions(options))
  167. .map(response -> ModelBridgeInternal.createCosmosStoredProcedureResponse(response)).single();
  168. return this.cosmosContainer.getDatabase().getClient().getTracerProvider().traceEnabledCosmosResponsePublisher(responseMono,
  169. context,
  170. spanName,
  171. cosmosContainer.getDatabase().getId(),
  172. cosmosContainer.getDatabase().getClient().getServiceEndpoint());
  173. }
  174. private Mono<CosmosStoredProcedureResponse> deleteInternal(CosmosStoredProcedureRequestOptions options,
  175. Context context) {
  176. if (options == null) {
  177. options = new CosmosStoredProcedureRequestOptions();
  178. }
  179. String spanName = "deleteStoredProcedure." + cosmosContainer.getId();
  180. Mono<CosmosStoredProcedureResponse> responseMono = cosmosContainer.getDatabase()
  181. .getDocClientWrapper()
  182. .deleteStoredProcedure(getLink(), ModelBridgeInternal.toRequestOptions(options))
  183. .map(response -> ModelBridgeInternal.createCosmosStoredProcedureResponse(response))
  184. .single();
  185. return this.cosmosContainer.getDatabase().getClient().getTracerProvider().traceEnabledCosmosResponsePublisher(responseMono,
  186. context,
  187. spanName,
  188. cosmosContainer.getDatabase().getId(),
  189. cosmosContainer.getDatabase().getClient().getServiceEndpoint());
  190. }
  191. private Mono<CosmosStoredProcedureResponse> executeInternal(List<Object> procedureParams,
  192. CosmosStoredProcedureRequestOptions options,
  193. Context context) {
  194. if (options == null) {
  195. options = new CosmosStoredProcedureRequestOptions();
  196. }
  197. String spanName = "executeStoredProcedure." + cosmosContainer.getId();
  198. Mono<CosmosStoredProcedureResponse> responseMono = cosmosContainer.getDatabase()
  199. .getDocClientWrapper()
  200. .executeStoredProcedure(getLink(), ModelBridgeInternal.toRequestOptions(options), procedureParams)
  201. .map(response -> ModelBridgeInternal.createCosmosStoredProcedureResponse(response))
  202. .single();
  203. return this.cosmosContainer.getDatabase().getClient().getTracerProvider().traceEnabledCosmosResponsePublisher(responseMono, context, spanName, cosmosContainer.getDatabase().getId(), cosmosContainer.getDatabase().getClient().getServiceEndpoint());
  204. }
  205. private Mono<CosmosStoredProcedureResponse> replaceInternal(CosmosStoredProcedureProperties storedProcedureSettings,
  206. CosmosStoredProcedureRequestOptions options,
  207. Context context) {
  208. if (options == null) {
  209. options = new CosmosStoredProcedureRequestOptions();
  210. }
  211. String spanName = "replaceStoredProcedure." + cosmosContainer.getId();
  212. Mono<CosmosStoredProcedureResponse> responseMono = cosmosContainer.getDatabase()
  213. .getDocClientWrapper()
  214. .replaceStoredProcedure(new StoredProcedure(ModelBridgeInternal.toJsonFromJsonSerializable(
  215. ModelBridgeInternal.getResource(storedProcedureSettings))),
  216. ModelBridgeInternal.toRequestOptions(options))
  217. .map(response -> ModelBridgeInternal.createCosmosStoredProcedureResponse(response))
  218. .single();
  219. return this.cosmosContainer.getDatabase().getClient().getTracerProvider().traceEnabledCosmosResponsePublisher(responseMono, context, spanName, cosmosContainer.getDatabase().getId(), cosmosContainer.getDatabase().getClient().getServiceEndpoint());
  220. }
  221. }