Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ gocode/flyweight/src/*/*.go
!gocode/flyweight/src/*/*_test.go
gocode/flyweight/src/*/*/*.go
!gocode/flyweight/src/*/*/*_test.go
gocode/flyweight/src/otf/code-generation-schema.sbeir
gocode/flyweight/src/otf/*.sbeir

# csharp
csharp/*/bin
Expand Down
38 changes: 38 additions & 0 deletions gocode/flyweight/src/otf/flyweight_group_block_length_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package otf

import (
"encoding/binary"
"github.com/real-logic/simple-binary-encoding/otf/test/flyweight_group_block_length"
"github.com/stretchr/testify/assert"
"testing"
)

//go:generate java --add-opens java.base/jdk.internal.misc=ALL-UNNAMED -Dsbe.output.dir=. -Dsbe.target.language=golang -Dsbe.go.generate.generate.flyweights=true -Dsbe.generate.ir=true -Dsbe.keyword.append.token=X -jar ../../../${SBE_JAR} ../../../../sbe-tool/src/test/resources/flyweight-group-block-length-schema.xml

const groupBlockLengthMessage = "group blockLength is less than the fields present at the acting version [E108]"

// wrapQuotes wraps a Quotes message whose group header carries the given blockLength and count.
func wrapQuotes(actingVersion uint64, blockLength uint16, count uint16) *flyweight_group_block_length.Quotes {
buffer := make([]byte, 8+4+int(count)*16)
binary.LittleEndian.PutUint16(buffer[8:], blockLength)
binary.LittleEndian.PutUint16(buffer[10:], count)

var quotes flyweight_group_block_length.Quotes
return quotes.WrapForDecode(buffer, 8, 0, actingVersion, uint64(len(buffer)))
}

func TestGroupBlockLengthMustCoverFieldsAtActingVersion(t *testing.T) {
assert := assert.New(t)

// A version 0 entry holds only the 8 byte mantissa: side was added to the composite in version 1.
assert.NotPanics(func() { wrapQuotes(0, 8, 2).Entries() })
assert.PanicsWithValue(groupBlockLengthMessage, func() { wrapQuotes(0, 7, 2).Entries() })
assert.PanicsWithValue(groupBlockLengthMessage, func() { wrapQuotes(0, 0, 3).Entries() })

// From version 1 the entry also holds side, and the constant field adds no bytes.
assert.NotPanics(func() { wrapQuotes(1, 9, 2).Entries() })
assert.PanicsWithValue(groupBlockLengthMessage, func() { wrapQuotes(1, 8, 2).Entries() })

// An empty group consumes nothing, whatever its blockLength.
assert.NotPanics(func() { wrapQuotes(1, 0, 0).Entries() })
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,74 @@ public GolangFlyweightGenerator(
this.outputManager = outputManager;
}

// Records, per schema version, the end of the bytes the fixed fields occupy from that version on. Constant fields
// occupy no bytes and composite members newer than the acting version are only counted from their own version.
private static void collectFieldExtents(
final List<Token> tokens,
final int typeIndex,
final int baseOffset,
final int enclosingVersion,
final SortedMap<Integer, Integer> extents)
{
final Token typeToken = tokens.get(typeIndex);
if (typeToken.isConstantEncoding())
{
return;
}

final int version = Math.max(enclosingVersion, typeToken.version());
final int offset = baseOffset + typeToken.offset();
if (Signal.BEGIN_COMPOSITE != typeToken.signal())
{
extents.merge(version, offset + typeToken.encodedLength(), Math::max);
return;
}

final int endIndex = typeIndex + typeToken.componentTokenCount() - 1;
for (int i = typeIndex + 1; i < endIndex; i += tokens.get(i).componentTokenCount())
{
collectFieldExtents(tokens, i, offset, version, extents);
}
}

private static String generateMinBlockLengthCheck(final List<Token> fields, final String indent)
{
final SortedMap<Integer, Integer> extents = new TreeMap<>();
for (int i = 0, size = fields.size(); i < size; )
{
final Token fieldToken = fields.get(i);
if (fieldToken.signal() == Signal.BEGIN_FIELD && !fieldToken.isConstantEncoding())
{
collectFieldExtents(fields, i + 1, 0, fieldToken.version(), extents);
}

i += fieldToken.componentTokenCount();
}

extents.values().removeIf(end -> end <= 0);
if (extents.isEmpty())
{
return "";
}

final StringBuilder sb = new StringBuilder();
final Integer baseLength = extents.remove(0);
sb.append(indent).append("\tminBlockLength := uint64(").append(null == baseLength ? 0 : baseLength)
.append(")\n");
extents.forEach(
(version, end) -> sb
.append(indent).append("\tif actingVersion >= ").append(version)
.append(" && minBlockLength < ").append(end).append(" {\n")
.append(indent).append("\t\tminBlockLength = ").append(end).append("\n")
.append(indent).append("\t}\n"));
sb.append(indent).append("\tif g.count > 0 && g.blockLength < minBlockLength {\n")
.append(indent)
.append("\t\tpanic(\"group blockLength is less than the fields present at the acting version [E108]\")\n")
.append(indent).append("\t}\n");

return sb.toString();
}

private static void generateGroupClassHeader(
final StringBuilder sb,
final String groupClassName,
Expand Down Expand Up @@ -155,13 +223,14 @@ private static void generateGroupClassHeader(
indent + "\tg.bufferLength = bufferLength\n" +
indent + "\tg.blockLength = uint64(dimensions.BlockLength())\n" +
indent + "\tg.count = uint64(dimensions.NumInGroup())\n" +
"%4$s" +
indent + "\tg.index = 0\n" +
indent + "\tg.actingVersion = actingVersion\n" +
indent + "\tg.initialPosition = *pos\n" +
indent + "\tg.positionPtr = pos\n" +
indent + "\t*g.positionPtr = *g.positionPtr + %1$d\n" +
indent + "}\n",
dimensionHeaderLength, dimensionsClassName, groupClassName);
dimensionHeaderLength, dimensionsClassName, groupClassName, generateMinBlockLengthCheck(fields, indent));

final long minCount = numInGroupToken.encoding().applicableMinValue().longValue();
final String minCheck = minCount > 0 ? "count < " + minCount + " || " : "";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sbe:messageSchema xmlns:sbe="http://fixprotocol.io/2016/sbe"
package="flyweight_group_block_length"
id="10"
version="1"
semanticVersion="1"
description="Group entries whose fixed fields include versioned and constant members"
byteOrder="littleEndian">
<types>
<composite name="messageHeader" description="Message identifiers and length of message root">
<type name="blockLength" primitiveType="uint16"/>
<type name="templateId" primitiveType="uint16"/>
<type name="schemaId" primitiveType="uint16"/>
<type name="version" primitiveType="uint16"/>
</composite>
<composite name="groupSizeEncoding" description="Repeating group dimensions">
<type name="blockLength" primitiveType="uint16"/>
<type name="numInGroup" primitiveType="uint16"/>
</composite>
<enum name="Side" encodingType="uint8">
<validValue name="BUY">1</validValue>
<validValue name="SELL">2</validValue>
</enum>
<type name="Venue" primitiveType="char" presence="constant">X</type>
<composite name="Price">
<type name="mantissa" primitiveType="int64"/>
<ref name="side" type="Side" sinceVersion="1"/>
</composite>
</types>
<sbe:message name="Quotes" id="1">
<group name="entries" id="1" dimensionType="groupSizeEncoding">
<field name="price" id="1" type="Price"/>
<field name="venue" id="2" type="Venue"/>
</group>
</sbe:message>
</sbe:messageSchema>
Loading