Search in sources :

Example 1 with KeyMeta

use of org.apache.bookkeeper.stream.proto.kv.store.KeyMeta in project bookkeeper by apache.

the class MVCCRecordCoder method getSerializedSize.

@Override
public int getSerializedSize(MVCCRecord record) {
    KeyMeta meta = KeyMeta.newBuilder().setCreateRevision(record.getCreateRev()).setModRevision(record.getModRev()).setVersion(record.getVersion()).setValueType(record.getValueType()).build();
    int metaLen = meta.getSerializedSize();
    int valLen = record.getValue().readableBytes();
    return // meta len
    Integer.BYTES + // meta bytes
    metaLen + // val len
    Integer.BYTES + // val bytes
    valLen;
}
Also used : KeyMeta(org.apache.bookkeeper.stream.proto.kv.store.KeyMeta)

Example 2 with KeyMeta

use of org.apache.bookkeeper.stream.proto.kv.store.KeyMeta in project bookkeeper by apache.

the class MVCCRecordCoder method decode.

@Override
public MVCCRecord decode(ByteBuf data) {
    ByteBuf copy = data.slice();
    int metaLen = copy.readInt();
    ByteBuffer metaBuf = copy.slice(copy.readerIndex(), metaLen).nioBuffer();
    KeyMeta meta;
    try {
        meta = KeyMeta.parseFrom(metaBuf);
    } catch (InvalidProtocolBufferException e) {
        throw new StateStoreRuntimeException("Failed to deserialize key metadata", e);
    }
    copy.skipBytes(metaLen);
    int valLen = copy.readInt();
    ByteBuf valBuf = copy.retainedSlice(copy.readerIndex(), valLen);
    MVCCRecord record = MVCCRecord.newRecord();
    record.setCreateRev(meta.getCreateRevision());
    record.setModRev(meta.getModRevision());
    record.setVersion(meta.getVersion());
    record.setValue(valBuf, meta.getValueType());
    return record;
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ByteBuf(io.netty.buffer.ByteBuf) ByteBuffer(java.nio.ByteBuffer) KeyMeta(org.apache.bookkeeper.stream.proto.kv.store.KeyMeta) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Example 3 with KeyMeta

use of org.apache.bookkeeper.stream.proto.kv.store.KeyMeta in project bookkeeper by apache.

the class MVCCRecordCoder method encode.

@Override
public byte[] encode(MVCCRecord record) {
    KeyMeta meta = KeyMeta.newBuilder().setCreateRevision(record.getCreateRev()).setModRevision(record.getModRev()).setVersion(record.getVersion()).setValueType(record.getValueType()).build();
    int metaLen = meta.getSerializedSize();
    int valLen = record.getValue().readableBytes();
    int totalLen = // meta len
    Integer.BYTES + // meta bytes
    metaLen + // val len
    Integer.BYTES + // val bytes
    valLen;
    // NOTE: currently rocksdb jni only supports `byte[]`
    // we can improve this if rocksdb jni support ByteBuffer or ByteBuf
    byte[] data = new byte[totalLen];
    ByteBuf buf = Unpooled.wrappedBuffer(data);
    buf.writerIndex(0);
    buf.writeInt(metaLen);
    CodedOutputStream out = CodedOutputStream.newInstance(data, Integer.BYTES, metaLen);
    try {
        meta.writeTo(out);
    } catch (IOException e) {
        throw new StateStoreRuntimeException("Failed to serialize key metadata", e);
    }
    buf.writerIndex(buf.writerIndex() + metaLen);
    buf.writeInt(valLen);
    buf.writeBytes(record.getValue().slice());
    buf.release();
    return data;
}
Also used : CodedOutputStream(com.google.protobuf.CodedOutputStream) IOException(java.io.IOException) ByteBuf(io.netty.buffer.ByteBuf) KeyMeta(org.apache.bookkeeper.stream.proto.kv.store.KeyMeta) StateStoreRuntimeException(org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)

Aggregations

KeyMeta (org.apache.bookkeeper.stream.proto.kv.store.KeyMeta)3 ByteBuf (io.netty.buffer.ByteBuf)2 StateStoreRuntimeException (org.apache.bookkeeper.statelib.api.exceptions.StateStoreRuntimeException)2 CodedOutputStream (com.google.protobuf.CodedOutputStream)1 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)1 IOException (java.io.IOException)1 ByteBuffer (java.nio.ByteBuffer)1