latch_memo.cpp 4.0 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Wangyunlai on 2023/03/08.
//

#include "storage/trx/latch_memo.h"
#include "storage/buffer/frame.h"
#include "storage/default/disk_buffer_pool.h"
#include "common/lang/mutex.h"

LatchMemoItem::LatchMemoItem(LatchMemoType type, Frame *frame)
{
  this->type = type;
  this->frame = frame;
}

LatchMemoItem::LatchMemoItem(LatchMemoType type, common::SharedMutex *lock)
{
  this->type = type;
  this->lock = lock;
}

////////////////////////////////////////////////////////////////////////////////

LatchMemo::LatchMemo(DiskBufferPool *buffer_pool) : buffer_pool_(buffer_pool)
{}

LatchMemo::~LatchMemo()
{
  this->release();
}

RC LatchMemo::get_page(PageNum page_num, Frame *&frame)
{
  frame = nullptr;

  RC rc = buffer_pool_->get_this_page(page_num, &frame);
  if (rc != RC::SUCCESS) {
    return rc;
  }

  items_.emplace_back(LatchMemoType::PIN, frame);
  return RC::SUCCESS;
}

RC LatchMemo::allocate_page(Frame *&frame)
{
  frame = nullptr;
  
  RC rc = buffer_pool_->allocate_page(&frame);
  if (rc == RC::SUCCESS) {
    items_.emplace_back(LatchMemoType::PIN, frame);
    ASSERT(frame->pin_count() == 1, "allocate a new frame. frame=%s", to_string(*frame).c_str());
  }
  
  return rc;
}

void LatchMemo::dispose_page(PageNum page_num)
{
  disposed_pages_.emplace_back(page_num);
}

void LatchMemo::latch(Frame *frame, LatchMemoType type)
{
  switch (type) {
    case LatchMemoType::EXCLUSIVE: {
      frame->write_latch();
    } break;
    case LatchMemoType::SHARED: {
      frame->read_latch();
    } break;
    default: {
      ASSERT(false, "invalid latch type: %d", static_cast<int>(type));
    }
  }

  items_.emplace_back(type, frame);
}

void LatchMemo::xlatch(Frame *frame)
{
  this->latch(frame, LatchMemoType::EXCLUSIVE);
}

void LatchMemo::slatch(Frame *frame)
{
  this->latch(frame, LatchMemoType::SHARED);
}

bool LatchMemo::try_slatch(Frame *frame)
{
  bool ret = frame->try_read_latch();
  if (ret) {
    items_.emplace_back(LatchMemoType::SHARED, frame);
  }
  return ret;
}

void LatchMemo::xlatch(common::SharedMutex *lock)
{
  lock->lock();
  items_.emplace_back(LatchMemoType::EXCLUSIVE, lock);
  LOG_DEBUG("lock root success");
}

void LatchMemo::slatch(common::SharedMutex *lock)
{
  lock->lock_shared();
  items_.emplace_back(LatchMemoType::SHARED, lock);
}

void LatchMemo::release_item(LatchMemoItem &item)
{
  switch (item.type) {
    case LatchMemoType::EXCLUSIVE: {
      if (item.frame != nullptr) {
        item.frame->write_unlatch();
      } else {
        LOG_DEBUG("release root lock");
        item.lock->unlock();
      }
    } break;
    case LatchMemoType::SHARED: {
      if (item.frame != nullptr) {
        item.frame->read_unlatch();
      } else {
        item.lock->unlock_shared();
      }
    } break;
    case LatchMemoType::PIN: {
      buffer_pool_->unpin_page(item.frame);
    } break;

    default: {
      ASSERT(false, "invalid latch type: %d", static_cast<int>(item.type));
    }
  }
}

void LatchMemo::release()
{
  int point = static_cast<int>(items_.size());
  release_to(point);

  for (PageNum page_num : disposed_pages_) {
    buffer_pool_->dispose_page(page_num);
  }
  disposed_pages_.clear();
}

void LatchMemo::release_to(int point)
{
  ASSERT(point >= 0 && point <= static_cast<int>(items_.size()), 
         "invalid memo point. point=%d, items size=%d",
         point, static_cast<int>(items_.size()));

  auto iter = items_.begin();
  for (int i = point - 1; i >= 0; i--, ++iter) {
    LatchMemoItem &item = items_[i];
    release_item(item);
  }
  items_.erase(items_.begin(), iter);
}