| 39 | } |
| 40 | |
| 41 | int TopNNode::Heap::InsertTupleRow(TopNNode* node, TupleRow* input_row) { |
| 42 | const TupleDescriptor& tuple_desc = *node->output_tuple_desc_; |
| 43 | if (priority_queue_.Size() < heap_capacity()) { |
| 44 | // Add all tuples (including any ties) until we hit capacity. |
| 45 | Tuple* insert_tuple = reinterpret_cast<Tuple*>( |
| 46 | node->tuple_pool_->Allocate(node->tuple_byte_size())); |
| 47 | insert_tuple->MaterializeExprs<false, false>(input_row, tuple_desc, |
| 48 | node->output_tuple_expr_evals_, node->tuple_pool_.get()); |
| 49 | |
| 50 | priority_queue_.Push(insert_tuple); |
| 51 | return 0; |
| 52 | } |
| 53 | |
| 54 | // We're at capacity - compare to the first row in the priority queue to see if |
| 55 | // we need to insert this row into the queue. |
| 56 | DCHECK(!priority_queue_.Empty()); |
| 57 | Tuple* top_tuple = priority_queue_.Top(); |
| 58 | node->tmp_tuple_->MaterializeExprs<false, true>(input_row, tuple_desc, |
| 59 | node->output_tuple_expr_evals_, nullptr); |
| 60 | if (include_ties()) { |
| 61 | return InsertTupleWithTieHandling(*node->order_cmp_, node, node->tmp_tuple_); |
| 62 | } else { |
| 63 | if (node->order_cmp_->Less(node->tmp_tuple_, top_tuple)) { |
| 64 | // Pop off the old head, and replace with the new tuple. Deep copy into 'top_tuple' |
| 65 | // to reuse the fixed-length memory of 'top_tuple'. |
| 66 | node->tmp_tuple_->DeepCopy(top_tuple, tuple_desc, node->tuple_pool_.get()); |
| 67 | // Re-heapify from the top element and down. |
| 68 | priority_queue_.HeapifyFromTop(); |
| 69 | return 1; |
| 70 | } |
| 71 | return 0; |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | int TopNNode::Heap::InsertTupleWithTieHandling( |
| 76 | const TupleRowComparator& cmp, TopNNode* node, Tuple* materialized_tuple) { |
no test coverage detected