def store_full_txns(block_model: "l1_block_model.L1BlockModel") -> None: """ Store the transactions object as a single file per block in storage. Also updates the indexes for each indexed transaction in ES with block information. """ _log.info("[TRANSACTION DAO] Putting transaction to storage") storage.put(f"{FOLDER}/{block_model.block_id}", block_model.export_as_full_transactions().encode("utf-8")) block_model.store_transaction_payloads() txn_dict: Dict[str, Dict[str, Dict[str, Any]]] = {} txn_dict[redisearch.Indexes.transaction.value] = {} # O(N) loop where N = # of txn # Could optimize by grouping indexing of transactions in the block with matchking txn_types using redisearch.put_many_documents for txn in block_model.transactions: txn_dict[redisearch.Indexes.transaction.value][f"txn-{txn.txn_id}"] = {"block_id": txn.block_id} try: if not txn_dict.get(txn.txn_type): txn_dict[txn.txn_type] = {} txn_dict[txn.txn_type][txn.txn_id] = txn.export_as_search_index() except redis.exceptions.ResponseError as e: # If the index doesn't exist, we don't care that we couldn't place the index (transaction type is probably deleted) if str(e) != "Unknown index name": raise else: _log.warning(f"Txn type {txn.txn_type} for txn {txn.txn_id} failed to index. (Transaction type may simply be deleted?) Ignoring") # O(N) loop where N = # of txn types + 1 for key, value in txn_dict.items(): # key = index, value = document redisearch.put_many_documents(key, value, upsert=True)
def test_put_many_documents_respects_params(self): mock_indexer = MagicMock(return_value=MagicMock(add_document=MagicMock(), commit=MagicMock())) redisearch._get_redisearch_index_client = MagicMock(return_value=MagicMock(batch_indexer=mock_indexer)) redisearch.put_many_documents(index="banana", documents={"doc1": {"fruit": "apple"}, "doc2": {"vegetable": "avocado"}}, partial_update=True) mock_indexer.assert_called_once_with(chunk_size=1000) mock_indexer.return_value.add_document.assert_any_call("doc1", fruit="apple", replace=True, partial=True) mock_indexer.return_value.add_document.assert_any_call("doc2", vegetable="avocado", replace=True, partial=True) self.assertEqual(2, mock_indexer.return_value.add_document.call_count) mock_indexer.return_value.commit.assert_called_once()