/driver-core/src/main/com/mongodb/internal/operation/retry/AttachmentKeys.java

https://github.com/jyemin/mongo-java-driver · Java · 106 lines · 69 code · 17 blank · 20 comment · 6 complexity · f11340685de2e457c7bd8101ad14916e 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.internal.operation.retry;
  17. import com.mongodb.annotations.Immutable;
  18. import com.mongodb.bulk.BulkWriteResult;
  19. import com.mongodb.internal.async.function.LoopState.AttachmentKey;
  20. import com.mongodb.internal.operation.MixedBulkWriteOperation.BulkWriteTracker;
  21. import org.bson.BsonDocument;
  22. import java.util.HashSet;
  23. import java.util.Set;
  24. import java.util.function.Supplier;
  25. import static com.mongodb.assertions.Assertions.assertTrue;
  26. import static com.mongodb.assertions.Assertions.fail;
  27. /**
  28. * A class with {@code static} methods providing access to {@link AttachmentKey}s relevant when implementing retryable operations.
  29. *
  30. * @see AttachmentKey
  31. */
  32. public final class AttachmentKeys {
  33. private static final AttachmentKey<Integer> MAX_WIRE_VERSION = new DefaultAttachmentKey<>("maxWireVersion");
  34. private static final AttachmentKey<BsonDocument> COMMAND = new DefaultAttachmentKey<>("command");
  35. private static final AttachmentKey<Boolean> RETRYABLE_COMMAND_FLAG = new DefaultAttachmentKey<>("retryableCommandFlag");
  36. private static final AttachmentKey<Supplier<String>> COMMAND_DESCRIPTION_SUPPLIER = new DefaultAttachmentKey<>(
  37. "commandDescriptionSupplier");
  38. private static final AttachmentKey<BulkWriteTracker> BULK_WRITE_TRACKER = new DefaultAttachmentKey<>("bulkWriteTracker");
  39. private static final AttachmentKey<BulkWriteResult> BULK_WRITE_RESULT = new DefaultAttachmentKey<>("bulkWriteResult");
  40. public static AttachmentKey<Integer> maxWireVersion() {
  41. return MAX_WIRE_VERSION;
  42. }
  43. public static AttachmentKey<BsonDocument> command() {
  44. return COMMAND;
  45. }
  46. public static AttachmentKey<Boolean> retryableCommandFlag() {
  47. return RETRYABLE_COMMAND_FLAG;
  48. }
  49. public static AttachmentKey<Supplier<String>> commandDescriptionSupplier() {
  50. return COMMAND_DESCRIPTION_SUPPLIER;
  51. }
  52. public static AttachmentKey<BulkWriteTracker> bulkWriteTracker() {
  53. return BULK_WRITE_TRACKER;
  54. }
  55. public static AttachmentKey<BulkWriteResult> bulkWriteResult() {
  56. return BULK_WRITE_RESULT;
  57. }
  58. private AttachmentKeys() {
  59. fail();
  60. }
  61. @Immutable
  62. private static final class DefaultAttachmentKey<V> implements AttachmentKey<V> {
  63. private static final Set<String> AVOID_KEY_DUPLICATION = new HashSet<>();
  64. private final String key;
  65. private DefaultAttachmentKey(final String key) {
  66. assertTrue(AVOID_KEY_DUPLICATION.add(key));
  67. this.key = key;
  68. }
  69. @Override
  70. public boolean equals(final Object o) {
  71. if (this == o) {
  72. return true;
  73. }
  74. if (o == null || getClass() != o.getClass()) {
  75. return false;
  76. }
  77. final DefaultAttachmentKey<?> that = (DefaultAttachmentKey<?>) o;
  78. return key.equals(that.key);
  79. }
  80. @Override
  81. public int hashCode() {
  82. return key.hashCode();
  83. }
  84. @Override
  85. public String toString() {
  86. return key;
  87. }
  88. }
  89. }