/*
 * 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.storage.pagememory.mv.gc;

import java.util.concurrent.atomic.AtomicLong;
import org.apache.ignite.internal.hlc.HybridTimestamp;
import org.apache.ignite.internal.lang.IgniteInternalCheckedException;
import org.apache.ignite.internal.pagememory.PageMemory;
import org.apache.ignite.internal.pagememory.reuse.ReuseList;
import org.apache.ignite.internal.pagememory.tree.BplusTree;
import org.apache.ignite.internal.pagememory.tree.io.BplusIo;
import org.apache.ignite.internal.storage.RowId;
import org.apache.ignite.internal.storage.StorageException;
import org.apache.ignite.internal.storage.pagememory.mv.gc.io.GcInnerIo;
import org.apache.ignite.internal.storage.pagememory.mv.gc.io.GcIo;
import org.apache.ignite.internal.storage.pagememory.mv.gc.io.GcLeafIo;
import org.apache.ignite.internal.storage.pagememory.mv.gc.io.GcMetaIo;
import org.jetbrains.annotations.Nullable;

/**
 * {@link BplusTree} implementation for garbage collection of obsolete row versions in version chains.
 */
public class GcQueue extends BplusTree<GcRowVersion, GcRowVersion> {
    /**
     * Constructor.
     *
     * @param grpId Group ID.
     * @param grpName Group name.
     * @param partId Partition id.
     * @param pageMem Page memory.
     * @param globalRmvId Global remove ID, for a tree that was created for the first time it can be {@code 0}, for restored ones it
     *      must be greater than or equal to the previous value.
     * @param metaPageId Meta page ID.
     * @param reuseList Reuse list, {@code null} if absent.
     * @param initNew {@code True} if need to create and fill in special pages for working with a tree (for example, when creating it
     *      for the first time), {@code false} if not necessary (for example, when restoring a tree).
     * @throws IgniteInternalCheckedException If failed.
     */
    public GcQueue(
            int grpId,
            String grpName,
            int partId,
            PageMemory pageMem,
            AtomicLong globalRmvId,
            long metaPageId,
            @Nullable ReuseList reuseList,
            boolean initNew
    ) throws IgniteInternalCheckedException {
        super(
                "GarbageCollectionTree",
                grpId,
                grpName,
                partId,
                pageMem,
                globalRmvId,
                metaPageId,
                reuseList
        );

        setIos(GcInnerIo.VERSIONS, GcLeafIo.VERSIONS, GcMetaIo.VERSIONS);

        initTree(initNew);
    }

    @Override
    protected int compare(BplusIo<GcRowVersion> io, long pageAddr, int idx, GcRowVersion row) {
        GcIo gcIo = (GcIo) io;

        return gcIo.compare(pageAddr, idx, row);
    }

    @Override
    public GcRowVersion getRow(BplusIo<GcRowVersion> io, long pageAddr, int idx, Object x) {
        GcIo gcIo = (GcIo) io;

        return gcIo.getRow(pageAddr, idx, partId);
    }

    /**
     * Adds a row version to the garbage collection queue.
     */
    public void add(RowId rowId, HybridTimestamp timestamp, long link) {
        try {
            put(new GcRowVersion(rowId, timestamp, link));
        } catch (IgniteInternalCheckedException e) {
            throw new StorageException(
                    "Error occurred while adding row version to the garbage collection queue: [rowId={}, timestamp={}, {}]",
                    e,
                    rowId, timestamp
            );
        }
    }

    /**
     * Removes a row version from the garbage collection queue.
     *
     * @return {@code true} if the row version was removed from the garbage collection queue.
     */
    public boolean remove(RowId rowId, HybridTimestamp timestamp, long link) {
        try {
            return removex(new GcRowVersion(rowId, timestamp, link));
        } catch (IgniteInternalCheckedException e) {
            throw new StorageException(
                    "Error occurred while deleting row version form the garbage collection queue: [rowId={}, timestamp={}, {}]",
                    e,
                    rowId, timestamp
            );
        }
    }

    /**
     * Returns the first element from the garbage collection queue, {@code null} if the queue is empty.
     */
    public @Nullable GcRowVersion getFirst() {
        try {
            return findFirst();
        } catch (IgniteInternalCheckedException e) {
            throw new StorageException("Error occurred while getting the first element from the garbage collection queue", e);
        }
    }
}
