Skip to content

Commit 6852773

Browse files
committed
[Java] Support resetting group count to current index to allow for streaming operations where the group count is difficult to determine upfront but can be limited and reset down to current index value when group is complete.
1 parent c55ee4d commit 6852773

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

sbe-tool/src/main/java/uk/co/real_logic/sbe/generation/java/JavaGenerator.java

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ private void generateGroupDecoderClassHeader(
342342
final String indent)
343343
{
344344
final Token groupToken = tokens.get(index);
345+
final String className = formatClassName(groupName);
345346
final int dimensionHeaderLen = tokens.get(index + 1).encodedLength();
346347

347348
final Token blockLengthToken = Generators.findFirst("blockLength", tokens, index);
@@ -372,14 +373,27 @@ private void generateGroupDecoderClassHeader(
372373
.append(indent).append(" if (buffer != this.buffer)\n")
373374
.append(indent).append(" {\n")
374375
.append(indent).append(" this.buffer = buffer;\n")
375-
.append(indent).append(" }\n")
376-
.append(indent).append(" index = -1;\n")
376+
.append(indent).append(" }\n\n")
377+
.append(indent).append(" index = 0;\n")
377378
.append(indent).append(" final int limit = parentMessage.limit();\n")
378379
.append(indent).append(" parentMessage.limit(limit + HEADER_SIZE);\n")
379380
.append(indent).append(" blockLength = (int)").append(blockLengthGet).append(";\n")
380381
.append(indent).append(" count = (int)").append(numInGroupGet).append(";\n")
381382
.append(indent).append(" }\n");
382383

384+
sb.append("\n")
385+
.append(indent).append(" public ").append(className).append(" next()\n")
386+
.append(indent).append(" {\n")
387+
.append(indent).append(" if (index >= count)\n")
388+
.append(indent).append(" {\n")
389+
.append(indent).append(" throw new java.util.NoSuchElementException();\n")
390+
.append(indent).append(" }\n\n")
391+
.append(indent).append(" offset = parentMessage.limit();\n")
392+
.append(indent).append(" parentMessage.limit(offset + blockLength);\n")
393+
.append(indent).append(" ++index;\n\n")
394+
.append(indent).append(" return this;\n")
395+
.append(indent).append(" }\n");
396+
383397
sb.append("\n")
384398
.append(indent).append(" public static int sbeHeaderSize()\n")
385399
.append(indent).append(" {\n")
@@ -392,7 +406,6 @@ private void generateGroupDecoderClassHeader(
392406
.append(indent).append(" return ").append(tokens.get(index).encodedLength()).append(";\n")
393407
.append(indent).append(" }\n");
394408

395-
final String className = formatClassName(groupName);
396409
sb.append("\n")
397410
.append(indent).append(" public int actingBlockLength()\n")
398411
.append(indent).append(" {\n")
@@ -412,20 +425,7 @@ private void generateGroupDecoderClassHeader(
412425
.append(indent).append(" }\n\n")
413426
.append(indent).append(" public boolean hasNext()\n")
414427
.append(indent).append(" {\n")
415-
.append(indent).append(" return (index + 1) < count;\n")
416-
.append(indent).append(" }\n");
417-
418-
sb.append("\n")
419-
.append(indent).append(" public ").append(className).append(" next()\n")
420-
.append(indent).append(" {\n")
421-
.append(indent).append(" if (index + 1 >= count)\n")
422-
.append(indent).append(" {\n")
423-
.append(indent).append(" throw new java.util.NoSuchElementException();\n")
424-
.append(indent).append(" }\n\n")
425-
.append(indent).append(" offset = parentMessage.limit();\n")
426-
.append(indent).append(" parentMessage.limit(offset + blockLength);\n")
427-
.append(indent).append(" ++index;\n\n")
428-
.append(indent).append(" return this;\n")
428+
.append(indent).append(" return index < count;\n")
429429
.append(indent).append(" }\n");
430430
}
431431

@@ -477,35 +477,25 @@ private void generateGroupEncoderClassHeader(
477477
ind + " {\n" +
478478
ind + " this.buffer = buffer;\n" +
479479
ind + " }\n\n" +
480-
ind + " index = -1;\n" +
480+
ind + " index = 0;\n" +
481481
ind + " this.count = count;\n" +
482482
ind + " final int limit = parentMessage.limit();\n" +
483+
ind + " initialLimit = limit;\n" +
483484
ind + " parentMessage.limit(limit + HEADER_SIZE);\n" +
484485
ind + " %5$s;\n" +
485486
ind + " %6$s;\n" +
486-
ind + " }\n\n",
487+
ind + " }\n",
487488
parentMessageClassName,
488489
mutableBuffer,
489490
numInGroupToken.encoding().applicableMinValue().longValue(),
490491
numInGroupToken.encoding().applicableMaxValue().longValue(),
491492
blockLengthPut,
492493
numInGroupPut);
493494

494-
sb.append(ind).append(" public static int sbeHeaderSize()\n")
495-
.append(ind).append(" {\n")
496-
.append(ind).append(" return HEADER_SIZE;\n")
497-
.append(ind).append(" }\n");
498-
499-
sb.append("\n")
500-
.append(ind).append(" public static int sbeBlockLength()\n")
501-
.append(ind).append(" {\n")
502-
.append(ind).append(" return ").append(blockLength).append(";\n")
503-
.append(ind).append(" }\n");
504-
505495
sb.append("\n")
506496
.append(ind).append(" public ").append(encoderName(groupName)).append(" next()\n")
507497
.append(ind).append(" {\n")
508-
.append(ind).append(" if (index + 1 >= count)\n")
498+
.append(ind).append(" if (index >= count)\n")
509499
.append(ind).append(" {\n")
510500
.append(ind).append(" throw new java.util.NoSuchElementException();\n")
511501
.append(ind).append(" }\n\n")
@@ -514,6 +504,30 @@ private void generateGroupEncoderClassHeader(
514504
.append(ind).append(" ++index;\n\n")
515505
.append(ind).append(" return this;\n")
516506
.append(ind).append(" }\n");
507+
508+
final String countOffset = "initialLimit + " + numInGroupToken.offset();
509+
final String resetCountPut = generatePut(
510+
numInGroupType, countOffset, numInGroupValue, byteOrderString(numInGroupToken.encoding()));
511+
512+
sb.append("\n")
513+
.append(ind).append(" public int resetCountToIndex()\n")
514+
.append(ind).append(" {\n")
515+
.append(ind).append(" count = index;\n")
516+
.append(ind).append(" ").append(resetCountPut).append(";\n\n")
517+
.append(ind).append(" return count;\n")
518+
.append(ind).append(" }\n");
519+
520+
sb.append("\n")
521+
.append(ind).append(" public static int sbeHeaderSize()\n")
522+
.append(ind).append(" {\n")
523+
.append(ind).append(" return HEADER_SIZE;\n")
524+
.append(ind).append(" }\n");
525+
526+
sb.append("\n")
527+
.append(ind).append(" public static int sbeBlockLength()\n")
528+
.append(ind).append(" {\n")
529+
.append(ind).append(" return ").append(blockLength).append(";\n")
530+
.append(ind).append(" }\n");
517531
}
518532

519533
private static String primitiveTypeName(final Token token)
@@ -595,7 +609,8 @@ private void generateGroupEncoderClassDeclaration(
595609
indent + " private %4$s buffer;\n" +
596610
indent + " private int count;\n" +
597611
indent + " private int index;\n" +
598-
indent + " private int offset;\n",
612+
indent + " private int offset;\n" +
613+
indent + " private int initialLimit;\n",
599614
className,
600615
dimensionHeaderSize,
601616
parentMessageClassName,

0 commit comments

Comments
 (0)