Timequery is an utility library to query time based events. It's small, slow and unmaintainable.
Requires JDK 1.8.
To add a dependency on Guava using Maven, use the following:
<dependency>
<groupId>org.weirdloop</groupId>
<artifactId>time-query</artifactId>
<version>1.0</version>
</dependency>
Time events can hold the any type of data, but keys are temporal instances.
TimeEvent.create(Instant.now());
or
TimeEvent.create(Instant.now(), 3);
Holds a queue with time events.
TimeQueue queue = TimeQueue.create(new LinkedList<>());
queue.add(TimeEvent.create(instant, value));
select count(event) from events where time > now() -1h groupy by time(5m), event
The equivalent of this typical query, at least on time-series databases, using TimeQuery.
Map<Integer, Long> histogram = TimeQuery.create(events)
.downTo(Span.of(create(minusAnHour), create(now)))
.groupBy(Buckets.of(5, ChronoUnit.MINUTES))
.histogram();
// Result
{0, 3}, {1, 5}, {2, 10}, ... {bucket-index, count}
Or just the interval windows:
TimeWindow<TimeEvent> windows = TimeQuery.create(events)
.groupBy(Buckets.of(5, ChronoUnit.MINUTES))
.windows();
// Result
TimeWindow:
[0] -> TimeWindowInterval@{TimeEvent[0], TimeEvent[1], TimeEvent[2], TimeEvent[3], TimeEvent[4]}
[1] -> TimeWindowInterval@{TimeEvent[5], TimeEvent[6], TimeEvent[7], TimeEvent[8], TimeEvent[9]}
Or using an interval spans:
TimeWindow<TimeEvent> timeWindow = TimeQuery.create(events)
.interval(Span.openClosed(create(fourHoursAgo), create(twoHoursAgo)))
.windows();
// Result
TimeWindow:
TimeWindowInterval@{TimeEvent[0], TimeEvent[1], ...}
Or even:
TimeWindow rangeQuery = TimeQuery.create(events)
.downTo(Span.closedOpen(create(AnHourAgo), create(now)))
.groupBy(Buckets.of(1, ChronoUnit.MINUTES))
.windows();