use of com.alipay.sofa.jraft.entity.codec.v2.LogOutter.PBLogEntry in project dingo by dingodb.
the class V2Encoder method encode.
@Override
public byte[] encode(final LogEntry log) {
Requires.requireNonNull(log, "Null log");
final LogId logId = log.getId();
final PBLogEntry.Builder builder = //
PBLogEntry.newBuilder().setType(//
log.getType()).setIndex(//
logId.getIndex()).setTerm(logId.getTerm());
final List<PeerId> peers = log.getPeers();
if (hasPeers(peers)) {
encodePeers(builder, peers);
}
final List<PeerId> oldPeers = log.getOldPeers();
if (hasPeers(oldPeers)) {
encodeOldPeers(builder, oldPeers);
}
final List<PeerId> learners = log.getLearners();
if (hasPeers(learners)) {
encodeLearners(builder, learners);
}
final List<PeerId> oldLearners = log.getOldLearners();
if (hasPeers(oldLearners)) {
encodeOldLearners(builder, oldLearners);
}
if (log.hasChecksum()) {
builder.setChecksum(log.getChecksum());
}
builder.setData(log.getData() != null ? ZeroByteStringHelper.wrap(log.getData()) : ByteString.EMPTY);
final PBLogEntry pbLogEntry = builder.build();
final int bodyLen = pbLogEntry.getSerializedSize();
final byte[] ret = new byte[LogEntryV2CodecFactory.HEADER_SIZE + bodyLen];
// write header
int i = 0;
for (; i < LogEntryV2CodecFactory.MAGIC_BYTES.length; i++) {
ret[i] = LogEntryV2CodecFactory.MAGIC_BYTES[i];
}
ret[i++] = LogEntryV2CodecFactory.VERSION;
// avoid memory copy for only 3 bytes
for (; i < LogEntryV2CodecFactory.HEADER_SIZE; i++) {
ret[i] = LogEntryV2CodecFactory.RESERVED[i - LogEntryV2CodecFactory.MAGIC_BYTES.length - 1];
}
// write body
writeToByteArray(pbLogEntry, ret, i, bodyLen);
return ret;
}
use of com.alipay.sofa.jraft.entity.codec.v2.LogOutter.PBLogEntry in project sofa-jraft by sofastack.
the class V2Decoder method decode.
@Override
public LogEntry decode(final byte[] bs) {
if (bs == null || bs.length < LogEntryV2CodecFactory.HEADER_SIZE) {
return null;
}
int i = 0;
for (; i < LogEntryV2CodecFactory.MAGIC_BYTES.length; i++) {
if (bs[i] != LogEntryV2CodecFactory.MAGIC_BYTES[i]) {
return null;
}
}
if (bs[i++] != LogEntryV2CodecFactory.VERSION) {
return null;
}
// Ignored reserved
i += LogEntryV2CodecFactory.RESERVED.length;
try {
final PBLogEntry entry = PBLogEntry.parseFrom(ZeroByteStringHelper.wrap(bs, i, bs.length - i));
final LogEntry log = new LogEntry();
log.setType(entry.getType());
log.getId().setIndex(entry.getIndex());
log.getId().setTerm(entry.getTerm());
if (entry.hasChecksum()) {
log.setChecksum(entry.getChecksum());
}
if (entry.getPeersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getPeersCount());
for (final ByteString bstring : entry.getPeersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setPeers(peers);
}
if (entry.getOldPeersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getOldPeersCount());
for (final ByteString bstring : entry.getOldPeersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setOldPeers(peers);
}
if (entry.getLearnersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getLearnersCount());
for (final ByteString bstring : entry.getLearnersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setLearners(peers);
}
if (entry.getOldLearnersCount() > 0) {
final List<PeerId> peers = new ArrayList<>(entry.getOldLearnersCount());
for (final ByteString bstring : entry.getOldLearnersList()) {
peers.add(JRaftUtils.getPeerId(AsciiStringUtil.unsafeDecode(bstring)));
}
log.setOldLearners(peers);
}
final ByteString data = entry.getData();
if (!data.isEmpty()) {
log.setData(ByteBuffer.wrap(ZeroByteStringHelper.getByteArray(data)));
}
return log;
} catch (final InvalidProtocolBufferException e) {
LOG.error("Fail to decode pb log entry", e);
return null;
}
}
use of com.alipay.sofa.jraft.entity.codec.v2.LogOutter.PBLogEntry in project sofa-jraft by sofastack.
the class V2Encoder method encode.
@Override
public byte[] encode(final LogEntry log) {
Requires.requireNonNull(log, "Null log");
final LogId logId = log.getId();
final PBLogEntry.Builder builder = //
PBLogEntry.newBuilder().setType(//
log.getType()).setIndex(//
logId.getIndex()).setTerm(logId.getTerm());
final List<PeerId> peers = log.getPeers();
if (hasPeers(peers)) {
encodePeers(builder, peers);
}
final List<PeerId> oldPeers = log.getOldPeers();
if (hasPeers(oldPeers)) {
encodeOldPeers(builder, oldPeers);
}
final List<PeerId> learners = log.getLearners();
if (hasPeers(learners)) {
encodeLearners(builder, learners);
}
final List<PeerId> oldLearners = log.getOldLearners();
if (hasPeers(oldLearners)) {
encodeOldLearners(builder, oldLearners);
}
if (log.hasChecksum()) {
builder.setChecksum(log.getChecksum());
}
builder.setData(log.getData() != null ? ZeroByteStringHelper.wrap(log.getData()) : ByteString.EMPTY);
final PBLogEntry pbLogEntry = builder.build();
final int bodyLen = pbLogEntry.getSerializedSize();
final byte[] ret = new byte[LogEntryV2CodecFactory.HEADER_SIZE + bodyLen];
// write header
int i = 0;
for (; i < LogEntryV2CodecFactory.MAGIC_BYTES.length; i++) {
ret[i] = LogEntryV2CodecFactory.MAGIC_BYTES[i];
}
ret[i++] = LogEntryV2CodecFactory.VERSION;
// avoid memory copy for only 3 bytes
for (; i < LogEntryV2CodecFactory.HEADER_SIZE; i++) {
ret[i] = LogEntryV2CodecFactory.RESERVED[i - LogEntryV2CodecFactory.MAGIC_BYTES.length - 1];
}
// write body
writeToByteArray(pbLogEntry, ret, i, bodyLen);
return ret;
}
Aggregations