<commit_before>/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2013 The eXist Project
 * http://exist-db.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exist.storage.journal;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.storage.DBBroker;

import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardOpenOption.READ;
import static org.exist.storage.journal.Journal.LOG_ENTRY_BACK_LINK_LEN;
import static org.exist.storage.journal.Journal.LOG_ENTRY_BASE_LEN;
import static org.exist.storage.journal.Journal.LOG_ENTRY_HEADER_LEN;

/**
 * Read log entries from the journal file. This class is used during recovery to scan the
 * last journal file. It uses a memory-mapped byte buffer on the file.
 * Journal entries can be read forward (during redo) or backward (during undo).
 *
 * @author wolf
 */
public class JournalReader implements AutoCloseable {

    private static final Logger LOG = LogManager.getLogger(JournalReader.class);

    private final DBBroker broker;
    private final int fileNumber;
    private final ByteBuffer header = ByteBuffer.allocateDirect(LOG_ENTRY_HEADER_LEN);
    private ByteBuffer payload = ByteBuffer.allocateDirect(8192);  // 8 KB
    @Nullable
    private SeekableByteChannel fc;

    /**
     * Opens the specified file for reading.
     *
     * @param broker     the database broker
     * @param file       the journal file
     * @param fileNumber the number of the journal file
     * @throws LogException if the journal cannot be opened
     */
    public JournalReader(final DBBroker broker, final Path file, final int fileNumber) throws LogException {
        this.broker = broker;
        this.fileNumber = fileNumber;
        try {
            fc = Files.newByteChannel(file, READ);
        } catch (final IOException e) {
            throw new LogException("Failed to read journal file " + file.toAbsolutePath().toString(), e);
        }
    }

    /**
     * Returns the next entry found from the current position.
     *
     * @return the next entry, or null if there are no more entries.
     * @throws LogException if an entry could not be read due to an inconsistency on disk.
     */
    public @Nullable
    Loggable nextEntry() throws LogException {
        try {
            checkOpen();

            // are we at the end of the journal?
            if (fc.position() + LOG_ENTRY_BASE_LEN > fc.size()) {
                return null;
            }
        } catch (final IOException e) {<commit_msg>[bugfix] Ensure the journal file is closed if an error occurs when opening it<commit_after>/*
 * eXist Open Source Native XML Database
 * Copyright (C) 2001-2013 The eXist Project
 * http://exist-db.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.exist.storage.journal;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.exist.storage.DBBroker;

import javax.annotation.Nullable;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.SeekableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.nio.file.StandardOpenOption.READ;
import static org.exist.storage.journal.Journal.LOG_ENTRY_BACK_LINK_LEN;
import static org.exist.storage.journal.Journal.LOG_ENTRY_BASE_LEN;
import static org.exist.storage.journal.Journal.LOG_ENTRY_HEADER_LEN;

/**
 * Read log entries from the journal file. This class is used during recovery to scan the
 * last journal file. It uses a memory-mapped byte buffer on the file.
 * Journal entries can be read forward (during redo) or backward (during undo).
 *
 * @author wolf
 */
public class JournalReader implements AutoCloseable {

    private static final Logger LOG = LogManager.getLogger(JournalReader.class);

    private final DBBroker broker;
    private final int fileNumber;
    private final ByteBuffer header = ByteBuffer.allocateDirect(LOG_ENTRY_HEADER_LEN);
    private ByteBuffer payload = ByteBuffer.allocateDirect(8192);  // 8 KB
    @Nullable
    private SeekableByteChannel fc;

    /**
     * Opens the specified file for reading.
     *
     * @param broker     the database broker
     * @param file       the journal file
     * @param fileNumber the number of the journal file
     * @throws LogException if the journal cannot be opened
     */
    public JournalReader(final DBBroker broker, final Path file, final int fileNumber) throws LogException {
        this.broker = broker;
        this.fileNumber = fileNumber;
        try {
            this.fc = Files.newByteChannel(file, READ);
        } catch (final IOException e) {
            close();
            throw new LogException("Failed to read journal file " + file.toAbsolutePath().toString(), e);
        }
    }

    /**
     * Returns the next entry found from the current position.
     *
     * @return the next entry, or null if there are no more entries.
     * @throws LogException if an entry could not be read due to an inconsistency on disk.
     */
    public @Nullable
    Loggable nextEntry() throws LogException {
        try {
            checkOpen();

            // are we at the end of the journal?
            if (fc.position() + LOG_ENTRY_BASE_LEN > fc.size()) {
                return null;
            }
        } catch (final IOException e) {
{'fraction_non_alphanumeric': 0.07436342592592593, 'fraction_numerical': 0.0078125, 'mean_word_length': 3.1395209580838324, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6980036', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1796, 'n_words': 746}