<commit_before>package org.metaborg.spoofax.core.context;

import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.annotation.Nullable;

import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.metaborg.core.context.ContextIdentifier;
import org.metaborg.core.context.IContext;
import org.metaborg.core.context.IContextInternal;
import org.metaborg.core.language.ILanguageImpl;
import org.metaborg.runtime.task.engine.ITaskEngine;
import org.metaborg.runtime.task.engine.TaskManager;
import org.metaborg.spoofax.core.terms.ITermFactoryService;
import org.metaborg.util.concurrent.ClosableLock;
import org.metaborg.util.concurrent.IClosableLock;
import org.metaborg.util.log.ILogger;
import org.metaborg.util.log.LoggerUtils;
import org.spoofax.interpreter.library.index.IIndex;
import org.spoofax.interpreter.library.index.IndexManager;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.terms.ParseError;

import com.google.inject.Injector;

public class IndexTaskContext implements IContext, IContextInternal, IIndexTaskContext {
    private static final ILogger logger = LoggerUtils.logger(IndexTaskContext.class);

    private final Injector injector;

    private final ITermFactory termFactory;
    private final ReadWriteLock lock;

    private final ContextIdentifier identifier;

    private IIndex index;
    private ITaskEngine taskEngine;


    public IndexTaskContext(Injector injector, ITermFactoryService termFactoryService, ContextIdentifier identifier) {
        this.injector = injector;

        this.termFactory = termFactoryService.get(identifier.language);
        this.lock = new ReentrantReadWriteLock(true);

        this.identifier = identifier;
    }


    @Override public ContextIdentifier identifier() {
        return identifier;
    }

    @Override public FileObject location() {
        return identifier.location;
    }

    @Override public ILanguageImpl language() {
        return identifier.language;
    }

    @Override public Injector injector() {
        return injector;
    }


    @Override public @Nullable IIndex index() {
        return index;
    }

    @Override public @Nullable ITaskEngine taskEngine() {
        return taskEngine;
    }


    @Override public IClosableLock read() {
        if(index == null || taskEngine == null) {
            // THREADING: temporarily acquire a write lock when initializing the index, need exclusive access.
            try(IClosableLock lock = writeLock()) {
                /*
                 * THREADING: re-check if index/task engine are still null now that we have exclusive access, there
                 * could have been a context switch before acquiring the lock. Check is also needed because the null
                 * check before is disjunct.
                 */
                if(index == null) {
                    index = initIndex();
                }
                if(taskEngine == null) {
                    taskEngine = initTaskEngine();
                }
            }
        }

        index.recover();
        taskEngine.recover();

        return readLock();
    }

    private IClosableLock readLock() {
        final Lock readLock = lock.readLock();
        final IClosableLock lock = new ClosableLock(readLock);
        return lock;
    }<commit_msg>Load index and task engine, instead of initializing them as empty.<commit_after>package org.metaborg.spoofax.core.context;

import java.io.IOException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.annotation.Nullable;

import org.apache.commons.vfs2.AllFileSelector;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.metaborg.core.context.ContextIdentifier;
import org.metaborg.core.context.IContext;
import org.metaborg.core.context.IContextInternal;
import org.metaborg.core.language.ILanguageImpl;
import org.metaborg.runtime.task.engine.ITaskEngine;
import org.metaborg.runtime.task.engine.TaskManager;
import org.metaborg.spoofax.core.terms.ITermFactoryService;
import org.metaborg.util.concurrent.ClosableLock;
import org.metaborg.util.concurrent.IClosableLock;
import org.metaborg.util.log.ILogger;
import org.metaborg.util.log.LoggerUtils;
import org.spoofax.interpreter.library.index.IIndex;
import org.spoofax.interpreter.library.index.IndexManager;
import org.spoofax.interpreter.terms.ITermFactory;
import org.spoofax.terms.ParseError;

import com.google.inject.Injector;

public class IndexTaskContext implements IContext, IContextInternal, IIndexTaskContext {
    private static final ILogger logger = LoggerUtils.logger(IndexTaskContext.class);

    private final Injector injector;

    private final ITermFactory termFactory;
    private final ReadWriteLock lock;

    private final ContextIdentifier identifier;

    private IIndex index;
    private ITaskEngine taskEngine;


    public IndexTaskContext(Injector injector, ITermFactoryService termFactoryService, ContextIdentifier identifier) {
        this.injector = injector;

        this.termFactory = termFactoryService.get(identifier.language);
        this.lock = new ReentrantReadWriteLock(true);

        this.identifier = identifier;
    }


    @Override public ContextIdentifier identifier() {
        return identifier;
    }

    @Override public FileObject location() {
        return identifier.location;
    }

    @Override public ILanguageImpl language() {
        return identifier.language;
    }

    @Override public Injector injector() {
        return injector;
    }


    @Override public @Nullable IIndex index() {
        return index;
    }

    @Override public @Nullable ITaskEngine taskEngine() {
        return taskEngine;
    }


    @Override public IClosableLock read() {
        if(index == null || taskEngine == null) {
            // THREADING: temporarily acquire a write lock when initializing the index, need exclusive access.
            try(IClosableLock lock = writeLock()) {
                /*
                 * THREADING: re-check if index/task engine are still null now that we have exclusive access, there
                 * could have been a context switch before acquiring the lock. Check is also needed because the null
                 * check before is disjunct.
                 */
                if(index == null) {
                    index = loadIndex();
                }
                if(taskEngine == null) {
                    taskEngine = loadTaskEngine();
                }
            }
        }

        index.recover();
        taskEngine.recover();

        return readLock();
    }

    private IClosableLock readLock() {
        final Lock readLock = lock.readLock();
        final IClosableLock lock = new ClosableLock(readLock);
        return lock;
    }
{'fraction_non_alphanumeric': 0.08226510776165658, 'fraction_numerical': 0.0008451894633046908, 'mean_word_length': 2.975363941769317, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '835617', 'n_tokens_mistral': 1971, 'n_tokens_neox': 1895, 'n_words': 460}