Skip to content

Commit 395af7a

Browse files
committed
Use Optional to express verbose/summary results
JAVA-5527
1 parent f67af3f commit 395af7a

File tree

6 files changed

+124
-124
lines changed

6 files changed

+124
-124
lines changed

driver-core/src/main/com/mongodb/ClientBulkWriteException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ public List<WriteConcernError> getWriteConcernErrors() {
118118
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
119119
*
120120
* @return The indexed {@link WriteError}s.
121-
* @see ClientBulkWriteResult#getInsertResults()
122-
* @see ClientBulkWriteResult#getUpdateResults()
123-
* @see ClientBulkWriteResult#getDeleteResults()
121+
* @see ClientBulkWriteResult.Verbose#getInsertResults()
122+
* @see ClientBulkWriteResult.Verbose#getUpdateResults()
123+
* @see ClientBulkWriteResult.Verbose#getDeleteResults()
124124
*/
125125
public Map<Long, WriteError> getWriteErrors() {
126126
return writeErrors;

driver-core/src/main/com/mongodb/client/model/bulk/ClientBulkWriteOptions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static ClientBulkWriteOptions clientBulkWriteOptions() {
8080
ClientBulkWriteOptions comment(@Nullable BsonValue comment);
8181

8282
/**
83-
* Enables or disables requesting {@linkplain ClientBulkWriteResult#hasVerboseResults() verbose results}.
83+
* Enables or disables requesting {@linkplain ClientBulkWriteResult#getVerbose() verbose results}.
8484
*
8585
* @param verboseResults The flag specifying whether to request verbose results.
8686
* If {@code null}, the client defaults to {@code false}.

driver-core/src/main/com/mongodb/client/result/bulk/ClientBulkWriteResult.java

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.mongodb.client.model.bulk.ClientWriteModel;
2424

2525
import java.util.Map;
26+
import java.util.Optional;
2627

2728
/**
2829
* The result of a successful or partially successful client-level bulk write operation.
@@ -43,17 +44,6 @@ public interface ClientBulkWriteResult {
4344
*/
4445
boolean isAcknowledged();
4546

46-
/**
47-
* Indicates whether there are {@linkplain ClientBulkWriteOptions#verboseResults(Boolean) verbose results}
48-
* of individual operations.
49-
* If not, then {@link #getInsertResults()}, {@link #getUpdateResults()}, {@link #getDeleteResults()}
50-
* throw {@link UnsupportedOperationException}.
51-
*
52-
* @return Whether there are verbose results.
53-
* @throws UnsupportedOperationException If this result is not {@linkplain #isAcknowledged() acknowledged}.
54-
*/
55-
boolean hasVerboseResults();
56-
5747
/**
5848
* The number of documents that were inserted across all insert operations.
5949
*
@@ -95,44 +85,54 @@ public interface ClientBulkWriteResult {
9585
long getDeletedCount();
9686

9787
/**
98-
* The indexed {@link ClientInsertOneResult}s.
99-
* The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s
100-
* in the client-level bulk write operation.
101-
* <p>
102-
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
88+
* The {@linkplain ClientBulkWriteOptions#verboseResults(Boolean) verbose results} of individual operations.
10389
*
104-
* @return The indexed {@link ClientInsertOneResult}s.
105-
* @throws UnsupportedOperationException If this result is either not {@linkplain #isAcknowledged() acknowledged},
106-
* or does not have {@linkplain #hasVerboseResults() verbose results}.
107-
* @see ClientBulkWriteException#getWriteErrors()
90+
* @return {@link Optional} verbose results of individual operations.
91+
* @throws UnsupportedOperationException If this result is not {@linkplain #isAcknowledged() acknowledged}.
10892
*/
109-
Map<Long, ClientInsertOneResult> getInsertResults();
93+
Optional<Verbose> getVerbose();
11094

11195
/**
112-
* The indexed {@link ClientUpdateResult}s.
113-
* The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s
114-
* in the client-level bulk write operation.
115-
* <p>
116-
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
96+
* The {@linkplain ClientBulkWriteResult#getVerbose() verbose results} of individual operations.
11797
*
118-
* @return The indexed {@link ClientUpdateResult}s.
119-
* @throws UnsupportedOperationException If this result is either not {@linkplain #isAcknowledged() acknowledged},
120-
* or does not have {@linkplain #hasVerboseResults() verbose results}.
121-
* @see ClientBulkWriteException#getWriteErrors()
98+
* @since 5.3
12299
*/
123-
Map<Long, ClientUpdateResult> getUpdateResults();
100+
@Evolving
101+
interface Verbose {
102+
/**
103+
* The indexed {@link ClientInsertOneResult}s.
104+
* The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s
105+
* in the client-level bulk write operation.
106+
* <p>
107+
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
108+
*
109+
* @return The indexed {@link ClientInsertOneResult}s.
110+
* @see ClientBulkWriteException#getWriteErrors()
111+
*/
112+
Map<Long, ClientInsertOneResult> getInsertResults();
124113

125-
/**
126-
* The indexed {@link ClientDeleteResult}s.
127-
* The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s
128-
* in the client-level bulk write operation.
129-
* <p>
130-
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
131-
*
132-
* @return The indexed {@link ClientDeleteResult}s.
133-
* @throws UnsupportedOperationException If this result is either not {@linkplain #isAcknowledged() acknowledged},
134-
* or does not have {@linkplain #hasVerboseResults() verbose results}.
135-
* @see ClientBulkWriteException#getWriteErrors()
136-
*/
137-
Map<Long, ClientDeleteResult> getDeleteResults();
114+
/**
115+
* The indexed {@link ClientUpdateResult}s.
116+
* The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s
117+
* in the client-level bulk write operation.
118+
* <p>
119+
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
120+
*
121+
* @return The indexed {@link ClientUpdateResult}s.
122+
* @see ClientBulkWriteException#getWriteErrors()
123+
*/
124+
Map<Long, ClientUpdateResult> getUpdateResults();
125+
126+
/**
127+
* The indexed {@link ClientDeleteResult}s.
128+
* The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s
129+
* in the client-level bulk write operation.
130+
* <p>
131+
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
132+
*
133+
* @return The indexed {@link ClientDeleteResult}s.
134+
* @see ClientBulkWriteException#getWriteErrors()
135+
*/
136+
Map<Long, ClientDeleteResult> getDeleteResults();
137+
}
138138
}

driver-core/src/main/com/mongodb/internal/client/result/bulk/AcknowledgedSummaryClientBulkWriteResult.java

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
package com.mongodb.internal.client.result.bulk;
1717

1818
import com.mongodb.client.result.bulk.ClientBulkWriteResult;
19-
import com.mongodb.client.result.bulk.ClientDeleteResult;
20-
import com.mongodb.client.result.bulk.ClientInsertOneResult;
21-
import com.mongodb.client.result.bulk.ClientUpdateResult;
2219

23-
import java.util.Map;
2420
import java.util.Objects;
21+
import java.util.Optional;
22+
23+
import static java.util.Optional.empty;
2524

2625
/**
2726
* This class is not part of the public API and may be removed or changed at any time.
@@ -51,11 +50,6 @@ public boolean isAcknowledged() {
5150
return true;
5251
}
5352

54-
@Override
55-
public boolean hasVerboseResults() {
56-
return false;
57-
}
58-
5953
@Override
6054
public long getInsertedCount() {
6155
return insertedCount;
@@ -82,22 +76,8 @@ public long getDeletedCount() {
8276
}
8377

8478
@Override
85-
public Map<Long, ClientInsertOneResult> getInsertResults() throws UnsupportedOperationException {
86-
throw noVerboseResultsException();
87-
}
88-
89-
@Override
90-
public Map<Long, ClientUpdateResult> getUpdateResults() throws UnsupportedOperationException {
91-
throw noVerboseResultsException();
92-
}
93-
94-
@Override
95-
public Map<Long, ClientDeleteResult> getDeleteResults() throws UnsupportedOperationException {
96-
throw noVerboseResultsException();
97-
}
98-
99-
private static UnsupportedOperationException noVerboseResultsException() {
100-
return new UnsupportedOperationException("Verbose results are not available");
79+
public Optional<Verbose> getVerbose() {
80+
return empty();
10181
}
10282

10383
@Override

driver-core/src/main/com/mongodb/internal/client/result/bulk/AcknowledgedVerboseClientBulkWriteResult.java

Lines changed: 69 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,39 +22,31 @@
2222

2323
import java.util.Map;
2424
import java.util.Objects;
25+
import java.util.Optional;
2526

26-
import static java.util.Collections.unmodifiableMap;
27+
import static java.util.Optional.of;
2728

2829
/**
2930
* This class is not part of the public API and may be removed or changed at any time.
3031
*/
3132
public final class AcknowledgedVerboseClientBulkWriteResult implements ClientBulkWriteResult {
3233
private final AcknowledgedSummaryClientBulkWriteResult summaryResults;
33-
private final Map<Long, ClientInsertOneResult> insertResults;
34-
private final Map<Long, ClientUpdateResult> updateResults;
35-
private final Map<Long, ClientDeleteResult> deleteResults;
34+
private final Verbose verbose;
3635

3736
public AcknowledgedVerboseClientBulkWriteResult(
3837
final AcknowledgedSummaryClientBulkWriteResult summaryResults,
3938
final Map<Long, ClientInsertOneResult> insertResults,
4039
final Map<Long, ClientUpdateResult> updateResults,
4140
final Map<Long, ClientDeleteResult> deleteResults) {
4241
this.summaryResults = summaryResults;
43-
this.insertResults = unmodifiableMap(insertResults);
44-
this.updateResults = unmodifiableMap(updateResults);
45-
this.deleteResults = unmodifiableMap(deleteResults);
42+
this.verbose = new Verbose(insertResults, updateResults, deleteResults);
4643
}
4744

4845
@Override
4946
public boolean isAcknowledged() {
5047
return true;
5148
}
5249

53-
@Override
54-
public boolean hasVerboseResults() {
55-
return true;
56-
}
57-
5850
@Override
5951
public long getInsertedCount() {
6052
return summaryResults.getInsertedCount();
@@ -81,18 +73,8 @@ public long getDeletedCount() {
8173
}
8274

8375
@Override
84-
public Map<Long, ClientInsertOneResult> getInsertResults() {
85-
return insertResults;
86-
}
87-
88-
@Override
89-
public Map<Long, ClientUpdateResult> getUpdateResults() {
90-
return updateResults;
91-
}
92-
93-
@Override
94-
public Map<Long, ClientDeleteResult> getDeleteResults() {
95-
return deleteResults;
76+
public Optional<ClientBulkWriteResult.Verbose> getVerbose() {
77+
return of(verbose);
9678
}
9779

9880
@Override
@@ -105,14 +87,12 @@ public boolean equals(final Object o) {
10587
}
10688
final AcknowledgedVerboseClientBulkWriteResult that = (AcknowledgedVerboseClientBulkWriteResult) o;
10789
return Objects.equals(summaryResults, that.summaryResults)
108-
&& Objects.equals(insertResults, that.insertResults)
109-
&& Objects.equals(updateResults, that.updateResults)
110-
&& Objects.equals(deleteResults, that.deleteResults);
90+
&& Objects.equals(verbose, that.verbose);
11191
}
11292

11393
@Override
11494
public int hashCode() {
115-
return Objects.hash(summaryResults, insertResults, updateResults, deleteResults);
95+
return Objects.hash(summaryResults, verbose);
11696
}
11797

11898
@Override
@@ -123,9 +103,67 @@ public String toString() {
123103
+ ", matchedCount=" + summaryResults.getMatchedCount()
124104
+ ", modifiedCount=" + summaryResults.getModifiedCount()
125105
+ ", deletedCount=" + summaryResults.getDeletedCount()
126-
+ ", insertResults=" + insertResults
127-
+ ", updateResults=" + updateResults
128-
+ ", deleteResults=" + deleteResults
106+
+ ", insertResults=" + verbose.insertResults
107+
+ ", updateResults=" + verbose.updateResults
108+
+ ", deleteResults=" + verbose.deleteResults
129109
+ '}';
130110
}
111+
112+
private static final class Verbose implements ClientBulkWriteResult.Verbose {
113+
private final Map<Long, ClientInsertOneResult> insertResults;
114+
private final Map<Long, ClientUpdateResult> updateResults;
115+
private final Map<Long, ClientDeleteResult> deleteResults;
116+
117+
Verbose(
118+
final Map<Long, ClientInsertOneResult> insertResults,
119+
final Map<Long, ClientUpdateResult> updateResults,
120+
final Map<Long, ClientDeleteResult> deleteResults) {
121+
this.insertResults = insertResults;
122+
this.updateResults = updateResults;
123+
this.deleteResults = deleteResults;
124+
}
125+
126+
@Override
127+
public Map<Long, ClientInsertOneResult> getInsertResults() {
128+
return insertResults;
129+
}
130+
131+
@Override
132+
public Map<Long, ClientUpdateResult> getUpdateResults() {
133+
return updateResults;
134+
}
135+
136+
@Override
137+
public Map<Long, ClientDeleteResult> getDeleteResults() {
138+
return deleteResults;
139+
}
140+
141+
@Override
142+
public boolean equals(final Object o) {
143+
if (this == o) {
144+
return true;
145+
}
146+
if (o == null || getClass() != o.getClass()) {
147+
return false;
148+
}
149+
final Verbose verbose = (Verbose) o;
150+
return Objects.equals(insertResults, verbose.insertResults)
151+
&& Objects.equals(updateResults, verbose.updateResults)
152+
&& Objects.equals(deleteResults, verbose.deleteResults);
153+
}
154+
155+
@Override
156+
public int hashCode() {
157+
return Objects.hash(insertResults, updateResults, deleteResults);
158+
}
159+
160+
@Override
161+
public String toString() {
162+
return "AcknowledgedVerboseClientBulkWriteResult.Verbose{"
163+
+ ", insertResults=" + insertResults
164+
+ ", updateResults=" + updateResults
165+
+ ", deleteResults=" + deleteResults
166+
+ '}';
167+
}
168+
}
131169
}

driver-core/src/main/com/mongodb/internal/client/result/bulk/UnacknowledgedClientBulkWriteResult.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@
1717

1818
import com.mongodb.annotations.Immutable;
1919
import com.mongodb.client.result.bulk.ClientBulkWriteResult;
20-
import com.mongodb.client.result.bulk.ClientDeleteResult;
21-
import com.mongodb.client.result.bulk.ClientInsertOneResult;
22-
import com.mongodb.client.result.bulk.ClientUpdateResult;
2320

24-
import java.util.Map;
21+
import java.util.Optional;
2522

2623
/**
2724
* This class is not part of the public API and may be removed or changed at any time.
@@ -38,11 +35,6 @@ public boolean isAcknowledged() {
3835
return false;
3936
}
4037

41-
@Override
42-
public boolean hasVerboseResults() throws UnsupportedOperationException {
43-
throw createUnacknowledgedResultsException();
44-
}
45-
4638
@Override
4739
public long getInsertedCount() throws UnsupportedOperationException {
4840
throw createUnacknowledgedResultsException();
@@ -69,17 +61,7 @@ public long getDeletedCount() throws UnsupportedOperationException {
6961
}
7062

7163
@Override
72-
public Map<Long, ClientInsertOneResult> getInsertResults() throws UnsupportedOperationException {
73-
throw createUnacknowledgedResultsException();
74-
}
75-
76-
@Override
77-
public Map<Long, ClientUpdateResult> getUpdateResults() throws UnsupportedOperationException {
78-
throw createUnacknowledgedResultsException();
79-
}
80-
81-
@Override
82-
public Map<Long, ClientDeleteResult> getDeleteResults() throws UnsupportedOperationException {
64+
public Optional<Verbose> getVerbose() throws UnsupportedOperationException {
8365
throw createUnacknowledgedResultsException();
8466
}
8567

0 commit comments

Comments
 (0)