Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor constructors #22

Merged
Merged
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
27 changes: 25 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,34 @@ RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription();
sdSubscription.subscribeElement("event_key@12345","paramKey");
----

Create a parser with the desired InputStream
Create a parser and set inputStream manually with automatic newline termination, defaults true

[source,java]
----
RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription);
parser.setInputStream(inputStream);
----

Create a parser and set inputStream manually with explicit newline termination

[source,java]
----
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, false);
parser.setInputStream(inputStream);
----

You can pass the inputStream from constructor with automatic newline termination, defaults true

[source,java]
----
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);
----

You can pass the inputStream from constructor with explicit newline termination

[source,java]
----
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream, false);
----

Next event can be extracted as follows
Expand Down
68 changes: 32 additions & 36 deletions src/main/java/com/teragrep/rlo_06/RFC5424Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,49 +54,46 @@ public final class RFC5424Parser {
private final ParserResultSet resultset;
private final Consumer<Stream> streamConsumer;

public RFC5424Parser() {
this(new InputStream() {
@Override
public int read() {
return -1;
}
});
public RFC5424Parser(RFC5424ParserSubscription subscription, RFC5424ParserSDSubscription sdSubscription) {
this(subscription, sdSubscription, true);
}

public RFC5424Parser(InputStream inputStream) {
this(inputStream, new RFC5424ParserSubscription());
}
public RFC5424Parser(InputStream inputStream, RFC5424ParserSubscription subscription) {
this(inputStream, subscription, new RFC5424ParserSDSubscription());
public RFC5424Parser(RFC5424ParserSubscription subscription, RFC5424ParserSDSubscription sdSubscription, boolean lineFeedTermination) {
this.resultset = new ParserResultSet(subscription, sdSubscription);
this.streamConsumer = new Priority(this.resultset.PRIORITY)
.andThen(new Version(this.resultset.VERSION)
.andThen(new Timestamp(this.resultset.TIMESTAMP)
.andThen(new Hostname(this.resultset.HOSTNAME)
.andThen(new AppName(this.resultset.APPNAME)
.andThen(new ProcId(this.resultset.PROCID)
.andThen(new MsgId(this.resultset.MSGID)
.andThen(new StructuredData(this.resultset)
.andThen(new Msg(this.resultset.MSG, lineFeedTermination))
)
)
)
)
)
)
);
}

public RFC5424Parser(InputStream inputStream, RFC5424ParserSubscription subscription, RFC5424ParserSDSubscription sdSubscription) {
this(inputStream, subscription, sdSubscription, true);
public RFC5424Parser(RFC5424ParserSubscription subscription, RFC5424ParserSDSubscription sdSubscription, InputStream inputStream) {
this(subscription, sdSubscription, inputStream, true);
}

public RFC5424Parser(InputStream inputStream, RFC5424ParserSubscription subscription, RFC5424ParserSDSubscription sdSubscription, boolean lineFeedTermination) {
public RFC5424Parser(RFC5424ParserSubscription subscription, RFC5424ParserSDSubscription sdSubscription, InputStream inputStream, boolean lineFeedTermination) {
this.stream = new Stream(inputStream);
this.resultset = new ParserResultSet(subscription, sdSubscription);

Priority priority = new Priority(this.resultset.PRIORITY);
Version version = new Version(this.resultset.VERSION);
Timestamp timestamp = new Timestamp(this.resultset.TIMESTAMP);
Hostname hostname = new Hostname(this.resultset.HOSTNAME);
AppName appName = new AppName(this.resultset.APPNAME);
ProcId procId = new ProcId(this.resultset.PROCID);
MsgId msgId = new MsgId(this.resultset.MSGID);
StructuredData structuredData = new StructuredData(this.resultset);
Msg msg = new Msg(this.resultset.MSG, lineFeedTermination);

this.streamConsumer = priority
.andThen(version
.andThen(timestamp
.andThen(hostname
.andThen(appName
.andThen(procId
.andThen(msgId
.andThen(structuredData
.andThen(msg)
this.streamConsumer = new Priority(this.resultset.PRIORITY)
.andThen(new Version(this.resultset.VERSION)
.andThen(new Timestamp(this.resultset.TIMESTAMP)
.andThen(new Hostname(this.resultset.HOSTNAME)
.andThen(new AppName(this.resultset.APPNAME)
.andThen(new ProcId(this.resultset.PROCID)
.andThen(new MsgId(this.resultset.MSGID)
.andThen(new StructuredData(this.resultset)
.andThen(new Msg(this.resultset.MSG, lineFeedTermination))
)
)
)
Expand All @@ -106,7 +103,6 @@ public RFC5424Parser(InputStream inputStream, RFC5424ParserSubscription subscrip
);
}


public void setInputStream(InputStream inputStream) {
stream = new Stream(inputStream);
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/java/com/teragrep/rlo_06/BrokenSyntaxTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ void testBrokenFail() throws Exception {
RFC5424ParserSubscription subscription = new RFC5424ParserSubscription();
subscription.subscribeAll();
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription();
RFC5424Parser parser = new RFC5424Parser(null, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription);
parser.setInputStream(new ByteArrayInputStream( (SYSLOG_MESSAGE).getBytes()));
Assertions.assertThrows(ParseException.class, parser::next);
}
Expand All @@ -24,7 +24,7 @@ void testNilTimestamp() throws Exception {
RFC5424ParserSubscription subscription = new RFC5424ParserSubscription();
subscription.subscribeAll();
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription(true);
RFC5424Parser parser = new RFC5424Parser(null, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription);
ResultSetAsString resultsetAsString = new ResultSetAsString(parser.get());
parser.setInputStream(new ByteArrayInputStream( (input).getBytes()));
parser.next();
Expand All @@ -44,7 +44,7 @@ void testOpenSD() throws Exception {
RFC5424ParserSubscription subscription = new RFC5424ParserSubscription();
subscription.subscribeAll();
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription();
RFC5424Parser parser = new RFC5424Parser(null, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription);
parser.setInputStream(new ByteArrayInputStream((SYSLOG_MESSAGE).getBytes()));
Assertions.assertThrows(ParseException.class, parser::next);
}
Expand All @@ -57,7 +57,7 @@ void testAllNil() throws Exception {
subscription.subscribeAll();
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription(true);
InputStream inputStream = new ByteArrayInputStream( (input).getBytes());
RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);
ResultSetAsString resultsetAsString = new ResultSetAsString(parser.get());
parser.next();
Assertions.assertEquals("2", resultsetAsString.getPriority(), "Priority");
Expand Down
18 changes: 0 additions & 18 deletions src/test/java/com/teragrep/rlo_06/EmptyParserInputTest.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void testAllSubscription() throws IOException {

InputStream inputStream = new ByteArrayInputStream( (SYSLOG_MESSAGE).getBytes());

RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);

Assertions.assertTrue(parser.next());

Expand All @@ -44,7 +44,7 @@ public void testAllSubscriptionAsMap() throws IOException {

InputStream inputStream = new ByteArrayInputStream( (SYSLOG_MESSAGE).getBytes());

RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);

Assertions.assertTrue(parser.next());

Expand Down
20 changes: 13 additions & 7 deletions src/test/java/com/teragrep/rlo_06/tests/PerformanceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void testLongPayloadPerformance() throws Exception {
sdSubscription.subscribeElement("ID_A@1","u");

InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes());
RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);



Expand Down Expand Up @@ -100,7 +100,7 @@ void testShortPayloadPerformance() throws Exception {
sdSubscription.subscribeElement("ID_A@1","u");

InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes());
RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);


Instant instant1 = Instant.now();
Expand All @@ -125,10 +125,11 @@ void testOnlyMsgSub() throws Exception {

String SYSLOG_MESSAGE = "<14>1 2014-06-20T09:14:07.12345+00:00 host01 systemd DEA MSG-01 [ID_A@1 u=\"\\\"3\" e=\"t\"][ID_B@2 n=\"9\"][event_id@48577 hostname=\"sc-99-99-14-247\" uuid=\"0FD92E51B37748EB90CD894CCEE63907\" unixtime=\"1612047600.0\" id_source=\"source\"][event_node_source@48577 hostname=\"sc-99-99-14-247\" source=\"f17_ssmis_20210131v7.nc\" source_module=\"imfile\"][event_node_relay@48577 hostname=\"localhost\" source=\"sc-99-99-14-247\" source_module=\"imrelp\"][event_version@48577 major=\"2\" minor=\"2\" hostname=\"localhost\" version_source=\"relay\"][event_node_router@48577 source=\"logrouter.example.com\" source_module=\"imrelp\" hostname=\"localhost\"][teragrep@48577 streamname=\"log:f17:0\" directory=\"com_teragrep_audit\" unixtime=\"1612047600.0\"] sigsegv\n";
RFC5424ParserSubscription subscription = new RFC5424ParserSubscription();
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription();
subscription.add(ParserEnum.MSG);

InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes());
RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, null);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);


Instant instant1 = Instant.now();
Expand All @@ -153,8 +154,9 @@ void testNothingSub() throws Exception {

String SYSLOG_MESSAGE = "<14>1 2014-06-20T09:14:07.12345+00:00 host01 systemd DEA MSG-01 [ID_A@1 u=\"\\\"3\" e=\"t\"][ID_B@2 n=\"9\"][event_id@48577 hostname=\"sc-99-99-14-247\" uuid=\"0FD92E51B37748EB90CD894CCEE63907\" unixtime=\"1612047600.0\" id_source=\"source\"][event_node_source@48577 hostname=\"sc-99-99-14-247\" source=\"f17_ssmis_20210131v7.nc\" source_module=\"imfile\"][event_node_relay@48577 hostname=\"localhost\" source=\"sc-99-99-14-247\" source_module=\"imrelp\"][event_version@48577 major=\"2\" minor=\"2\" hostname=\"localhost\" version_source=\"relay\"][event_node_router@48577 source=\"logrouter.example.com\" source_module=\"imrelp\" hostname=\"localhost\"][teragrep@48577 streamname=\"log:f17:0\" directory=\"com_teragrep_audit\" unixtime=\"1612047600.0\"] sigsegv\n";
RFC5424ParserSubscription subscription = new RFC5424ParserSubscription();
InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes());
RFC5424Parser parser = new RFC5424Parser(inputStream, subscription);
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription();
InputStream inputStream = new ByteArrayInputStream(SYSLOG_MESSAGE.getBytes());
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);

Instant instant1 = Instant.now();
long count = 10000000;
Expand All @@ -178,7 +180,10 @@ void testDashesFullSub() throws Exception {

String SYSLOG_MESSAGE = "<2>1 - - - - - ";
InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes());
RFC5424Parser parser = new RFC5424Parser(inputStream, null);
RFC5424ParserSubscription subscription = new RFC5424ParserSubscription();
subscription.subscribeAll();
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription();
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);

Instant instant1 = Instant.now();
long count = 100000000;
Expand All @@ -203,7 +208,8 @@ void testDashesNoSub() throws Exception {
String SYSLOG_MESSAGE = "<2>1 - - - - - ";
RFC5424ParserSubscription subscription = new RFC5424ParserSubscription();
InputStream inputStream = new ByteArrayInputStream( SYSLOG_MESSAGE.getBytes());
RFC5424Parser parser = new RFC5424Parser(inputStream, subscription);
RFC5424ParserSDSubscription sdSubscription = new RFC5424ParserSDSubscription();
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);

Instant instant1 = Instant.now();
long count = 100000000;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public void read() throws IOException {
String SYSLOG_MESSAGE = "<14>1 2014-06-20T09:14:07.12345+00:00 host01 systemd DEA MSG-01 [ID_A@1 u=\"\\\"3\" e=\"t\"][ID_B@2 n=\"9\"][event_id@48577 hostname=\"sc-99-99-14-247\" uuid=\"0FD92E51B37748EB90CD894CCEE63907\" unixtime=\"1612047600.0\" id_source=\"source\"][event_node_source@48577 hostname=\"sc-99-99-14-247\" source=\"f17_ssmis_20210131v7.nc\" source_module=\"imfile\"][event_node_relay@48577 hostname=\"localhost\" source=\"sc-99-99-14-247\" source_module=\"imrelp\"][event_version@48577 major=\"2\" minor=\"2\" hostname=\"localhost\" version_source=\"relay\"][event_node_router@48577 source=\"logrouter.example.com\" source_module=\"imrelp\" hostname=\"localhost\"][teragrep@48577 streamname=\"log:f17:0\" directory=\"com_teragrep_audit\" unixtime=\"1612047600.0\"] msg\n";
InputStream inputStream = new ByteArrayInputStream( (SYSLOG_MESSAGE).getBytes());

RFC5424Parser parser = new RFC5424Parser(inputStream, subscription, sdSubscription);
RFC5424Parser parser = new RFC5424Parser(subscription, sdSubscription, inputStream);

Assertions.assertTrue(parser.next());

Expand Down
Loading