Skip to content

Commit

Permalink
Cache last DiskArray APPageIdx when appending to the disk array
Browse files Browse the repository at this point in the history
  • Loading branch information
benjaminwinger committed Mar 22, 2024
1 parent 6b4d510 commit e16b1e0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/include/storage/storage_structure/disk_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class BaseDiskArrayInternal {
auto [apPageIdx, isNewlyAdded] =
diskArray.getAPPageIdxAndAddAPToPIPIfNecessaryForWriteTrxNoLock(
&header, apCursor.pageIdx);
diskArray.lastAPPageIdx = apPageIdx;
if (isNewlyAdded || walPageIdxAndFrame.originalPageIdx == common::INVALID_PAGE_IDX) {
getPage(apPageIdx, isNewlyAdded);
}
Expand Down Expand Up @@ -315,6 +316,8 @@ class BaseDiskArrayInternal {
std::vector<PIPWrapper> pips;
PIPUpdates pipUpdates;
std::shared_mutex diskArraySharedMtx;
// For write transactions only
common::page_idx_t lastAPPageIdx;
};

template<typename U>
Expand Down
12 changes: 9 additions & 3 deletions src/storage/storage_structure/disk_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "common/cast.h"
#include "common/constants.h"
#include "common/string_format.h"
#include "common/types/types.h"
#include "common/utils.h"
#include "storage/storage_structure/db_file_utils.h"
#include "storage/storage_utils.h"
Expand Down Expand Up @@ -36,13 +37,15 @@ PIPWrapper::PIPWrapper(FileHandle& fileHandle, page_idx_t pipPageIdx) : pipPageI
BaseDiskArrayInternal::BaseDiskArrayInternal(
FileHandle& fileHandle, page_idx_t headerPageIdx, uint64_t elementSize)
: header{elementSize}, fileHandle{fileHandle}, headerPageIdx{headerPageIdx},
hasTransactionalUpdates{false}, bufferManager{nullptr}, wal{nullptr} {}
hasTransactionalUpdates{false}, bufferManager{nullptr}, wal{nullptr}, lastAPPageIdx{
INVALID_PAGE_IDX} {}

BaseDiskArrayInternal::BaseDiskArrayInternal(FileHandle& fileHandle, DBFileID dbFileID,
page_idx_t headerPageIdx, BufferManager* bufferManager, WAL* wal,
transaction::Transaction* transaction)
: fileHandle{fileHandle}, dbFileID{dbFileID}, headerPageIdx{headerPageIdx},
hasTransactionalUpdates{false}, bufferManager{bufferManager}, wal{wal} {
hasTransactionalUpdates{false}, bufferManager{bufferManager}, wal{wal},
lastAPPageIdx{INVALID_PAGE_IDX} {
auto [fileHandleToPin, pageIdxToPin] = DBFileUtils::getFileHandleAndPhysicalPageIdxToPin(
ku_dynamic_cast<FileHandle&, BMFileHandle&>(fileHandle), headerPageIdx, *wal,
transaction->getType());
Expand Down Expand Up @@ -146,6 +149,7 @@ uint64_t BaseDiskArrayInternal::pushBackNoLock(std::span<uint8_t> val) {
auto apCursor = getAPIdxAndOffsetInAP(elementIdx);
auto [apPageIdx, isNewlyAdded] = getAPPageIdxAndAddAPToPIPIfNecessaryForWriteTrxNoLock(
(DiskArrayHeader*)frame, apCursor.pageIdx);
lastAPPageIdx = apPageIdx;
// Now do the push back.
DBFileUtils::updatePage((BMFileHandle&)(fileHandle), dbFileID, apPageIdx, isNewlyAdded,
*bufferManager, *wal, [&apCursor, &val](uint8_t* frame) -> void {
Expand Down Expand Up @@ -287,7 +291,9 @@ uint64_t BaseDiskArrayInternal::readUInt64HeaderFieldNoLock(
std::pair<page_idx_t, bool>
BaseDiskArrayInternal::getAPPageIdxAndAddAPToPIPIfNecessaryForWriteTrxNoLock(
DiskArrayHeader* updatedDiskArrayHeader, page_idx_t apIdx) {
if (apIdx < updatedDiskArrayHeader->numAPs) {
if (apIdx == updatedDiskArrayHeader->numAPs - 1 && lastAPPageIdx != INVALID_PAGE_IDX) {
return std::make_pair(lastAPPageIdx, false /*not a new page*/);
} else if (apIdx < updatedDiskArrayHeader->numAPs) {
// If the apIdx of the array page is < updatedDiskArrayHeader->numAPs, we do not have to
// add a new array page, so directly return the pageIdx of the apIdx.
return std::make_pair(getAPPageIdxNoLock(apIdx, TransactionType::WRITE),
Expand Down

0 comments on commit e16b1e0

Please sign in to comment.