Skip to content

Commit

Permalink
chore: use loadNextItemPos()
Browse files Browse the repository at this point in the history
  • Loading branch information
areyouok committed Nov 17, 2024
1 parent af95e52 commit 3f3798c
Showing 1 changed file with 11 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.github.dtprj.dongting.fiber.FrameCallResult;
import com.github.dtprj.dongting.log.DtLog;
import com.github.dtprj.dongting.log.DtLogs;
import com.github.dtprj.dongting.raft.RaftException;
import com.github.dtprj.dongting.raft.impl.FileUtil;
import com.github.dtprj.dongting.raft.impl.RaftStatusImpl;
import com.github.dtprj.dongting.raft.impl.RaftUtil;
Expand Down Expand Up @@ -141,25 +142,17 @@ public FrameCallResult execute(Void input) {

private FrameCallResult afterPosLoad(Long pos) {
if (logFiles.startPosOfFile(pos) == pos && index - 1 >= logFiles.getFirstIndex()) {
return Fiber.call(idxFiles.loadLogPos(index - 1), this::afterPosLoad2);
return Fiber.call(loadNextItemPos(index - 1), this::afterPosLoad2);
}
idxFiles.truncateTail(index);
logFiles.truncateTail(index, pos);
return Fiber.frameReturn();
}

private FrameCallResult afterPosLoad2(Long pos) {
return Fiber.call(logFiles.loadHeader(pos), h -> afterLoadHeader(h, pos));
}

private FrameCallResult afterLoadHeader(LogHeader h, long pos) {
if (h.crcMatch()) {
idxFiles.truncateTail(index);
logFiles.truncateTail(index, pos + h.totalLen);
return Fiber.frameReturn();
} else {
throw new ChecksumException("header checksum mismatch: " + pos);
}
idxFiles.truncateTail(index);
logFiles.truncateTail(index, pos);
return Fiber.frameReturn();
}
};

Expand Down Expand Up @@ -223,6 +216,12 @@ private FrameCallResult afterLoadPos(Long pos) {
}

private FrameCallResult afterLoadHeader(LogHeader header, long pos) {
if (!header.crcMatch()) {
throw new ChecksumException("log header crc mismatch: " + pos);
}
if (header.isEndMagic()) {
throw new RaftException("unexpected end magic: " + pos);
}
long nextPos = pos + header.totalLen;
setResult(nextPos);
return Fiber.frameReturn();
Expand Down

0 comments on commit 3f3798c

Please sign in to comment.