private def len()

in storehaus-algebra/src/main/scala/com/twitter/storehaus/algebra/query/CalendarTimeQueryRange.scala [51:87]


  private def len(dr: DateRange) =
    AbsoluteDuration.fromMillisecs(dr.end.timestamp - dr.start.timestamp + 1L)

  private def outsideRange(filterDR: DateRange, child: DateRange) =
    child.start >= filterDR.end || child.end <= filterDR.start

  def query(dr: DateRange): Set[CalendarBucket] =
    extract(dr, Set(dr), 0, allBuckets, Set[CalendarBucket]())

  @tailrec
  private def extract(
    filterDr: DateRange,
    drSet: Set[DateRange],
    curIndx: Int,
    remainingDurations: List[Duration],
    acc: Set[CalendarBucket]
  ): Set[CalendarBucket] = {
    remainingDurations match {
      case Nil => acc
      case head :: tail =>
        // expand the DR
        val expandedOut = drSet.map { dr =>
          DateRange(head.floorOf(dr.start), head.floorOf(dr.end) + head)
            .each(head)
            .filter(!outsideRange(filterDr, _))
            .filter(len(_).toMillisecs > 1L)
            .toSet
        }.foldLeft(Set[DateRange]())(_ ++ _)
        // Things which only partially fit in this time range
        val others = expandedOut.filter(!filterDr.contains(_))
        // Things which fit fully in this time range
        val fullyInRange = expandedOut
          .filter(filterDr.contains)
          .map(x => CalendarBucket(curIndx, x.start.timestamp))
        extract(filterDr, others, curIndx + 1, tail, acc ++ fullyInRange)
    }
  }