optimize timeboundary for min or max bound

This commit is contained in:
Xavier Léauté 2016-03-11 14:00:32 -08:00
parent f381c6066e
commit 6f0d6ef0e9
3 changed files with 122 additions and 54 deletions

View File

@ -176,11 +176,11 @@ public class TimeBoundaryQuery extends BaseQuery<Result<TimeBoundaryResultValue>
final DateTime minTime;
final DateTime maxTime;
if (bound.equalsIgnoreCase(MIN_TIME)) {
if (isMinTime()) {
ts = min;
minTime = min;
maxTime = null;
} else if (bound.equalsIgnoreCase(MAX_TIME)) {
} else if (isMaxTime()) {
ts = max;
minTime = null;
maxTime = max;
@ -193,6 +193,16 @@ public class TimeBoundaryQuery extends BaseQuery<Result<TimeBoundaryResultValue>
return buildResult(ts, minTime, maxTime);
}
boolean isMinTime()
{
return bound.equalsIgnoreCase(MIN_TIME);
}
boolean isMaxTime()
{
return bound.equalsIgnoreCase(MAX_TIME);
}
@Override
public String toString()
{

View File

@ -65,8 +65,8 @@ public class TimeBoundaryQueryQueryToolChest
return segments;
}
final T min = segments.get(0);
final T max = segments.get(segments.size() - 1);
final T min = query.isMaxTime() ? null : segments.get(0);
final T max = query.isMinTime() ? null : segments.get(segments.size() - 1);
return Lists.newArrayList(
Iterables.filter(

View File

@ -40,66 +40,66 @@ import java.util.List;
*/
public class TimeBoundaryQueryQueryToolChestTest
{
private static final TimeBoundaryQuery TIME_BOUNDARY_QUERY = new TimeBoundaryQuery(
new TableDataSource("test"),
null,
null,
null
);
private static final TimeBoundaryQuery MAXTIME_BOUNDARY_QUERY = new TimeBoundaryQuery(
new TableDataSource("test"),
null,
TimeBoundaryQuery.MAX_TIME,
null
);
private static final TimeBoundaryQuery MINTIME_BOUNDARY_QUERY = new TimeBoundaryQuery(
new TableDataSource("test"),
null,
TimeBoundaryQuery.MIN_TIME,
null
);
private static LogicalSegment createLogicalSegment(final Interval interval)
{
return new LogicalSegment()
{
@Override
public Interval getInterval()
{
return interval;
}
};
}
@Test
public void testFilterSegments() throws Exception
{
List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments(
null,
TIME_BOUNDARY_QUERY,
Arrays.asList(
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01/P1D");
}
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T01/PT1H");
}
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T02/PT1H");
}
}
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
createLogicalSegment(new Interval("2013-01-02/P1D")),
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
)
);
Assert.assertEquals(segments.size(), 3);
Assert.assertEquals(6, segments.size());
List<LogicalSegment> expected = Arrays.asList(
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01/P1D");
}
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T01/PT1H");
}
},
new LogicalSegment()
{
@Override
public Interval getInterval()
{
return new Interval("2013-01-01T02/PT1H");
}
}
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
);
for (int i = 0; i < segments.size(); i++) {
@ -107,6 +107,64 @@ public class TimeBoundaryQueryQueryToolChestTest
}
}
@Test
public void testMaxTimeFilterSegments() throws Exception
{
List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments(
MAXTIME_BOUNDARY_QUERY,
Arrays.asList(
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
createLogicalSegment(new Interval("2013-01-02/P1D")),
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
)
);
Assert.assertEquals(3, segments.size());
List<LogicalSegment> expected = Arrays.asList(
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
);
for (int i = 0; i < segments.size(); i++) {
Assert.assertEquals(segments.get(i).getInterval(), expected.get(i).getInterval());
}
}
@Test
public void testMinTimeFilterSegments() throws Exception
{
List<LogicalSegment> segments = new TimeBoundaryQueryQueryToolChest().filterSegments(
MINTIME_BOUNDARY_QUERY,
Arrays.asList(
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H")),
createLogicalSegment(new Interval("2013-01-02/P1D")),
createLogicalSegment(new Interval("2013-01-03T01/PT1H")),
createLogicalSegment(new Interval("2013-01-03T02/PT1H")),
createLogicalSegment(new Interval("2013-01-03/P1D"))
)
);
Assert.assertEquals(3, segments.size());
List<LogicalSegment> expected = Arrays.asList(
createLogicalSegment(new Interval("2013-01-01/P1D")),
createLogicalSegment(new Interval("2013-01-01T01/PT1H")),
createLogicalSegment(new Interval("2013-01-01T02/PT1H"))
);
for (int i = 0; i < segments.size(); i++) {
Assert.assertEquals(segments.get(i).getInterval(), expected.get(i).getInterval());
}
}
@Test
public void testCacheStrategy() throws Exception
{