Exemplo n.º 1
0
 def test_search_all_options(self, mock_query):
     mock_search = MagicMock()
     redisearch._get_redisearch_index_client = MagicMock(return_value=MagicMock(search=mock_search))
     mock_query_params = MagicMock()
     mock_query.return_value = MagicMock(paging=MagicMock(return_value=mock_query_params))
     redisearch.search(index="banana", query_str="100", only_id=True, verbatim=True, offset=5, limit=100, sort_by="timestamp", sort_asc=False)
     redisearch._get_redisearch_index_client.assert_called_once_with("banana")
     mock_query.assert_called_once_with("100")
     mock_search.assert_called_once_with(mock_query_params)
Exemplo n.º 2
0
def get_contract_id_by_txn_type(txn_type: str) -> str:
    results = redisearch.search(
        index=redisearch.Indexes.smartcontract.value, query_str=f"@sc_name:{{{redisearch.get_escaped_redisearch_string(txn_type)}}}", only_id=True
    ).docs
    if results:
        return results[0].id
    raise exceptions.NotFound(f"Smart contract {txn_type} could not be found.")
Exemplo n.º 3
0
def query_blocks_v1(params: Dict[str, Any], parse: bool = False) -> "RSearch":
    """Returns block matching block id, with query parameters accepted.
    Args:
        block_id: string Block id to search for.
        params: Dictionary of redisearch query options
        parse: whether or not we should parse contents
    """
    try:
        query_result = redisearch.search(
            index=redisearch.Indexes.block.value,
            query_str=params["q"],
            only_id=params.get("id_only"),
            offset=params.get("offset"),
            limit=params.get("limit"),
            sort_by=params.get("sort_by"),
            sort_asc=params.get("sort_asc"),
        )
    except redis.exceptions.ResponseError as e:
        # Detect if this is a syntax error; if so, throw it back as a 400 with the message
        if str(e).startswith("Syntax error"):
            raise exceptions.BadRequest(str(e))
        else:
            raise
    result: "RSearch" = {"total": query_result.total, "results": []}
    if params.get("id_only"):
        result["results"] = [x.id for x in query_result.docs]
    else:
        blocks = []
        for block in query_result.docs:
            blocks.append(get_block_by_id_v1(block.id, parse))
        result["results"] = blocks
    return result
Exemplo n.º 4
0
def list_all_contract_ids() -> List[str]:
    query_result = redisearch.search(
        index=redisearch.Indexes.smartcontract.value,
        query_str="*",
        limit=10000,
        only_id=True)
    contract_ids = []
    for index in query_result.docs:
        contract_ids.append(index.id)
    return contract_ids
Exemplo n.º 5
0
def _query_l5_verification(l5_dc_id: str, timestamp: str) -> str:
    query_result = redisearch.search(
        index=redisearch.Indexes.verification.value,
        query_str=f"@dc_id:{{{l5_dc_id}}} @timestamp:[{int(timestamp)+1} +inf]",
        only_id=True,
        limit=1,
        sort_by="timestamp",
        sort_asc=True,
    )
    return query_result.docs[0].id if query_result and len(
        query_result.docs) > 0 else None
Exemplo n.º 6
0
def query_transactions_v1(params: Dict[str, Any],
                          parse: bool = True) -> "RSearch":
    """invoke queries on redisearch indexes
    Args:
        params: Dictionary of redisearch query options
        parse: If true, parse the transaction payload before returning
    Returns:
        {"results": [], "total": total} storage objects matching search query
    """
    if not params.get("transaction_type"):
        raise exceptions.ValidationException(
            "transaction_type must be supplied for transaction queries")
    try:
        query_result = redisearch.search(
            index=params["transaction_type"],
            query_str=params["q"],
            only_id=params.get("id_only"),
            verbatim=params.get("verbatim"),
            offset=params.get("offset"),
            limit=params.get("limit"),
            sort_by=params.get("sort_by"),
            sort_asc=params.get("sort_asc"),
        )
    except redis.exceptions.ResponseError as e:
        error_str = str(e)
        # Detect if this is a syntax error; if so, throw it back as a 400 with the message
        if error_str.startswith("Syntax error"):
            raise exceptions.BadRequest(error_str)
        # If unknown index, user provided a bad transaction type
        elif error_str == "Unknown Index name":
            raise exceptions.BadRequest("Invalid transaction type")
        else:
            raise
    result: "RSearch" = {"total": query_result.total, "results": []}
    if params.get("id_only"):
        result["results"] = [x.id for x in query_result.docs]
    else:
        transactions = []
        for doc in query_result.docs:
            block_id = doc.block_id
            transaction_id = doc.id
            retrieved_txn = storage.select_transaction(block_id,
                                                       transaction_id)
            if parse:
                retrieved_txn["payload"] = json.loads(retrieved_txn["payload"])
            transactions.append(retrieved_txn)
        result["results"] = transactions
    return result