Skip to content

Commit 0568642

Browse files
committed
fix missing operations GE, LE for time qualifiers. Cleanup and improved testing
1 parent 0cbc7f5 commit 0568642

8 files changed

Lines changed: 162 additions & 22 deletions

File tree

src/main/java/com/teragrep/pth_06/ast/analyze/ScanPlanCollection.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ public List<ScanPlan> asList() {
108108
final String journaldbName = config.archiveConfig.dbJournalDbName;
109109
final String streamdbName = config.archiveConfig.dbStreamDbName;
110110
final String bloomdbName = config.archiveConfig.bloomDbName;
111-
final boolean hideDatabaseExceptions = config.archiveConfig.hideDatabaseExceptions;
112111
final Connection connection;
113112
try {
114113
connection = DriverManager.getConnection(url, userName, password);
@@ -118,7 +117,7 @@ public List<ScanPlan> asList() {
118117
}
119118

120119
final Settings settings;
121-
if (hideDatabaseExceptions) {
120+
if (config.archiveConfig.hideDatabaseExceptions) {
122121
settings = new Settings()
123122
.withRenderMapping(new RenderMapping().withSchemata(new MappedSchema().withInput("streamdb").withOutput(streamdbName), new MappedSchema().withInput("journaldb").withOutput(journaldbName), new MappedSchema().withInput("bloomdb").withOutput(bloomdbName))).withThrowExceptions(ThrowExceptions.THROW_NONE);
124123
LOGGER.warn("SQL Exceptions set to THROW_NONE");

src/main/java/com/teragrep/pth_06/ast/transform/PrunedInvalidTimeQualifier.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public final class PrunedInvalidTimeQualifier implements ExpressionTransformatio
6060
private final List<String> acceptedOperations;
6161

6262
public PrunedInvalidTimeQualifier(final Expression origin) {
63-
this(origin, Arrays.asList("EQUALS", "LEQ", "GEQ", "MORE_THAN", "LESS_THAN"));
63+
this(origin, Arrays.asList("EQUALS", "GE", "LE", "LEQ", "GEQ"));
6464
}
6565

6666
private PrunedInvalidTimeQualifier(final Expression origin, List<String> acceptedOperations) {

src/main/java/com/teragrep/pth_06/ast/transform/WithDefaultValues.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
package com.teragrep.pth_06.ast.transform;
4747

4848
import com.teragrep.pth_06.ast.Expression;
49+
import com.teragrep.pth_06.ast.PrintAST;
4950
import com.teragrep.pth_06.ast.xml.AndExpression;
5051
import com.teragrep.pth_06.ast.xml.OrExpression;
5152
import com.teragrep.pth_06.ast.xml.XMLValueExpressionImpl;
@@ -55,6 +56,7 @@
5556
import java.util.ArrayList;
5657
import java.util.HashSet;
5758
import java.util.List;
59+
import java.util.Objects;
5860
import java.util.Set;
5961

6062
public final class WithDefaultValues implements ExpressionTransformation<Expression> {
@@ -168,4 +170,26 @@ private Set<Expression.Tag> expressionTags(final Expression expr) {
168170
}
169171
return tags;
170172
}
173+
174+
@Override
175+
public boolean equals(final Object o) {
176+
if (o == null) {
177+
return false;
178+
}
179+
if (getClass() != o.getClass()) {
180+
return false;
181+
}
182+
final WithDefaultValues that = (WithDefaultValues) o;
183+
return defaultMinusHours == that.defaultMinusHours && Objects.equals(root, that.root);
184+
}
185+
186+
@Override
187+
public int hashCode() {
188+
return Objects.hash(defaultMinusHours, root);
189+
}
190+
191+
@Override
192+
public String toString() {
193+
return new PrintAST(transformed()).asString();
194+
}
171195
}

src/main/java/com/teragrep/pth_06/ast/xml/XMLQuery.java

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
package com.teragrep.pth_06.ast.xml;
4747

4848
import com.teragrep.pth_06.ast.Expression;
49+
import com.teragrep.pth_06.ast.PrintAST;
4950
import org.slf4j.Logger;
5051
import org.slf4j.LoggerFactory;
5152
import org.w3c.dom.Document;
@@ -62,6 +63,7 @@
6263
import java.io.StringReader;
6364
import java.util.ArrayList;
6465
import java.util.List;
66+
import java.util.Objects;
6567

6668
public final class XMLQuery {
6769

@@ -98,16 +100,12 @@ private Expression elementToNode(final Element element) {
98100
final Expression result;
99101
switch (tagName.toLowerCase()) {
100102
case "and":
101-
final List<Expression> andExpMembers = visitLogical(element);
102-
final Expression andLeft = andExpMembers.get(0);
103-
final Expression andRight = andExpMembers.get(1);
104-
result = new AndExpression(andLeft, andRight);
103+
final List<Expression> andExpressionChildren = visitLogical(element);
104+
result = new AndExpression(andExpressionChildren);
105105
break;
106106
case "or":
107-
final List<Expression> expressions = visitLogical(element);
108-
final Expression left = expressions.get(0);
109-
final Expression right = expressions.get(1);
110-
result = new OrExpression(left, right);
107+
final List<Expression> orExpressionChildren = visitLogical(element);
108+
result = new OrExpression(orExpressionChildren);
111109
break;
112110
case "index":
113111
result = visitLeaf(element, Expression.Tag.INDEX);
@@ -156,6 +154,31 @@ private List<Expression> visitLogical(final Element element) {
156154
throw new IllegalArgumentException("Element children contained a non Element node");
157155
}
158156
}
157+
if (expressions.isEmpty()) {
158+
throw new IllegalStateException("Logical expression had no children");
159+
}
159160
return expressions;
160161
}
162+
163+
@Override
164+
public boolean equals(final Object o) {
165+
if (o == null) {
166+
return false;
167+
}
168+
if (getClass() != o.getClass()) {
169+
return false;
170+
}
171+
final XMLQuery xmlQuery = (XMLQuery) o;
172+
return Objects.equals(xmlString, xmlQuery.xmlString);
173+
}
174+
175+
@Override
176+
public int hashCode() {
177+
return Objects.hashCode(xmlString);
178+
}
179+
180+
@Override
181+
public String toString() {
182+
return new PrintAST(asAST()).asString();
183+
}
161184
}

src/main/java/com/teragrep/pth_06/config/HBaseConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public HBaseConfig(final Map<String, String> opts) {
7272
}
7373

7474
public Configuration asHadoopConfig() {
75-
Configuration config = new Configuration(HBaseConfiguration.create());
75+
final Configuration config = HBaseConfiguration.create();
7676
config.set("hbase.master.hostname", hostname);
7777
config.set("hbase.regionserver.hostname", regionServerHostname);
7878
config.set("hbase.zookeeper.quorum", zookeeperQuorum);
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Teragrep Archive Datasource (pth_06)
3+
* Copyright (C) 2021-2024 Suomen Kanuuna Oy
4+
*
5+
* This program is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Affero General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Affero General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU Affero General Public License
16+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
17+
*
18+
*
19+
* Additional permission under GNU Affero General Public License version 3
20+
* section 7
21+
*
22+
* If you modify this Program, or any covered work, by linking or combining it
23+
* with other code, such other code is not for that reason alone subject to any
24+
* of the requirements of the GNU Affero GPL version 3 as long as this Program
25+
* is the same Program as licensed from Suomen Kanuuna Oy without any additional
26+
* modifications.
27+
*
28+
* Supplemented terms under GNU Affero General Public License version 3
29+
* section 7
30+
*
31+
* Origin of the software must be attributed to Suomen Kanuuna Oy. Any modified
32+
* versions must be marked as "Modified version of" The Program.
33+
*
34+
* Names of the licensors and authors may not be used for publicity purposes.
35+
*
36+
* No rights are granted for use of trade names, trademarks, or service marks
37+
* which are in The Program if any.
38+
*
39+
* Licensee must indemnify licensors and authors for any liability that these
40+
* contractual assumptions impose on licensors and authors.
41+
*
42+
* To the extent this program is licensed as part of the Commercial versions of
43+
* Teragrep, the applicable Commercial License may apply to this file if you as
44+
* a licensee so wish it.
45+
*/
46+
package com.teragrep.pth_06.ast;
47+
48+
import com.teragrep.pth_06.ast.xml.AndExpression;
49+
import com.teragrep.pth_06.ast.xml.XMLValueExpressionImpl;
50+
import org.junit.jupiter.api.Assertions;
51+
import org.junit.jupiter.api.Test;
52+
53+
public final class PrintASTTest {
54+
55+
@Test
56+
public void printSingleValue() {
57+
Expression expression = new XMLValueExpressionImpl("value", "operation", Expression.Tag.INDEX);
58+
String print = new PrintAST(expression).asString();
59+
String expected = "VALUE(INDEX val=value op=operation)";
60+
Assertions.assertEquals(expected, print);
61+
}
62+
63+
@Test
64+
public void printLogical() {
65+
Expression left = new XMLValueExpressionImpl("earliest", "GE", Expression.Tag.EARLIEST);
66+
Expression right = new XMLValueExpressionImpl("latest", "LE", Expression.Tag.LATEST);
67+
Expression expression = new AndExpression(left, right);
68+
String print = new PrintAST(expression).asString();
69+
String expected = "AND\n" + " VALUE(EARLIEST val=earliest op=GE)\n" + " VALUE(LATEST val=latest op=LE)";
70+
Assertions.assertEquals(expected, print);
71+
}
72+
73+
}

src/test/java/com/teragrep/pth_06/ast/transform/PrunedInvalidTimeQualifierTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,17 @@ public void testAllEqualsRemainsUnchanged() {
8585
Expression transformed = new PrunedInvalidTimeQualifier(andExpression).transformed();
8686
Assertions.assertEquals(andExpression, transformed);
8787
}
88+
89+
@Test
90+
public void testSupportedOperations() {
91+
Expression equals = new XMLValueExpressionImpl("1000", "EQUALS", Expression.Tag.EARLIEST);
92+
Expression GE = new XMLValueExpressionImpl("1000", "GE", Expression.Tag.EARLIEST);
93+
Expression LE = new XMLValueExpressionImpl("1000", "LE", Expression.Tag.EARLIEST);
94+
Expression GEQ = new XMLValueExpressionImpl("1000", "GEQ", Expression.Tag.EARLIEST);
95+
Expression LEQ = new XMLValueExpressionImpl("1000", "LEQ", Expression.Tag.EARLIEST);
96+
AndExpression andExpression = new AndExpression(Arrays.asList(equals, GE, LE, GEQ, LEQ));
97+
Expression pruned = new PrunedInvalidTimeQualifier(andExpression).transformed();
98+
Assertions.assertTrue(pruned.isLogical());
99+
Assertions.assertEquals(andExpression.children().size(), pruned.asLogical().children().size());
100+
}
88101
}

src/test/java/com/teragrep/pth_06/ast/xml/XMLQueryTest.java

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
package com.teragrep.pth_06.ast.xml;
4747

4848
import com.teragrep.pth_06.ast.Expression;
49-
import com.teragrep.pth_06.ast.PrintAST;
5049
import org.junit.jupiter.api.Assertions;
5150
import org.junit.jupiter.api.Test;
5251

@@ -56,20 +55,29 @@ public final class XMLQueryTest {
5655
public void testXMLFromQuery() {
5756
String query = "<AND><OR><index value=\"index_1\" operation=\"EQUALS\"/><sourcetype value=\"index_2\" operation=\"EQUALS\"/></OR><earliest value=\"1000\" operation=\"EQUALS\"/></AND>";
5857
XMLQuery xmlQuery = new XMLQuery(query);
59-
Expression root = xmlQuery.asAST();
60-
String result = new PrintAST(root).asString();
61-
String expected = "AND\n" + " OR\n" + " VALUE(INDEX val=index_1 op=EQUALS)\n"
62-
+ " VALUE(SOURCETYPE val=index_2 op=EQUALS)\n" + " VALUE(EARLIEST val=1000 op=EQUALS)";
63-
Assertions.assertEquals(expected, result);
58+
Expression expected = new AndExpression(
59+
new OrExpression(new XMLValueExpressionImpl("index_1", "EQUALS", Expression.Tag.INDEX), new XMLValueExpressionImpl("index_2", "EQUALS", Expression.Tag.SOURCETYPE)), new XMLValueExpressionImpl("1000", "EQUALS", Expression.Tag.EARLIEST)
60+
);
61+
Assertions.assertEquals(expected, xmlQuery.asAST());
6462
}
6563

6664
@Test
6765
public void testSingleElementQuery() {
6866
String query = "<index value=\"index_1\" operation=\"EQUALS\"/>";
6967
XMLQuery xmlQuery = new XMLQuery(query);
70-
Expression root = xmlQuery.asAST();
71-
String result = new PrintAST(root).asString();
72-
String expected = "VALUE(INDEX val=index_1 op=EQUALS)";
73-
Assertions.assertEquals(expected, result);
68+
Expression expected = new XMLValueExpressionImpl("index_1", "EQUALS", Expression.Tag.INDEX);
69+
Assertions.assertEquals(expected, xmlQuery.asAST());
70+
}
71+
72+
@Test
73+
public void testIndexPlusTimeQualifier() {
74+
String queryXML = "<AND><index operation=\"EQUALS\" value=\"example\"/><earliest operation=\"GE\" value=\"1447400598\"/></AND>";
75+
XMLQuery query = new XMLQuery(queryXML);
76+
Expression expected = new AndExpression(
77+
new XMLValueExpressionImpl("example", "EQUALS", Expression.Tag.INDEX),
78+
new XMLValueExpressionImpl("1447400598", "GE", Expression.Tag.EARLIEST)
79+
);
80+
Assertions.assertEquals(expected, query.asAST());
81+
7482
}
7583
}

0 commit comments

Comments
 (0)