40
40
@ Immutable
41
41
public abstract class TaggableReadPreference extends ReadPreference {
42
42
private final List <TagSet > tagSetList = new ArrayList <TagSet >();
43
- private final long maxStalenessMS ;
43
+ private final Long maxStalenessMS ;
44
44
45
45
TaggableReadPreference () {
46
- maxStalenessMS = 0 ;
46
+ this . maxStalenessMS = null ;
47
47
}
48
48
49
- TaggableReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
49
+ TaggableReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
50
50
notNull ("tagSetList" , tagSetList );
51
- isTrueArgument ("maxStaleness >= 0" , maxStalenessMS >= 0 );
52
- this .maxStalenessMS = maxStalenessMS ;
51
+ isTrueArgument ("maxStaleness is null or >= 0" , maxStaleness == null || maxStaleness >= 0 );
52
+ this .maxStalenessMS = maxStaleness == null ? null : MILLISECONDS . convert ( maxStaleness , timeUnit ) ;
53
53
54
54
for (final TagSet tagSet : tagSetList ) {
55
55
this .tagSetList .add (tagSet );
@@ -69,7 +69,7 @@ public BsonDocument toDocument() {
69
69
readPrefObject .put ("tags" , tagsListToBsonArray ());
70
70
}
71
71
72
- if (maxStalenessMS != 0 ) {
72
+ if (maxStalenessMS != null ) {
73
73
readPrefObject .put ("maxStalenessMS" , new BsonInt64 (maxStalenessMS ));
74
74
}
75
75
return readPrefObject ;
@@ -89,13 +89,16 @@ public List<TagSet> getTagSetList() {
89
89
* Gets the maximum acceptable staleness of a secondary in order to be considered for read operations.
90
90
*
91
91
* @param timeUnit the time unit in which to return the value
92
- * @return the maximum acceptable staleness in the given time unit. The default is 0, meaning there is no staleness check.
92
+ * @return the maximum acceptable staleness in the given time unit, or null if the value is not set
93
93
*
94
94
* @since 3.4
95
95
* @mongodb.server.release 3.4
96
96
*/
97
- public long getMaxStaleness (final TimeUnit timeUnit ) {
97
+ public Long getMaxStaleness (final TimeUnit timeUnit ) {
98
98
notNull ("timeUnit" , timeUnit );
99
+ if (maxStalenessMS == null ) {
100
+ return null ;
101
+ }
99
102
return timeUnit .convert (maxStalenessMS , TimeUnit .MILLISECONDS );
100
103
}
101
104
@@ -104,7 +107,7 @@ public String toString() {
104
107
return "ReadPreference{"
105
108
+ "name=" + getName ()
106
109
+ (tagSetList .isEmpty () ? "" : ", tagSetList=" + tagSetList )
107
- + (maxStalenessMS == 0 ? "" : ", maxStalenessMS=" + maxStalenessMS )
110
+ + (maxStalenessMS == null ? "" : ", maxStalenessMS=" + maxStalenessMS )
108
111
+ '}' ;
109
112
}
110
113
@@ -119,7 +122,7 @@ public boolean equals(final Object o) {
119
122
120
123
TaggableReadPreference that = (TaggableReadPreference ) o ;
121
124
122
- if (maxStalenessMS != that .maxStalenessMS ) {
125
+ if (maxStalenessMS != null ? ! maxStalenessMS . equals ( that .maxStalenessMS ) : that . maxStalenessMS != null ) {
123
126
return false ;
124
127
}
125
128
if (!tagSetList .equals (that .tagSetList )) {
@@ -133,7 +136,7 @@ public boolean equals(final Object o) {
133
136
public int hashCode () {
134
137
int result = tagSetList .hashCode ();
135
138
result = 31 * result + getName ().hashCode ();
136
- result = 31 * result + (int ) ( maxStalenessMS ^ ( maxStalenessMS >>> 32 ) );
139
+ result = 31 * result + (maxStalenessMS != null ? maxStalenessMS . hashCode () : 0 );
137
140
return result ;
138
141
}
139
142
@@ -154,7 +157,7 @@ protected static ClusterDescription copyClusterDescription(final ClusterDescript
154
157
155
158
protected List <ServerDescription > selectFreshServers (final ClusterDescription clusterDescription ,
156
159
final List <ServerDescription > servers ) {
157
- if (getMaxStaleness (MILLISECONDS ) == 0 ) {
160
+ if (getMaxStaleness (MILLISECONDS ) == null ) {
158
161
return servers ;
159
162
}
160
163
@@ -248,8 +251,8 @@ static class SecondaryReadPreference extends TaggableReadPreference {
248
251
SecondaryReadPreference () {
249
252
}
250
253
251
- SecondaryReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
252
- super (tagSetList , maxStalenessMS );
254
+ SecondaryReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
255
+ super (tagSetList , maxStaleness , timeUnit );
253
256
}
254
257
255
258
@ Override
@@ -283,8 +286,8 @@ static class SecondaryPreferredReadPreference extends SecondaryReadPreference {
283
286
SecondaryPreferredReadPreference () {
284
287
}
285
288
286
- SecondaryPreferredReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
287
- super (tagSetList , maxStalenessMS );
289
+ SecondaryPreferredReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
290
+ super (tagSetList , maxStaleness , timeUnit );
288
291
}
289
292
290
293
@ Override
@@ -310,8 +313,8 @@ static class NearestReadPreference extends TaggableReadPreference {
310
313
NearestReadPreference () {
311
314
}
312
315
313
- NearestReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
314
- super (tagSetList , maxStalenessMS );
316
+ NearestReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
317
+ super (tagSetList , maxStaleness , timeUnit );
315
318
}
316
319
317
320
@@ -347,8 +350,8 @@ static class PrimaryPreferredReadPreference extends SecondaryReadPreference {
347
350
PrimaryPreferredReadPreference () {
348
351
}
349
352
350
- PrimaryPreferredReadPreference (final List <TagSet > tagSetList , final long maxStalenessMS ) {
351
- super (tagSetList , maxStalenessMS );
353
+ PrimaryPreferredReadPreference (final List <TagSet > tagSetList , final Long maxStaleness , final TimeUnit timeUnit ) {
354
+ super (tagSetList , maxStaleness , timeUnit );
352
355
}
353
356
354
357
@ Override
0 commit comments