Skip to content

Commit 5028f56

Browse files
authored
add migrate epoch command (#109)
* add admin transformation and migrate epoch command * unify table option tokens * clean up parser grammar, remove duplicate admin mode rule * add syntax parsing test - avoids file dependant util classes, improved naming * added tests for invalid syntax and empty strings * add tests for single quotes, format code * simplify admin migrate command parser rules * move | admin migrate command to | teragrep exec migrate * remove optional table option
1 parent 6e187d1 commit 5028f56

3 files changed

Lines changed: 92 additions & 3 deletions

File tree

src/main/antlr4/imports/COMMAND_TERAGREP_MODE.g4

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,10 +88,12 @@ COMMAND_TERAGREP_MODE_LOAD: 'load';
8888
COMMAND_TERAGREP_MODE_SYSTEM: 'system';
8989
COMMAND_TERAGREP_MODE_VERSION: 'version';
9090
COMMAND_TERAGREP_MODE_ARCHIVE: 'archive';
91+
COMMAND_TERAGREP_MODE_MIGRATE: 'migrate' | 'MIGRATE';
92+
COMMAND_TERAGREP_MODE_EPOCH: 'epoch' | 'EPOCH';
9193
COMMAND_TERAGREP_MODE_SUMMARY: 'summary' -> pushMode(DEFAULT_MODE);
9294
COMMAND_TERAGREP_MODE_RETENTION: 'retention=' -> pushMode(GET_SPAN);
9395
COMMAND_TERAGREP_MODE_CODEC: 'codec=' -> pushMode(GET_STRING);
94-
COMMAND_TERAGREP_MODE_TABLE: 'table' -> pushMode(GET_FIELD);
96+
COMMAND_TERAGREP_MODE_TABLE: ('table' | 'TABLE') '='? -> pushMode(GET_FIELD);
9597

9698
COMMAND_TERAGREP_MODE_HDFS_FORMAT: 'format=';
9799
COMMAND_TERAGREP_MODE_CSV_FORMAT: ('csv'|'CSV');

src/main/antlr4/imports/DPLParserTransform_teragrep.g4

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ t_execParameter
6161
| t_tokenizerParameter
6262
| t_regexextractParameter
6363
| t_dynatraceParameter
64-
| t_forEachBatchParameter)
64+
| t_forEachBatchParameter
65+
| t_migrateParameter)
6566
;
6667

6768
t_forEachBatchParameter
@@ -209,4 +210,9 @@ t_retentionParameter
209210

210211
t_codecParameter
211212
: COMMAND_TERAGREP_MODE_CODEC stringType
212-
;
213+
;
214+
215+
t_migrateParameter
216+
: COMMAND_TERAGREP_MODE_MIGRATE COMMAND_TERAGREP_MODE_EPOCH
217+
;
218+

src/test/java/com/teragrep/pth_03/tests/TeragrepSyntaxTests.java

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,24 @@
4747

4848
import com.teragrep.pth_03.ParserStructureTestingUtility;
4949
import com.teragrep.pth_03.ParserSyntaxTestingUtility;
50+
import com.teragrep.pth_03.antlr.DPLLexer;
51+
import com.teragrep.pth_03.antlr.DPLParser;
52+
import org.antlr.v4.runtime.BailErrorStrategy;
53+
import org.antlr.v4.runtime.CharStream;
54+
import org.antlr.v4.runtime.CharStreams;
55+
import org.antlr.v4.runtime.CommonTokenStream;
56+
import org.antlr.v4.runtime.Token;
57+
import org.antlr.v4.runtime.misc.ParseCancellationException;
5058
import org.junit.jupiter.api.Assertions;
5159
import org.junit.jupiter.params.ParameterizedTest;
5260
import org.junit.jupiter.params.provider.ValueSource;
5361
import org.w3c.dom.NodeList;
5462

63+
import java.util.Arrays;
64+
import java.util.List;
65+
import java.util.Locale;
66+
import java.util.stream.Collectors;
67+
5568
import static org.junit.jupiter.api.Assertions.assertEquals;
5669

