Search in sources :

Example 1 with EntryType

use of io.dingodb.raft.entity.EnumOutter.EntryType in project dingo by dingodb.

the class V1Encoder method encode.

@Override
public byte[] encode(final LogEntry log) {
    if (log.hasLearners()) {
        throw new IllegalArgumentException("V1 log entry encoder doesn't support learners");
    }
    EntryType type = log.getType();
    LogId id = log.getId();
    List<PeerId> peers = log.getPeers();
    List<PeerId> oldPeers = log.getOldPeers();
    ByteBuffer data = log.getData();
    // magic number 1 byte
    int totalLen = 1;
    final int iType = type.getNumber();
    final long index = id.getIndex();
    final long term = id.getTerm();
    // type(4) + index(8) + term(8)
    totalLen += 4 + 8 + 8;
    int peerCount = 0;
    // peer count
    totalLen += 4;
    final List<String> peerStrs = new ArrayList<>(peerCount);
    if (peers != null) {
        peerCount = peers.size();
        for (final PeerId peer : peers) {
            final String peerStr = peer.toString();
            // peer len (short in 2 bytes)
            // peer str
            totalLen += 2 + peerStr.length();
            peerStrs.add(peerStr);
        }
    }
    int oldPeerCount = 0;
    // old peer count
    totalLen += 4;
    final List<String> oldPeerStrs = new ArrayList<>(oldPeerCount);
    if (oldPeers != null) {
        oldPeerCount = oldPeers.size();
        for (final PeerId peer : oldPeers) {
            final String peerStr = peer.toString();
            // peer len (short in 2 bytes)
            // peer str
            totalLen += 2 + peerStr.length();
            oldPeerStrs.add(peerStr);
        }
    }
    final int bodyLen = data != null ? data.remaining() : 0;
    totalLen += bodyLen;
    final byte[] content = new byte[totalLen];
    // {0} magic
    content[0] = LogEntryV1CodecFactory.MAGIC;
    // 1-5 type
    Bits.putInt(content, 1, iType);
    // 5-13 index
    Bits.putLong(content, 5, index);
    // 13-21 term
    Bits.putLong(content, 13, term);
    // peers
    // 21-25 peer count
    Bits.putInt(content, 21, peerCount);
    int pos = 25;
    for (final String peerStr : peerStrs) {
        final byte[] ps = AsciiStringUtil.unsafeEncode(peerStr);
        Bits.putShort(content, pos, (short) peerStr.length());
        System.arraycopy(ps, 0, content, pos + 2, ps.length);
        pos += 2 + ps.length;
    }
    // old peers
    // old peers count
    Bits.putInt(content, pos, oldPeerCount);
    pos += 4;
    for (final String peerStr : oldPeerStrs) {
        final byte[] ps = AsciiStringUtil.unsafeEncode(peerStr);
        Bits.putShort(content, pos, (short) peerStr.length());
        System.arraycopy(ps, 0, content, pos + 2, ps.length);
        pos += 2 + ps.length;
    }
    // data
    if (data != null) {
        System.arraycopy(data.array(), data.position(), content, pos, data.remaining());
    }
    return content;
}
Also used : EntryType(io.dingodb.raft.entity.EnumOutter.EntryType) ArrayList(java.util.ArrayList) LogId(io.dingodb.raft.entity.LogId) ByteBuffer(java.nio.ByteBuffer) PeerId(io.dingodb.raft.entity.PeerId)

Aggregations

EntryType (io.dingodb.raft.entity.EnumOutter.EntryType)1 LogId (io.dingodb.raft.entity.LogId)1 PeerId (io.dingodb.raft.entity.PeerId)1 ByteBuffer (java.nio.ByteBuffer)1 ArrayList (java.util.ArrayList)1