<commit_before>/*
 *  Copyright 2012-2013 <NAME>
 *
 *  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 org.cojen.tupl;

import java.io.IOException;

/**
 * 
 *
 * @author <NAME>
 * @see ReplRedoEngine
 */
final class ReplRedoWriter extends RedoWriter {
    private final ReplRedoEngine mEngine;
    private final ReplicationManager mManager;

    private long mCheckpointNum;
    private long mCheckpointPos;
    private long mCheckpointTxnId;

    private volatile boolean mIsLeader;

    ReplRedoWriter(ReplRedoEngine engine) {
        super(4096, 0);
        mEngine = engine;
        mManager = engine.mManager;
    }

    synchronized void initCheckpointNumber(long num) {
        mCheckpointNum = num;
    }

    public void recover(long initialTxnId, EventListener listener) throws IOException {
        mEngine.startReceiving(initialTxnId);
        mManager.recover(listener);
    }

    @Override
    public long store(long indexId, byte[] key, byte[] value, DurabilityMode mode)
        throws IOException
    {
        // Pass SYNC mode to flush the buffer and obtain the commit position. Return value from
        // this method indicates if an actual sync should be performed.
        long pos = super.store(indexId, key, value, DurabilityMode.SYNC);
        if (pos < 0) {
            throw unmodifiable();
        }
        // Replication makes no distinction between SYNC and NO_SYNC durability. The NO_REDO
        // mode is not expected to be passed into this method.
        return mode == DurabilityMode.NO_FLUSH ? 0 : pos;
    }

    @Override
    public long storeNoLock(long indexId, byte[] key, byte[] value, DurabilityMode mode)
        throws IOException
    {
        // Ditto comments from above.
        long pos = super.storeNoLock(indexId, key, value, DurabilityMode.SYNC);
        if (pos < 0) {
            throw unmodifiable();
        }
        return mode == DurabilityMode.NO_FLUSH ? 0 : pos;
    }

    @Override
    public long txnCommitFinal(long txnId, DurabilityMode mode) throws IOException {
        // Ditto comments from above.
        long pos = super.txnCommitFinal(txnId, DurabilityMode.SYNC);
        if (pos < 0) {
            throw unmodifiable();
        }
        return mode == DurabilityMode.NO_FLUSH ? 0 : pos;
    }

    @Override
    public void txnCommitSync(long commitPos) throws IOException {
        super.txnCommitSync(commitPos);
        if (commitPos > 0) {
            mManager.confirm(commitPos);
        }
    }

    @Override
    public synchronized void close(Throwable cause) throws IOException {
        super.close(cause);
        forceAndClose();
    }

    @Override
    public final long encoding() {
        return mManager.encoding();
    }

    @Override
    boolean isOpen() {
        // Returning false all the time prevents close and shutdown messages from being
        // written. They aren't very useful anyhow, considering that they don't prevent new log<commit_msg>Repl commit should only call confirm.<commit_after>/*
 *  Copyright 2012-2013 <NAME>
 *
 *  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 org.cojen.tupl;

import java.io.IOException;

/**
 * 
 *
 * @author <NAME>
 * @see ReplRedoEngine
 */
final class ReplRedoWriter extends RedoWriter {
    private final ReplRedoEngine mEngine;
    private final ReplicationManager mManager;

    private long mCheckpointNum;
    private long mCheckpointPos;
    private long mCheckpointTxnId;

    private volatile boolean mIsLeader;

    ReplRedoWriter(ReplRedoEngine engine) {
        super(4096, 0);
        mEngine = engine;
        mManager = engine.mManager;
    }

    synchronized void initCheckpointNumber(long num) {
        mCheckpointNum = num;
    }

    public void recover(long initialTxnId, EventListener listener) throws IOException {
        mEngine.startReceiving(initialTxnId);
        mManager.recover(listener);
    }

    @Override
    public long store(long indexId, byte[] key, byte[] value, DurabilityMode mode)
        throws IOException
    {
        // Pass SYNC mode to flush the buffer and obtain the commit position. Return value from
        // this method indicates if an actual sync should be performed.
        long pos = super.store(indexId, key, value, DurabilityMode.SYNC);
        if (pos < 0) {
            throw unmodifiable();
        }
        // Replication makes no distinction between SYNC and NO_SYNC durability. The NO_REDO
        // mode is not expected to be passed into this method.
        return mode == DurabilityMode.NO_FLUSH ? 0 : pos;
    }

    @Override
    public long storeNoLock(long indexId, byte[] key, byte[] value, DurabilityMode mode)
        throws IOException
    {
        // Ditto comments from above.
        long pos = super.storeNoLock(indexId, key, value, DurabilityMode.SYNC);
        if (pos < 0) {
            throw unmodifiable();
        }
        return mode == DurabilityMode.NO_FLUSH ? 0 : pos;
    }

    @Override
    public long txnCommitFinal(long txnId, DurabilityMode mode) throws IOException {
        // Ditto comments from above.
        long pos = super.txnCommitFinal(txnId, DurabilityMode.SYNC);
        if (pos < 0) {
            throw unmodifiable();
        }
        return mode == DurabilityMode.NO_FLUSH ? 0 : pos;
    }

    @Override
    public void txnCommitSync(long commitPos) throws IOException {
        if (commitPos > 0) {
            mManager.confirm(commitPos);
        }
    }

    @Override
    public synchronized void close(Throwable cause) throws IOException {
        super.close(cause);
        forceAndClose();
    }

    @Override
    public final long encoding() {
        return mManager.encoding();
    }

    @Override
    boolean isOpen() {
        // Returning false all the time prevents close and shutdown messages from being
        // written. They aren't very useful anyhow, considering that they don't prevent new log
{'fraction_non_alphanumeric': 0.07759861790958825, 'fraction_numerical': 0.006910452058738843, 'mean_word_length': 2.582774626095926, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, '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': '1684608', 'n_tokens_mistral': 1931, 'n_tokens_neox': 1881, 'n_words': 667}