Esempio n. 1
0
def query(options, collection_name, num_to_skip,
          num_to_return, query, field_selector, opts, check_keys=False):
    """Get a **query** message.
    """
    data = struct.pack("<I", options)
    data += bson._make_c_string(collection_name)
    data += struct.pack("<i", num_to_skip)
    data += struct.pack("<i", num_to_return)
    if check_keys:
        # Temporarily remove $clusterTime to avoid an error from the $-prefix.
        cluster_time = query.pop('$clusterTime', None)
        encoded = bson.BSON.encode(query, True, opts)
        if cluster_time is not None:
            extra = bson._name_value_to_bson(
                b"$clusterTime\x00", cluster_time, False, opts)
            encoded = (
                bson._PACK_INT(len(encoded) + len(extra))
                + encoded[4:-1] + extra + b'\x00')
            query['$clusterTime'] = cluster_time
    else:
        encoded = bson.BSON.encode(query, False, opts)
    data += encoded
    max_bson_size = len(encoded)
    if field_selector is not None:
        encoded = bson.BSON.encode(field_selector, False, opts)
        data += encoded
        max_bson_size = max(len(encoded), max_bson_size)
    (request_id, query_message) = __pack_message(2004, data)
    return (request_id, query_message, max_bson_size)
Esempio n. 2
0
def query(options,
          collection_name,
          num_to_skip,
          num_to_return,
          query,
          field_selector,
          opts,
          check_keys=False):
    """Get a **query** message.
    """
    data = struct.pack("<I", options)
    data += bson._make_c_string(collection_name)
    data += struct.pack("<i", num_to_skip)
    data += struct.pack("<i", num_to_return)
    if check_keys:
        # Temporarily remove $clusterTime to avoid an error from the $-prefix.
        cluster_time = query.pop('$clusterTime', None)
        encoded = bson.BSON.encode(query, True, opts)
        if cluster_time is not None:
            extra = bson._name_value_to_bson(b"$clusterTime\x00", cluster_time,
                                             False, opts)
            encoded = (bson._PACK_INT(len(encoded) + len(extra)) +
                       encoded[4:-1] + extra + b'\x00')
            query['$clusterTime'] = cluster_time
    else:
        encoded = bson.BSON.encode(query, False, opts)
    data += encoded
    max_bson_size = len(encoded)
    if field_selector is not None:
        encoded = bson.BSON.encode(field_selector, False, opts)
        data += encoded
        max_bson_size = max(len(encoded), max_bson_size)
    (request_id, query_message) = __pack_message(2004, data)
    return (request_id, query_message, max_bson_size)
Esempio n. 3
0
def _query(options, collection_name, num_to_skip,
           num_to_return, query, field_selector, opts, check_keys):
    """Get an OP_QUERY message."""
    encode = _dict_to_bson  # Make local. Uses extensions.
    if check_keys and "$clusterTime" in query:
        # Temporarily remove $clusterTime to avoid an error from the $-prefix.
        cluster_time = query.pop('$clusterTime')
        encoded = encode(query, True, opts)
        extra = bson._name_value_to_bson(
            b"$clusterTime\x00", cluster_time, False, opts)
        encoded = (
            _pack_int(len(encoded) + len(extra))
            + encoded[4:-1] + extra + b'\x00')
        query['$clusterTime'] = cluster_time
    else:
        encoded = encode(query, check_keys, opts)
    if field_selector:
        efs = encode(field_selector, False, opts)
    else:
        efs = b""
    max_bson_size = max(len(encoded), len(efs))
    return b"".join([
        _pack_int(options),
        _make_c_string(collection_name),
        _pack_int(num_to_skip),
        _pack_int(num_to_return),
        encoded,
        efs]), max_bson_size