Untitled

mail@pastecode.io avatar
unknown
c_cpp
6 months ago
3.1 kB
2
Indexable
Never
//===----------------------------------------------------------------------===//
//
//                         BusTub
//
// insert_executor.cpp
//
// Identification: src/execution/insert_executor.cpp
//
// Copyright (c) 2015-2021, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//

// #include <_types/_uint32_t.h>
// #include <_types/_uint64_t.h>
#include "execution/executors/insert_executor.h"
#include <memory>
#include "concurrency/transaction_manager.h"

namespace bustub {

InsertExecutor::InsertExecutor(ExecutorContext *exec_ctx, const InsertPlanNode *plan,
                               std::unique_ptr<AbstractExecutor> &&child_executor)
    : AbstractExecutor(exec_ctx) {
  child_executor_ = std::move(child_executor);
  plan_ = plan;
  table_info_ = exec_ctx_->GetCatalog()->GetTable(plan_->GetTableOid());
  first_call_ = false;
}

void InsertExecutor::Init() {
  child_executor_->Init();
  first_call_ = true;
}

auto InsertExecutor::Next([[maybe_unused]] Tuple *tuple, RID *rid) -> bool {
  if (!first_call_) {
    return false;
  }
  first_call_ = false;
  Tuple c_tuple;
  RID c_rid;
  TupleMeta tuplemeta;
  tuplemeta.is_deleted_ = false;
  // tuplemeta.ts_ = 0;  // TODO(chien-yu-liu): update timestamp
  tuplemeta.ts_ = exec_ctx_->GetTransaction()->GetTransactionTempTs();

  // RID final_rid;

  int32_t count = 0;
  std::vector<IndexInfo *> index_infos = exec_ctx_->GetCatalog()->GetTableIndexes(table_info_->name_);
  while (child_executor_->Next(&c_tuple, &c_rid)) {
    auto insert_rid = table_info_->table_->InsertTuple(tuplemeta, c_tuple, exec_ctx_->GetLockManager(),
                                                       exec_ctx_->GetTransaction(), plan_->GetTableOid());
    count++;

    for (auto &index_info : index_infos) {
      auto metadata = index_info->index_->GetMetadata();
      auto key_attr = metadata->GetKeyAttrs();
      auto key_schema = index_info->index_->GetKeySchema();
      auto key = c_tuple.KeyFromTuple(table_info_->schema_, *key_schema, key_attr);

      index_info->index_->InsertEntry(key, insert_rid.value(), exec_ctx_->GetTransaction());
    }

    // final_rid = insert_rid.value();
    exec_ctx_->GetTransaction()->AppendWriteSet(plan_->table_oid_, insert_rid.value());
    // break;
  }
  Value v(INTEGER, count);
  Tuple new_tuple({v}, &plan_->OutputSchema());
  *tuple = new_tuple;

  // added ?????
  // auto transaction_id = exec_ctx_->GetTransaction()->GetTransactionId();
  // std::cout << "insert's transaction_id = " << transaction_id << std::endl;
  // exec_ctx_->GetTransaction()->AppendWriteSet(plan_->table_oid_, final_rid);  // add???
  // std::cout << "AppendWriteSet called for " << plan_->table_oid_ << ", inserted_rid = " << final_rid << std::endl;
  // table_info_->table_->UpdateTupleInPlace(tuplemeta, new_tuple, final_rid, nullptr);
  // exec_ctx_->GetTransactionManager()->UpdateUndoLink(final_rid, std::nullopt, nullptr);
  return true;
}

}  // namespace bustub
Leave a Comment