Skip to content

Commit 48b9614

Browse files
committed
Create and document Java sync improved bulk write API
JAVA-5527
1 parent 01aff5a commit 48b9614

File tree

38 files changed

+2738
-0
lines changed

38 files changed

+2738
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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;
17+
18+
import com.mongodb.bulk.WriteConcernError;
19+
import com.mongodb.client.model.bulk.ClientWriteModel;
20+
import com.mongodb.client.result.bulk.ClientBulkWriteResult;
21+
import com.mongodb.lang.Nullable;
22+
23+
import java.util.List;
24+
import java.util.Map;
25+
import java.util.Optional;
26+
27+
import static com.mongodb.assertions.Assertions.isTrueArgument;
28+
import static java.util.Collections.emptyList;
29+
import static java.util.Collections.emptyMap;
30+
import static java.util.Collections.unmodifiableList;
31+
import static java.util.Collections.unmodifiableMap;
32+
import static java.util.Optional.ofNullable;
33+
34+
/**
35+
* The result of an unsuccessful or partially unsuccessful client-level bulk write operation.
36+
*
37+
* @see ClientBulkWriteResult
38+
* @since 5.3
39+
* @serial exclude
40+
*/
41+
public final class ClientBulkWriteException extends MongoServerException {
42+
private static final long serialVersionUID = 1;
43+
44+
@Nullable
45+
private final MongoException error;
46+
private final List<WriteConcernError> writeConcernErrors;
47+
private final Map<Long, WriteError> writeErrors;
48+
@Nullable
49+
private final ClientBulkWriteResult partialResult;
50+
51+
private ClientBulkWriteException(
52+
final String message,
53+
@Nullable final MongoException error,
54+
@Nullable final List<WriteConcernError> writeConcernErrors,
55+
@Nullable final Map<Long, WriteError> writeErrors,
56+
@Nullable final ClientBulkWriteResult partialResult,
57+
final ServerAddress serverAddress) {
58+
super(message, serverAddress);
59+
// BULK-TODO Should ClientBulkWriteException.getCode be the same as error.getCode,
60+
// and getErrorLabels/hasErrorLabel contain the same labels as error.getErrorLabels?
61+
this.error = error;
62+
this.writeConcernErrors = writeConcernErrors == null ? emptyList() : unmodifiableList(writeConcernErrors);
63+
this.writeErrors = writeErrors == null ? emptyMap() : unmodifiableMap(writeErrors);
64+
this.partialResult = partialResult;
65+
}
66+
67+
/**
68+
* Creates a {@link ClientBulkWriteException}.
69+
* At least one of {@code error}, {@code writeConcernErrors}, {@code writeErrors}, {@code partialResult}
70+
* must be non-{@code null} or non-empty.
71+
*
72+
* @param error The {@linkplain #getError() top-level error}.
73+
* @param writeConcernErrors The {@linkplain #getWriteConcernErrors() write concern errors}.
74+
* @param writeErrors The {@linkplain #getWriteErrors() write errors}.
75+
* @param partialResult The {@linkplain #getPartialResult() partial result}.
76+
* @param serverAddress The {@linkplain MongoServerException#getServerAddress() server address}.
77+
* @return The requested {@link ClientBulkWriteException}.
78+
*/
79+
public static ClientBulkWriteException create(
80+
@Nullable final MongoException error,
81+
@Nullable final List<WriteConcernError> writeConcernErrors,
82+
@Nullable final Map<Long, WriteError> writeErrors,
83+
@Nullable final ClientBulkWriteResult partialResult,
84+
final ServerAddress serverAddress) {
85+
isTrueArgument("At least one of `writeConcernErrors`, `writeErrors`, `partialResult` must be non-null or non-empty",
86+
!(writeConcernErrors == null || writeConcernErrors.isEmpty())
87+
|| !(writeErrors == null || writeErrors.isEmpty())
88+
|| partialResult != null);
89+
String message = "Client-level bulk write operation error on server " + serverAddress + "."
90+
+ (error == null ? "" : " Top-level error: " + error + ".")
91+
+ (writeErrors == null || writeErrors.isEmpty() ? "" : " Write errors: " + writeErrors + ".")
92+
+ (writeConcernErrors == null || writeConcernErrors.isEmpty() ? "" : " Write concern errors: " + writeConcernErrors + ".")
93+
+ (partialResult == null ? "" : " Partial result: " + partialResult + ".");
94+
return new ClientBulkWriteException(message, error, writeConcernErrors, writeErrors, partialResult, serverAddress);
95+
}
96+
97+
/**
98+
* The top-level error. That is an error that is neither a {@linkplain #getWriteConcernErrors() write concern error},
99+
* nor is an {@linkplain #getWriteErrors() error of an individual write operation}.
100+
*
101+
* @return The top-level error. {@linkplain Optional#isPresent() Present} only if a top-level error occurred.
102+
*/
103+
public Optional<MongoException> getError() {
104+
return ofNullable(error);
105+
}
106+
107+
/**
108+
* The {@link WriteConcernError}s that occurred while executing the client-level bulk write operation.
109+
* <p>
110+
* There are no guarantees on mutability of the {@link List} returned.</p>
111+
*
112+
* @return The {@link WriteConcernError}s.
113+
*/
114+
public List<WriteConcernError> getWriteConcernErrors() {
115+
return writeConcernErrors;
116+
}
117+
118+
/**
119+
* The indexed {@link WriteError}s.
120+
* The {@linkplain Map#keySet() keys} are the indexes of the corresponding {@link ClientWriteModel}s
121+
* in the corresponding client-level bulk write operation.
122+
* <p>
123+
* There are no guarantees on mutability or iteration order of the {@link Map} returned.</p>
124+
*
125+
* @return The indexed {@link WriteError}s.
126+
* @see ClientBulkWriteResult#getInsertResults()
127+
* @see ClientBulkWriteResult#getUpdateResults()
128+
* @see ClientBulkWriteResult#getDeleteResults()
129+
*/
130+
public Map<Long, WriteError> getWriteErrors() {
131+
return writeErrors;
132+
}
133+
134+
/**
135+
* The result of the successful part of a client-level bulk write operation.
136+
*
137+
* @return The successful partial result. {@linkplain Optional#isPresent() Present} only if at least one
138+
* {@linkplain ClientWriteModel individual write operation} succeed.
139+
*/
140+
public Optional<ClientBulkWriteResult> getPartialResult() {
141+
return ofNullable(partialResult);
142+
}
143+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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.model.bulk;
17+
18+
import com.mongodb.annotations.Sealed;
19+
import com.mongodb.client.model.Filters;
20+
import com.mongodb.client.result.bulk.ClientBulkWriteResult;
21+
import com.mongodb.internal.client.model.bulk.ConcreteClientBulkWriteOptions;
22+
import com.mongodb.lang.Nullable;
23+
import org.bson.BsonValue;
24+
import org.bson.conversions.Bson;
25+
26+
/**
27+
* The options to apply when executing a client-level bulk write operation.
28+
*
29+
* @since 5.3
30+
*/
31+
@Sealed
32+
public interface ClientBulkWriteOptions {
33+
/**
34+
* Creates the default options.
35+
*
36+
* @return The default options.
37+
*/
38+
static ClientBulkWriteOptions clientBulkWriteOptions() {
39+
return new ConcreteClientBulkWriteOptions();
40+
}
41+
42+
/**
43+
* Enables or disables ordered execution of {@linkplain ClientWriteModel individual write operations}.
44+
* In an ordered execution a failure of an individual operation prevents the rest of them
45+
* from being executed.
46+
* In an unordered execution failures of individual operations do not prevent the rest of them
47+
* from being executed.
48+
*
49+
* @param ordered The ordered flag. If {@code null}, the client defaults to {@code true}.
50+
* @return {@code this}.
51+
*/
52+
ClientBulkWriteOptions ordered(@Nullable Boolean ordered);
53+
54+
/**
55+
* Disables or enables checking against document validation rules, a.k.a., schema validation.
56+
*
57+
* @param bypassDocumentValidation The flag specifying whether to bypass the document validation rules.
58+
* {@code null} represents the server default.
59+
* @return {@code this}.
60+
*/
61+
ClientBulkWriteOptions bypassDocumentValidation(@Nullable Boolean bypassDocumentValidation);
62+
63+
/**
64+
* Sets variables that can be referenced from {@linkplain ClientWriteModel individual write operations}
65+
* with the {@code "$$"} syntax, which in turn requires using {@link Filters#expr(Object)} when specifying filters.
66+
* Values must be constants or expressions that do not reference fields.
67+
*
68+
* @param let The variables. {@code null} represents the server default.
69+
* @return {@code this}.
70+
* @mongodb.driver.manual reference/aggregation-variables/ Variables in Aggregation Expressions
71+
*/
72+
ClientBulkWriteOptions let(@Nullable Bson let);
73+
74+
/**
75+
* Sets the comment to attach to the {@code bulkWrite} administration command.
76+
*
77+
* @param comment The comment. {@code null} represents the server default.
78+
* @return {@code this}.
79+
*/
80+
ClientBulkWriteOptions comment(@Nullable BsonValue comment);
81+
82+
/**
83+
* Enables or disables requesting {@linkplain ClientBulkWriteResult#hasVerboseResults() verbose results}.
84+
*
85+
* @param verboseResults The flag specifying whether to request verbose results.
86+
* If {@code null}, the client defaults to {@code false}.
87+
* This value corresponds inversely to the {@code errorsOnly} field of the {@code bulkWrite} administration command.
88+
* @return {@code this}.
89+
*/
90+
ClientBulkWriteOptions verboseResults(@Nullable Boolean verboseResults);
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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.model.bulk;
17+
18+
import com.mongodb.annotations.Sealed;
19+
import com.mongodb.client.model.Collation;
20+
import com.mongodb.internal.client.model.bulk.ConcreteClientDeleteOptions;
21+
import com.mongodb.lang.Nullable;
22+
import org.bson.conversions.Bson;
23+
24+
/**
25+
* The options to apply when deleting documents.
26+
*
27+
* @since 5.3
28+
*/
29+
@Sealed
30+
public interface ClientDeleteOptions {
31+
/**
32+
* Creates the default options.
33+
*
34+
* @return The default options.
35+
*/
36+
static ClientDeleteOptions clientDeleteOptions() {
37+
return new ConcreteClientDeleteOptions();
38+
}
39+
40+
/**
41+
* Sets the collation.
42+
*
43+
* @param collation The collation. {@code null} represents the server default.
44+
* @return {@code this}.
45+
*/
46+
ClientDeleteOptions collation(@Nullable Collation collation);
47+
48+
/**
49+
* Sets the index specification,
50+
* {@code null}-ifies {@linkplain #hintString(String) hint string}.
51+
*
52+
* @param hint The index specification. {@code null} represents the server default.
53+
* @return {@code this}.
54+
*/
55+
ClientDeleteOptions hint(@Nullable Bson hint);
56+
57+
/**
58+
* Sets the index name,
59+
* {@code null}-ifies {@linkplain #hint(Bson) hint}.
60+
*
61+
* @param hintString The index name. {@code null} represents the server default.
62+
* @return {@code this}.
63+
*/
64+
ClientDeleteOptions hintString(@Nullable String hintString);
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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.model.bulk;
17+
18+
import com.mongodb.annotations.Sealed;
19+
import com.mongodb.client.model.Collation;
20+
import com.mongodb.internal.client.model.bulk.ConcreteClientReplaceOptions;
21+
import com.mongodb.lang.Nullable;
22+
import org.bson.conversions.Bson;
23+
24+
/**
25+
* The options to apply when replacing documents.
26+
*
27+
* @since 5.3
28+
*/
29+
@Sealed
30+
public interface ClientReplaceOptions {
31+
/**
32+
* Creates the default options.
33+
*
34+
* @return The default options.
35+
*/
36+
static ClientReplaceOptions clientReplaceOptions() {
37+
return new ConcreteClientReplaceOptions();
38+
}
39+
40+
/**
41+
* Sets the collation.
42+
*
43+
* @param collation The collation. {@code null} represents the server default.
44+
* @return {@code this}.
45+
*/
46+
ClientReplaceOptions collation(@Nullable Collation collation);
47+
48+
/**
49+
* Sets the index specification,
50+
* {@code null}-ifies {@linkplain #hintString(String) hint string}.
51+
*
52+
* @param hint The index specification. {@code null} represents the server default.
53+
* @return {@code this}.
54+
*/
55+
ClientReplaceOptions hint(@Nullable Bson hint);
56+
57+
/**
58+
* Sets the index name,
59+
* {@code null}-ifies {@linkplain #hint(Bson) hint}.
60+
*
61+
* @param hintString The index name. {@code null} represents the server default.
62+
* @return {@code this}.
63+
*/
64+
ClientReplaceOptions hintString(@Nullable String hintString);
65+
66+
/**
67+
* Enables or disables creation of a document if no documents match the filter.
68+
*
69+
* @param upsert The upsert flag. {@code null} represents the server default.
70+
* @return {@code this}.
71+
*/
72+
ClientReplaceOptions upsert(@Nullable Boolean upsert);
73+
}

0 commit comments

Comments
 (0)