/*
 * 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.jackrabbit.oak.segment;

import org.apache.jackrabbit.oak.segment.file.CompactedNodeState;
import org.apache.jackrabbit.oak.segment.file.cancel.Canceller;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.IOException;

import static org.apache.jackrabbit.oak.plugins.memory.EmptyNodeState.EMPTY_NODE;

/**
 * A compactor compacts the differences between two {@link NodeState}s
 * ({@code before} and {@code after}) on top of a third {@link NodeState}
 * ({@code onto}).
 * <p>
 * The compaction can be done in two ways:
 * <ul>
 *     <li>Compacting down, where the differences between {@code before} and
 *     {@code after} are compacted on top of the {@code after} state. This is
 *     useful, because it allows for partial compactions. I.e. compactions that
 *     are "soft-cancelled" before the full compaction process completes.
 *     <p>
 *     During "down compaction", parts of the tree are compacted and the respective
 *     node states are substituted with their compacted counterparts. This results
 *     in an {@link #equals(Object)| equal} node state at any moment in the process,
 *     which means that if the compaction is cancelled, a valid, albeit only
 *     partially compacted state can be returned.
 *     <p>
 *     After a partially completed "down compaction", the segments referenced by the
 *     root record can be of different generations. However, the stable identifier
 *     of the compacted records remain unchanged.
 *     </li>
 *     <li>Compacting up, where the differences between {@code before} and
 *     {@code after} are compacted on top of {@code before}. This is useful to
 *     create a new {@link NodeState} that contains the changes from
 *     {@code after} but is based on {@code before}, often on an empty node-state.
 *     <p>
 *     "Up compaction" results in a fully compacted state, but it can only be "hard-canceller",
 *     i.e. either the full compaction is done, or nothing at all ({@code null} is returned
 *     if cancelled).
 *     <p>
 *     Generally, "up compaction" is more thorough than "down compaction", because
 *     the entire content tree is rewritten. Furthermore, it guarantees that after
 *     compaction all reachable segments are of the same, new generation.
 *     </li>
 * </ul>
 * <p>
 * The compaction process can be cancelled through a {@link Canceller}. If
 * cancellation is requested, the compaction will either abort completely or,
 * in case of compacting down, return a partially compacted state.
 */
public abstract class Compactor {

    /**
     * Convenience method to run {@link #compactDown(NodeState, NodeState, Canceller, Canceller)},
     * where the {@code before} state is the empty node state.
     *
     * @param state         the (after) node state to compact
     * @param hardCanceller the trigger for hard cancellation, will abandon compaction if cancelled
     * @param softCanceller the trigger for soft cancellation, will return partially compacted state if cancelled
     * @return              the fully or partially compacted node state, or {@code null} if hard-cancelled
     * @throws IOException will throw exception if any errors occur during compaction
     */
    public final @Nullable CompactedNodeState compactDown(
            @NotNull NodeState state,
            @NotNull Canceller hardCanceller,
            @NotNull Canceller softCanceller
    ) throws IOException {
        return compactDown(EMPTY_NODE, state, hardCanceller, softCanceller);
    }

    /**
     * compact the differences between {@code after} and {@code before} on top of {@code after}.
     * @param before        the node state to diff against from {@code after}
     * @param after         the node state diffed against {@code before}
     * @param hardCanceller the trigger for hard cancellation, will abandon compaction if cancelled
     * @param softCanceller the trigger for soft cancellation, will return partially compacted state if cancelled
     * @return              the compacted node state or {@code null} if hard-cancelled
     * @throws IOException  will throw exception if any errors occur during compaction
     */
    public abstract @Nullable CompactedNodeState compactDown(
            @NotNull NodeState before,
            @NotNull NodeState after,
            @NotNull Canceller hardCanceller,
            @NotNull Canceller softCanceller
    ) throws IOException;

    /**
     * Convenience method to run {@link #compactUp(NodeState, NodeState, Canceller)},
     * where the {@code before} state is the empty node state.
     *
     * @param state         the (after) node state to compact
     * @param canceller the trigger for hard cancellation, will abandon compaction if cancelled
     * @return              the fully compacted node state, or {@code null} if hard-cancelled
     * @throws IOException will throw exception if any errors occur during compaction
     */
    public final @Nullable CompactedNodeState compactUp(
            @NotNull NodeState state,
            @NotNull Canceller canceller
    ) throws IOException {
        return compactUp(EMPTY_NODE, state, canceller);
    }

    /**
     * compact the differences between {@code after} and {@code before} on top of {@code before}.
     */
    public final @Nullable CompactedNodeState compactUp(
            @NotNull NodeState before,
            @NotNull NodeState after,
            @NotNull Canceller canceller
    ) throws IOException {
        return compact(before, after, before, canceller);
    }

    /**
     * compact the differences between {@code after} and {@code before} on top of {@code onto}.
     * @param before        the node state to diff against from {@code after}
     * @param after         the node state diffed against {@code before}
     * @param onto          the node state to compact to apply the diff to
     * @param canceller     the trigger for hard cancellation, will abandon compaction if cancelled
     * @return              the compacted node state or {@code null} if hard-cancelled
     * @throws IOException  will throw exception if any errors occur during compaction
     */
    public abstract @Nullable CompactedNodeState compact(
            @NotNull NodeState before,
            @NotNull NodeState after,
            @NotNull NodeState onto,
            @NotNull Canceller canceller
    ) throws IOException;
}
