Skip to content

Commit 1bf840c

Browse files
committed
[Java]: Added javadoc and tidy up of OTF decoder.
1 parent 0419a6c commit 1bf840c

File tree

10 files changed

+131
-8
lines changed

10 files changed

+131
-8
lines changed

build.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,9 @@
257257
<java classname="uk.co.real_logic.sbe.examples.ExampleUsingGeneratedStubExtension">
258258
<classpath refid="examples.classpath"/>
259259
</java>
260+
<java classname="uk.co.real_logic.sbe.examples.OtfExample">
261+
<classpath refid="examples.classpath"/>
262+
</java>
260263
</target>
261264

262265
<target name="cpp:examples:gen" depends="dist" description="Generate and run C++ generated code example">

examples/java/uk/co/real_logic/sbe/examples/ExampleUsingGeneratedStub.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ public class ExampleUsingGeneratedStub
5151

5252
public static void main(final String[] args)throws Exception
5353
{
54+
System.out.println("\n*** Basic Stub Example ***");
55+
5456
final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4096);
5557
final DirectBuffer directBuffer = new DirectBuffer(byteBuffer);
5658
final short messageTemplateVersion = 0;

examples/java/uk/co/real_logic/sbe/examples/ExampleUsingGeneratedStubExtension.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public class ExampleUsingGeneratedStubExtension
5252

