Skip to content

Commit

Permalink
append to swapfiles if they exist already
Browse files Browse the repository at this point in the history
  • Loading branch information
asmuth committed Feb 25, 2013
1 parent 9aa1430 commit 1031601
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions fnordmetric-enterprise/src/SwapFile.scala
Expand Up @@ -14,7 +14,9 @@ import java.nio.ByteOrder
import scala.collection.mutable.ListBuffer

class SwapFile(metric_key: MetricKey) {
var write_pos = 0

val buffer = ByteBuffer.allocate(512)
buffer.order(ByteOrder.BIG_ENDIAN)

// each sample is 18 bytes big (2 bytes header, 8 bytes time and
// 8 bytes value as double precision ieee 754 float)
Expand All @@ -26,8 +28,8 @@ class SwapFile(metric_key: MetricKey) {
val file = new RandomAccessFile(new File(
FnordMetric.CONFIG('swap_prefix), file_name), "rwd")

val buffer = ByteBuffer.allocate(512)
buffer.order(ByteOrder.BIG_ENDIAN)
var write_pos = file.length.toInt
println(write_pos)

// adds a new (time, value) tuple to be written to the swap file
// but does not write it yet. this method is not thread safe!
Expand All @@ -47,7 +49,7 @@ class SwapFile(metric_key: MetricKey) {
def flush : Unit = {
file.synchronized {
file.seek(write_pos)
file.write(buffer.array)
file.write(buffer.array, 0, buffer.position)
}

write_pos += BLOCK_SIZE
Expand Down

0 comments on commit 1031601

Please sign in to comment.