sashapolo commented on code in PR #7198: URL: https://github.com/apache/ignite-3/pull/7198#discussion_r2607125995
########## modules/raft/src/main/java/org/apache/ignite/internal/raft/storage/segstore/SegmentPayloadParser.java: ########## @@ -0,0 +1,160 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.raft.storage.segstore; + +import static org.apache.ignite.internal.raft.storage.segstore.SegmentFileManager.FORMAT_VERSION; +import static org.apache.ignite.internal.raft.storage.segstore.SegmentFileManager.MAGIC_NUMBER; +import static org.apache.ignite.internal.raft.storage.segstore.SegmentFileManager.SWITCH_SEGMENT_RECORD; +import static org.apache.ignite.internal.raft.storage.segstore.SegmentPayload.CRC_SIZE_BYTES; +import static org.apache.ignite.internal.raft.storage.segstore.SegmentPayload.RESET_RECORD_MARKER; +import static org.apache.ignite.internal.raft.storage.segstore.SegmentPayload.TRUNCATE_PREFIX_RECORD_MARKER; +import static org.apache.ignite.internal.raft.storage.segstore.SegmentPayload.TRUNCATE_SUFFIX_RECORD_MARKER; + +import java.nio.ByteBuffer; +import java.nio.file.Path; +import java.util.Arrays; +import org.apache.ignite.internal.raft.util.VarlenEncoder; +import org.apache.ignite.internal.util.FastCrc; + +class SegmentPayloadParser { + private final int stripes; + + SegmentPayloadParser(int stripes) { + this.stripes = stripes; + } + + WriteModeIndexMemTable recoverMemtable(SegmentFile segmentFile, Path segmentFilePath, boolean validateCrc) { + ByteBuffer buffer = segmentFile.buffer(); + + validateSegmentFileHeader(buffer, segmentFilePath); + + var memtable = new IndexMemTable(stripes); + + while (!endOfSegmentReached(buffer)) { + int segmentFilePayloadOffset = buffer.position(); + + long groupId = buffer.getLong(); + + int payloadLength = buffer.getInt(); + + int crcPosition; + + if (payloadLength == TRUNCATE_SUFFIX_RECORD_MARKER) { + long lastLogIndexKept = buffer.getLong(); + + crcPosition = buffer.position(); + + buffer.position(segmentFilePayloadOffset); + + // CRC violation signals the end of meaningful data in the segment file. + if (validateCrc && !isCrcValid(buffer, crcPosition)) { + break; + } + + memtable.truncateSuffix(groupId, lastLogIndexKept); + } else if (payloadLength == TRUNCATE_PREFIX_RECORD_MARKER) { + long firstLogIndexKept = buffer.getLong(); + + crcPosition = buffer.position(); + + buffer.position(segmentFilePayloadOffset); + + // CRC violation signals the end of meaningful data in the segment file. + if (validateCrc && !isCrcValid(buffer, crcPosition)) { + break; + } + + memtable.truncatePrefix(groupId, firstLogIndexKept); + } else if (payloadLength == RESET_RECORD_MARKER) { Review Comment: Note to reviewer: all code in this class has been moved from `SegmentFileManager`, only this clause was added -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
