-
-
Save thejefflarson/4696656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Daybreak | |
class BackgroundCompaction | |
def initialize(file, options = {}) | |
@db = Daybreak::DB.new(file) | |
@thread = Thread.new(&method(:run)) | |
@options = options | |
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? | |
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 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment