Skip to content

Commit 46318dc

Browse files
committed
Improved V2Batch: iterators don't change V2Batch internal state any more. It now implement Closable, for auto-release.
1 parent c9f5488 commit 46318dc

File tree

2 files changed

+61
-50
lines changed

2 files changed

+61
-50
lines changed

src/main/java/org/logstash/beats/V2Batch.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package org.logstash.beats;
22

3+
import java.io.Closeable;
4+
import java.util.Iterator;
5+
import java.util.NoSuchElementException;
6+
37
import io.netty.buffer.ByteBuf;
48
import io.netty.buffer.PooledByteBufAllocator;
59

6-
import java.util.Iterator;
7-
810
/**
911
* Implementation of {@link Batch} for the v2 protocol backed by ByteBuf. *must* be released after use.
1012
*/
11-
public class V2Batch implements Batch {
13+
public class V2Batch implements Batch, Closeable {
1214
private ByteBuf internalBuffer = PooledByteBufAllocator.DEFAULT.buffer();
1315
private int written = 0;
14-
private int read = 0;
1516
private static final int SIZE_OF_INT = 4;
1617
private int batchSize;
1718
private int highestSequence = -1;
@@ -27,20 +28,27 @@ public byte getProtocol() {
2728
return Protocol.VERSION_2;
2829
}
2930

30-
public Iterator<Message> iterator(){
31-
internalBuffer.resetReaderIndex();
31+
public Iterator<Message> iterator() {
3232
return new Iterator<Message>() {
33+
private int read = 0;
34+
private ByteBuf readerBuffer = internalBuffer.asReadOnly();
35+
{
36+
readerBuffer.resetReaderIndex();
37+
}
3338
@Override
3439
public boolean hasNext() {
3540
return read < written;
3641
}
3742

3843
@Override
3944
public Message next() {
40-
int sequenceNumber = internalBuffer.readInt();
41-
int readableBytes = internalBuffer.readInt();
42-
Message message = new Message(sequenceNumber, internalBuffer.slice(internalBuffer.readerIndex(), readableBytes));
43-
internalBuffer.readerIndex(internalBuffer.readerIndex() + readableBytes);
45+
if (read >= written) {
46+
throw new NoSuchElementException();
47+
}
48+
int sequenceNumber = readerBuffer.readInt();
49+
int readableBytes = readerBuffer.readInt();
50+
Message message = new Message(sequenceNumber, readerBuffer.slice(readerBuffer.readerIndex(), readableBytes));
51+
readerBuffer.readerIndex(readerBuffer.readerIndex() + readableBytes);
4452
message.setBatch(V2Batch.this);
4553
read++;
4654
return message;
@@ -101,4 +109,10 @@ void addMessage(int sequenceNumber, ByteBuf buffer, int size) {
101109
public void release() {
102110
internalBuffer.release();
103111
}
112+
113+
@Override
114+
public void close() {
115+
release();
116+
}
117+
104118
}

src/test/java/org/logstash/beats/V2BatchTest.java

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,49 @@ public class V2BatchTest {
2020

2121
@Test
2222
public void testIsEmpty() {
23-
V2Batch batch = new V2Batch();
24-
assertTrue(batch.isEmpty());
25-
ByteBuf content = messageContents();
26-
batch.addMessage(1, content, content.readableBytes());
27-
assertFalse(batch.isEmpty());
23+
try (V2Batch batch = new V2Batch()){
24+
assertTrue(batch.isEmpty());
25+
ByteBuf content = messageContents();
26+
batch.addMessage(1, content, content.readableBytes());
27+
assertFalse(batch.isEmpty());
28+
}
2829
}
2930

3031
@Test
3132
public void testSize() {
32-
V2Batch batch = new V2Batch();
33-
assertEquals(0, batch.size());
34-
ByteBuf content = messageContents();
35-
batch.addMessage(1, content, content.readableBytes());
36-
assertEquals(1, batch.size());
33+
try (V2Batch batch = new V2Batch()) {
34+
assertEquals(0, batch.size());
35+
ByteBuf content = messageContents();
36+
batch.addMessage(1, content, content.readableBytes());
37+
assertEquals(1, batch.size());
38+
}
3739
}
3840

3941
@Test
40-
public void TestGetProtocol() {
41-
assertEquals(Protocol.VERSION_2, new V2Batch().getProtocol());
42+
public void testGetProtocol() {
43+
try (V2Batch batch = new V2Batch()) {
44+
assertEquals(Protocol.VERSION_2, batch.getProtocol());
45+
}
4246
}
4347

4448
@Test
45-
public void TestCompleteReturnTrueWhenIReceiveTheSameAmountOfEvent() {
46-
V2Batch batch = new V2Batch();
47-
int numberOfEvent = 2;
48-
49-
batch.setBatchSize(numberOfEvent);
50-
51-
for(int i = 1; i <= numberOfEvent; i++) {
52-
ByteBuf content = messageContents();
53-
batch.addMessage(i, content, content.readableBytes());
49+
public void testCompleteReturnTrueWhenIReceiveTheSameAmountOfEvent() {
50+
try (V2Batch batch = new V2Batch()) {
51+
int numberOfEvent = 2;
52+
batch.setBatchSize(numberOfEvent);
53+
for (int i = 1; i <= numberOfEvent; i++) {
54+
ByteBuf content = messageContents();
55+
batch.addMessage(i, content, content.readableBytes());
56+
}
57+
assertTrue(batch.isComplete());
5458
}
55-
56-
assertTrue(batch.isComplete());
5759
}
5860

5961
@Test
6062
public void testBigBatch() {
61-
V2Batch batch = new V2Batch();
62-
int size = 4096;
63-
assertEquals(0, batch.size());
64-
try {
63+
try (V2Batch batch = new V2Batch()) {
64+
int size = 4096;
65+
assertEquals(0, batch.size());
6566
ByteBuf content = messageContents();
6667
for (int i = 0; i < size; i++) {
6768
batch.addMessage(i, content, content.readableBytes());
@@ -71,8 +72,6 @@ public void testBigBatch() {
7172
for (Message message : batch) {
7273
assertEquals(message.getSequence(), i++);
7374
}
74-
}finally {
75-
batch.release();
7675
}
7776
}
7877

@@ -91,17 +90,15 @@ public void testHighSequence(){
9190
assertEquals(startSequenceNumber + numberOfEvent, batch.getHighestSequence());
9291
}
9392

94-
9593
@Test
96-
public void TestCompleteReturnWhenTheNumberOfEventDoesntMatchBatchSize() {
97-
V2Batch batch = new V2Batch();
98-
int numberOfEvent = 2;
99-
100-
batch.setBatchSize(numberOfEvent);
101-
ByteBuf content = messageContents();
102-
batch.addMessage(1, content, content.readableBytes());
103-
104-
assertFalse(batch.isComplete());
94+
public void testCompleteReturnWhenTheNumberOfEventDoesntMatchBatchSize() {
95+
try (V2Batch batch = new V2Batch()) {
96+
int numberOfEvent = 2;
97+
batch.setBatchSize(numberOfEvent);
98+
ByteBuf content = messageContents();
99+
batch.addMessage(1, content, content.readableBytes());
100+
assertFalse(batch.isComplete());
101+
}
105102
}
106103

107104
public static ByteBuf messageContents() {
@@ -114,4 +111,4 @@ public static ByteBuf messageContents() {
114111
throw new RuntimeException(e);
115112
}
116113
}
117-
}
114+
}

0 commit comments

Comments
 (0)