in storehaus-algebra/src/main/scala/com/twitter/storehaus/algebra/query/TimeRangeQuery.scala [40:61]
def normalize(in: TimeRange): TimeRange =
TimeRange(toBoundary(in.start), toBoundary(in.end))
protected def toBoundary(time: Long): Long =
(time / minBucketMs) * minBucketMs
def query(thisquery: TimeRange): Set[BucketTime] = {
// find the minimal covering set of [start,end]
@tailrec
def queryRec(q: TimeRange, acc: Set[BucketTime]): Set[BucketTime] = {
if (q.start >= q.end) acc
else {
val (bucketSize, idx) = buckets.dropWhile { case (size, _) =>
(q.start % size == 0) && (q.start + size <= q.end)
}.head
queryRec(TimeRange(q.start + bucketSize, q.end),
acc + BucketTime(idx, q.start/bucketSize))
}
}
// Kick off the recursion
queryRec(thisquery, Set[BucketTime]())
}