/driver-core/src/test/functional/com/mongodb/client/syncadapter/SyncConnection.java

https://github.com/jyemin/mongo-java-driver · Java · 141 lines · 109 code · 17 blank · 15 comment · 0 complexity · c036e95ae769b7d0c138354494138eeb MD5 · raw file

  1. /*
  2. * Copyright 2008-present MongoDB, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.mongodb.client.syncadapter;
  17. import com.mongodb.MongoNamespace;
  18. import com.mongodb.ReadPreference;
  19. import com.mongodb.RequestContext;
  20. import com.mongodb.ServerApi;
  21. import com.mongodb.WriteConcernResult;
  22. import com.mongodb.connection.ConnectionDescription;
  23. import com.mongodb.internal.bulk.DeleteRequest;
  24. import com.mongodb.internal.bulk.InsertRequest;
  25. import com.mongodb.internal.bulk.UpdateRequest;
  26. import com.mongodb.internal.connection.AsyncConnection;
  27. import com.mongodb.internal.connection.Connection;
  28. import com.mongodb.internal.connection.QueryResult;
  29. import com.mongodb.internal.connection.SplittablePayload;
  30. import com.mongodb.internal.session.SessionContext;
  31. import org.bson.BsonDocument;
  32. import org.bson.FieldNameValidator;
  33. import org.bson.codecs.Decoder;
  34. import java.util.List;
  35. public final class SyncConnection implements Connection {
  36. private final AsyncConnection wrapped;
  37. public SyncConnection(final AsyncConnection connection) {
  38. wrapped = connection;
  39. }
  40. @Override
  41. public int getCount() {
  42. return wrapped.getCount();
  43. }
  44. @Override
  45. public int release() {
  46. return wrapped.release();
  47. }
  48. @Override
  49. public Connection retain() {
  50. wrapped.retain();
  51. return this;
  52. }
  53. @Override
  54. public ConnectionDescription getDescription() {
  55. return wrapped.getDescription();
  56. }
  57. @Override
  58. public WriteConcernResult insert(final MongoNamespace namespace, final boolean ordered, final InsertRequest insertRequest,
  59. final RequestContext requestContext) {
  60. SupplyingCallback<WriteConcernResult> callback = new SupplyingCallback<>();
  61. wrapped.insertAsync(namespace, ordered, insertRequest, requestContext, callback);
  62. return callback.get();
  63. }
  64. @Override
  65. public WriteConcernResult update(final MongoNamespace namespace, final boolean ordered, final UpdateRequest updateRequest,
  66. final RequestContext requestContext) {
  67. SupplyingCallback<WriteConcernResult> callback = new SupplyingCallback<>();
  68. wrapped.updateAsync(namespace, ordered, updateRequest, requestContext, callback);
  69. return callback.get();
  70. }
  71. @Override
  72. public WriteConcernResult delete(final MongoNamespace namespace, final boolean ordered, final DeleteRequest deleteRequest,
  73. final RequestContext requestContext) {
  74. SupplyingCallback<WriteConcernResult> callback = new SupplyingCallback<>();
  75. wrapped.deleteAsync(namespace, ordered, deleteRequest, requestContext, callback);
  76. return callback.get();
  77. }
  78. @Override
  79. public <T> T command(final String database, final BsonDocument command, final FieldNameValidator fieldNameValidator,
  80. final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext,
  81. final ServerApi serverApi, final RequestContext requestContext) {
  82. SupplyingCallback<T> callback = new SupplyingCallback<>();
  83. wrapped.commandAsync(database, command, fieldNameValidator, readPreference, commandResultDecoder, sessionContext, serverApi,
  84. requestContext, callback);
  85. return callback.get();
  86. }
  87. @Override
  88. public <T> T command(final String database, final BsonDocument command, final FieldNameValidator commandFieldNameValidator,
  89. final ReadPreference readPreference, final Decoder<T> commandResultDecoder, final SessionContext sessionContext,
  90. final ServerApi serverApi, final RequestContext requestContext, final boolean responseExpected, final SplittablePayload payload,
  91. final FieldNameValidator payloadFieldNameValidator) {
  92. SupplyingCallback<T> callback = new SupplyingCallback<>();
  93. wrapped.commandAsync(database, command, commandFieldNameValidator, readPreference, commandResultDecoder, sessionContext,
  94. serverApi, requestContext, responseExpected, payload, payloadFieldNameValidator, callback);
  95. return callback.get();
  96. }
  97. @Override
  98. public <T> QueryResult<T> query(final MongoNamespace namespace, final BsonDocument queryDocument, final BsonDocument fields,
  99. final int skip, final int limit, final int batchSize, final boolean secondaryOk, final boolean tailableCursor,
  100. final boolean awaitData, final boolean noCursorTimeout, final boolean partial, final boolean oplogReplay,
  101. final Decoder<T> resultDecoder, final RequestContext requestContext) {
  102. SupplyingCallback<QueryResult<T>> callback = new SupplyingCallback<>();
  103. wrapped.queryAsync(namespace, queryDocument, fields, skip, limit, batchSize, secondaryOk, tailableCursor, awaitData,
  104. noCursorTimeout, partial, oplogReplay, resultDecoder, requestContext, callback);
  105. return callback.get();
  106. }
  107. @Override
  108. public <T> QueryResult<T> getMore(final MongoNamespace namespace, final long cursorId, final int numberToReturn,
  109. final Decoder<T> resultDecoder, final RequestContext requestContext) {
  110. SupplyingCallback<QueryResult<T>> callback = new SupplyingCallback<>();
  111. wrapped.getMoreAsync(namespace, cursorId, numberToReturn, resultDecoder, requestContext, callback);
  112. return callback.get();
  113. }
  114. @Override
  115. public void killCursor(final MongoNamespace namespace, final List<Long> cursors, final RequestContext requestContext) {
  116. SupplyingCallback<Void> callback = new SupplyingCallback<>();
  117. wrapped.killCursorAsync(namespace, cursors, requestContext, callback);
  118. callback.get();
  119. }
  120. @Override
  121. public void markAsPinned(final PinningMode pinningMode) {
  122. wrapped.markAsPinned(pinningMode);
  123. }
  124. }