<commit_before>/**
 * Copyright 2010 - 2014 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 jetbrains.exodus.tree.btree;

import jetbrains.exodus.ByteIterable;
import jetbrains.exodus.log.*;
import jetbrains.exodus.log.iterate.CompoundByteIterable;
import jetbrains.exodus.log.iterate.CompressedUnsignedLongByteIterable;
import org.jetbrains.annotations.NotNull;

final class BTreeDupMutable extends BTreeMutable {

    BTreeMutable mainTree;

    @NotNull
    ByteIterable key;
    long address = Loggable.NULL_ADDRESS;

    BTreeDupMutable(@NotNull BTreeBalancePolicy balancePolicy, @NotNull Log log, long structureId,
                    @NotNull BTreeBase dupTree, @NotNull ByteIterable key) {
        super(balancePolicy, log, structureId, false, dupTree);
        size = dupTree.size;
        this.key = key;
    }

    @Override
    protected void addExpiredLoggable(@NotNull Loggable loggable) {
        mainTree.addExpiredLoggable(loggable);
    }

    @Override
    protected void addExpiredLoggable(long address) {
        mainTree.addExpiredLoggable(address);
    }

    @Override
    protected void decrementSize(final long delta) {
        super.decrementSize(delta);
        mainTree.decrementSize(delta);
    }

    @Override
    protected void incrementSize() {
        super.incrementSize();
        mainTree.incrementSize();
    }

    @Override
    public long save() {
        if (address != Loggable.NULL_ADDRESS) {
            throw new IllegalStateException("Duplicates sub-tree already saved");
        }
        final BasePageMutable rootPage = getRoot();
        final byte type = rootPage.isBottom() ? BTreeBase.LEAF_DUP_BOTTOM_ROOT : BTreeBase.LEAF_DUP_INTERNAL_ROOT;
        final ByteIterable keyIterable = CompressedUnsignedLongByteIterable.getIterable(key.getLength());
        ByteIterable sizeIterable;
        long startAddress = log.getHighAddress(); // remember high address before saving the data
        final ByteIterable rootDataIterable = rootPage.getData();
        ByteIterable[] iterables;
        long result;
        final boolean canReTry;
        if (log.isLastFileAddress(startAddress)) {
            sizeIterable = CompressedUnsignedLongByteIterable.getIterable(size << 1);
            iterables = new ByteIterable[]{keyIterable, key, sizeIterable, rootDataIterable};
            result = log.tryWrite(new LoggableToWrite(type, new CompoundByteIterable(iterables), structureId));
            if (result >= 0) {
                address = result;
                return result;
            } else {
                canReTry = false;
            }
        } else {
            canReTry = true;
        }
        if (log.read(startAddress).getType() == NullLoggable.TYPE) {
            final long lengthBound = log.getFileLengthBound();
            startAddress += (lengthBound - startAddress % lengthBound);
        }
        sizeIterable = CompressedUnsignedLongByteIterable.getIterable((size << 1) + 1);
        final ByteIterable offsetIterable =
                CompressedUnsignedLongByteIterable.getIterable(log.getHighAddress() - startAddress);
        iterables = new ByteIterable[]{keyIterable, key, sizeIterable, offsetIterable, rootDataIterable};
        final Loggable loggable = new LoggableToWrite(type, new CompoundByteIterable(iterables), structureId);
        result = canReTry ? log.tryWrite(loggable) : log.writeContinuously(loggable);
        if (result < 0) {
            if (canReTry) {
                iterables[3] = CompressedUnsignedLongByteIterable.getIterable(log.getHighAddress() - startAddress);
                result = log.writeContinuously(new LoggableToWrite(type, new CompoundByteIterable(iterables), structureId));

                if (result < 0) {
                    throw new TooBigLoggableException();
                }
            } else {
                throw new TooBigLoggableException();
            }
        }
        address = result;
        return result;
    }

    @Override
    protected byte getBottomPageType() {
        return DUP_BOTTOM;
    }<commit_msg>minor<commit_after>/**
 * Copyright 2010 - 2014 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 jetbrains.exodus.tree.btree;

import jetbrains.exodus.ByteIterable;
import jetbrains.exodus.log.*;
import jetbrains.exodus.log.iterate.CompoundByteIterable;
import jetbrains.exodus.log.iterate.CompressedUnsignedLongByteIterable;
import org.jetbrains.annotations.NotNull;

final class BTreeDupMutable extends BTreeMutable {

    BTreeMutable mainTree;

    @NotNull
    ByteIterable key;
    long address = Loggable.NULL_ADDRESS;

    BTreeDupMutable(@NotNull BTreeBalancePolicy balancePolicy, @NotNull Log log, long structureId,
                    @NotNull BTreeBase dupTree, @NotNull ByteIterable key) {
        super(balancePolicy, log, structureId, false, dupTree);
        size = dupTree.size;
        this.key = key;
    }

    @Override
    protected void addExpiredLoggable(@NotNull Loggable loggable) {
        mainTree.addExpiredLoggable(loggable);
    }

    @Override
    protected void addExpiredLoggable(long address) {
        mainTree.addExpiredLoggable(address);
    }

    @Override
    protected void decrementSize(final long delta) {
        super.decrementSize(delta);
        mainTree.decrementSize(delta);
    }

    @Override
    protected void incrementSize() {
        super.incrementSize();
        mainTree.incrementSize();
    }

    @Override
    public long save() {
        if (address != Loggable.NULL_ADDRESS) {
            throw new IllegalStateException("Duplicates sub-tree already saved");
        }
        final BasePageMutable rootPage = getRoot();
        final byte type = rootPage.isBottom() ? BTreeBase.LEAF_DUP_BOTTOM_ROOT : BTreeBase.LEAF_DUP_INTERNAL_ROOT;
        final ByteIterable keyIterable = CompressedUnsignedLongByteIterable.getIterable(key.getLength());
        ByteIterable sizeIterable;
        long startAddress = log.getHighAddress(); // remember high address before saving the data
        final ByteIterable rootDataIterable = rootPage.getData();
        ByteIterable[] iterables;
        long result;
        final boolean canRetry;
        if (log.isLastFileAddress(startAddress)) {
            sizeIterable = CompressedUnsignedLongByteIterable.getIterable(size << 1);
            iterables = new ByteIterable[]{keyIterable, key, sizeIterable, rootDataIterable};
            result = log.tryWrite(new LoggableToWrite(type, new CompoundByteIterable(iterables), structureId));
            if (result >= 0) {
                address = result;
                return result;
            } else {
                canRetry = false;
            }
        } else {
            canRetry = true;
        }
        if (NullLoggable.isNullLoggable(log.read(startAddress))) {
            final long lengthBound = log.getFileLengthBound();
            startAddress += (lengthBound - startAddress % lengthBound);
        }
        sizeIterable = CompressedUnsignedLongByteIterable.getIterable((size << 1) + 1);
        final ByteIterable offsetIterable =
                CompressedUnsignedLongByteIterable.getIterable(log.getHighAddress() - startAddress);
        iterables = new ByteIterable[]{keyIterable, key, sizeIterable, offsetIterable, rootDataIterable};
        final Loggable loggable = new LoggableToWrite(type, new CompoundByteIterable(iterables), structureId);
        result = canRetry ? log.tryWrite(loggable) : log.writeContinuously(loggable);
        if (result < 0) {
            if (canRetry) {
                iterables[3] = CompressedUnsignedLongByteIterable.getIterable(log.getHighAddress() - startAddress);
                result = log.writeContinuously(new LoggableToWrite(type, new CompoundByteIterable(iterables), structureId));

                if (result < 0) {
                    throw new TooBigLoggableException();
                }
            } else {
                throw new TooBigLoggableException();
            }
        }
        address = result;
        return result;
    }

    @Override
    protected byte getBottomPageType() {
        return DUP_BOTTOM;
    }
{'fraction_non_alphanumeric': 0.08353299934654759, 'fraction_numerical': 0.004138531910259203, 'mean_word_length': 2.769704433497537, 'pattern_counts': {'":': 0, '<': 15, '<?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': '1221279', 'n_tokens_mistral': 2578, 'n_tokens_neox': 2467, 'n_words': 623}