All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.tarantool.driver.api.cursor.TarantoolCursorBase Maven / Gradle / Ivy

Go to download

Tarantool Cartridge driver for Tarantool versions 1.10+ based on Netty framework

There is a newer version: 0.14.0
Show newest version
package io.tarantool.driver.api.cursor;

import io.tarantool.driver.exceptions.TarantoolClientException;
import io.tarantool.driver.exceptions.TarantoolSpaceOperationException;
import io.tarantool.driver.protocol.Packable;

import java.util.Collection;

/**
 * Generic cursor implementation that performs client calls
 * with TarantoolSpaceOperations.
 *
 * @author Vladimir Rogach
 */
public abstract class TarantoolCursorBase>
    implements TarantoolCursor {

    protected abstract void fetchNextTuples();

    protected abstract boolean advanceIterator();

    protected abstract T getCurrentValue();

    /**
     * If batchSize is less than condition limit
     * we need to recalculate limit for each batch.
     *
     * @param conditionLimit initial limit (specified in condition)
     * @param batchSize      size of a batch
     * @param spaceOffset    current count of fetched elements
     * @return effective limit for the current batch
     */
    protected static long calcLimit(long conditionLimit, long batchSize, long spaceOffset) {
        if (conditionLimit > 0) {
            long tuplesToFetchLeft = conditionLimit - spaceOffset;
            if (tuplesToFetchLeft < batchSize) {
                return tuplesToFetchLeft;
            }
        }
        return batchSize;
    }

    @Override
    public boolean next() throws TarantoolClientException {
        if (!advanceIterator()) {
            fetchNextTuples();
            return advanceIterator();
        }
        return true;
    }

    @Override
    public T get() throws TarantoolSpaceOperationException {
        if (getCurrentValue() == null) {
            throw new TarantoolSpaceOperationException(
                "Unexpected cursor state: check that next() is called");
        }
        return getCurrentValue();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy