Commit 879c2406 authored by Paul Asmuth's avatar Paul Asmuth
Browse files

basic ring buffer search/retrieval procedure

parent 6f4ceb6c
Loading
Loading
Loading
Loading
+60 −3
Original line number Diff line number Diff line
@@ -7,6 +7,8 @@

package com.fnordmetric.enterprise

import scala.collection.mutable.ListBuffer

case class MetricKey(key: String, mode: String, flush_interval: Long)

class Metric(key: MetricKey) {
@@ -49,6 +51,7 @@ class Metric(key: MetricKey) {
  def flush_rbuf = this.synchronized {
    val flush_range = rbuf.size - rbuf_seek_pos

    // FIXPAUL: wrong order!
    for (sample <- rbuf.tail(flush_range))
      swap.put(sample._1, sample._2)

@@ -70,12 +73,66 @@ class Metric(key: MetricKey) {

  // returns all aggregated values for this metric in the specified time
  // range. if time1 is 0 then only the first value at time0 is returned
  // note that time0 > time1! this method is threadsafe
  def values_in(time0: Long, time1: Long) : List[(Long, Double)] = {
    val lst = List[(Long, Double)]()
    val lst = ListBuffer[(Long, Double)]()
    var found_start : Long = 0

    // FIXPAUL: skip!

    val rbuf_snap = rbuf.tail(rbuf.size)
    var rbuf_last : Long = java.lang.Long.MAX_VALUE
    var rbuf_pos = 0

    // search our rbuf snapshot backwards
    while (rbuf_pos > 0 && rbuf_pos < rbuf_snap.size) {
      val cur = rbuf_snap(rbuf_pos)

      // since the snapshot is not atomic, we need to check if we hit
      // rbuf wrap and exit if so
      if (cur._1 < rbuf_last)
        rbuf_last = cur._1
      else
        rbuf_pos = -1

      // if we are already beyond time1 we can exit
      if (time1 != 0 && (cur._1 < time1))
        rbuf_pos = -1

      // if we are only looking for a single value and already beyond time0
      // plus flush_interval and didnt find a value yet, we can exit
      if (time1 == 0 && (cur._1 < (time0 + key.flush_interval)))
        rbuf_pos = -1

      // continues only if we didn't hit the buffer wrap
      if (rbuf_pos > 0) {

        // check if we found the start of the range yet
        if (cur._1 <= time0 && ((cur._1 >= time1) || time1 == 0)) {

          // collect all matching items
          lst += cur

          if (found_start == 0)
            found_start = cur._1

          // if we are looking only for a single value we can exit now
          if (time1 == 0)
            rbuf_pos = -1

        }
      }
    }

    // exit if we have already seen the whole time range and don't need to
    // search the swapfile anymore
    if (rbuf_last <= time1)
      return lst.toList

    // here be dragons
    // FIXPAUL: here be dragons -> search in swapfile
    println("SEARCH_SWPFILE")

    lst
    lst.toList
  }

}