<commit_before>/**
 * Copyright 2010 - 2015 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.entitystore.iterate;

import jetbrains.exodus.ExodusException;
import jetbrains.exodus.entitystore.*;
import jetbrains.exodus.entitystore.util.EntityIdSet;
import jetbrains.exodus.env.Cursor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.NoSuchElementException;

@SuppressWarnings({"AssignmentToStaticFieldFromInstanceMethod"})
public abstract class EntityIteratorBase implements EntityIterator {

    public static final EntityIteratorBase EMPTY;

    static {
        EMPTY = new NonDisposableEntityIterator(EntityIterableBase.EMPTY) {

            @Override
            public boolean skip(int number) {
                return false;
            }

            protected boolean hasNextImpl() {
                return false;
            }

            @Override
            @Nullable
            public EntityId nextIdImpl() {
                return null;
            }
        };
    }

    private static int nextIdCounter = 0;

    @NotNull
    private final EntityIterableBase iterable;
    private boolean finished;
    private boolean disposed;
    private Cursor cursor;

    protected EntityIteratorBase(@NotNull final EntityIterableBase iterable) {
        this.iterable = iterable;
        cursor = null;
        finished = iterable == EntityIterableBase.EMPTY;
        disposed = false;
    }

    @NotNull
    public EntityIterableBase getIterable() {
        return iterable;
    }

    @Override
    public final boolean hasNext() {
        try {
            if (finished) {
                return false;
            }
            checkDisposed();
            final boolean result = hasNextImpl();
            if (!result) {
                finished = true;
                disposeIfShouldBe();
            }
            return result;
        } catch (ExodusException e) {
            disposeIfShouldBe();
            throw e;
        }
    }

    @Override
    @Nullable
    public Entity next() {
        throwNoSuchElementExceptionIfNecessary();
        checkDisposed();
        final EntityId nextEntityId = nextId();
        return nextEntityId == null ? null : iterable.getEntity(nextEntityId);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("EntityIterator.remove() is not supported.");
    }

    @Override
    public boolean skip(int number) {
        if (finished) {
            return false;
        }
        checkDisposed();
        try {
            while (number-- > 0 && hasNextImpl()) {
                nextIdImpl();
            }
            return hasNextImpl();
        } catch (ExodusException e) {
            disposeIfShouldBe();
            throw e;
        }
    }

    @Override
    @Nullable
    public EntityId nextId() {
        throwNoSuchElementExceptionIfNecessary();
        try {
            if ((++nextIdCounter & 0xff) == 0) {
                // do not check QueryCancellingPolicy too often
                final QueryCancellingPolicy cancellingPolicy = iterable.getTransaction().getQueryCancellingPolicy();
                if (cancellingPolicy != null && cancellingPolicy.needToCancel()) {
                    cancellingPolicy.doCancel();
                }
            }
            return nextIdImpl();
        } catch (ExodusException e) {
            disposeIfShouldBe();
            throw e;
        }
    }

    @Nullable
    public EntityId getLast() {
        EntityId result = null;
        while (hasNext()) {
            result = nextId();<commit_msg>periodic checking QueryCancellingPolicy without using of static counter<commit_after>/**
 * Copyright 2010 - 2015 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.entitystore.iterate;

import jetbrains.exodus.ExodusException;
import jetbrains.exodus.entitystore.*;
import jetbrains.exodus.entitystore.util.EntityIdSet;
import jetbrains.exodus.env.Cursor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.NoSuchElementException;

@SuppressWarnings({"AssignmentToStaticFieldFromInstanceMethod"})
public abstract class EntityIteratorBase implements EntityIterator {

    public static final EntityIteratorBase EMPTY;

    static {
        EMPTY = new NonDisposableEntityIterator(EntityIterableBase.EMPTY) {

            @Override
            public boolean skip(int number) {
                return false;
            }

            protected boolean hasNextImpl() {
                return false;
            }

            @Override
            @Nullable
            public EntityId nextIdImpl() {
                return null;
            }
        };
    }

    @NotNull
    private final EntityIterableBase iterable;
    private boolean finished;
    private boolean disposed;
    private Cursor cursor;

    protected EntityIteratorBase(@NotNull final EntityIterableBase iterable) {
        this.iterable = iterable;
        cursor = null;
        finished = iterable == EntityIterableBase.EMPTY;
        disposed = false;
    }

    @NotNull
    public EntityIterableBase getIterable() {
        return iterable;
    }

    @Override
    public final boolean hasNext() {
        try {
            if (finished) {
                return false;
            }
            checkDisposed();
            final boolean result = hasNextImpl();
            if (!result) {
                finished = true;
                disposeIfShouldBe();
            }
            return result;
        } catch (ExodusException e) {
            disposeIfShouldBe();
            throw e;
        }
    }

    @Override
    @Nullable
    public Entity next() {
        throwNoSuchElementExceptionIfNecessary();
        checkDisposed();
        final EntityId nextEntityId = nextId();
        return nextEntityId == null ? null : iterable.getEntity(nextEntityId);
    }

    @Override
    public void remove() {
        throw new UnsupportedOperationException("EntityIterator.remove() is not supported.");
    }

    @Override
    public boolean skip(int number) {
        if (finished) {
            return false;
        }
        checkDisposed();
        try {
            while (number-- > 0 && hasNextImpl()) {
                nextIdImpl();
            }
            return hasNextImpl();
        } catch (ExodusException e) {
            disposeIfShouldBe();
            throw e;
        }
    }

    @Override
    @Nullable
    public EntityId nextId() {
        throwNoSuchElementExceptionIfNecessary();
        try {
            final EntityId result = nextIdImpl();
            if ((System.identityHashCode(result) & 0x1ff) == 314) {
                // do not check QueryCancellingPolicy too often
                final QueryCancellingPolicy cancellingPolicy = iterable.getTransaction().getQueryCancellingPolicy();
                if (cancellingPolicy != null && cancellingPolicy.needToCancel()) {
                    cancellingPolicy.doCancel();
                }
            }
            return result;
        } catch (ExodusException e) {
            disposeIfShouldBe();
            throw e;
        }
    }

    @Nullable
    public EntityId getLast() {
        EntityId result = null;
        while (hasNext()) {
            result = nextId();
{'fraction_non_alphanumeric': 0.08002378121284186, 'fraction_numerical': 0.004042806183115339, 'mean_word_length': 2.039754246476328, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3755323', 'n_tokens_mistral': 2185, 'n_tokens_neox': 2067, 'n_words': 599}