From e32ad7f2f382df32f256ae1963e5542320dbb0d2 Mon Sep 17 00:00:00 2001 From: elviske Date: Mon, 5 Jul 2021 18:31:00 +0800 Subject: [PATCH] refine logEntries implement --- src/kvstore/raftex/RaftPart.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/kvstore/raftex/RaftPart.cpp b/src/kvstore/raftex/RaftPart.cpp index df56a43c0c9..8944f363196 100644 --- a/src/kvstore/raftex/RaftPart.cpp +++ b/src/kvstore/raftex/RaftPart.cpp @@ -1580,13 +1580,11 @@ void RaftPart::processAppendLogRequest( size_t numLogs = req.get_log_str_list().size(); LogID firstId = req.get_last_log_id_sent() + 1; - std::vector logEntries (req.get_log_str_list().begin(), - req.get_log_str_list().end()); - + size_t diffIndex = 0; // may be need to rollback wal_ if (!(req.get_last_log_id_sent() == lastLogId_ && req.get_last_log_term_sent() == lastLogTerm_)) { // check the diff index in log - size_t diffIndex = 0; + { std::unique_ptr it = wal_->iterator(firstId,firstId + numLogs); for (size_t i = 0; i < numLogs && it->valid(); i++, ++(*it), diffIndex ++ ) { @@ -1625,13 +1623,15 @@ void RaftPart::processAppendLogRequest( } // update msg - logEntries = std::vector (req.get_log_str_list().begin() + diffIndex, - req.get_log_str_list().end()); firstId = firstId + diffIndex; numLogs = numLogs - diffIndex; } // Append new logs + std::vector logEntries = std::vector ( + std::make_move_iterator(req.get_log_str_list().begin() + diffIndex), + std::make_move_iterator(req.get_log_str_list().end())); + LogStrListIterator iter(firstId, req.get_log_term(), logEntries); if (wal_->appendLogs(iter)) {