/*
* Copyright 2018-2022 Open Networking Foundation and others. All rights reserved.
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
package io.atomix.storage.journal.index;
+import org.eclipse.jdt.annotation.Nullable;
+
/**
- * Journal index.
+ * Index of a particular JournalSegment.
*/
public interface JournalIndex {
+ /**
+ * Adds an entry for the given index at the given position.
+ *
+ * @param index the index for which to add the entry
+ * @param position the position of the given index
+ */
+ void index(long index, int position);
- /**
- * Adds an entry for the given index at the given position.
- *
- * @param index the index for which to add the entry
- * @param position the position of the given index
- */
- void index(long index, int position);
-
- /**
- * Looks up the position of the given index.
- *
- * @param index the index to lookup
- * @return the position of the given index or a lesser index
- */
- Position lookup(long index);
-
- /**
- * Truncates the index to the given index.
- *
- * @param index the index to which to truncate the index
- */
- void truncate(long index);
+ /**
+ * Looks up the position of the given index.
+ *
+ * @param index the index to lookup
+ * @return the position of the given index or a lesser index, or {@code null}
+ */
+ @Nullable Position lookup(long index);
+ /**
+ * Truncates the index to the given index and returns its position, if available.
+ *
+ * @param index the index to which to truncate the index, or {@code null}
+ * @return the position of the given index or a lesser index, or {@code null}
+ */
+ @Nullable Position truncate(long index);
}
*/
package io.atomix.storage.journal.index;
+import java.util.Map.Entry;
+import org.eclipse.jdt.annotation.Nullable;
+
/**
* Journal index position.
*/
public record Position(long index, int position) {
+ public Position(final Entry<Long, Integer> entry) {
+ this(entry.getKey(), entry.getValue());
+ }
+ public static @Nullable Position ofNullable(final Entry<Long, Integer> entry) {
+ return entry == null ? null : new Position(entry);
+ }
}
/*
* Copyright 2018-2022 Open Networking Foundation and others. All rights reserved.
+ * Copyright (c) 2024 PANTHEON.tech, s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
package io.atomix.storage.journal.index;
-import java.util.Map;
import java.util.TreeMap;
/**
- * Sparse index.
+ * A {@link JournalIndex} maintaining target density.
*/
-public class SparseJournalIndex implements JournalIndex {
- private static final int MIN_DENSITY = 1000;
- private final int density;
- private final TreeMap<Long, Integer> positions = new TreeMap<>();
-
- public SparseJournalIndex(double density) {
- this.density = (int) Math.ceil(MIN_DENSITY / (density * MIN_DENSITY));
- }
-
- @Override
- public void index(long index, int position) {
- if (index % density == 0) {
- positions.put(index, position);
+public final class SparseJournalIndex implements JournalIndex {
+ private static final int MIN_DENSITY = 1000;
+
+ private final int density;
+ private final TreeMap<Long, Integer> positions = new TreeMap<>();
+
+ public SparseJournalIndex() {
+ density = MIN_DENSITY;
+ }
+
+ public SparseJournalIndex(final double density) {
+ this.density = (int) Math.ceil(MIN_DENSITY / (density * MIN_DENSITY));
+ }
+
+ @Override
+ public void index(final long index, final int position) {
+ if (index % density == 0) {
+ positions.put(index, position);
+ }
+ }
+
+ @Override
+ public Position lookup(final long index) {
+ return Position.ofNullable(positions.floorEntry(index));
+ }
+
+ @Override
+ public Position truncate(final long index) {
+ positions.tailMap(index, false).clear();
+ return Position.ofNullable(positions.lastEntry());
}
- }
-
- @Override
- public Position lookup(long index) {
- Map.Entry<Long, Integer> entry = positions.floorEntry(index);
- return entry != null ? new Position(entry.getKey(), entry.getValue()) : null;
- }
-
- @Override
- public void truncate(long index) {
- positions.tailMap(index, false).clear();
- }
}