Browse files

perform the ring buffer search without copying the buffer

  • Loading branch information...
1 parent 2cc45d3 commit 91e47b043ff1238f48f71518fa2043a004253b7c @paulasmuth committed Feb 25, 2013
Showing with 14 additions and 4 deletions.
  1. +3 −3 fnordmetric-enterprise/src/Metric.scala
  2. +11 −1 fnordmetric-enterprise/src/RingBuffer.scala
View
6 fnordmetric-enterprise/src/Metric.scala
@@ -99,13 +99,13 @@ class Metric(key: MetricKey) {
// we may have to load one more value from the swapfile instead from
// the in memory ring buffer
val rbuf_snap_len = rbuf.size
- val rbuf_snap_pos = rbuf.position
+ val rbuf_snap_pos = rbuf.cursor
// search the ring buffer backwards without synchronization. the basic
// assumption here is that the system time will only progress forward.
// if the system time should jump backwards this would race
- while (rbuf_pos >= 0 && rbuf_pos < rbuf_snap.size) {
- val cur = rbuf_snap(rbuf_pos)
+ while (rbuf_pos >= 0 && rbuf_pos < rbuf_snap_len) {
+ val cur = rbuf.at(rbuf_snap_pos, rbuf_pos)
// since this is not synchronized, we need to check if we hit the
// rbuf wrapping point and exit if so. this code would race if the
View
12 fnordmetric-enterprise/src/RingBuffer.scala
@@ -51,15 +51,25 @@ class RingBuffer[T: Manifest](capacity: Int) {
lst.toList
}
+ // retrieves one item from the ring buffer at offset by walking the ring
+ // buffer starting at position in reverse chronological order (from most
+ // recent to oldest)
+ def at(position: Int, offset: Int) : T =
+ backend((((position - offset) % capacity) + capacity) % capacity)
+
// removes the first num items from the start of the ring buffer (oldest
// items get removed first)
def seek(num: Int) = {
start = (start + num) % capacity
size -= num
}
- // returns the remaning number of free slots in the ringbuffer
+ // returns the remaning number of free slots in the ring buffer
def remaining : Int =
capacity - size
+ // returns a curser to the current end position of the ring buffer
+ def cursor : Int =
+ end
+
}

0 comments on commit 91e47b0

Please sign in to comment.