class Thrash

Public Class Methods

new(args={}) click to toggle source
# File lib/thrash/thrash.rb, line 3
def initialize(args={})
  # number of items to keep in buffer before writing
  @buffer_max = args[:buffer_max] || 100_000
  create_buffer!
end

Public Instance Methods

add(bucket, obj) click to toggle source

add object to bucket check if bucket is full and if it is write out and flush

# File lib/thrash/thrash.rb, line 12
def add(bucket, obj)
  @buffer[bucket] << obj
  check_and_write bucket
end
Also aliased as: write
finalize() click to toggle source

write and flush remaining buckets

# File lib/thrash/thrash.rb, line 21
def finalize
  @buffer.each_key do |bucket|
    write_and_flush bucket
  end
  clear_buffer!
  nil
end
write(bucket, obj)

I'm not sure which one to use

Alias for: add

Private Instance Methods

bucket_full?(bucket) click to toggle source

check if a buffer is over capacity

# File lib/thrash/thrash.rb, line 46
def bucket_full?(bucket)
  @buffer[bucket].size >= @buffer_max
end
check_and_write(bucket) click to toggle source

check if a buffer is full if so, write out and flush

# File lib/thrash/thrash.rb, line 39
def check_and_write(bucket)
  if bucket_full? bucket
    write_and_flush bucket
  end
end
clear_buffer!() click to toggle source

empty the buffer

# File lib/thrash/thrash.rb, line 62
def clear_buffer!
  @buffer.clear
end
create_buffer!() click to toggle source

instantiates a new buffer which for now is just a hash

# File lib/thrash/thrash.rb, line 33
def create_buffer!
  @buffer = Hash.new { |h, k| h[k] = Array.new }
end
write_and_flush(bucket) click to toggle source

write out a bucket and flush its contents

# File lib/thrash/thrash.rb, line 52
def write_and_flush(bucket)
  File.open(bucket, 'a+') do |o|
    @buffer[bucket].each do |v|
      o.write(v)
    end
  end
  clear_buffer!
end