<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.index.lucene;

import java.util.List;

import javax.annotation.Nonnull;

import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Service;
import org.apache.jackrabbit.oak.plugins.index.aggregate.NodeAggregator;
import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
import org.apache.jackrabbit.oak.spi.commit.Observer;
import org.apache.jackrabbit.oak.spi.query.QueryIndex;
import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.apache.lucene.analysis.Analyzer;

import com.google.common.collect.ImmutableList;

/**
 * A provider for Lucene indexes.
 * 
 * @see LuceneIndex
 */
@Component
@Service({ QueryIndexProvider.class, Observer.class })
public class LuceneIndexProvider implements QueryIndexProvider, Observer {

    protected final IndexTracker tracker = new IndexTracker();

    /**
     * TODO how to inject this in an OSGi friendly way?
     */
    protected Analyzer analyzer = LuceneIndexConstants.ANALYZER;

    protected NodeAggregator aggregator = null;

    @Deactivate
    public void close() {
        tracker.close();
    }

    //----------------------------------------------------------< Observer >--

    @Override
    public void contentChanged(NodeState root, CommitInfo info) {
        tracker.update(root);
    }

    //------------------------------------------------< QueryIndexProvider >--

    @Override @Nonnull
    public List<QueryIndex> getQueryIndexes(NodeState nodeState) {
        return ImmutableList.<QueryIndex> of(newLuceneIndex());
    }

    protected LuceneIndex newLuceneIndex() {
        return new LuceneIndex(tracker, analyzer, aggregator);<commit_msg>OAK-1462 - Aggregate Index isn't usable in an osgi environment<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.index.lucene;

import java.io.Closeable;
import java.util.List;

import javax.annotation.Nonnull;

import org.apache.jackrabbit.oak.plugins.index.aggregate.NodeAggregator;
import org.apache.jackrabbit.oak.spi.commit.CommitInfo;
import org.apache.jackrabbit.oak.spi.commit.Observer;
import org.apache.jackrabbit.oak.spi.query.QueryIndex;
import org.apache.jackrabbit.oak.spi.query.QueryIndexProvider;
import org.apache.jackrabbit.oak.spi.state.NodeState;
import org.apache.lucene.analysis.Analyzer;

import com.google.common.collect.ImmutableList;

/**
 * A provider for Lucene indexes.
 * 
 * @see LuceneIndex
 */
public class LuceneIndexProvider implements QueryIndexProvider, Observer, Closeable {

    protected final IndexTracker tracker = new IndexTracker();

    /**
     * TODO how to inject this in an OSGi friendly way?
     */
    protected Analyzer analyzer = LuceneIndexConstants.ANALYZER;

    protected NodeAggregator aggregator = null;

    public void close() {
        tracker.close();
    }

    //----------------------------------------------------------< Observer >--

    @Override
    public void contentChanged(NodeState root, CommitInfo info) {
        tracker.update(root);
    }

    //------------------------------------------------< QueryIndexProvider >--

    @Override @Nonnull
    public List<QueryIndex> getQueryIndexes(NodeState nodeState) {
        return ImmutableList.<QueryIndex> of(newLuceneIndex());
    }

    protected LuceneIndex newLuceneIndex() {
        return new LuceneIndex(tracker, analyzer, aggregator);
{'fraction_non_alphanumeric': 0.12573558258140446, 'fraction_numerical': 0.0023538642604943117, 'mean_word_length': 3.242096505823627, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5222378', 'n_tokens_mistral': 1390, 'n_tokens_neox': 1329, 'n_words': 427}