5770
public class TeragrepSyntaxTests {
@@ -558,4 +571,72 @@ void testTeragrepGetConfig(String arg) {
558571
Assertions.assertEquals("get", configGetNodes.item(0).getTextContent());
559572
Assertions.assertEquals("config", configGetNodes.item(1).getTextContent());
560573
}
574+
575+
@ParameterizedTest(name = "{index} command = ''{0}''")
576+
@ValueSource(strings = {
577+
"| teragrep exec migrate epoch",
578+
"| teragrep exec MIGRATE EPOCH"
579+
})
580+
void testMigrateCommandTokenStrings(final String command) {
581+
final CharStream input = CharStreams.fromString(command);
582+
final DPLLexer lexer = new DPLLexer(input);
583+
final CommonTokenStream tokens = new CommonTokenStream(lexer);
584+
tokens.fill();
585+
final List<String> tokenStrings = tokens.getTokens().stream()
586+
.filter(t -> t.getChannel() == Token.DEFAULT_CHANNEL)
587+
.map(Token::getText)
588+
.map(s -> s.toLowerCase(Locale.ROOT))
589+
.map(s -> s.replace("\"", "").replace("'", "")) // remove quotes
590+
.collect(Collectors.toList());
591+
final List<String> expectedTokenStrings = Arrays.asList("|", "teragrep", "exec", "migrate", "epoch", "<eof>");
592+
Assertions.assertEquals(expectedTokenStrings, tokenStrings);
593+
}
594+
595+
@ParameterizedTest(name = "{index} command = ''{0}''")
596+
@ValueSource(strings = {
597+
"| teragrep exec migrate epoch",
598+
"| teragrep exec MIGRATE EPOCH"
599+
})
600+
public void testMigrateEpochCommand(final String command) {
601+
final ParserStructureTestingUtility util = new ParserStructureTestingUtility();
602+
final String hierarchyXPath = "/root/transformStatement/teragrepTransformation/t_execParameter/t_migrateParameter\n";
603+
final Object hierarchyResult = Assertions.assertDoesNotThrow(() -> util.xpathQuery(command, hierarchyXPath, false));
604+
final NodeList hierarchyNodes = (NodeList) hierarchyResult;
605+
Assertions.assertEquals(1, hierarchyNodes.getLength(),
606+
"expected hierarchyNodes length to be 1 for input <" + command + ">");
607+
}
608+
609+
@ParameterizedTest(name = "{index} command = ''{0}''")
610+
@ValueSource(strings = {
611+
"| teragrep exec migrate epoch"
612+
})
613+
public void adminMigrateEpochSyntaxParseTest(final String command) {
614+
Assertions.assertDoesNotThrow(() -> {
615+
final CharStream input = CharStreams.fromString(command);
616+
final DPLLexer lexer = new DPLLexer(input);
617+
final DPLParser parser = new DPLParser(new CommonTokenStream(lexer));
618+
parser.setErrorHandler(new BailErrorStrategy()); // first syntax error aborts parsing
619+
final DPLParser.RootContext root = parser.root();
620+
Assertions.assertNotNull(root);
621+
Assertions.assertEquals(0, parser.getNumberOfSyntaxErrors());
622+
});
623+
}
624+
625+
@ParameterizedTest(name = "{index} command = ''{0}''")
626+
@ValueSource(strings = {
627+
"| teragrep exec migrate",
628+
"| teragrep exec migrate migrate epoch",
629+
"| teragrep exec migrate epoch epoch",
630+
"| teragrep exec MiGrAtE epoch",
631+
"| teragrep exec migrate EpOcH"
632+
})
633+
public void testInvalidRegexMigrateThrowsException(final String command) {
634+
Assertions.assertThrows(ParseCancellationException.class, () -> {
635+
final CharStream input = CharStreams.fromString(command);
636+
final DPLLexer lexer = new DPLLexer(input);
637+
final DPLParser parser = new DPLParser(new CommonTokenStream(lexer));
638+
parser.setErrorHandler(new BailErrorStrategy()); // first syntax error aborts parsing
639+
parser.root();
640+
});
641+
}
561642
}

0 commit comments

Comments
 (0)