2 * Copyright (c) 2024 PANTHEON.tech, s.r.o. and others. All rights reserved.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package io.atomix.storage.journal;
18 import java.nio.ByteBuffer;
19 import java.util.zip.CRC32;
20 import org.eclipse.jdt.annotation.NonNull;
23 * An {@link Indexed} entry read from {@link JournalSegment}.
25 * @param checksum The {@link CRC32} checksum of data
26 * @param bytes Entry bytes
28 record SegmentEntry(int checksum, @NonNull ByteBuffer bytes) {
30 * The size of the header, comprising of:
32 * <li>32-bit signed entry length</li>
33 * <li>32-bit unsigned CRC32 checksum</li>
36 static final int HEADER_BYTES = Integer.BYTES + Integer.BYTES;
39 if (bytes.remaining() < 1) {
40 throw new IllegalArgumentException("Invalid entry bytes " + bytes);
45 * Compute the {@link CRC32} checksum of a buffer. Note that the buffer will be consumed during this process.
47 * @param bytes buffer to checksum
48 * @return the checksum
50 static int computeChecksum(final ByteBuffer bytes) {
51 final var crc32 = new CRC32();
53 return (int) crc32.getValue();