Search in sources :

Example 1 with SQLExceptionCode

use of org.apache.phoenix.exception.SQLExceptionCode in project phoenix by apache.

the class ConnectionlessQueryServicesImpl method incrementSequences.

@Override
public void incrementSequences(List<SequenceAllocation> sequenceAllocations, long timestamp, long[] values, SQLException[] exceptions) throws SQLException {
    int i = 0;
    for (SequenceAllocation sequenceAllocation : sequenceAllocations) {
        SequenceKey key = sequenceAllocation.getSequenceKey();
        SequenceInfo info = sequenceMap.get(key);
        if (info == null) {
            exceptions[i] = new SequenceNotFoundException(key.getSchemaName(), key.getSequenceName());
        } else {
            boolean increaseSeq = info.incrementBy > 0;
            if (info.limitReached) {
                SQLExceptionCode code = increaseSeq ? SQLExceptionCode.SEQUENCE_VAL_REACHED_MAX_VALUE : SQLExceptionCode.SEQUENCE_VAL_REACHED_MIN_VALUE;
                exceptions[i] = new SQLExceptionInfo.Builder(code).build().buildException();
            } else {
                values[i] = info.sequenceValue;
                info.sequenceValue += info.incrementBy * info.cacheSize;
                info.limitReached = SequenceUtil.checkIfLimitReached(info);
                if (info.limitReached && info.cycle) {
                    info.sequenceValue = increaseSeq ? info.minValue : info.maxValue;
                    info.limitReached = false;
                }
            }
        }
        i++;
    }
    i = 0;
    for (SQLException e : exceptions) {
        if (e != null) {
            sequenceMap.remove(sequenceAllocations.get(i).getSequenceKey());
        }
        i++;
    }
}
Also used : SQLExceptionCode(org.apache.phoenix.exception.SQLExceptionCode) SequenceKey(org.apache.phoenix.schema.SequenceKey) SequenceInfo(org.apache.phoenix.schema.SequenceInfo) SQLException(java.sql.SQLException) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) SequenceAllocation(org.apache.phoenix.schema.SequenceAllocation) SQLExceptionInfo(org.apache.phoenix.exception.SQLExceptionInfo)

Example 2 with SQLExceptionCode

use of org.apache.phoenix.exception.SQLExceptionCode in project phoenix by apache.

the class Sequence method dropSequence.

public long dropSequence(Result result) throws SQLException {
    Cell statusKV = result.rawCells()[0];
    long timestamp = statusKV.getTimestamp();
    int statusCode = PInteger.INSTANCE.getCodec().decodeInt(statusKV.getValueArray(), statusKV.getValueOffset(), SortOrder.getDefault());
    SQLExceptionCode code = statusCode == 0 ? null : SQLExceptionCode.fromErrorCode(statusCode);
    if (code == null) {
        // Insert delete marker so that point-in-time sequences work
        insertSequenceValue(new SequenceValue(timestamp, true));
        return timestamp;
    }
    // }
    throw new SQLExceptionInfo.Builder(code).setSchemaName(key.getSchemaName()).setTableName(key.getSequenceName()).build().buildException();
}
Also used : SQLExceptionCode(org.apache.phoenix.exception.SQLExceptionCode) Cell(org.apache.hadoop.hbase.Cell)

Example 3 with SQLExceptionCode

use of org.apache.phoenix.exception.SQLExceptionCode in project phoenix by apache.

the class Sequence method returnValue.

public boolean returnValue(Result result) throws SQLException {
    Cell statusKV = result.rawCells()[0];
    if (statusKV.getValueLength() == 0) {
        // No error, but unable to return sequence values
        return false;
    }
    long timestamp = statusKV.getTimestamp();
    int statusCode = PInteger.INSTANCE.getCodec().decodeInt(statusKV.getValueArray(), statusKV.getValueOffset(), SortOrder.getDefault());
    if (statusCode == SUCCESS) {
        // Success - update nextValue down to currentValue
        SequenceValue value = findSequenceValue(timestamp);
        if (value == null) {
            throw new EmptySequenceCacheException(key.getSchemaName(), key.getSequenceName());
        }
        return true;
    }
    SQLExceptionCode code = SQLExceptionCode.fromErrorCode(statusCode);
    // }
    throw new SQLExceptionInfo.Builder(code).setSchemaName(key.getSchemaName()).setTableName(key.getSequenceName()).build().buildException();
}
Also used : SQLExceptionCode(org.apache.phoenix.exception.SQLExceptionCode) Cell(org.apache.hadoop.hbase.Cell)