5353
public static void main(final String[] args)throws Exception
5454
{
55+
System.out.println("\n*** Extension Stub Example ***");
56+
5557
final ByteBuffer byteBuffer = ByteBuffer.allocateDirect(4096);
5658
final DirectBuffer directBuffer = new DirectBuffer(byteBuffer);
5759
final short messageTemplateVersion = 0;

examples/java/uk/co/real_logic/sbe/examples/OtfExample.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public class OtfExample
4242

4343
public static void main(final String[] args) throws Exception
4444
{
45+
System.out.println("\n*** OTF Example ***\n");
46+
4547
// Encode up message and schema as if we just got them off the wire.
4648
final ByteBuffer encodedSchemaBuffer = ByteBuffer.allocateDirect(SCHEMA_BUFFER_CAPACITY);
4749
encodeSchema(encodedSchemaBuffer);
@@ -65,6 +67,7 @@ public static void main(final String[] args) throws Exception
6567
bufferOffset += headerDecoder.size();
6668

6769
// Given the header information we can select the appropriate message template to do the decode.
70+
// The OTF Java classes are thread safe so the same instances can be reused across multiple threads.
6871
final OtfGroupSizeDecoder groupSizeDecoder = new OtfGroupSizeDecoder(ir.getType(OtfGroupSizeDecoder.GROUP_SIZE_ENCODING_NAME));
6972
final OtfVarDataDecoder varDataDecoder = new OtfVarDataDecoder(ir.getType(OtfVarDataDecoder.VAR_DATA_ENCODING_NAME));
7073
final List<Token> msgTokens = ir.getMessage(templateId);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Copyright 2013 Real Logic Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package uk.co.real_logic.sbe.otf;
17+
18+
import uk.co.real_logic.sbe.codec.java.DirectBuffer;
19+
import uk.co.real_logic.sbe.ir.Token;
20+
21+
import java.util.List;
22+
23+
/**
24+
* Abstract {@link TokenListener} that can be extended when not all callback methods are required.
25+
* <p/>
26+
* By extending this class their is a possibility for the optimizer to elide unused methods otherwise requiring polymorphic dispatch.
27+
*/
28+
public abstract class AbstractTokenListener implements TokenListener
29+
{
30+
public void onBeginMessage(final Token token)
31+
{
32+
// no op
33+
}
34+
35+
public void onEndMessage(final Token token)
36+
{
37+
// no op
38+
}
39+
40+
public void onEncoding(final Token fieldToken, final DirectBuffer buffer, final int bufferIndex, final Token typeToken, final int actingVersion)
41+
{
42+
// no op
43+
}
44+
45+
public void onEnum(final Token fieldToken, final DirectBuffer buffer, final int bufferIndex,
46+
final List<Token> tokens, final int fromIndex, final int toIndex, final int actingVersion)
47+
{
48+
// no op
49+
}
50+
51+
public void onBitSet(final Token fieldToken, final DirectBuffer buffer,
52+
final int bufferIndex, final List<Token> tokens, final int fromIndex, final int toIndex, final int actingVersion)
53+
{
54+
// no op
55+
}
56+
57+
public void onBeginComposite(final Token fieldToken, final List<Token> tokens, final int fromIndex, final int toIndex)
58+
{
59+
// no op
60+
}
61+
62+
public void onEndComposite(final Token fieldToken, final List<Token> tokens, final int fromIndex, final int toIndex)
63+
{
64+
// no op
65+
}
66+
67+
public void onBeginGroup(final Token token, final int groupIndex, final int numInGroup)
68+
{
69+
// no op
70+
}
71+
72+
public void onEndGroup(final Token token, final int groupIndex, final int numInGroup)
73+
{
74+
// no op
75+
}
76+
77+
public void onVarData(final Token fieldToken, final DirectBuffer buffer, final int bufferIndex, final int length, final Token typeToken)
78+
{
79+
// no op
80+
}
81+
}

main/java/uk/co/real_logic/sbe/otf/OtfGroupSizeDecoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.List;
2525

2626
/**
27-
* Used to decode the group size header.
27+
* Used to decode group size headers.
28+
* <p/>
29+
* This class is thread safe.
2830
*/
2931
public class OtfGroupSizeDecoder
3032
{

main/java/uk/co/real_logic/sbe/otf/OtfHeaderDecoder.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
* Used to decode a message header while doing on-the-fly decoding of a message stream.
2727
* <p/>
2828
* Meta data is cached to improve the performance of decoding headers.
29+
* <p/>
30+
* This class is thread safe.
2931
*/
3032
public class OtfHeaderDecoder
3133
{

main/java/uk/co/real_logic/sbe/otf/OtfMessageDecoder.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,26 @@
2222

2323
import java.util.List;
2424

25+
/**
26+
* On-the-fly decoder that dynamically decodes messages based on the IR for a schema.
27+
* <p/>
28+
* The contents of the messages are structurally decomposed and passed to a {@link TokenListener} for decoding the primitive values.
29+
* <p/>
30+
* The design keeps all state on the stack to maximise performance and avoid object allocation. The message decoder can be used reused by
31+
* repeatably calling {@link OtfMessageDecoder#decode(DirectBuffer, int, int, int, java.util.List, TokenListener)}
32+
* and is thread safe to be used across multiple threads.
33+
*/
2534
public class OtfMessageDecoder
2635
{
2736
private final OtfGroupSizeDecoder groupSizeDecoder;
2837
private final OtfVarDataDecoder varDataDecoder;
2938

39+
/**
40+
* Construct a message decoder with provided decoders for the group and var data headers. The provided decoders are expected to be thread safe.
41+
*
42+
* @param groupSizeDecoder for decoding the repeating group header.
43+
* @param varDataDecoder for decoding the var data field header.
44+
*/
3045
public OtfMessageDecoder(final OtfGroupSizeDecoder groupSizeDecoder, final OtfVarDataDecoder varDataDecoder)
3146
{
3247
Verify.notNull(groupSizeDecoder, "groupSizeDecoder");
@@ -36,6 +51,17 @@ public OtfMessageDecoder(final OtfGroupSizeDecoder groupSizeDecoder, final OtfVa
3651
this.varDataDecoder = varDataDecoder;
3752
}
3853

54+
/**
55+
* Decode a message from the provided buffer based on the message schema described with IR {@link uk.co.real_logic.sbe.ir.Token}s.
56+
*
57+
* @param buffer containing the encoded message.
58+
* @param bufferIndex at which the message encoding starts in the buffer.
59+
* @param actingVersion of the encoded message for dealing with extension fields.
60+
* @param blockLength of the root message fields.
61+
* @param msgTokens in IR format describing the message structure.
62+
* @param listener to callback for decoding the primitive values as discovered in the structure.
63+
* @return the index in the underlying buffer after decoding.
64+
*/
3965
public int decode(final DirectBuffer buffer,
4066
int bufferIndex,
4167
final int actingVersion,
@@ -71,7 +97,7 @@ private static void decodeFields(final DirectBuffer buffer,
7197
{
7298
for (int i = fromIndex; i < toIndex; i++)
7399
{
74-
if (tokens.get(i).signal() == Signal.BEGIN_FIELD)
100+
if (Signal.BEGIN_FIELD == tokens.get(i).signal())
75101
{
76102
i = decodeField(buffer, bufferIndex, tokens, i, actingVersion, listener);
77103
}
@@ -164,8 +190,8 @@ private static int decodeField(final DirectBuffer buffer,
164190

165191
switch (typeToken.signal())
166192
{
167-
case ENCODING:
168-
listener.onEncoding(fieldToken, buffer, bufferIndex + typeToken.offset(), typeToken, actingVersion);
193+
case BEGIN_COMPOSITE:
194+
decodeComposite(fieldToken, buffer, bufferIndex + typeToken.offset(), tokens, fromIndex + 1, toIndex - 1, actingVersion, listener);
169195
break;
170196

171197
case BEGIN_ENUM:
@@ -176,8 +202,8 @@ private static int decodeField(final DirectBuffer buffer,
176202
listener.onBitSet(fieldToken, buffer, bufferIndex + typeToken.offset(), tokens, fromIndex + 1, toIndex - 1, actingVersion);
177203
break;
178204

179-
case BEGIN_COMPOSITE:
180-
decodeComposite(fieldToken, buffer, bufferIndex + typeToken.offset(), tokens, fromIndex + 1, toIndex - 1, actingVersion, listener);
205+
case ENCODING:
206+
listener.onEncoding(fieldToken, buffer, bufferIndex + typeToken.offset(), typeToken, actingVersion);
181207
break;
182208
}
183209

main/java/uk/co/real_logic/sbe/otf/OtfVarDataDecoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
import java.util.List;
2525

2626
/**
27-
* Used to decode the var data field.
27+
* Used to decode var data fields.
28+
* <p/>
29+
* This class is thread safe.
2830
*/
2931
public class OtfVarDataDecoder
3032
{

main/java/uk/co/real_logic/sbe/otf/Util.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
public class Util
2828
{
2929
/**
30-
* Get an integer value from a buffer.
30+
* Get an integer value from a buffer at a given index.
3131
*
3232
* @param buffer from which to read.
3333
* @param bufferIndex at which he integer should be read.

0 commit comments

Comments
 (0)