1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.allanbank.mongodb.client.message;
21
22 import java.io.IOException;
23 import java.util.Iterator;
24
25 import com.allanbank.mongodb.ReadPreference;
26 import com.allanbank.mongodb.bson.Document;
27 import com.allanbank.mongodb.bson.Element;
28 import com.allanbank.mongodb.bson.impl.EmptyDocument;
29 import com.allanbank.mongodb.bson.io.BsonOutputStream;
30 import com.allanbank.mongodb.bson.io.BufferingBsonOutputStream;
31 import com.allanbank.mongodb.bson.io.StringEncoder;
32 import com.allanbank.mongodb.client.Operation;
33 import com.allanbank.mongodb.client.VersionRange;
34 import com.allanbank.mongodb.error.DocumentToLargeException;
35
36
37
38
39
40
41
42
43
44
45 public class Command extends AbstractMessage {
46
47
48 public static final String COMMAND_COLLECTION = "$cmd";
49
50
51 private static final int HEADROOM = 16 * 1024;
52
53
54
55
56
57 private boolean myAllowJumbo = false;
58
59
60 private final Document myCommand;
61
62
63 private final Document myRoutingDocument;
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public Command(final String databaseName, final String collectionName,
78 final Document commandDocument) {
79 this(databaseName, collectionName, commandDocument,
80 ReadPreference.PRIMARY);
81 }
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103 public Command(final String databaseName, final String collectionName,
104 final Document commandDocument, final Document routingDocument,
105 final ReadPreference readPreference,
106 final VersionRange requiredServerVersion) {
107 super(databaseName, collectionName, readPreference,
108 requiredServerVersion);
109
110 myCommand = commandDocument;
111 myRoutingDocument = routingDocument;
112 }
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 public Command(final String databaseName, final String collectionName,
130 final Document commandDocument, final ReadPreference readPreference) {
131 this(databaseName, collectionName, commandDocument, readPreference,
132 null);
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 public Command(final String databaseName, final String collectionName,
154 final Document commandDocument,
155 final ReadPreference readPreference,
156 final VersionRange requiredServerVersion) {
157 this(databaseName, collectionName, commandDocument,
158 EmptyDocument.INSTANCE, readPreference, requiredServerVersion);
159 }
160
161
162
163
164
165
166
167
168
169
170 @Override
171 public boolean equals(final Object object) {
172 boolean result = false;
173 if (this == object) {
174 result = true;
175 }
176 else if ((object != null) && (getClass() == object.getClass())) {
177 final Command other = (Command) object;
178
179 result = super.equals(object) && myCommand.equals(other.myCommand);
180 }
181 return result;
182 }
183
184
185
186
187
188
189 public Document getCommand() {
190 return myCommand;
191 }
192
193
194
195
196
197
198
199 @Override
200 public String getOperationName() {
201
202 final Iterator<Element> iter = myCommand.iterator();
203 if (iter.hasNext()) {
204 return iter.next().getName();
205 }
206
207
208 return "command";
209 }
210
211
212
213
214
215
216 public Document getRoutingDocument() {
217 return myRoutingDocument;
218 }
219
220
221
222
223
224
225 @Override
226 public int hashCode() {
227 int result = 1;
228 result = (31 * result) + super.hashCode();
229 result = (31 * result) + myCommand.hashCode();
230 return result;
231 }
232
233
234
235
236
237
238
239
240 public boolean isAllowJumbo() {
241 return myAllowJumbo;
242 }
243
244
245
246
247
248
249
250
251
252
253 public void setAllowJumbo(final boolean allowJumbo) {
254 myAllowJumbo = allowJumbo;
255 }
256
257
258
259
260
261
262
263 @Override
264 public int size() {
265 int size = HEADER_SIZE + 18;
266
267 size += StringEncoder.utf8Size(myDatabaseName);
268
269
270
271
272
273 size += myCommand.size();
274
275 return size;
276 }
277
278
279
280
281
282
283
284 @Override
285 public String toString() {
286 final StringBuilder builder = new StringBuilder();
287 builder.append("Command[");
288 builder.append(getOperationName());
289 builder.append(", db=");
290 builder.append(myDatabaseName);
291 builder.append(", collection=");
292 builder.append(myCollectionName);
293 if (getReadPreference() != null) {
294 builder.append(", readPreference=");
295 builder.append(getReadPreference());
296 }
297 if (getRequiredVersionRange() != null) {
298 builder.append(", requiredVersionRange=");
299 builder.append(getRequiredVersionRange());
300 }
301 builder.append("]: ");
302 builder.append(myCommand);
303
304 return builder.toString();
305 }
306
307
308
309
310
311
312
313 @Override
314 public void validateSize(final int maxDocumentSize)
315 throws DocumentToLargeException {
316 final long size = myCommand.size();
317
318 if (isAllowJumbo()) {
319 if ((maxDocumentSize + HEADROOM) < size) {
320 throw new DocumentToLargeException((int) size, maxDocumentSize
321 + HEADROOM, myCommand);
322 }
323 }
324 else if (maxDocumentSize < size) {
325 throw new DocumentToLargeException((int) size, maxDocumentSize,
326 myCommand);
327 }
328 }
329
330
331
332
333
334
335
336 @Override
337 public void write(final int messageId, final BsonOutputStream out)
338 throws IOException {
339 final int numberToSkip = 0;
340 final int numberToReturn = -1;
341 final int flags = computeFlags();
342
343 int size = HEADER_SIZE;
344 size += 4;
345 size += out.sizeOfCString(myDatabaseName, ".", COMMAND_COLLECTION);
346 size += 4;
347 size += 4;
348 size += myCommand.size();
349
350 writeHeader(out, messageId, 0, Operation.QUERY, size);
351 out.writeInt(flags);
352 out.writeCString(myDatabaseName, ".", COMMAND_COLLECTION);
353 out.writeInt(numberToSkip);
354 out.writeInt(numberToReturn);
355 out.writeDocument(myCommand);
356 }
357
358
359
360
361
362
363
364 @Override
365 public void write(final int messageId, final BufferingBsonOutputStream out)
366 throws IOException {
367 final int numberToSkip = 0;
368 final int numberToReturn = -1;
369 final int flags = computeFlags();
370
371 final long start = writeHeader(out, messageId, 0, Operation.QUERY);
372 out.writeInt(flags);
373 out.writeCString(myDatabaseName, ".", COMMAND_COLLECTION);
374 out.writeInt(numberToSkip);
375 out.writeInt(numberToReturn);
376 out.writeDocument(myCommand);
377 finishHeader(out, start);
378
379 out.flushBuffer();
380 }
381
382
383
384
385
386
387 private int computeFlags() {
388 int flags = 0;
389 if (getReadPreference().isSecondaryOk()) {
390 flags += Query.REPLICA_OK_FLAG_BIT;
391 }
392 return flags;
393 }
394 }