|
28 | 28 | from bson import (CodecOptions, |
29 | 29 | decode, |
30 | 30 | encode, |
| 31 | + _decode_selective, |
31 | 32 | _dict_to_bson, |
32 | 33 | _make_c_string) |
33 | 34 | from bson.codec_options import DEFAULT_CODEC_OPTIONS |
34 | | -from bson.raw_bson import _inflate_bson, DEFAULT_RAW_BSON_OPTIONS |
| 35 | +from bson.raw_bson import (_inflate_bson, DEFAULT_RAW_BSON_OPTIONS, |
| 36 | + RawBSONDocument) |
35 | 37 | from bson.py3compat import b, StringIO |
36 | 38 | from bson.son import SON |
37 | 39 |
|
@@ -442,28 +444,30 @@ def get_message(self, dummy0, sock_info, use_cmd=False): |
442 | 444 | return get_more(ns, self.ntoreturn, self.cursor_id, ctx) |
443 | 445 |
|
444 | 446 |
|
445 | | -# TODO: Use OP_MSG once the server is able to respond with document streams. |
446 | 447 | class _RawBatchQuery(_Query): |
447 | 448 | def use_command(self, socket_info, exhaust): |
448 | 449 | # Compatibility checks. |
449 | 450 | super(_RawBatchQuery, self).use_command(socket_info, exhaust) |
450 | | - |
| 451 | + # Use OP_MSG when available. |
| 452 | + if socket_info.op_msg_enabled and not exhaust: |
| 453 | + return True |
451 | 454 | return False |
452 | 455 |
|
453 | 456 | def get_message(self, set_slave_ok, sock_info, use_cmd=False): |
454 | | - # Always pass False for use_cmd. |
455 | 457 | return super(_RawBatchQuery, self).get_message( |
456 | | - set_slave_ok, sock_info, False) |
| 458 | + set_slave_ok, sock_info, use_cmd) |
457 | 459 |
|
458 | 460 |
|
459 | 461 | class _RawBatchGetMore(_GetMore): |
460 | 462 | def use_command(self, socket_info, exhaust): |
| 463 | + # Use OP_MSG when available. |
| 464 | + if socket_info.op_msg_enabled and not exhaust: |
| 465 | + return True |
461 | 466 | return False |
462 | 467 |
|
463 | 468 | def get_message(self, set_slave_ok, sock_info, use_cmd=False): |
464 | | - # Always pass False for use_cmd. |
465 | 469 | return super(_RawBatchGetMore, self).get_message( |
466 | | - set_slave_ok, sock_info, False) |
| 470 | + set_slave_ok, sock_info, use_cmd) |
467 | 471 |
|
468 | 472 |
|
469 | 473 | class _CursorAddress(tuple): |
@@ -1492,7 +1496,7 @@ def __init__(self, flags, cursor_id, number_returned, documents): |
1492 | 1496 | self.number_returned = number_returned |
1493 | 1497 | self.documents = documents |
1494 | 1498 |
|
1495 | | - def raw_response(self, cursor_id=None): |
| 1499 | + def raw_response(self, cursor_id=None, user_fields=None): |
1496 | 1500 | """Check the response header from the database, without decoding BSON. |
1497 | 1501 |
|
1498 | 1502 | Check the response for errors and unpack. |
@@ -1602,8 +1606,15 @@ def __init__(self, flags, payload_document): |
1602 | 1606 | self.flags = flags |
1603 | 1607 | self.payload_document = payload_document |
1604 | 1608 |
|
1605 | | - def raw_response(self, cursor_id=None): |
1606 | | - raise NotImplementedError |
| 1609 | + def raw_response(self, cursor_id=None, user_fields={}): |
| 1610 | + """ |
| 1611 | + cursor_id is ignored |
| 1612 | + user_fields is used to determine which fields must not be decoded |
| 1613 | + """ |
| 1614 | + inflated_response = _decode_selective( |
| 1615 | + RawBSONDocument(self.payload_document), user_fields, |
| 1616 | + DEFAULT_RAW_BSON_OPTIONS) |
| 1617 | + return [inflated_response] |
1607 | 1618 |
|
1608 | 1619 | def unpack_response(self, cursor_id=None, |
1609 | 1620 | codec_options=_UNICODE_REPLACE_CODEC_OPTIONS, |
|
0 commit comments