<commit_before>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed 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 com.intellij.util.indexing;

import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.util.NotNullLazyValue;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import gnu.trove.THashSet;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

/**
 * @author peter
 */
public class AdditionalIndexableFileSet implements IndexableFileSet {

  private NotNullLazyValue<Set<VirtualFile>> myRoots = new NotNullLazyValue<Set<VirtualFile>>() {
    @NotNull
    @Override
    protected Set<VirtualFile> compute() {
      THashSet<VirtualFile> virtualFiles = new THashSet<VirtualFile>();
      if (myExtensions == null) {
        myExtensions = Extensions.getExtensions(IndexableSetContributor.EP_NAME);
      }
        for (IndexedRootsProvider provider : myExtensions) {
          virtualFiles.addAll(IndexableSetContributor.getRootsToIndex(provider));
        }
      return virtualFiles;
    }
  };

  private IndexedRootsProvider[] myExtensions;

  public AdditionalIndexableFileSet() {
  }

  public AdditionalIndexableFileSet(IndexedRootsProvider... extensions) {
    myExtensions = extensions;
  }

  public boolean isInSet(VirtualFile file) {
    for (final VirtualFile root : myRoots.getValue()) {
      if (VfsUtil.isAncestor(root, file, false)) {
        return true;
      }<commit_msg>[r=peter] fix invalid virtual file access when indexing files from additional roots<commit_after>/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed 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 com.intellij.util.indexing;

import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import gnu.trove.THashSet;

import java.util.Set;

/**
 * @author peter
 */
public class AdditionalIndexableFileSet implements IndexableFileSet {
  private volatile Set<VirtualFile> cachedFiles;
  private volatile IndexedRootsProvider[] myExtensions;

  public AdditionalIndexableFileSet(IndexedRootsProvider... extensions) {
    myExtensions = extensions;
  }

  private Set<VirtualFile> getFiles() {
    Set<VirtualFile> files = cachedFiles;
    if (files == null || filesInvalidated(files)) {
      cachedFiles = files = collectFiles();
    }
    return files;
  }

  private Set<VirtualFile> collectFiles() {
    THashSet<VirtualFile> virtualFiles = new THashSet<VirtualFile>();
    if (myExtensions == null) {
      myExtensions = Extensions.getExtensions(IndexableSetContributor.EP_NAME);
    }
    for (IndexedRootsProvider provider : myExtensions) {
      virtualFiles.addAll(IndexableSetContributor.getRootsToIndex(provider));
    }
    return virtualFiles;
  }

  public static boolean filesInvalidated(Set<VirtualFile> files) {
    for (VirtualFile file : files) {
      if (!file.isValid()) {
        return true;
      }
    }
    return false;
  }

  public AdditionalIndexableFileSet() {
  }

  public boolean isInSet(VirtualFile file) {
    for (final VirtualFile root : getFiles()) {
      if (VfsUtil.isAncestor(root, file, false)) {
        return true;
      }
{'fraction_non_alphanumeric': 0.09204152249134948, 'fraction_numerical': 0.005536332179930796, 'mean_word_length': 3.9896432681242806, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '5399433', 'n_tokens_mistral': 1267, 'n_tokens_neox': 1228, 'n_words': 353}