Skip to content

Instantly share code, notes, and snippets.

@minad
Created January 17, 2013 03:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save minad/4553253 to your computer and use it in GitHub Desktop.
Save minad/4553253 to your computer and use it in GitHub Desktop.
module Daybreak
class BackgroundCompaction
def initialize(file)
@db = Daybreak::DB.new(file)
@thread = Thread.new(&method(:run))
end
def stop
@stop = true
@thread.join
@db.close
end
private
def run
until @stop
@db.compact if compact_needed?
end
end
def compact_needed?(options = {})
return true if options[:force]
options[:ratio] ||= 2 # Two log records per table record
options[:reduction] ||= 4096 # One filesystem block, otherwise we won't gain
logsize, bytesize, size = @db.logsize, @db.bytesize, @db.size
logsize > options[:ratio] * size || # Log size vs table size ratio
bytesize - (bytesize * size / logsize) > options[:reduction] # Estimate log size reduction
end
end
end
@thejefflarson
Copy link

We should add this to master right? Small fix:

https://gist.github.com/4696656

Also doesn't the until in run need to be a Thread.pass ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment