-
Notifications
You must be signed in to change notification settings - Fork 556
/
LogEntryDescriptor.java
180 lines (138 loc) · 6.27 KB
/
LogEntryDescriptor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Licensed under the Zeebe Community License 1.0. You may not use this file
* except in compliance with the Zeebe Community License 1.0.
*/
package io.zeebe.logstreams.impl.log;
import static io.zeebe.dispatcher.impl.log.DataFrameDescriptor.alignedLength;
import static io.zeebe.dispatcher.impl.log.DataFrameDescriptor.lengthOffset;
import static io.zeebe.dispatcher.impl.log.DataFrameDescriptor.messageOffset;
import static org.agrona.BitUtil.SIZE_OF_LONG;
import static org.agrona.BitUtil.SIZE_OF_SHORT;
import io.zeebe.protocol.Protocol;
import org.agrona.DirectBuffer;
import org.agrona.MutableDirectBuffer;
/**
* *
*
* <pre>
* 0 1 2 3
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
* | VERSION | R |
* +---------------------------------------------------------------+
* | POSITION |
* | |
* +---------------------------------------------------------------+
* | SOURCE EVENT POSITION |
* | |
* +---------------------------------------------------------------+
* | KEY |
* | |
* +---------------------------------------------------------------+
* | TIMESTAMP |
* | |
* +---------------------------------------------------------------+
* | METADATA LENGTH | unused |
* +---------------------------------------------------------------+
* | ...METADATA... |
* +---------------------------------------------------------------+
* | ...VALUE... |
* +---------------------------------------------------------------+
* </pre>
*/
public final class LogEntryDescriptor {
public static final long KEY_NULL_VALUE = -1;
public static final int VERSION_OFFSET;
public static final int POSITION_OFFSET;
public static final int SOURCE_EVENT_POSITION_OFFSET;
public static final int KEY_OFFSET;
public static final int TIMESTAMP_OFFSET;
public static final int METADATA_LENGTH_OFFSET;
public static final int HEADER_BLOCK_LENGTH;
public static final int METADATA_OFFSET;
static {
int offset = 0;
VERSION_OFFSET = offset;
offset += SIZE_OF_SHORT;
// reserved offset
offset += SIZE_OF_SHORT;
POSITION_OFFSET = offset;
offset += SIZE_OF_LONG;
SOURCE_EVENT_POSITION_OFFSET = offset;
offset += SIZE_OF_LONG;
KEY_OFFSET = offset;
offset += SIZE_OF_LONG;
TIMESTAMP_OFFSET = offset;
offset += SIZE_OF_LONG;
METADATA_LENGTH_OFFSET = offset;
offset += SIZE_OF_SHORT;
// UNUSED BLOCK
offset += SIZE_OF_SHORT;
HEADER_BLOCK_LENGTH = offset;
METADATA_OFFSET = offset;
}
public static int getFragmentLength(final DirectBuffer buffer, final int offset) {
return alignedLength(buffer.getInt(lengthOffset(offset), Protocol.ENDIANNESS));
}
public static int headerLength(final int metadataLength) {
return HEADER_BLOCK_LENGTH + metadataLength;
}
public static int positionOffset(final int offset) {
return POSITION_OFFSET + offset;
}
public static long getPosition(final DirectBuffer buffer, final int offset) {
return buffer.getLong(positionOffset(messageOffset(offset)), Protocol.ENDIANNESS);
}
public static void setPosition(
final MutableDirectBuffer buffer, final int offset, final long position) {
buffer.putLong(positionOffset(offset), position, Protocol.ENDIANNESS);
}
public static int sourceEventPositionOffset(final int offset) {
return SOURCE_EVENT_POSITION_OFFSET + offset;
}
public static long getSourceEventPosition(final DirectBuffer buffer, final int offset) {
return buffer.getLong(sourceEventPositionOffset(offset), Protocol.ENDIANNESS);
}
public static void setSourceEventPosition(
final MutableDirectBuffer buffer, final int offset, final long sourceEventPosition) {
buffer.putLong(sourceEventPositionOffset(offset), sourceEventPosition, Protocol.ENDIANNESS);
}
public static int keyOffset(final int offset) {
return KEY_OFFSET + offset;
}
public static long getKey(final DirectBuffer buffer, final int offset) {
return buffer.getLong(keyOffset(offset), Protocol.ENDIANNESS);
}
public static void setKey(final MutableDirectBuffer buffer, final int offset, final long key) {
buffer.putLong(keyOffset(offset), key, Protocol.ENDIANNESS);
}
public static int timestampOffset(final int offset) {
return TIMESTAMP_OFFSET + offset;
}
public static long getTimestamp(final DirectBuffer buffer, final int offset) {
return buffer.getLong(timestampOffset(offset), Protocol.ENDIANNESS);
}
public static void setTimestamp(
final MutableDirectBuffer buffer, final int offset, final long timestamp) {
buffer.putLong(timestampOffset(offset), timestamp, Protocol.ENDIANNESS);
}
public static int metadataLengthOffset(final int offset) {
return METADATA_LENGTH_OFFSET + offset;
}
public static short getMetadataLength(final DirectBuffer buffer, final int offset) {
return buffer.getShort(metadataLengthOffset(offset), Protocol.ENDIANNESS);
}
public static void setMetadataLength(
final MutableDirectBuffer buffer, final int offset, final short metadataLength) {
buffer.putShort(metadataLengthOffset(offset), metadataLength, Protocol.ENDIANNESS);
}
public static int metadataOffset(final int offset) {
return METADATA_OFFSET + offset;
}
public static int valueOffset(final int offset, final int metadataLength) {
return METADATA_OFFSET + metadataLength + offset;
}
}