Example 4 with SQLExceptionCode

use of org.apache.phoenix.exception.SQLExceptionCode in project phoenix by apache.

the class Sequence method increment.

private long increment(SequenceValue value, ValueOp op, long numToAllocate) throws SQLException {
    boolean increasingSeq = value.incrementBy > 0 && op != ValueOp.VALIDATE_SEQUENCE;
    // check if the the sequence has already reached the min/max limit
    if (value.limitReached && op != ValueOp.VALIDATE_SEQUENCE) {
        if (value.cycle) {
            value.limitReached = false;
            throw EMPTY_SEQUENCE_CACHE_EXCEPTION;
        } else {
            SQLExceptionCode code = increasingSeq ? SQLExceptionCode.SEQUENCE_VAL_REACHED_MAX_VALUE : SQLExceptionCode.SEQUENCE_VAL_REACHED_MIN_VALUE;
            throw SequenceUtil.getException(this.key.getSchemaName(), this.key.getSequenceName(), code);
        }
    }
    long returnValue = value.currentValue;
    if (op == ValueOp.INCREMENT_SEQUENCE) {
        boolean overflowOrUnderflow = false;
        // advance currentValue while checking for overflow
        try {
            // advance by numToAllocate * the increment amount
            value.currentValue = LongMath.checkedAdd(value.currentValue, numToAllocate * value.incrementBy);
        } catch (ArithmeticException e) {
            overflowOrUnderflow = true;
        }
        // if overflow or limit was reached
        if (overflowOrUnderflow || (increasingSeq && value.currentValue > value.maxValue) || (!increasingSeq && value.currentValue < value.minValue)) {
            value.limitReached = true;
        }
    }
    return returnValue;
}
Also used : SQLExceptionCode(org.apache.phoenix.exception.SQLExceptionCode)

Example 5 with SQLExceptionCode

use of org.apache.phoenix.exception.SQLExceptionCode in project phoenix by apache.

the class Sequence method createSequence.

public long createSequence(Result result, long minValue, long maxValue, boolean cycle) throws SQLException {
    Cell statusKV = result.rawCells()[0];
    long timestamp = statusKV.getTimestamp();
    int statusCode = PInteger.INSTANCE.getCodec().decodeInt(statusKV.getValueArray(), statusKV.getValueOffset(), SortOrder.getDefault());
    if (statusCode == 0) {
        // Success - add sequence value and return timestamp
        SequenceValue value = new SequenceValue(timestamp, minValue, maxValue, cycle);
        insertSequenceValue(value);
        return timestamp;
    }
    SQLExceptionCode code = SQLExceptionCode.fromErrorCode(statusCode);
    throw new SQLExceptionInfo.Builder(code).setSchemaName(key.getSchemaName()).setTableName(key.getSequenceName()).build().buildException();
}
Also used : SQLExceptionCode(org.apache.phoenix.exception.SQLExceptionCode) Cell(org.apache.hadoop.hbase.Cell)

Aggregations

SQLExceptionCode (org.apache.phoenix.exception.SQLExceptionCode)8 Cell (org.apache.hadoop.hbase.Cell)4 SQLException (java.sql.SQLException)1 Matcher (java.util.regex.Matcher)1 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)1 SequenceAllocation (org.apache.phoenix.schema.SequenceAllocation)1 SequenceInfo (org.apache.phoenix.schema.SequenceInfo)1 SequenceKey (org.apache.phoenix.schema.SequenceKey)1 SequenceNotFoundException (org.apache.phoenix.schema.SequenceNotFoundException)1 Test (org.junit.Test)1