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
24 import com.allanbank.mongodb.ReadPreference;
25 import com.allanbank.mongodb.bson.Document;
26 import com.allanbank.mongodb.bson.io.BsonInputStream;
27 import com.allanbank.mongodb.bson.io.BsonOutputStream;
28 import com.allanbank.mongodb.bson.io.BufferingBsonOutputStream;
29 import com.allanbank.mongodb.bson.io.StringEncoder;
30 import com.allanbank.mongodb.client.Message;
31 import com.allanbank.mongodb.client.Operation;
32 import com.allanbank.mongodb.error.DocumentToLargeException;
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 public class Query extends AbstractMessage implements CursorableMessage {
61
62
63 public static final int AWAIT_DATA_FLAG_BIT = 0x20;
64
65
66 public static final int DEFAULT_BATCH_SIZE = 101;
67
68
69 public static final int EXHAUST_FLAG_BIT = 0x40;
70
71
72 public static final int NO_CURSOR_TIMEOUT_FLAG_BIT = 0x10;
73
74
75 public static final int OPLOG_REPLAY_FLAG_BIT = 0x08;
76
77
78 public static final int PARTIAL_FLAG_BIT = 0x80;
79
80
81 public static final int REPLICA_OK_FLAG_BIT = 0x04;
82
83
84 public static final int TAILABLE_CURSOR_FLAG_BIT = 0x02;
85
86
87
88
89
90 private final boolean myAwaitData;
91
92
93 private final int myBatchSize;
94
95
96 private final boolean myExhaust;
97
98
99 private final int myLimit;
100
101
102
103
104 private int myMessageSize;
105
106
107 private final boolean myNoCursorTimeout;
108
109
110 private final int myNumberToReturn;
111
112
113
114
115 private final int myNumberToSkip;
116
117
118
119
120 private final boolean myPartial;
121
122
123
124
125
126 private final Document myQuery;
127
128
129 private final Document myReturnFields;
130
131
132
133
134
135 private final boolean myTailable;
136
137
138
139
140
141
142
143
144
145
146
147 public Query(final Header header, final BsonInputStream in)
148 throws IOException {
149 final long position = in.getBytesRead();
150 final long end = (position + header.getLength()) - Header.SIZE;
151
152 final int flags = in.readInt();
153 init(in.readCString());
154 myNumberToSkip = in.readInt();
155 myNumberToReturn = in.readInt();
156 myQuery = in.readDocument();
157 if (in.getBytesRead() < end) {
158 myReturnFields = in.readDocument();
159 }
160 else {
161 myReturnFields = null;
162 }
163 myAwaitData = (flags & AWAIT_DATA_FLAG_BIT) == AWAIT_DATA_FLAG_BIT;
164 myExhaust = (flags & EXHAUST_FLAG_BIT) == EXHAUST_FLAG_BIT;
165 myNoCursorTimeout = (flags & NO_CURSOR_TIMEOUT_FLAG_BIT) == NO_CURSOR_TIMEOUT_FLAG_BIT;
166 myPartial = (flags & PARTIAL_FLAG_BIT) == PARTIAL_FLAG_BIT;
167 myTailable = (flags & TAILABLE_CURSOR_FLAG_BIT) == TAILABLE_CURSOR_FLAG_BIT;
168
169 myLimit = 0;
170 myBatchSize = 0;
171 myMessageSize = -1;
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 public Query(final String databaseName, final String collectionName,
211 final Document query, final Document returnFields,
212 final int batchSize, final int limit, final int numberToSkip,
213 final boolean tailable, final ReadPreference readPreference,
214 final boolean noCursorTimeout, final boolean awaitData,
215 final boolean exhaust, final boolean partial) {
216 super(databaseName, collectionName, readPreference, QueryVersionVisitor
217 .version(query));
218
219 myQuery = query;
220 myReturnFields = returnFields;
221 myLimit = limit;
222 myBatchSize = batchSize;
223 myNumberToSkip = numberToSkip;
224 myTailable = tailable;
225 myNoCursorTimeout = noCursorTimeout;
226 myAwaitData = awaitData;
227 myExhaust = exhaust;
228 myPartial = partial;
229 myMessageSize = -1;
230
231 if (isBatchSizeSet()) {
232 if (isLimitSet() && (myLimit <= myBatchSize)) {
233 myNumberToReturn = -myLimit;
234 }
235 else {
236 myNumberToReturn = myBatchSize;
237 }
238 }
239 else if (isLimitSet() && (myLimit <= DEFAULT_BATCH_SIZE)) {
240 myNumberToReturn = -myLimit;
241 }
242 else {
243 myNumberToReturn = 0;
244 }
245 }
246
247
248
249
250
251
252
253
254
255
256 @Override
257 public boolean equals(final Object object) {
258 boolean result = false;
259 if (this == object) {
260 result = true;
261 }
262 else if ((object != null) && (getClass() == object.getClass())) {
263 final Query other = (Query) object;
264
265 result = super.equals(object)
266 && (myAwaitData == other.myAwaitData)
267 && (myExhaust == other.myExhaust)
268 && (myNoCursorTimeout == other.myNoCursorTimeout)
269 && (myPartial == other.myPartial)
270 && (myTailable == other.myTailable)
271 && (myBatchSize == other.myBatchSize)
272 && (myLimit == other.myLimit)
273 && (myNumberToReturn == other.myNumberToReturn)
274 && (myNumberToSkip == other.myNumberToSkip)
275 && myQuery.equals(other.myQuery)
276 && ((myReturnFields == other.myReturnFields) || ((myReturnFields != null) && myReturnFields
277 .equals(other.myReturnFields)));
278 }
279 return result;
280 }
281
282
283
284
285
286
287 @Override
288 public int getBatchSize() {
289 return myBatchSize;
290 }
291
292
293
294
295
296
297 @Override
298 public int getLimit() {
299 return myLimit;
300 }
301
302
303
304
305
306
307 public int getNumberToReturn() {
308 return myNumberToReturn;
309 }
310
311
312
313
314
315
316
317
318 public int getNumberToSkip() {
319 return myNumberToSkip;
320 }
321
322
323
324
325
326
327
328 @Override
329 public String getOperationName() {
330 return Operation.QUERY.name();
331 }
332
333
334
335
336
337
338
339
340 public Document getQuery() {
341 return myQuery;
342 }
343
344
345
346
347
348
349
350 public Document getReturnFields() {
351 return myReturnFields;
352 }
353
354
355
356
357
358
359 @Override
360 public int hashCode() {
361 int result = 1;
362 result = (31 * result) + super.hashCode();
363 result = (31 * result) + (myAwaitData ? 1 : 3);
364 result = (31 * result) + (myExhaust ? 1 : 7);
365 result = (31 * result) + (myNoCursorTimeout ? 1 : 11);
366 result = (31 * result) + (myPartial ? 1 : 13);
367 result = (31 * result) + (myTailable ? 1 : 19);
368 result = (31 * result) + myBatchSize;
369 result = (31 * result) + myLimit;
370 result = (31 * result) + myNumberToReturn;
371 result = (31 * result) + myNumberToSkip;
372 result = (31 * result) + myQuery.hashCode();
373 result = (31 * result)
374 + (myReturnFields == null ? 1 : myReturnFields.hashCode());
375 return result;
376 }
377
378
379
380
381
382
383
384
385 public boolean isAwaitData() {
386 return myAwaitData;
387 }
388
389
390
391
392
393
394 public boolean isBatchSizeSet() {
395 return 0 < myBatchSize;
396 }
397
398
399
400
401
402
403 public boolean isExhaust() {
404 return myExhaust;
405 }
406
407
408
409
410
411
412 public boolean isLimitSet() {
413 return 0 < myLimit;
414 }
415
416
417
418
419
420
421 public boolean isNoCursorTimeout() {
422 return myNoCursorTimeout;
423 }
424
425
426
427
428
429
430 public boolean isPartial() {
431 return myPartial;
432 }
433
434
435
436
437
438
439
440
441 public boolean isTailable() {
442 return myTailable;
443 }
444
445
446
447
448
449
450
451 @Override
452 public int size() {
453
454 int size = HEADER_SIZE + 14;
455
456 size += StringEncoder.utf8Size(myDatabaseName);
457
458 size += StringEncoder.utf8Size(myCollectionName);
459
460
461
462 size += myQuery.size();
463 if (myReturnFields != null) {
464 size += myReturnFields.size();
465 }
466
467 return size;
468 }
469
470
471
472
473
474
475
476
477 @Override
478 public void validateSize(final int maxDocumentSize)
479 throws DocumentToLargeException {
480 if (myMessageSize < 0) {
481 long size = 0;
482 if (myQuery != null) {
483 size += myQuery.size();
484 }
485 if (myReturnFields != null) {
486 size += myReturnFields.size();
487 }
488
489 myMessageSize = (int) size;
490 }
491
492 if (maxDocumentSize < myMessageSize) {
493 throw new DocumentToLargeException(myMessageSize, maxDocumentSize,
494 myQuery);
495 }
496 }
497
498
499
500
501
502
503
504
505
506 @Override
507 public void write(final int messageId, final BsonOutputStream out)
508 throws IOException {
509 final int flags = computeFlags();
510
511 int size = HEADER_SIZE;
512 size += 4;
513 size += out.sizeOfCString(myDatabaseName, ".", myCollectionName);
514 size += 4;
515 size += 4;
516 size += myQuery.size();
517 if (myReturnFields != null) {
518 size += myReturnFields.size();
519 }
520
521 writeHeader(out, messageId, 0, Operation.QUERY, size);
522 out.writeInt(flags);
523 out.writeCString(myDatabaseName, ".", myCollectionName);
524 out.writeInt(myNumberToSkip);
525 out.writeInt(myNumberToReturn);
526 out.writeDocument(myQuery);
527 if (myReturnFields != null) {
528 out.writeDocument(myReturnFields);
529 }
530 }
531
532
533
534
535
536
537
538
539
540 @Override
541 public void write(final int messageId, final BufferingBsonOutputStream out)
542 throws IOException {
543 final int flags = computeFlags();
544
545 final long start = writeHeader(out, messageId, 0, Operation.QUERY);
546 out.writeInt(flags);
547 out.writeCString(myDatabaseName, ".", myCollectionName);
548 out.writeInt(myNumberToSkip);
549 out.writeInt(myNumberToReturn);
550 out.writeDocument(myQuery);
551 if (myReturnFields != null) {
552 out.writeDocument(myReturnFields);
553 }
554 finishHeader(out, start);
555
556 out.flushBuffer();
557 }
558
559
560
561
562
563
564 private int computeFlags() {
565 int flags = 0;
566 if (myAwaitData) {
567 flags += AWAIT_DATA_FLAG_BIT;
568 }
569 if (myExhaust) {
570 flags += EXHAUST_FLAG_BIT;
571 }
572 if (myNoCursorTimeout) {
573 flags += NO_CURSOR_TIMEOUT_FLAG_BIT;
574 }
575 if (myPartial) {
576 flags += PARTIAL_FLAG_BIT;
577 }
578 if (getReadPreference().isSecondaryOk()) {
579 flags += REPLICA_OK_FLAG_BIT;
580 }
581 if (myTailable) {
582 flags += TAILABLE_CURSOR_FLAG_BIT;
583 }
584 return flags;
585 }
586 }