Skip to content

Commit a2a1884

Browse files
committed
code cleanup and remove unused old objects
1 parent d884df0 commit a2a1884

22 files changed

Lines changed: 163 additions & 1053 deletions

src/main/java/com/teragrep/pth10/ast/DPLParserConfig.java

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,10 @@
4545
*/
4646
package com.teragrep.pth10.ast;
4747

48-
import com.teragrep.pth10.ast.time.DefaultFormatAbsoluteTimestamp;
49-
import com.teragrep.pth10.ast.time.RelativeTimeParser;
50-
import com.teragrep.pth10.ast.time.RelativeTimestamp;
48+
import com.teragrep.pth10.ast.time.DPLTimestampImpl;
5149
import org.slf4j.Logger;
5250
import org.slf4j.LoggerFactory;
5351

54-
import java.sql.Timestamp;
5552
import java.util.LinkedHashMap;
5653
import java.util.Map;
5754

@@ -105,20 +102,7 @@ public String getEarliest() {
105102
* @param earliest string value like -1h or actual timestamp
106103
*/
107104
public void setEarliest(String earliest) {
108-
long earliestEpoch = 0;
109-
Timestamp now = new Timestamp(System.currentTimeMillis());
110-
RelativeTimeParser rtParser = new RelativeTimeParser();
111-
112-
// Try to check if it is relative and catch exception
113-
try {
114-
RelativeTimestamp rtTimestamp = rtParser.parse(earliest); // can throw error if not relative timestamp
115-
earliestEpoch = rtTimestamp.calculate(now).getEpochSecond();
116-
}
117-
catch (NumberFormatException ne) {
118-
// absolute time
119-
earliestEpoch = new DefaultFormatAbsoluteTimestamp(earliest).zonedDateTime().toEpochSecond();
120-
}
121-
105+
final long earliestEpoch = new DPLTimestampImpl(earliest).zonedDateTime().toEpochSecond();
122106
config.put("earliest", earliest);
123107
config.put("earliestEpoch", earliestEpoch);
124108
}
@@ -139,20 +123,7 @@ public String getLatest() {
139123
* @param latest string value like -1h or actual timestamp
140124
*/
141125
public void setLatest(String latest) {
142-
long latestEpoch = 0;
143-
Timestamp now = new Timestamp(System.currentTimeMillis());
144-
RelativeTimeParser rtParser = new RelativeTimeParser();
145-
146-
// Try to check if it is relative and catch exception
147-
try {
148-
RelativeTimestamp rtTimestamp = rtParser.parse(latest); // can throw exception if not relative timestamp
149-
latestEpoch = rtTimestamp.calculate(now).getEpochSecond();
150-
}
151-
catch (NumberFormatException ne) {
152-
// absolute time
153-
latestEpoch = new DefaultFormatAbsoluteTimestamp(latest).zonedDateTime().toEpochSecond();
154-
}
155-
126+
final long latestEpoch = new DPLTimestampImpl(latest).zonedDateTime().toEpochSecond();
156127
config.put("latest", latest);
157128
config.put("latestEpoch", latestEpoch);
158129
}

src/main/java/com/teragrep/pth10/ast/commands/evalstatement/UDFs/Mvrange.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@
4545
*/
4646
package com.teragrep.pth10.ast.commands.evalstatement.UDFs;
4747

48-
import com.teragrep.pth10.ast.time.RelativeTimeParser;
4948
import com.teragrep.pth10.ast.time.RelativeTimestamp;
5049
import org.apache.spark.sql.api.java.UDF3;
5150

5251
import java.io.Serializable;
53-
import java.sql.Timestamp;
52+
import java.time.Instant;
53+
import java.time.ZoneId;
54+
import java.time.ZonedDateTime;
5455
import java.util.ArrayList;
5556
import java.util.List;
5657

@@ -102,11 +103,11 @@ else if (stepObj instanceof String) {
102103
long time = start;
103104
rv.add(String.valueOf(time));
104105

105-
RelativeTimeParser rtParser = new RelativeTimeParser();
106-
RelativeTimestamp rtTimestamp = rtParser.parse("+" + stepStr);
107106
// Go until incremented past end
108107
while (time < end) {
109-
time = rtTimestamp.calculate(new Timestamp(time * 1000L)).getEpochSecond();
108+
final ZonedDateTime startTime = ZonedDateTime.ofInstant(Instant.ofEpochSecond(time), ZoneId.of("UTC"));
109+
final RelativeTimestamp relativeTimestamp = new RelativeTimestamp("+" + stepStr, startTime);
110+
time = relativeTimestamp.zonedDateTime().toEpochSecond();
110111

111112
// If time went past end, stop incrementing and don't add to mv field
112113
if (time > end) {

src/main/java/com/teragrep/pth10/ast/commands/evalstatement/UDFs/Relative_time.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@
4545
*/
4646
package com.teragrep.pth10.ast.commands.evalstatement.UDFs;
4747

48-
import com.teragrep.pth10.ast.time.RelativeTimeParser;
4948
import com.teragrep.pth10.ast.time.RelativeTimestamp;
5049
import org.apache.spark.sql.api.java.UDF2;
5150

5251
import java.io.Serializable;
53-
import java.sql.Timestamp;
52+
import java.time.Instant;
53+
import java.time.ZoneId;
54+
import java.time.ZonedDateTime;
5455

5556
/**
5657
* UDF for command relative_time(unixtime, modifier)<br>
@@ -63,9 +64,10 @@ public class Relative_time implements UDF2<Long, String, Long>, Serializable {
6364

6465
@Override
6566
public Long call(Long unixtime, String modifier) throws Exception {
66-
RelativeTimeParser rtParser = new RelativeTimeParser();
67-
RelativeTimestamp rtTimestamp = rtParser.parse(modifier);
68-
return rtTimestamp.calculate(new Timestamp(unixtime * 1000L)).getEpochSecond();
67+
final ZonedDateTime startTime = ZonedDateTime
68+
.ofInstant(Instant.ofEpochSecond(unixtime), ZoneId.systemDefault());
69+
final RelativeTimestamp relativeTimestamp = new RelativeTimestamp(modifier, startTime);
70+
return relativeTimestamp.zonedDateTime().toEpochSecond();
6971
}
7072

7173
}

src/main/java/com/teragrep/pth10/ast/time/BetweenYearsTimestamp.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

src/main/java/com/teragrep/pth10/ast/time/DPLTimestampImpl.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
*/
4646
package com.teragrep.pth10.ast.time;
4747

48+
import org.slf4j.Logger;
49+
import org.slf4j.LoggerFactory;
50+
4851
import java.time.ZoneId;
4952
import java.time.ZonedDateTime;
5053
import java.util.Objects;
@@ -56,10 +59,15 @@
5659
*/
5760
public final class DPLTimestampImpl implements DPLTimestamp {
5861

62+
private static final Logger LOGGER = LoggerFactory.getLogger(DPLTimestampImpl.class);
5963
private final String value;
6064
private final String timeformat;
6165
private final ZoneId zoneId;
6266

67+
public DPLTimestampImpl(final String value) {
68+
this(value, "", TimeZone.getDefault().toZoneId());
69+
}
70+
6371
public DPLTimestampImpl(final String value, final String timeformat) {
6472
this(value, timeformat, TimeZone.getDefault().toZoneId());
6573
}
@@ -71,15 +79,16 @@ public DPLTimestampImpl(final String value, final String timeformat, final ZoneI
7179
}
7280

7381
public ZonedDateTime zonedDateTime() {
82+
LOGGER.info("Incoming value <{}> to timestamp", value);
7483
final DPLTimestamp timestamp;
75-
final NewRelativeTimestamp relativeTimestamp = new NewRelativeTimestamp(value, zoneId);
84+
final RelativeTimestamp relativeTimestamp = new RelativeTimestamp(value, zoneId);
7685
if (!relativeTimestamp.isStub()) {
7786
timestamp = relativeTimestamp;
7887
}
7988
else {
8089
timestamp = new AbsoluteTimestamp(value, timeformat, zoneId);
8190
}
82-
return new BetweenYearsTimestamp(1000, 9999, timestamp).zonedDateTime();
91+
return timestamp.zonedDateTime();
8392
}
8493

8594
@Override

src/main/java/com/teragrep/pth10/ast/time/DefaultFormatAbsoluteTimestamp.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public ZonedDateTime zonedDateTime() {
9393
try {
9494
return absoluteTimestamp.zonedDateTime();
9595
}
96-
catch (RuntimeException ex) {
96+
catch (final RuntimeException ex) {
9797
LOGGER.debug("Couldn't parse value, exception <{}>", ex.getMessage());
9898
// passthrough
9999
}

src/main/java/com/teragrep/pth10/ast/time/NewRelativeTimestamp.java

Lines changed: 0 additions & 102 deletions
This file was deleted.

src/main/java/com/teragrep/pth10/ast/time/OffsetTimestamp.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public OffsetTimestamp(final Text offsetText, final ZonedDateTime startTime) {
7878

7979
@Override
8080
public ZonedDateTime zonedDateTime() {
81-
LOGGER.info("start time <{}>", startTime);
81+
LOGGER.debug("start time <{}>", startTime);
8282
final OffsetUnit unit = offsetUnit();
8383
final long amount = offsetAmount();
8484
final ZonedDateTime updatedTime;
@@ -112,8 +112,21 @@ public ZonedDateTime zonedDateTime() {
112112
throw new RuntimeException("Unsupported unit");
113113
}
114114

115-
LOGGER.info("offset time <{}>", updatedTime);
116-
return updatedTime;
115+
// ensure that year is between 1000-9999
116+
final long updatedYear = updatedTime.getYear();
117+
final ZonedDateTime updatedTimeWithYearBetweenRange;
118+
if (updatedYear > 9999) {
119+
updatedTimeWithYearBetweenRange = updatedTime.withYear(9999);
120+
}
121+
else if (updatedYear < 1000) {
122+
updatedTimeWithYearBetweenRange = updatedTime.withYear(1000);
123+
}
124+
else {
125+
updatedTimeWithYearBetweenRange = updatedTime.plusYears(amount);
126+
}
127+
128+
LOGGER.debug("offset time <{}>", updatedTimeWithYearBetweenRange);
129+
return updatedTimeWithYearBetweenRange;
117130
}
118131

119132
@Override

0 commit comments

Comments
 (0)