Skip to content

Commit

Permalink
Add lsn parameter in constructor of log records
Browse files Browse the repository at this point in the history
  • Loading branch information
hnjylwb committed Mar 11, 2024
1 parent f1ccf2f commit 8f10042
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/bin/huadb-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void parse_log(const fs::path &path) {
std::exit(1);
}
while (lsn < next_lsn) {
auto log = huadb::LogRecord::DeserializeFrom(buffer + lsn);
auto log = huadb::LogRecord::DeserializeFrom(lsn, buffer + lsn);
std::cout << log->ToString() << std::endl;
lsn += log->GetSize();
}
Expand Down
52 changes: 23 additions & 29 deletions src/log/log_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ lsn_t LogManager::AppendInsertLog(xid_t xid, oid_t oid, pageid_t page_id, slotid
if (att_.find(xid) == att_.end()) {
throw DbException(std::to_string(xid) + " does not exist in att (in AppendInsertLog)");
}
auto log = std::make_shared<InsertLog>(xid, att_[xid], oid, page_id, slot_id, offset, size, new_record);
lsn_t lsn = next_lsn_;
next_lsn_ += log->GetSize();
auto log = std::make_shared<InsertLog>(NULL_LSN, xid, att_[xid], oid, page_id, slot_id, offset, size, new_record);
lsn_t lsn = next_lsn_.fetch_add(log->GetSize(), std::memory_order_relaxed);
log->SetLSN(lsn);
att_[xid] = lsn;
log_buffer_.push_back(std::move(log));
Expand All @@ -45,9 +44,8 @@ lsn_t LogManager::AppendDeleteLog(xid_t xid, oid_t oid, pageid_t page_id, slotid
if (att_.find(xid) == att_.end()) {
throw DbException(std::to_string(xid) + " does not exist in att (in AppendDeleteLog)");
}
auto log = std::make_shared<DeleteLog>(xid, att_[xid], oid, page_id, slot_id);
lsn_t lsn = next_lsn_;
next_lsn_ += log->GetSize();
auto log = std::make_shared<DeleteLog>(NULL_LSN, xid, att_[xid], oid, page_id, slot_id);
lsn_t lsn = next_lsn_.fetch_add(log->GetSize(), std::memory_order_relaxed);
log->SetLSN(lsn);
att_[xid] = lsn;
log_buffer_.push_back(std::move(log));
Expand All @@ -61,15 +59,16 @@ lsn_t LogManager::AppendNewPageLog(xid_t xid, oid_t oid, pageid_t prev_page_id,
if (xid != DDL_XID && att_.find(xid) == att_.end()) {
throw DbException(std::to_string(xid) + " does not exist in att (in AppendNewPageLog)");
}
std::shared_ptr<NewPageLog> log;
xid_t log_xid;
if (xid == DDL_XID) {
log = std::make_shared<NewPageLog>(xid, NULL_LSN, oid, prev_page_id, page_id);
log_xid = NULL_XID;
} else {
log = std::make_shared<NewPageLog>(xid, att_[xid], oid, prev_page_id, page_id);
log_xid = att_[xid];
}
lsn_t lsn = next_lsn_;
next_lsn_ += log->GetSize();
auto log = std::make_shared<NewPageLog>(NULL_LSN, xid, log_xid, oid, prev_page_id, page_id);
lsn_t lsn = next_lsn_.fetch_add(log->GetSize(), std::memory_order_relaxed);
log->SetLSN(lsn);

if (xid != DDL_XID) {
att_[xid] = lsn;
}
Expand All @@ -87,11 +86,10 @@ lsn_t LogManager::AppendBeginLog(xid_t xid) {
if (att_.find(xid) != att_.end()) {
throw DbException(std::to_string(xid) + " already exists in att");
}
auto log = std::make_shared<BeginLog>(xid, NULL_LSN);
lsn_t lsn = next_lsn_;
next_lsn_ += log->GetSize();
att_[xid] = lsn;
auto log = std::make_shared<BeginLog>(NULL_LSN, xid, NULL_LSN);
lsn_t lsn = next_lsn_.fetch_add(log->GetSize(), std::memory_order_relaxed);
log->SetLSN(lsn);
att_[xid] = lsn;
log_buffer_.push_back(std::move(log));
return lsn;
}
Expand All @@ -100,9 +98,8 @@ lsn_t LogManager::AppendCommitLog(xid_t xid) {
if (att_.find(xid) == att_.end()) {
throw DbException(std::to_string(xid) + " does not exist in att (in AppendCommitLog)");
}
auto log = std::make_shared<CommitLog>(xid, att_[xid]);
lsn_t lsn = next_lsn_;
next_lsn_ += log->GetSize();
auto log = std::make_shared<CommitLog>(NULL_LSN, xid, att_[xid]);
lsn_t lsn = next_lsn_.fetch_add(log->GetSize(), std::memory_order_relaxed);
log->SetLSN(lsn);
log_buffer_.push_back(std::move(log));
Flush(lsn);
Expand All @@ -114,9 +111,8 @@ lsn_t LogManager::AppendRollbackLog(xid_t xid) {
if (att_.find(xid) == att_.end()) {
throw DbException(std::to_string(xid) + " does not exist in att (in AppendRollbackLog)");
}
auto log = std::make_shared<RollbackLog>(xid, att_[xid]);
lsn_t lsn = next_lsn_;
next_lsn_ += log->GetSize();
auto log = std::make_shared<RollbackLog>(NULL_LSN, xid, att_[xid]);
lsn_t lsn = next_lsn_.fetch_add(log->GetSize(), std::memory_order_relaxed);
log->SetLSN(lsn);
log_buffer_.push_back(std::move(log));
Flush(lsn);
Expand All @@ -125,15 +121,13 @@ lsn_t LogManager::AppendRollbackLog(xid_t xid) {
}

lsn_t LogManager::Checkpoint(bool async) {
auto log = std::make_shared<BeginCheckpointLog>(NULL_XID, NULL_LSN);
lsn_t begin_lsn = next_lsn_;
next_lsn_ += log->GetSize();
log->SetLSN(begin_lsn);
log_buffer_.push_back(std::move(log));
auto begin_checkpoint_log = std::make_shared<BeginCheckpointLog>(NULL_LSN, NULL_XID, NULL_LSN);
lsn_t begin_lsn = next_lsn_.fetch_add(begin_checkpoint_log->GetSize(), std::memory_order_relaxed);
begin_checkpoint_log->SetLSN(begin_lsn);
log_buffer_.push_back(std::move(begin_checkpoint_log));

auto end_checkpoint_log = std::make_shared<EndCheckpointLog>(NULL_XID, NULL_LSN, att_, dpt_);
lsn_t end_lsn = next_lsn_;
next_lsn_ += end_checkpoint_log->GetSize();
auto end_checkpoint_log = std::make_shared<EndCheckpointLog>(NULL_LSN, NULL_XID, NULL_LSN, att_, dpt_);
lsn_t end_lsn = next_lsn_.fetch_add(end_checkpoint_log->GetSize(), std::memory_order_relaxed);
end_checkpoint_log->SetLSN(end_lsn);
log_buffer_.push_back(std::move(end_checkpoint_log));
Flush(end_lsn);
Expand Down
22 changes: 12 additions & 10 deletions src/log/log_record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace huadb {

LogRecord::LogRecord(LogType type, xid_t xid, lsn_t prev_lsn) : type_(type), xid_(xid), prev_lsn_(prev_lsn) {
LogRecord::LogRecord(LogType type, lsn_t lsn, xid_t xid, lsn_t prev_lsn)
: type_(type), lsn_(lsn), xid_(xid), prev_lsn_(prev_lsn) {
// LSN 为日志记录在日志文件中的位置,无需占用空间
size_ = sizeof(type_) + sizeof(xid_) + sizeof(prev_lsn_);
}

Expand All @@ -19,26 +21,26 @@ size_t LogRecord::SerializeTo(char *data) const {
return offset;
}

std::shared_ptr<LogRecord> LogRecord::DeserializeFrom(const char *data) {
std::shared_ptr<LogRecord> LogRecord::DeserializeFrom(lsn_t lsn, const char *data) {
LogType type;
memcpy(&type, data, sizeof(type));
switch (type) {
case LogType::INSERT:
return InsertLog::DeserializeFrom(data + sizeof(type));
return InsertLog::DeserializeFrom(lsn, data + sizeof(type));
case LogType::DELETE:
return DeleteLog::DeserializeFrom(data + sizeof(type));
return DeleteLog::DeserializeFrom(lsn, data + sizeof(type));
case LogType::NEW_PAGE:
return NewPageLog::DeserializeFrom(data + sizeof(type));
return NewPageLog::DeserializeFrom(lsn, data + sizeof(type));
case LogType::BEGIN:
return BeginLog::DeserializeFrom(data + sizeof(type));
return BeginLog::DeserializeFrom(lsn, data + sizeof(type));
case LogType::COMMIT:
return CommitLog::DeserializeFrom(data + sizeof(type));
return CommitLog::DeserializeFrom(lsn, data + sizeof(type));
case LogType::ROLLBACK:
return RollbackLog::DeserializeFrom(data + sizeof(type));
return RollbackLog::DeserializeFrom(lsn, data + sizeof(type));
case LogType::BEGIN_CHECKPOINT:
return BeginCheckpointLog::DeserializeFrom(data + sizeof(type));
return BeginCheckpointLog::DeserializeFrom(lsn, data + sizeof(type));
case LogType::END_CHECKPOINT:
return EndCheckpointLog::DeserializeFrom(data + sizeof(type));
return EndCheckpointLog::DeserializeFrom(lsn, data + sizeof(type));
default:
throw DbException("Unknown log type in DeserializeFrom");
}
Expand Down
4 changes: 2 additions & 2 deletions src/log/log_record.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ enum class LogType : enum_t {

class LogRecord {
public:
LogRecord(LogType type, xid_t xid, lsn_t prev_lsn);
LogRecord(LogType type, lsn_t lsn, xid_t xid, lsn_t prev_lsn);

// 序列化和反序列化
virtual size_t SerializeTo(char *data) const;
static std::shared_ptr<LogRecord> DeserializeFrom(const char *data);
static std::shared_ptr<LogRecord> DeserializeFrom(lsn_t lsn, const char *data);

// 撤销和重做
virtual void Undo(BufferPool &buffer_pool, Catalog &catalog, LogManager &log_manager, lsn_t lsn, lsn_t undo_next_lsn);
Expand Down
8 changes: 4 additions & 4 deletions src/log/log_records/begin_checkpoint_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@

namespace huadb {

BeginCheckpointLog::BeginCheckpointLog(xid_t xid, lsn_t prev_lsn)
: LogRecord(LogType::BEGIN_CHECKPOINT, xid, prev_lsn) {}
BeginCheckpointLog::BeginCheckpointLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn)
: LogRecord(LogType::BEGIN_CHECKPOINT, lsn, xid, prev_lsn) {}

size_t BeginCheckpointLog::SerializeTo(char *data) const {
size_t offset = LogRecord::SerializeTo(data);
assert(offset == size_);
return offset;
}

std::shared_ptr<BeginCheckpointLog> BeginCheckpointLog::DeserializeFrom(const char *data) {
std::shared_ptr<BeginCheckpointLog> BeginCheckpointLog::DeserializeFrom(lsn_t lsn, const char *data) {
xid_t xid;
lsn_t prev_lsn;
size_t offset = 0;
memcpy(&xid, data + offset, sizeof(xid));
offset += sizeof(xid);
memcpy(&prev_lsn, data + offset, sizeof(prev_lsn));
offset += sizeof(prev_lsn);
return std::make_shared<BeginCheckpointLog>(xid, prev_lsn);
return std::make_shared<BeginCheckpointLog>(lsn, xid, prev_lsn);
}

std::string BeginCheckpointLog::ToString() const {
Expand Down
4 changes: 2 additions & 2 deletions src/log/log_records/begin_checkpoint_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace huadb {

class BeginCheckpointLog : public LogRecord {
public:
BeginCheckpointLog(xid_t xid, lsn_t prev_lsn);
BeginCheckpointLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn);

size_t SerializeTo(char *data) const override;
static std::shared_ptr<BeginCheckpointLog> DeserializeFrom(const char *data);
static std::shared_ptr<BeginCheckpointLog> DeserializeFrom(lsn_t lsn, const char *data);

std::string ToString() const override;
};
Expand Down
6 changes: 3 additions & 3 deletions src/log/log_records/begin_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace huadb {

BeginLog::BeginLog(xid_t xid, lsn_t prev_lsn) : LogRecord(LogType::BEGIN, xid, prev_lsn) {}
BeginLog::BeginLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn) : LogRecord(LogType::BEGIN, lsn, xid, prev_lsn) {}

size_t BeginLog::SerializeTo(char *data) const {
size_t offset = LogRecord::SerializeTo(data);
assert(offset == size_);
return offset;
}

std::shared_ptr<BeginLog> BeginLog::DeserializeFrom(const char *data) {
std::shared_ptr<BeginLog> BeginLog::DeserializeFrom(lsn_t lsn, const char *data) {
xid_t xid;
lsn_t prev_lsn;
size_t offset = 0;
memcpy(&xid, data + offset, sizeof(xid));
offset += sizeof(xid);
memcpy(&prev_lsn, data + offset, sizeof(prev_lsn));
offset += sizeof(prev_lsn);
return std::make_shared<BeginLog>(xid, prev_lsn);
return std::make_shared<BeginLog>(lsn, xid, prev_lsn);
}

std::string BeginLog::ToString() const { return fmt::format("BeginLog\t\t[{}]", LogRecord::ToString()); }
Expand Down
4 changes: 2 additions & 2 deletions src/log/log_records/begin_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace huadb {

class BeginLog : public LogRecord {
public:
BeginLog(xid_t xid, lsn_t prev_lsn);
BeginLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn);

size_t SerializeTo(char *data) const override;
static std::shared_ptr<BeginLog> DeserializeFrom(const char *data);
static std::shared_ptr<BeginLog> DeserializeFrom(lsn_t lsn, const char *data);

std::string ToString() const override;
};
Expand Down
6 changes: 3 additions & 3 deletions src/log/log_records/commit_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

namespace huadb {

CommitLog::CommitLog(xid_t xid, lsn_t prev_lsn) : LogRecord(LogType::COMMIT, xid, prev_lsn) {}
CommitLog::CommitLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn) : LogRecord(LogType::COMMIT, lsn, xid, prev_lsn) {}

size_t CommitLog::SerializeTo(char *data) const {
size_t offset = LogRecord::SerializeTo(data);
assert(offset == size_);
return offset;
}

std::shared_ptr<CommitLog> CommitLog::DeserializeFrom(const char *data) {
std::shared_ptr<CommitLog> CommitLog::DeserializeFrom(lsn_t lsn, const char *data) {
xid_t xid;
lsn_t prev_lsn;
size_t offset = 0;
memcpy(&xid, data + offset, sizeof(xid));
offset += sizeof(xid);
memcpy(&prev_lsn, data + offset, sizeof(prev_lsn));
offset += sizeof(prev_lsn);
return std::make_shared<CommitLog>(xid, prev_lsn);
return std::make_shared<CommitLog>(lsn, xid, prev_lsn);
}

std::string CommitLog::ToString() const { return fmt::format("CommitLog\t\t[{}]", LogRecord::ToString()); }
Expand Down
4 changes: 2 additions & 2 deletions src/log/log_records/commit_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace huadb {

class CommitLog : public LogRecord {
public:
CommitLog(xid_t xid, lsn_t prev_lsn);
CommitLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn);

size_t SerializeTo(char *data) const override;
static std::shared_ptr<CommitLog> DeserializeFrom(const char *data);
static std::shared_ptr<CommitLog> DeserializeFrom(lsn_t lsn, const char *data);

std::string ToString() const override;
};
Expand Down
8 changes: 4 additions & 4 deletions src/log/log_records/delete_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace huadb {

DeleteLog::DeleteLog(xid_t xid, lsn_t prev_lsn, oid_t oid, pageid_t page_id, slotid_t slot_id)
: LogRecord(LogType::DELETE, xid, prev_lsn), oid_(oid), page_id_(page_id), slot_id_(slot_id) {
DeleteLog::DeleteLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn, oid_t oid, pageid_t page_id, slotid_t slot_id)
: LogRecord(LogType::DELETE, lsn, xid, prev_lsn), oid_(oid), page_id_(page_id), slot_id_(slot_id) {
size_ += sizeof(oid_) + sizeof(page_id_) + sizeof(slot_id_);
}

Expand All @@ -19,7 +19,7 @@ size_t DeleteLog::SerializeTo(char *data) const {
return offset;
}

std::shared_ptr<DeleteLog> DeleteLog::DeserializeFrom(const char *data) {
std::shared_ptr<DeleteLog> DeleteLog::DeserializeFrom(lsn_t lsn, const char *data) {
xid_t xid;
lsn_t prev_lsn;
oid_t oid;
Expand All @@ -36,7 +36,7 @@ std::shared_ptr<DeleteLog> DeleteLog::DeserializeFrom(const char *data) {
offset += sizeof(page_id);
memcpy(&slot_id, data + offset, sizeof(slot_id));
offset += sizeof(slot_id);
return std::make_shared<DeleteLog>(xid, prev_lsn, oid, page_id, slot_id);
return std::make_shared<DeleteLog>(lsn, xid, prev_lsn, oid, page_id, slot_id);
}

void DeleteLog::Undo(BufferPool &buffer_pool, Catalog &catalog, LogManager &log_manager, lsn_t lsn,
Expand Down
4 changes: 2 additions & 2 deletions src/log/log_records/delete_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace huadb {

class DeleteLog : public LogRecord {
public:
DeleteLog(xid_t xid, lsn_t prev_lsn, oid_t oid, pageid_t page_id, slotid_t slot_id);
DeleteLog(lsn_t lsn, xid_t xid, lsn_t prev_lsn, oid_t oid, pageid_t page_id, slotid_t slot_id);

size_t SerializeTo(char *data) const override;
static std::shared_ptr<DeleteLog> DeserializeFrom(const char *data);
static std::shared_ptr<DeleteLog> DeserializeFrom(lsn_t lsn, const char *data);

void Undo(BufferPool &buffer_pool, Catalog &catalog, LogManager &log_manager, lsn_t lsn,
lsn_t undo_next_lsn) override;
Expand Down
Loading

0 comments on commit 8f10042

Please sign in to comment.