@@ -28,6 +28,15 @@ public final class TranslogHeaderWriter {
2828 public static final int FIXED_DELETE_HEADER_SIZE = 30 ;
2929 public static final int FIXED_NO_OP_HEADER_SIZE = 21 ;
3030
31+ public static final int OPERATION_TYPE_OFFSET = 4 ;
32+ public static final int SERIALIZATION_FORMAT_OFFSET = 5 ;
33+ public static final int VERSION_OFFSET = 6 ;
34+ public static final int SEQ_NO_OFFSET = 14 ;
35+ public static final int PRIMARY_TERM_OFFSET = 22 ;
36+
37+ public static final int INDEX_AUTO_GENERATED_ID_TIMESTAMP_OFFSET = 30 ;
38+ public static final int INDEX_UID_LENGTH_OFFSET = 38 ;
39+
3140 private TranslogHeaderWriter () {
3241 // This class depends on the serialization format fitting in a single vInt byte. If we advance pass 127 we need to tweak the logic
3342 // to write 2 bytes.
@@ -53,16 +62,16 @@ private static void writeFastIndexHeader(RecyclerBytesStreamOutput buffer, Trans
5362
5463 int off = page .offset ;
5564 byte [] bytes = page .bytes ;
56- bytes [off + 4 ] = Translog .Operation .Type .INDEX .id ();
65+ bytes [off + OPERATION_TYPE_OFFSET ] = Translog .Operation .Type .INDEX .id ();
5766 // This is technically a vInt in the serialization, but until we advance past 127 we can just directly serialize as a byte
58- bytes [off + 5 ] = (byte ) Translog .Index .SERIALIZATION_FORMAT ;
59- ByteUtils .writeLongBE (index .version (), bytes , off + 6 );
60- ByteUtils .writeLongBE (index .getAutoGeneratedIdTimestamp (), bytes , off + 14 );
61- ByteUtils .writeLongBE (index .seqNo (), bytes , off + 22 );
62- ByteUtils .writeLongBE (index .primaryTerm (), bytes , off + 30 );
63- StreamOutput .putVInt (bytes , uid .length , off + 38 );
64- System .arraycopy (uid .bytes , uid .offset , bytes , off + 38 + uidVIntLen , uid .length );
65- bytes [off + 38 + uidVIntLen + uid .length ] = index .routing () == null ? (byte ) 0 : (byte ) 1 ;
67+ bytes [off + SERIALIZATION_FORMAT_OFFSET ] = (byte ) Translog .Index .SERIALIZATION_FORMAT ;
68+ ByteUtils .writeLongBE (index .version (), bytes , off + VERSION_OFFSET );
69+ ByteUtils .writeLongBE (index .seqNo (), bytes , off + SEQ_NO_OFFSET );
70+ ByteUtils .writeLongBE (index .primaryTerm (), bytes , off + PRIMARY_TERM_OFFSET );
71+ ByteUtils .writeLongBE (index .getAutoGeneratedIdTimestamp (), bytes , off + INDEX_AUTO_GENERATED_ID_TIMESTAMP_OFFSET );
72+ StreamOutput .putVInt (bytes , uid .length , off + INDEX_UID_LENGTH_OFFSET );
73+ System .arraycopy (uid .bytes , uid .offset , bytes , off + INDEX_UID_LENGTH_OFFSET + uidVIntLen , uid .length );
74+ bytes [off + INDEX_UID_LENGTH_OFFSET + uidVIntLen + uid .length ] = index .routing () == null ? (byte ) 0 : (byte ) 1 ;
6675
6776 long variableLengthStart = buffer .position ();
6877 // Write variable length items in header
@@ -111,12 +120,12 @@ private static void writeFastDeleteHeader(Translog.Delete delete, BytesRef page,
111120
112121 int off = page .offset ;
113122 byte [] bytes = page .bytes ;
114- bytes [off + 4 ] = Translog .Operation .Type .DELETE .id ();
123+ bytes [off + OPERATION_TYPE_OFFSET ] = Translog .Operation .Type .DELETE .id ();
115124 // This is technically a vInt in the serialization, but until we advance past 127 we can just directly serialize as a byte
116- bytes [off + 5 ] = (byte ) Translog .Delete .SERIALIZATION_FORMAT ;
117- ByteUtils .writeLongBE (delete .version (), bytes , off + 6 );
118- ByteUtils .writeLongBE (delete .seqNo (), bytes , off + 14 );
119- ByteUtils .writeLongBE (delete .primaryTerm (), bytes , off + 22 );
125+ bytes [off + SERIALIZATION_FORMAT_OFFSET ] = (byte ) Translog .Delete .SERIALIZATION_FORMAT ;
126+ ByteUtils .writeLongBE (delete .version (), bytes , off + VERSION_OFFSET );
127+ ByteUtils .writeLongBE (delete .seqNo (), bytes , off + SEQ_NO_OFFSET );
128+ ByteUtils .writeLongBE (delete .primaryTerm (), bytes , off + PRIMARY_TERM_OFFSET );
120129 StreamOutput .putVInt (bytes , uid .length , off + FIXED_DELETE_HEADER_SIZE );
121130 System .arraycopy (uid .bytes , uid .offset , bytes , off + FIXED_DELETE_HEADER_SIZE + uidVIntLen , uid .length );
122131
@@ -142,7 +151,7 @@ public static void writeNoOpHeader(RecyclerBytesStreamOutput buffer, Translog.No
142151 if (bytesRef != null ) {
143152 int off = bytesRef .offset ;
144153 byte [] bytes = bytesRef .bytes ;
145- bytes [off + 4 ] = Translog .Operation .Type .NO_OP .id ();
154+ bytes [off + OPERATION_TYPE_OFFSET ] = Translog .Operation .Type .NO_OP .id ();
146155 ByteUtils .writeLongBE (noop .seqNo (), bytes , off + 5 );
147156 ByteUtils .writeLongBE (noop .primaryTerm (), bytes , off + 13 );
148157
0 commit comments