<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.run.cli;

import java.io.IOException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counting;
import com.codahale.metrics.MetricRegistry;
import com.google.common.io.Closer;
import com.google.common.util.concurrent.MoreExecutors;
import org.apache.jackrabbit.oak.plugins.metric.MetricStatisticsProvider;
import org.apache.jackrabbit.oak.spi.blob.BlobStore;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
import org.apache.jackrabbit.oak.stats.StatisticsProvider;

import static java.lang.management.ManagementFactory.getPlatformMBeanServer;
import static java.util.Collections.emptyMap;

public class NodeStoreFixtureProvider {
    public static NodeStoreFixture create(Options options) throws Exception {
        return create(options, !options.getOptionBean(CommonOptions.class).isReadWrite());
    }

    public static NodeStoreFixture create(Options options, boolean readOnly) throws Exception {
        CommonOptions commonOpts = options.getOptionBean(CommonOptions.class);
        Whiteboard wb = options.getWhiteboard();
        Closer closer = Closer.create();
        BlobStoreFixture blobFixture = BlobStoreFixtureProvider.create(options);
        BlobStore blobStore = null;
        if (blobFixture != null) {
            blobStore = blobFixture.getBlobStore();
            closer.register(blobFixture);
        }

        StatisticsProvider statisticsProvider = createStatsProvider(options, wb, closer);
        wb.register(StatisticsProvider.class, statisticsProvider, emptyMap());

        NodeStore store;
        if (commonOpts.isMongo() || commonOpts.isRDB()) {
            store = DocumentFixtureProvider.configureDocumentMk(options, blobStore, wb, closer, readOnly);
        } else if (commonOpts.isOldSegment()) {
            store = SegmentFixtureProvider.create(options, blobStore, wb, closer, readOnly);
        } else {
            store = SegmentTarFixtureProvider.configureSegment(options, blobStore, wb, closer, readOnly);
        }

        return new SimpleNodeStoreFixture(store, blobStore, wb, closer);
    }

    private static StatisticsProvider createStatsProvider(Options options, Whiteboard wb, Closer closer) {
        if (options.getCommonOpts().isMetricsEnabled()) {
            ScheduledExecutorService executorService =
                    MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
            MetricStatisticsProvider statsProvider = new MetricStatisticsProvider(getPlatformMBeanServer(), executorService);
            closer.register(statsProvider);
            closer.register(() -> reportMetrics(statsProvider));
            wb.register(MetricRegistry.class, statsProvider.getRegistry(), emptyMap());
            return statsProvider;<commit_msg>OAK-6497 - Support old Segment NodeStore setups for oak-run index tooling<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.run.cli;

import java.io.File;
import java.io.IOException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Counting;
import com.codahale.metrics.MetricRegistry;
import com.google.common.io.Closer;
import com.google.common.util.concurrent.MoreExecutors;
import org.apache.jackrabbit.oak.plugins.metric.MetricStatisticsProvider;
import org.apache.jackrabbit.oak.segment.file.InvalidFileStoreVersionException;
import org.apache.jackrabbit.oak.spi.blob.BlobStore;
import org.apache.jackrabbit.oak.spi.state.NodeStore;
import org.apache.jackrabbit.oak.spi.whiteboard.Whiteboard;
import org.apache.jackrabbit.oak.stats.StatisticsProvider;

import static java.lang.management.ManagementFactory.getPlatformMBeanServer;
import static java.util.Collections.emptyMap;

public class NodeStoreFixtureProvider {
    public static NodeStoreFixture create(Options options) throws Exception {
        return create(options, !options.getOptionBean(CommonOptions.class).isReadWrite());
    }

    public static NodeStoreFixture create(Options options, boolean readOnly) throws Exception {
        CommonOptions commonOpts = options.getOptionBean(CommonOptions.class);
        Whiteboard wb = options.getWhiteboard();
        Closer closer = Closer.create();
        BlobStoreFixture blobFixture = BlobStoreFixtureProvider.create(options);
        BlobStore blobStore = null;
        if (blobFixture != null) {
            blobStore = blobFixture.getBlobStore();
            closer.register(blobFixture);
        }

        StatisticsProvider statisticsProvider = createStatsProvider(options, wb, closer);
        wb.register(StatisticsProvider.class, statisticsProvider, emptyMap());

        NodeStore store;
        if (commonOpts.isMongo() || commonOpts.isRDB()) {
            store = DocumentFixtureProvider.configureDocumentMk(options, blobStore, wb, closer, readOnly);
        } else if (commonOpts.isOldSegment()) {
            store = SegmentFixtureProvider.create(options, blobStore, wb, closer, readOnly);
        } else {
            try {
                store = SegmentTarFixtureProvider.configureSegment(options, blobStore, wb, closer, readOnly);
            } catch (InvalidFileStoreVersionException e) {
                if (oldSegmentStore(options)) {
                    store = SegmentFixtureProvider.create(options, blobStore, wb, closer, readOnly);
                } else {
                    throw e;
                }
            }
        }

        return new SimpleNodeStoreFixture(store, blobStore, wb, closer);
    }

    private static boolean oldSegmentStore(Options options) {
        String path = options.getOptionBean(CommonOptions.class).getStoreArg();
        File dir = new File(path);
        // manifest file was introduced with oak-segment-tar
        File manifest = new File(dir, "manifest");
        return !manifest.exists();
    }

    private static StatisticsProvider createStatsProvider(Options options, Whiteboard wb, Closer closer) {
        if (options.getCommonOpts().isMetricsEnabled()) {
            ScheduledExecutorService executorService =
                    MoreExecutors.getExitingScheduledExecutorService(new ScheduledThreadPoolExecutor(1));
            MetricStatisticsProvider statsProvider = new MetricStatisticsProvider(getPlatformMBeanServer(), executorService);
            closer.register(statsProvider);
            closer.register(() -> reportMetrics(statsProvider));
            wb.register(MetricRegistry.class, statsProvider.getRegistry(), emptyMap());
            return statsProvider;
{'fraction_non_alphanumeric': 0.08422555317630265, 'fraction_numerical': 0.0016654770402093743, 'mean_word_length': 3.725688589094997, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2374818', 'n_tokens_mistral': 2154, 'n_tokens_neox': 2120, 'n_words': 570}