lhotari commented on code in PR #26127: URL: https://github.com/apache/pulsar/pull/26127#discussion_r3519484157
########## managed-ledger/src/main/java/org/apache/bookkeeper/mledger/impl/PositionRangeSet.java: ########## @@ -0,0 +1,491 @@ +/* + * 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.bookkeeper.mledger.impl; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.collect.BoundType; +import com.google.common.collect.Range; +import io.github.merlimat.slog.Logger; +import it.unimi.dsi.fastutil.longs.Long2ObjectRBTreeMap; +import it.unimi.dsi.fastutil.longs.Long2ObjectSortedMap; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; +import org.apache.bookkeeper.mledger.Position; +import org.apache.bookkeeper.mledger.PositionFactory; +import org.apache.commons.lang3.mutable.MutableInt; +import org.apache.pulsar.common.util.collections.LongBitmap; +import org.apache.pulsar.common.util.collections.LongBitmaps; +import org.apache.pulsar.common.util.collections.LongPairRangeSet; + +/** + * Tracks deleted-message positions as ranges of {@link Position}s. + * + * <p>The implementation stores positions in a two-level structure: + * the ledger id is used as the map key, and the corresponding entry ids are stored in a + * {@link LongBitmap}. Bit {@code n} in the bitmap of ledger {@code L} represents the position + * {@code (L, n)}. + * + * <h2>Thread Safety</h2> + * + * <p>This class is not thread-safe. All methods require the caller to provide external + * synchronization. In normal usage, callers must hold the owning {@link ManagedCursorImpl}'s + * cursor lock before accessing this class. + * + * <p>The class intentionally does not maintain internal locking. The cursor lock is the single + * synchronization boundary for both this structure and related cursor state. + * + * <h2>Persistence Compatibility</h2> + * + * <p>The persisted representation remains compatible with the existing format using + * {@link LongBitmap#serializeToLongArray()} and {@link LongBitmap#deserializeFromLongArray(long[])}, + * which are compatible with the BitSet long[] format. + * + * <p>Entry ids are stored as bitmap indexes and therefore use {@code int} values. This is safe + * because {@code managedLedgerMaxEntriesPerLedger} is an {@code int}, so valid entry ids are within + * {@code [0, Integer.MAX_VALUE]}. + */ +class PositionRangeSet implements LongPairRangeSet<Position> { + + private static final Logger log = Logger.get(PositionRangeSet.class); + + private static final long EARLIEST_KEY = -1L; + private static final long EARLIEST_VALUE = -1L; + private static final long LATEST_KEY = Long.MAX_VALUE; + private static final long LATEST_VALUE = Long.MAX_VALUE; + + /** + * Maps ledger ID to a bitmap of deleted entry IDs within that ledger. + * Bit {@code n} in the bitmap represents entry {@code n} in the ledger. + */ + private final Long2ObjectSortedMap<LongBitmap> rangeBitmapMap = new Long2ObjectRBTreeMap<>(); + private final LongPairConsumer<Position> consumer; + private final boolean enableMultiEntry; + + private final LongBitmap dirtyLedgers = LongBitmaps.create(); + + private int cachedSize = 0; + private String cachedToString = "[]"; + private boolean updatedAfterCachedForSize = true; + private boolean updatedAfterCachedForToString = true; + + PositionRangeSet(LongPairConsumer<Position> consumer, boolean enableMultiEntry) { + this.consumer = consumer; + this.enableMultiEntry = enableMultiEntry; + } + + private static long lastPresentValue(LongBitmap bitmap) { + return bitmap.lastPresentValue(); + } + + @Override + public void addOpenClosed(long lowerKey, long lowerValueOpen, long upperKey, long upperValue) { Review Comment: I don't see this change in this PR yet. -- 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]
