<commit_before>/*
 * 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.plugins.blob;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Default implementation of {@link BlobGCMBean} based on a {@link BlobGarbageCollector}.
 */
public class BlobGC implements BlobGCMBean {
    private static final Logger log = LoggerFactory.getLogger(BlobGC.class);

    private final BlobGarbageCollector blobGarbageCollector;
    private final ExecutorService executorService;

    private Future<Long> gcOp;

    /**
     * @param blobGarbageCollector  Blob garbage collector
     * @param executorService  executor service for running the garbage collection task
     *                         in the background.
     */
    public BlobGC(
            @Nonnull BlobGarbageCollector blobGarbageCollector,
            @Nonnull ExecutorService executorService) {
        this.blobGarbageCollector = checkNotNull(blobGarbageCollector);
        this.executorService = checkNotNull(executorService);
    }

    @Nonnull
    @Override
    public String startBlobGC() {
        if (gcOp != null && !gcOp.isDone()) {
            return "Garbage collection already running";
        } else {
            gcOp = executorService.submit(new Callable<Long>() {
                @Override
                public Long call() throws Exception {
                    long t0 = System.nanoTime();
                    blobGarbageCollector.collectGarbage();
                    return System.nanoTime() - t0;
                }
            });
            return getBlobGCStatus();
        }
    }

    @Nonnull
    @Override
    public String getBlobGCStatus() {
        if (gcOp == null) {
            return "Garbage collection not started";
        } else if (gcOp.isCancelled()) {
            return "Garbage collection cancelled";
        } else if (gcOp.isDone()) {
            try {
                return "Garbage collection completed in " + formatTime(gcOp.get());
            } catch (InterruptedException e) {<commit_msg>OAK-1559: Expose BlobGCMBean for supported NodeStores Only require an Executor instead of an ExecutorService in the constructor of BlobGC<commit_after>/*
 * 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.plugins.blob;

import static com.google.common.base.Preconditions.checkNotNull;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.FutureTask;
import java.util.concurrent.RunnableFuture;
import java.util.concurrent.TimeUnit;

import javax.annotation.Nonnull;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Default implementation of {@link BlobGCMBean} based on a {@link BlobGarbageCollector}.
 */
public class BlobGC implements BlobGCMBean {
    private static final Logger log = LoggerFactory.getLogger(BlobGC.class);

    private final BlobGarbageCollector blobGarbageCollector;
    private final Executor executor;

    private RunnableFuture<Long> gcOp;

    /**
     * @param blobGarbageCollector  Blob garbage collector
     * @param executor              executor for running the garbage collection task
     */
    public BlobGC(
            @Nonnull BlobGarbageCollector blobGarbageCollector,
            @Nonnull Executor executor) {
        this.blobGarbageCollector = checkNotNull(blobGarbageCollector);
        this.executor = checkNotNull(executor);
    }

    @Nonnull
    @Override
    public String startBlobGC() {
        if (gcOp != null && !gcOp.isDone()) {
            return "Garbage collection already running";
        } else {
            gcOp = new FutureTask<Long>(new Callable<Long>() {
                @Override
                public Long call() throws Exception {
                    long t0 = System.nanoTime();
                    blobGarbageCollector.collectGarbage();
                    return System.nanoTime() - t0;
                }
            });
            executor.execute(gcOp);
            return getBlobGCStatus();
        }
    }

    @Nonnull
    @Override
    public String getBlobGCStatus() {
        if (gcOp == null) {
            return "Garbage collection not started";
        } else if (gcOp.isCancelled()) {
            return "Garbage collection cancelled";
        } else if (gcOp.isDone()) {
            try {
                return "Garbage collection completed in " + formatTime(gcOp.get());
            } catch (InterruptedException e) {
{'fraction_non_alphanumeric': 0.07895569620253165, 'fraction_numerical': 0.0031645569620253164, 'mean_word_length': 2.766984505363528, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 8, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3524902', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1617, 'n_words': 542}