<commit_before>/**
 * Copyright (c) 2002-2013 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.kernel.api.impl.index;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;

import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.util.FailureStorage;

public abstract class LuceneIndexPopulator implements IndexPopulator
{
    protected final LuceneDocumentStructure documentStructure;
    private final LuceneIndexWriterFactory indexWriterFactory;
    private final IndexWriterStatus writerStatus;
    private final DirectoryFactory dirFactory;
    private final File dirFile;
    private final FailureStorage failureStorage;
    private final long indexId;

    protected IndexWriter writer;
    private Directory directory;

    LuceneIndexPopulator(
            LuceneDocumentStructure documentStructure, LuceneIndexWriterFactory indexWriterFactory,
            IndexWriterStatus writerStatus, DirectoryFactory dirFactory, File dirFile,
            FailureStorage failureStorage, long indexId )
    {
        this.documentStructure = documentStructure;
        this.indexWriterFactory = indexWriterFactory;
        this.writerStatus = writerStatus;
        this.dirFactory = dirFactory;
        this.dirFile = dirFile;
        this.failureStorage = failureStorage;
        this.indexId = indexId;
        try
        {
            failureStorage.reserveForIndex( indexId );
        }
        catch ( IOException e )
        {
            throw new RuntimeException( e );
        }
    }

    @Override
    public void create() throws IOException
    {
        this.directory = dirFactory.open( dirFile );
        DirectorySupport.deleteDirectoryContents( directory );
        writer = indexWriterFactory.create( directory );
    }

    @Override
    public void drop() throws IOException
    {
        if ( writer != null )
        {
            writerStatus.close( writer );
        }
        
        try
        {
            DirectorySupport.deleteDirectoryContents( directory = directory == null ? dirFactory.open( dirFile ) : directory );
        }
        catch ( AlreadyClosedException e )
        {   // It was closed, open again just to be able to delete the files
            DirectorySupport.deleteDirectoryContents( directory = dirFactory.open( dirFile ) );
        }
        finally
        {
            directory.close();
        }
        failureStorage.clearForIndex( indexId );
    }

    @Override
    public void close( boolean populationCompletedSuccessfully ) throws IOException
    {
        try
        {
            if ( populationCompletedSuccessfully )
            {
                flush();
                writerStatus.commitAsOnline( writer );
            }
        }
        finally
        {
            if ( writer != null )
            {
                writerStatus.close( writer );
            }
            if ( directory != null )
            {
                directory.close();
            }
        }
    }
    
    @Override
    public void markAsFailed( String failure ) throws IOException
    {
        failureStorage.storeIndexFailure( indexId, failure );
    }

    protected abstract void flush() throws IOException;
}
<commit_msg>Move creation of FailureStorage to LuceneIndexPopulator.create()<commit_after>/**
 * Copyright (c) 2002-2013 "Neo Technology,"
 * Network Engine for Objects in Lund AB [http://neotechnology.com]
 *
 * This file is part of Neo4j.
 *
 * Neo4j is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.neo4j.kernel.api.impl.index;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;

import org.neo4j.kernel.api.index.IndexPopulator;
import org.neo4j.kernel.api.index.util.FailureStorage;

public abstract class LuceneIndexPopulator implements IndexPopulator
{
    protected final LuceneDocumentStructure documentStructure;
    private final LuceneIndexWriterFactory indexWriterFactory;
    private final IndexWriterStatus writerStatus;
    private final DirectoryFactory dirFactory;
    private final File dirFile;
    private final FailureStorage failureStorage;
    private final long indexId;

    protected IndexWriter writer;
    private Directory directory;

    LuceneIndexPopulator(
            LuceneDocumentStructure documentStructure, LuceneIndexWriterFactory indexWriterFactory,
            IndexWriterStatus writerStatus, DirectoryFactory dirFactory, File dirFile,
            FailureStorage failureStorage, long indexId )
    {
        this.documentStructure = documentStructure;
        this.indexWriterFactory = indexWriterFactory;
        this.writerStatus = writerStatus;
        this.dirFactory = dirFactory;
        this.dirFile = dirFile;
        this.failureStorage = failureStorage;
        this.indexId = indexId;
    }

    @Override
    public void create() throws IOException
    {
        this.directory = dirFactory.open( dirFile );
        DirectorySupport.deleteDirectoryContents( directory );
        failureStorage.reserveForIndex( indexId );
        writer = indexWriterFactory.create( directory );
    }

    @Override
    public void drop() throws IOException
    {
        if ( writer != null )
        {
            writerStatus.close( writer );
        }
        
        try
        {
            DirectorySupport.deleteDirectoryContents( directory = directory == null ? dirFactory.open( dirFile ) : directory );
        }
        catch ( AlreadyClosedException e )
        {   // It was closed, open again just to be able to delete the files
            DirectorySupport.deleteDirectoryContents( directory = dirFactory.open( dirFile ) );
        }
        finally
        {
            if ( directory != null )
            {
                directory.close();
            }
        }
        failureStorage.clearForIndex( indexId );
    }

    @Override
    public void close( boolean populationCompletedSuccessfully ) throws IOException
    {
        try
        {
            if ( populationCompletedSuccessfully )
            {
                flush();
                writerStatus.commitAsOnline( writer );
            }
        }
        finally
        {
            if ( writer != null )
            {
                writerStatus.close( writer );
            }
            if ( directory != null )
            {
                directory.close();
            }
        }
    }
    
    @Override
    public void markAsFailed( String failure ) throws IOException
    {
        failureStorage.storeIndexFailure( indexId, failure );
    }

    protected abstract void flush() throws IOException;
}

{'fraction_non_alphanumeric': 0.06942229854041457, 'fraction_numerical': 0.003434318655709555, 'mean_word_length': 2.3862126245847177, 'pattern_counts': {'":': 0, '<': 5, '<?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': '6351028', 'n_tokens_mistral': 2025, 'n_tokens_neox': 1944, 'n_words': 622}