src/backend/executor/delete_executor.cpp
/**
 * @brief Delete the table tuples using the position list in the logical tile.
 *
 * If truncate is on, then it will truncate the table itself.
 * @return true on success, false otherwise.
 */
@brief Delete the table tuples using the position list in the logical tile.
If truncate is on, then it will truncate the table itself.
@return true on success, false otherwise.
@brief Delete the table tuples using the position list in the logical tile . If truncate is on then it will truncate the table itself . @return true on success false otherwise .
bool DeleteExecutor::DExecute() {
  assert(target_table_);
  if (!children_[0]->Execute()) {
    return false;
  }
  std::unique_ptr<LogicalTile> source_tile(children_[0]->GetOutput());
  auto &pos_lists = source_tile.get()->GetPositionLists();
  storage::Tile *tile = source_tile->GetBaseTile(0);
  storage::TileGroup *tile_group = tile->GetTileGroup();
  storage::TileGroupHeader *tile_group_header = tile_group->GetHeader();
  auto tile_group_id = tile_group->GetTileGroupId();
  auto &transaction_manager =
      concurrency::TransactionManagerFactory::GetInstance();
  LOG_TRACE("Source tile : %p Tuples : %lu ", source_tile.get(),
            source_tile->GetTupleCount());
  LOG_TRACE("Transaction ID: %lu",
            executor_context_->GetTransaction()->GetTransactionId());
  for (oid_t visible_tuple_id : *source_tile) {
    oid_t physical_tuple_id = pos_lists[0][visible_tuple_id];
    ItemPointer old_location(tile_group_id, physical_tuple_id);
    LOG_TRACE("Visible Tuple id : %u, Physical Tuple id : %u ",
              visible_tuple_id, physical_tuple_id);
    if (transaction_manager.IsOwner(tile_group_header, physical_tuple_id) ==
        true) {
      transaction_manager.PerformDelete(old_location);
    } else if (transaction_manager.IsOwnable(tile_group_header,
                                             physical_tuple_id) == true) {
      if (transaction_manager.AcquireOwnership(tile_group_header, tile_group_id,
                                               physical_tuple_id) == false) {
        transaction_manager.SetTransactionResult(RESULT_FAILURE);
        return false;
      }
      std::unique_ptr<storage::Tuple> new_tuple(new storage::Tuple(target_table_->GetSchema(), true));
      expression::ContainerTuple<storage::TileGroup> old_tuple(
          tile_group, physical_tuple_id);
      ItemPointer new_location = target_table_->InsertEmptyVersion(new_tuple.get());
      if (new_location.IsNull() == true) {
        LOG_TRACE("Fail to insert new tuple. Set txn failure.");
        transaction_manager.SetTransactionResult(Result::RESULT_FAILURE);
        return false;
      }
      transaction_manager.PerformDelete(old_location, new_location);
      executor_context_->num_processed += 1;  
    } else {
      LOG_TRACE("Fail to update tuple. Set txn failure.");
      transaction_manager.SetTransactionResult(Result::RESULT_FAILURE);
      return false;
    }
  }
  return true;
}
bool DeleteExecutor :: DExecute ( ) { assert ( target_table_ ) ; if ( ! children_ [ 0 ] -> Execute ( ) ) { return false ; } std :: unique_ptr < LogicalTile > source_tile ( children_ [ 0 ] -> GetOutput ( ) ) ; auto & pos_lists = source_tile . get ( ) -> GetPositionLists ( ) ; storage :: Tile * tile = source_tile -> GetBaseTile ( 0 ) ; storage :: TileGroup * tile_group = tile -> GetTileGroup ( ) ; storage :: TileGroupHeader * tile_group_header = tile_group -> GetHeader ( ) ; auto tile_group_id = tile_group -> GetTileGroupId ( ) ; auto & transaction_manager = concurrency :: TransactionManagerFactory :: GetInstance ( ) ; LOG_TRACE ( " " , source_tile . get ( ) , source_tile -> GetTupleCount ( ) ) ; LOG_TRACE ( " " , executor_context_ -> GetTransaction ( ) -> GetTransactionId ( ) ) ; for ( oid_t visible_tuple_id : * source_tile ) { oid_t physical_tuple_id = pos_lists [ 0 ] [ visible_tuple_id ] ; ItemPointer old_location ( tile_group_id , physical_tuple_id ) ; LOG_TRACE ( " " , visible_tuple_id , physical_tuple_id ) ; if ( transaction_manager . IsOwner ( tile_group_header , physical_tuple_id ) == true ) { transaction_manager . PerformDelete ( old_location ) ; } else if ( transaction_manager . IsOwnable ( tile_group_header , physical_tuple_id ) == true ) { if ( transaction_manager . AcquireOwnership ( tile_group_header , tile_group_id , physical_tuple_id ) == false ) { transaction_manager . SetTransactionResult ( RESULT_FAILURE ) ; return false ; } std :: unique_ptr < storage :: Tuple > new_tuple ( new storage :: Tuple ( target_table_ -> GetSchema ( ) , true ) ) ; expression :: ContainerTuple < storage :: TileGroup > old_tuple ( tile_group , physical_tuple_id ) ; ItemPointer new_location = target_table_ -> InsertEmptyVersion ( new_tuple . get ( ) ) ; if ( new_location . IsNull ( ) == true ) { LOG_TRACE ( " " ) ; transaction_manager . SetTransactionResult ( Result :: RESULT_FAILURE ) ; return false ; } transaction_manager . PerformDelete ( old_location , new_location ) ; executor_context_ -> num_processed += 1 ; } else { LOG_TRACE ( " " ) ; transaction_manager . SetTransactionResult ( Result :: RESULT_FAILURE ) ; return false ; } } return true ; }
@brief Delete the table tuples using the position list in the logical tile.
@brief Delete the table tuples using the position list in the logical tile .
// Retrieve next tile. // Delete each tuple // if the thread is the owner of the tuple, then directly update in place. // if the tuple is not owned by any transaction and is visible to current // transaction. // if it is the latest version and not locked by other threads, then // insert a new version. // Make a copy of the original tuple and allocate a new tuple // finally insert updated tuple into the table // deleted one // transaction should be aborted as we cannot update the latest version.