| 155 | } |
| 156 | |
| 157 | void TopNNode::Heap::InsertMaterializedTuple( |
| 158 | TopNNode* node, Tuple* materialized_tuple) { |
| 159 | DCHECK(node->is_partitioned()); |
| 160 | const TupleDescriptor& tuple_desc = *node->output_tuple_desc_; |
| 161 | Tuple* insert_tuple = nullptr; |
| 162 | if (priority_queue_.Size() < heap_capacity()) { |
| 163 | // Add all tuples until we hit capacity. |
| 164 | insert_tuple = |
| 165 | reinterpret_cast<Tuple*>(node->tuple_pool_->Allocate(node->tuple_byte_size())); |
| 166 | materialized_tuple->DeepCopy(insert_tuple, tuple_desc, node->tuple_pool_.get()); |
| 167 | priority_queue_.Push(insert_tuple); |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | // We're at capacity - compare to the first row in the priority queue to see if |
| 172 | // we need to insert this row into the heap. |
| 173 | DCHECK(!priority_queue_.Empty()); |
| 174 | Tuple* top_tuple = priority_queue_.Top(); |
| 175 | if (!include_ties()) { |
| 176 | ++num_tuples_discarded_; // One of the tuples will be discarded. |
| 177 | int cmp_result = |
| 178 | node->intra_partition_order_cmp_->Compare(materialized_tuple, top_tuple); |
| 179 | if (cmp_result >= 0) return; |
| 180 | // Pop off the old head, and replace with the new tuple. Reuse the fixed-length |
| 181 | // memory of 'top_tuple' to reduce allocations. |
| 182 | materialized_tuple->DeepCopy(top_tuple, tuple_desc, node->tuple_pool_.get()); |
| 183 | priority_queue_.HeapifyFromTop(); |
| 184 | return; |
| 185 | } |
| 186 | num_tuples_discarded_ += InsertTupleWithTieHandling( |
| 187 | *node->intra_partition_order_cmp_, node, materialized_tuple); |
| 188 | } |
no test coverage detected