-
Notifications
You must be signed in to change notification settings - Fork 215
/
WriteResultAndErrors.java
173 lines (155 loc) · 6.21 KB
/
WriteResultAndErrors.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.thingsearch.service.persistence.write.model;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.thingsearch.service.updater.actors.MongoWriteModel;
import com.mongodb.MongoBulkWriteException;
import com.mongodb.bulk.BulkWriteError;
import com.mongodb.bulk.BulkWriteResult;
/**
* Data structure containing requested write models, write results and errors for reliable search index update.
*/
@Immutable
public final class WriteResultAndErrors {
private final List<MongoWriteModel> writeModels;
private final BulkWriteResult bulkWriteResult;
private final List<BulkWriteError> bulkWriteErrors;
@Nullable private final Throwable unexpectedError;
private final String bulkWriteCorrelationId;
private WriteResultAndErrors(
final Collection<MongoWriteModel> writeModels,
final BulkWriteResult bulkWriteResult,
final List<BulkWriteError> bulkWriteErrors,
@Nullable final Throwable unexpectedError,
final String bulkWriteCorrelationId) {
this.writeModels = writeModels.stream().toList();
this.bulkWriteResult = bulkWriteResult;
this.bulkWriteErrors = bulkWriteErrors;
this.unexpectedError = unexpectedError;
this.bulkWriteCorrelationId = bulkWriteCorrelationId;
}
/**
* Create a WriteResultAndErrors from a successful bulk write result.
*
* @param writeModels the write models requested.
* @param bulkWriteResult the successful bulk write result.
* @param bulkWriteCorrelationId a correlationId to use for correlating bulk write log statements.
* @return the write result without errors.
*/
public static WriteResultAndErrors success(final Collection<MongoWriteModel> writeModels,
final BulkWriteResult bulkWriteResult,
final String bulkWriteCorrelationId) {
return new WriteResultAndErrors(writeModels, bulkWriteResult, Collections.emptyList(), null, bulkWriteCorrelationId);
}
/**
* Create a WriteResultAndErrors from a MongoBulkWriteException.
*
* @param writeModels the requested write models.
* @param mongoBulkWriteException the exception.
* @param bulkWriteCorrelationId a correlationId to use for correlating bulk write log statements.
* @return the write result with errors.
*/
public static WriteResultAndErrors failure(final Collection<MongoWriteModel> writeModels,
final MongoBulkWriteException mongoBulkWriteException,
final String bulkWriteCorrelationId) {
return new WriteResultAndErrors(writeModels, mongoBulkWriteException.getWriteResult(),
mongoBulkWriteException.getWriteErrors(), null, bulkWriteCorrelationId);
}
/**
* Create a WriteResultAndErrors from an unexpected error. Getting called suggests a bug in Ditto or in its
* environment.
*
* @param writeModels the requested write models.
* @param unexpectedError the unexpected error.
* @param bulkWriteCorrelationId a correlationId to use for correlating bulk write log statements.
* @return the write result with an unexpected error.
*/
public static WriteResultAndErrors unexpectedError(final Collection<MongoWriteModel> writeModels,
final Throwable unexpectedError,
final String bulkWriteCorrelationId) {
return new WriteResultAndErrors(writeModels, BulkWriteResult.unacknowledged(), Collections.emptyList(),
unexpectedError, bulkWriteCorrelationId);
}
/**
* Retrieve the requested write models.
*
* @return the write models.
*/
public List<MongoWriteModel> getWriteModels() {
return writeModels;
}
/**
* Retrieve the bulk write result.
*
* @return the bulk write result.
*/
public BulkWriteResult getBulkWriteResult() {
return bulkWriteResult;
}
/**
* Retrieve the bulk write errors.
*
* @return the bulk write errors.
*/
public List<BulkWriteError> getBulkWriteErrors() {
return bulkWriteErrors;
}
/**
* Retrieve the unexpected error if any.
*
* @return the unexpected error.
*/
public Optional<Throwable> getUnexpectedError() {
return Optional.ofNullable(unexpectedError);
}
/**
* Retrieve the correlationId of the bulk write operation to use for correlating log statements.
*
* @return the correlationId of the bulk write operation.
*/
public String getBulkWriteCorrelationId() {
return bulkWriteCorrelationId;
}
@Override
public boolean equals(final Object o) {
if (o instanceof final WriteResultAndErrors that) {
return Objects.equals(writeModels, that.writeModels) &&
Objects.equals(bulkWriteResult, that.bulkWriteResult) &&
Objects.equals(bulkWriteErrors, that.bulkWriteErrors) &&
Objects.equals(unexpectedError, that.unexpectedError) &&
Objects.equals(bulkWriteCorrelationId, that.bulkWriteCorrelationId);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(writeModels, bulkWriteResult, bulkWriteErrors, unexpectedError, bulkWriteCorrelationId);
}
@Override
public String toString() {
return getClass().getSimpleName() +
"[writeModels=" + writeModels +
",bulkWriteResult=" + bulkWriteResult +
",bulkWriteErrors=" + bulkWriteErrors +
",unexpectedError=" + unexpectedError +
",bulkWriteCorrelationId=" + bulkWriteCorrelationId +
"]";
}
}