Search in sources :

Example 1 with SequenceInfo

use of org.apache.phoenix.schema.SequenceInfo 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 SequenceInfo

use of org.apache.phoenix.schema.SequenceInfo in project phoenix by apache.

the class ConnectionlessQueryServicesImpl method createSequence.

@Override
public long createSequence(String tenantId, String schemaName, String sequenceName, long startWith, long incrementBy, long cacheSize, long minValue, long maxValue, boolean cycle, long timestamp) throws SQLException {
    SequenceKey key = new SequenceKey(tenantId, schemaName, sequenceName, getSequenceSaltBuckets());
    if (sequenceMap.get(key) != null) {
        throw new SequenceAlreadyExistsException(schemaName, sequenceName);
    }
    sequenceMap.put(key, new SequenceInfo(startWith, incrementBy, minValue, maxValue, 1l, cycle));
    return timestamp;
}
Also used : SequenceKey(org.apache.phoenix.schema.SequenceKey) SequenceInfo(org.apache.phoenix.schema.SequenceInfo) SequenceAlreadyExistsException(org.apache.phoenix.schema.SequenceAlreadyExistsException)

Example 3 with SequenceInfo

use of org.apache.phoenix.schema.SequenceInfo in project phoenix by apache.

the class ConnectionlessQueryServicesImpl method validateSequences.

@Override
public void validateSequences(List<SequenceAllocation> sequenceAllocations, long timestamp, long[] values, SQLException[] exceptions, Sequence.ValueOp action) throws SQLException {
    int i = 0;
    for (SequenceAllocation sequenceAllocation : sequenceAllocations) {
        SequenceInfo info = sequenceMap.get(sequenceAllocation.getSequenceKey());
        if (info == null) {
            exceptions[i] = new SequenceNotFoundException(sequenceAllocation.getSequenceKey().getSchemaName(), sequenceAllocation.getSequenceKey().getSequenceName());
        } else {
            values[i] = info.sequenceValue;
        }
        i++;
    }
}
Also used : SequenceInfo(org.apache.phoenix.schema.SequenceInfo) SequenceNotFoundException(org.apache.phoenix.schema.SequenceNotFoundException) SequenceAllocation(org.apache.phoenix.schema.SequenceAllocation)

Aggregations

SequenceInfo (org.apache.phoenix.schema.SequenceInfo)3 SequenceAllocation (org.apache.phoenix.schema.SequenceAllocation)2 SequenceKey (org.apache.phoenix.schema.SequenceKey)2 SequenceNotFoundException (org.apache.phoenix.schema.SequenceNotFoundException)2 SQLException (java.sql.SQLException)1 SQLExceptionCode (org.apache.phoenix.exception.SQLExceptionCode)1 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)1 SequenceAlreadyExistsException (org.apache.phoenix.schema.SequenceAlreadyExistsException)1