/
DocumentFragment.java
326 lines (296 loc) · 12.4 KB
/
DocumentFragment.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
* Copyright (c) 2016 Couchbase, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.couchbase.client.java.subdoc;
import java.util.Collections;
import java.util.List;
import com.couchbase.client.core.CouchbaseException;
import com.couchbase.client.core.annotations.InterfaceAudience;
import com.couchbase.client.core.annotations.InterfaceStability;
import com.couchbase.client.core.message.ResponseStatus;
import com.couchbase.client.core.message.kv.MutationToken;
import com.couchbase.client.core.message.kv.subdoc.multi.Lookup;
import com.couchbase.client.core.message.kv.subdoc.multi.Mutation;
import com.couchbase.client.java.document.JsonDocument;
import com.couchbase.client.java.document.json.JsonArray;
import com.couchbase.client.java.document.json.JsonObject;
import com.couchbase.client.java.error.subdoc.SubDocumentException;
/**
* A fragment of a {@link JsonDocument JSON Document}, that is to say one or several JSON values from the document
* (including String, {@link JsonObject}, {@link JsonArray}, etc...), as returned and used in the sub-document API.
*
* @author Michael Nitschinger
* @author Simon Baslé
* @since 2.2
* @param <OPERATION> the broad type of subdocument operation, either {@link Lookup} or {@link Mutation}.
*/
@InterfaceStability.Committed
@InterfaceAudience.Public
public class DocumentFragment<OPERATION> {
private final String id;
private final long cas;
private final MutationToken mutationToken;
private final List<SubdocOperationResult<OPERATION>> resultList;
public DocumentFragment(String id, long cas, MutationToken mutationToken, List<SubdocOperationResult<OPERATION>> resultList) {
this.id = id;
this.cas = cas;
this.mutationToken = mutationToken;
this.resultList = resultList == null ? Collections.<SubdocOperationResult<OPERATION>>emptyList() : resultList;
}
/**
* @return the {@link JsonDocument#id() id} of the enclosing JSON document in which this fragment belongs.
*/
public String id() {
return this.id;
}
/**
* The CAS (Create-and-Set) is set by the SDK when mutating, reflecting the new CAS from the enclosing JSON document.
*
* @return the CAS value related to the enclosing JSON document.
*/
public long cas() {
return this.cas;
}
/**
* @return the updated {@link MutationToken} related to the enclosing JSON document after a mutation.
*/
public MutationToken mutationToken() {
return this.mutationToken;
}
/**
* @return the number of lookup or mutation specifications that were performed, which is also the number
* of results.
*/
public int size() {
return resultList.size();
}
/**
* Attempt to get the value corresponding to the first operation that targeted the given path, casted to a
* specific class.
* If the operation was successful, the value will be returned. Otherwise the adequate {@link SubDocumentException}
* will be thrown (mostly in the case of multiple lookups).
*
* If multiple operations targeted the same path, this method only considers the first one (see
* {@link #content(int, Class)} to get a result by index).
*
* @param path the path to look for.
* @param targetClass the expected type of the content, to use in a cast.
* @return the content if one could be retrieved and no error occurred.
*/
public <T> T content(String path, Class<T> targetClass) {
if (path == null) {
return null;
}
for (SubdocOperationResult<OPERATION> result : resultList) {
if (path.equals(result.path())) {
return interpretResult(result);
}
}
return null;
}
/**
* Attempt to get the value corresponding to the first operation that targeted the given path, as an Object.
* If the operation was successful, the value will be returned. Otherwise the adequate {@link SubDocumentException}
* will be thrown (mostly in the case of multiple lookups).
*
* If multiple operations targeted the same path, this method only considers the first one (see
* {@link #content(int)} to get a result by index).
*
* @param path the path to look for.
* @return the content if one could be retrieved and no error occurred.
*/
public Object content(String path) {
return this.content(path, Object.class);
}
/**
* Attempt to get the value corresponding to the n-th operation, casting it as a specific class.
* If the operation was successful, the value will be returned. Otherwise the adequate {@link SubDocumentException}
* will be thrown (mostly in the case of multiple lookups).
*
* @param index the 0-based index of the operation to look for.
* @param targetClass the expected type of the content, to use in a cast.
* @return the content if one could be retrieved and no error occurred.
*/
public <T> T content(int index, Class<T> targetClass) {
return interpretResult(resultList.get(index));
}
/**
* Attempt to get the value corresponding to the n-th operation, as an Object.
* If the operation was successful, the value will be returned. Otherwise the adequate {@link SubDocumentException}
* will be thrown (mostly in the case of multiple lookups).
*
* @param index the 0-based index of the operation to look for.
* @return the content if one could be retrieved and no error occurred.
*/
public Object content(int index) {
return this.content(index, Object.class);
}
/**
* Attempt to get the serialized form of the value corresponding to the n-th operation, as a raw array of bytes.
* If the operation was successful, the value will be returned. Otherwise the adequate {@link SubDocumentException}
* will be thrown (mostly in the case of multiple lookups).
*
* @param index the 0-based index of the operation to look for.
* @return the raw byte array corresponding to the serialized content, if one could be retrieved and
* no error occurred.
*/
public byte[] rawContent(int index) {
return interpretResultRaw(resultList.get(index));
}
/**
* Attempt to get the serialized form of the value corresponding to the first operation that targeted the given
* path, as an array of bytes.
*
* If the operation was successful, the value will be returned. Otherwise the adequate {@link SubDocumentException}
* will be thrown (mostly in the case of multiple lookups).
*
* If multiple operations targeted the same path, this method only considers the first one (see
* {@link #rawContent(int)} to get a result by index).
*
* @param path the path to look for.
* @return the raw byte array corresponding to the serialized content, if one could be retrieved and
* no error occurred.
*/
public byte[] rawContent(String path) {
if (path == null) {
return null;
}
for (SubdocOperationResult<OPERATION> result : resultList) {
if (path.equals(result.path())) {
return interpretResultRaw(result);
}
}
return null;
}
private <T> T interpretResult(SubdocOperationResult<OPERATION> result) {
if (result.status() == ResponseStatus.FAILURE && result.value() instanceof RuntimeException) {
//case where a fatal error happened while PARSING the response
throw (RuntimeException) result.value();
} else if (result.value() instanceof CouchbaseException) {
//case where the server returned an error for this operation
throw (CouchbaseException) result.value();
} else {
//case where the server returned a value (or null if not applicable) for this operation
return (T) result.value();
}
}
private byte[] interpretResultRaw(SubdocOperationResult<OPERATION> result) {
if (result.status() == ResponseStatus.FAILURE && result.value() instanceof RuntimeException) {
//case where a fatal error happened while PARSING the response
throw (RuntimeException) result.value();
} else if (result.value() instanceof CouchbaseException) {
//case where the server returned an error for this operation
throw (CouchbaseException) result.value();
} else {
//case where the server returned a raw value (or null if not applicable) for this operation
return result.rawValue();
}
}
/**
* Get the operation status code corresponding to the first operation that targeted the given path.
*
* This can be used in place of {@link #content(String)} in order to avoid an {@link CouchbaseException} being thrown.
*
* @param path the path of the desired operation.
* @return the status of the operation.
*/
public ResponseStatus status(String path) {
if (path == null) {
return null;
}
for (SubdocOperationResult<OPERATION> result : resultList) {
if (path.equals(result.path())) {
return result.status();
}
}
return null;
}
/**
* Get the operation status code corresponding to the n-th operation.
*
* This can be used in place of {@link #content(int)} in order to avoid an {@link CouchbaseException} being thrown.
*
* @param index the 0-based index of the desired operation.
* @return the status of the operation.
*/
public ResponseStatus status(int index) {
return resultList.get(index).status();
}
/**
* Checks whether the given path is part of this result set, eg. an operation targeted it, and the operation executed successfully.
*
* @return true if that path is part of the successful result set, false in any other case.
*/
public boolean exists(String path) {
if (path == null) {
return false;
}
for (SubdocOperationResult<OPERATION> result : resultList) {
if (path.equals(result.path()) && !(result.value() instanceof Exception)) {
return true;
}
}
return false;
}
/**
* Checks whether the given index is part of this result set and the operation was executed successfully.
*
* @return true if that path is part of the successful result set, false in any other case.
*/
public boolean exists(int specIndex) {
return specIndex >= 0 && specIndex < resultList.size()
&& !(resultList.get(specIndex).value() instanceof Exception);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("DocumentFragment{")
.append("id='").append(id).append('\'')
.append(", cas=").append(cas)
.append(", mutationToken=").append(mutationToken)
.append('}');
if (resultList != null && !resultList.isEmpty()) {
sb.append(resultList);
}
return sb.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DocumentFragment<?> that = (DocumentFragment<?>) o;
if (cas != that.cas) {
return false;
}
if (!id.equals(that.id)) {
return false;
}
if (mutationToken != null ? !mutationToken.equals(that.mutationToken) : that.mutationToken != null) {
return false;
}
return resultList.equals(that.resultList);
}
@Override
public int hashCode() {
int result = id.hashCode();
result = 31 * result + (int) (cas ^ (cas >>> 32));
result = 31 * result + (mutationToken != null ? mutationToken.hashCode() : 0);
result = 31 * result + resultList.hashCode();
return result;
}
}