Search in sources :

Example 6 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class MVCCStoreImpl method put.

private PutResult<K, V> put(long revision, WriteBatch batch, PutOp<K, V> op) {
    // parameters
    final K key = op.key();
    final V val = op.value();
    // raw key & value
    final byte[] rawKey = keyCoder.encode(key);
    final ByteBuf rawValBuf = valCoder.encodeBuf(val);
    MVCCRecord record;
    try {
        record = getKeyRecord(key, rawKey);
    } catch (StateStoreRuntimeException e) {
        rawValBuf.release();
        throw e;
    }
    // result
    final PutResultImpl<K, V> result = resultFactory.newPutResult(revision);
    MVCCRecord oldRecord = null;
    try {
        if (null != record) {
            // validate the update revision before applying the update to the record
            if (record.compareModRev(revision) >= 0) {
                result.code(Code.SMALLER_REVISION);
                return result;
            }
            if (ValueType.BYTES != record.getValueType()) {
                result.code(Code.ILLEGAL_OP);
                return result;
            }
            if (op.option().prevKv()) {
                // make a copy before modification
                oldRecord = record.duplicate();
            }
            record.setVersion(record.getVersion() + 1);
        } else {
            record = MVCCRecord.newRecord();
            record.setCreateRev(revision);
            record.setVersion(0);
        }
        record.setValue(rawValBuf, ValueType.BYTES);
        record.setModRev(revision);
        // write the mvcc record back
        batch.put(dataCfHandle, rawKey, recordCoder.encode(record));
        // finalize the result
        result.code(Code.OK);
        if (null != oldRecord) {
            KeyValueImpl<K, V> prevKV = oldRecord.asKVRecord(recordFactory, key, valCoder);
            result.prevKv(prevKV);
        }
        return result;
    } catch (StateStoreRuntimeException e) {
        result.close();
        throw e;
    } finally {
        if (null != record) {
            record.recycle();
        }
        if (null != oldRecord) {
            oldRecord.recycle();
        }
    }
}
Also used : KV(org.apache.bookkeeper.common.kv.KV) ByteBuf(io.netty.buffer.ByteBuf) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Example 7 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class MVCCStoreImpl method processPut.

synchronized PutResult<K, V> processPut(long revision, PutOp<K, V> op) {
    checkStoreOpen();
    WriteBatch batch = new WriteBatch();
    PutResult<K, V> result = null;
    try {
        result = put(revision, batch, op);
        executeBatch(batch);
        return result;
    } catch (StateStoreRuntimeException e) {
        if (null != result) {
            result.close();
        }
        throw e;
    } finally {
        RocksUtils.close(batch);
    }
}
Also used : KV(org.apache.bookkeeper.common.kv.KV) WriteBatch(org.rocksdb.WriteBatch) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Example 8 with StateStoreRuntimeException

use of org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException in project bookkeeper by apache.

the class MVCCStoreImpl method processDelete.

synchronized DeleteResult<K, V> processDelete(long revision, DeleteOp<K, V> op) {
    checkStoreOpen();
    WriteBatch batch = new WriteBatch();
    DeleteResult<K, V> result = null;
    try {
        result = delete(revision, batch, op, true);
        executeBatch(batch);
        return result;
    } catch (StateStoreRuntimeException e) {
        if (null != result) {
            result.close();
        }
        throw e;
    } finally {
        RocksUtils.close(batch);
    }
}
Also used : KV(org.apache.bookkeeper.common.kv.KV) WriteBatch(org.rocksdb.WriteBatch) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Aggregations

StateStoreRuntimeException (org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)8 KV (org.apache.bookkeeper.common.kv.KV)5 WriteBatch (org.rocksdb.WriteBatch)5 ByteBuf (io.netty.buffer.ByteBuf)3 KeyMeta (org.apache.bookkeeper.stream.proto.kv.store.KeyMeta)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1 CompareOp (org.apache.bookkeeper.api.kv.op.CompareOp)1 CompareResult (org.apache.bookkeeper.api.kv.op.CompareResult)1 DeleteOp (org.apache.bookkeeper.api.kv.op.DeleteOp)1 IncrementOp (org.apache.bookkeeper.api.kv.op.IncrementOp)1 Op (org.apache.bookkeeper.api.kv.op.Op)1 PutOp (org.apache.bookkeeper.api.kv.op.PutOp)1 RangeOp (org.apache.bookkeeper.api.kv.op.RangeOp)1 TxnOp (org.apache.bookkeeper.api.kv.op.TxnOp)1 DeleteResult (org.apache.bookkeeper.api.kv.result.DeleteResult)1 IncrementResult (org.apache.bookkeeper.api.kv.result.IncrementResult)1 PutResult (org.apache.bookkeeper.api.kv.result.PutResult)1