|
16 | 16 |
|
17 | 17 | package com.google.cloud.firestore; |
18 | 18 |
|
19 | | -import javax.annotation.Nonnull; |
| 19 | +import com.google.auto.value.AutoValue; |
| 20 | +import javax.annotation.Nullable; |
20 | 21 |
|
21 | | -/** Options used to disable request throttling in BulkWriter. */ |
22 | | -final class BulkWriterOptions { |
23 | | - |
24 | | - private final boolean enableThrottling; |
25 | | - |
26 | | - private BulkWriterOptions(boolean enableThrottling) { |
27 | | - this.enableThrottling = enableThrottling; |
28 | | - } |
| 22 | +/** Options used to configure request throttling in BulkWriter. */ |
| 23 | +@AutoValue |
| 24 | +abstract class BulkWriterOptions { |
| 25 | + /** |
| 26 | + * Return whether throttling is enabled. |
| 27 | + * |
| 28 | + * @return Whether throttling is enabled. |
| 29 | + */ |
| 30 | + abstract boolean getThrottlingEnabled(); |
29 | 31 |
|
30 | | - boolean isThrottlingEnabled() { |
31 | | - return enableThrottling; |
32 | | - } |
| 32 | + /** |
| 33 | + * Returns the initial maximum number of operations per second allowed by the throttler. |
| 34 | + * |
| 35 | + * @return The initial maximum number of operations per second allowed by the throttler. |
| 36 | + */ |
| 37 | + @Nullable |
| 38 | + abstract Double getInitialOpsPerSecond(); |
33 | 39 |
|
34 | 40 | /** |
35 | | - * An options object that will disable throttling in the created BulkWriter. |
| 41 | + * Returns the maximum number of operations per second allowed by the throttler. |
36 | 42 | * |
37 | | - * @return The BulkWriterOptions object. |
| 43 | + * <p>The throttler's allowed operations per second does not ramp up past the specified operations |
| 44 | + * per second. |
| 45 | + * |
| 46 | + * @return The maximum number of operations per second allowed by the throttler. |
38 | 47 | */ |
39 | | - @Nonnull |
40 | | - public static BulkWriterOptions withThrottlingDisabled() { |
41 | | - return new BulkWriterOptions(false); |
| 48 | + @Nullable |
| 49 | + abstract Double getMaxOpsPerSecond(); |
| 50 | + |
| 51 | + static Builder builder() { |
| 52 | + return new AutoValue_BulkWriterOptions.Builder() |
| 53 | + .setMaxOpsPerSecond(null) |
| 54 | + .setInitialOpsPerSecond(null) |
| 55 | + .setThrottlingEnabled(true); |
| 56 | + } |
| 57 | + |
| 58 | + abstract Builder toBuilder(); |
| 59 | + |
| 60 | + @AutoValue.Builder |
| 61 | + abstract static class Builder { |
| 62 | + /** |
| 63 | + * Sets whether throttling should be enabled. By default, throttling is enabled. |
| 64 | + * |
| 65 | + * @param enabled Whether throttling should be enabled. |
| 66 | + */ |
| 67 | + abstract Builder setThrottlingEnabled(boolean enabled); |
| 68 | + |
| 69 | + /** |
| 70 | + * Set the initial maximum number of operations per second allowed by the throttler. |
| 71 | + * |
| 72 | + * @param initialOpsPerSecond The initial maximum number of operations per second allowed by the |
| 73 | + * throttler. |
| 74 | + */ |
| 75 | + abstract Builder setInitialOpsPerSecond(@Nullable Double initialOpsPerSecond); |
| 76 | + |
| 77 | + /** |
| 78 | + * Set the initial maximum number of operations per second allowed by the throttler. |
| 79 | + * |
| 80 | + * @param initialOpsPerSecond The initial maximum number of operations per second allowed by the |
| 81 | + * throttler. |
| 82 | + */ |
| 83 | + Builder setInitialOpsPerSecond(int initialOpsPerSecond) { |
| 84 | + return setInitialOpsPerSecond(new Double(initialOpsPerSecond)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Set the maximum number of operations per second allowed by the throttler. |
| 89 | + * |
| 90 | + * @param maxOpsPerSecond The maximum number of operations per second allowed by the throttler. |
| 91 | + * The throttler's allowed operations per second does not ramp up past the specified |
| 92 | + * operations per second. |
| 93 | + */ |
| 94 | + abstract Builder setMaxOpsPerSecond(@Nullable Double maxOpsPerSecond); |
| 95 | + |
| 96 | + /** |
| 97 | + * Set the maximum number of operations per second allowed by the throttler. |
| 98 | + * |
| 99 | + * @param maxOpsPerSecond The maximum number of operations per second allowed by the throttler. |
| 100 | + * The throttler's allowed operations per second does not ramp up past the specified |
| 101 | + * operations per second. |
| 102 | + */ |
| 103 | + Builder setMaxOpsPerSecond(int maxOpsPerSecond) { |
| 104 | + return setMaxOpsPerSecond(new Double(maxOpsPerSecond)); |
| 105 | + } |
| 106 | + |
| 107 | + abstract BulkWriterOptions autoBuild(); |
| 108 | + |
| 109 | + BulkWriterOptions build() { |
| 110 | + BulkWriterOptions options = autoBuild(); |
| 111 | + Double initialRate = options.getInitialOpsPerSecond(); |
| 112 | + Double maxRate = options.getMaxOpsPerSecond(); |
| 113 | + |
| 114 | + if (initialRate != null && initialRate < 1) { |
| 115 | + throw FirestoreException.invalidState( |
| 116 | + "Value for argument 'initialOpsPerSecond' must be greater than 1, but was: " |
| 117 | + + initialRate.intValue()); |
| 118 | + } |
| 119 | + |
| 120 | + if (maxRate != null && maxRate < 1) { |
| 121 | + throw FirestoreException.invalidState( |
| 122 | + "Value for argument 'maxOpsPerSecond' must be greater than 1, but was: " |
| 123 | + + maxRate.intValue()); |
| 124 | + } |
| 125 | + |
| 126 | + if (maxRate != null && initialRate != null && initialRate > maxRate) { |
| 127 | + throw FirestoreException.invalidState( |
| 128 | + "'maxOpsPerSecond' cannot be less than 'initialOpsPerSecond'."); |
| 129 | + } |
| 130 | + |
| 131 | + if (!options.getThrottlingEnabled() && (maxRate != null || initialRate != null)) { |
| 132 | + throw FirestoreException.invalidState( |
| 133 | + "Cannot set 'initialOpsPerSecond' or 'maxOpsPerSecond' when 'throttlingEnabled' is set to false."); |
| 134 | + } |
| 135 | + return options; |
| 136 | + } |
42 | 137 | } |
43 | 138 | } |
0 commit comments