Navigation Menu

Skip to content

Commit

Permalink
Extract logic to generate unique file path as an utility
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Apr 28, 2015
1 parent f57f345 commit d293182
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
12 changes: 4 additions & 8 deletions lib/droonga/forward_buffer.rb
Expand Up @@ -141,15 +141,11 @@ def forward(buffered_message_path)
forwarded
end

MICRO_SECONDS_DECIMAL_PLACE = 6

def create_buffered_message_path(time_stamp=Time.now)
timestamp_part = time_stamp.iso8601(6)
sametime_count = 0
path = nil
begin
path = @data_directory + "#{timestamp_part}.#{sametime_count}#{SUFFIX}"
sametime_count += 1
end while path.exist?
path
basename = time_stamp.iso8601(MICRO_SECONDS_DECIMAL_PLACE)
Path.unique_file_path(@data_directory, basename, SUFFIX)
end

def on_forward(message, destination)
Expand Down
13 changes: 13 additions & 0 deletions lib/droonga/path.rb
Expand Up @@ -92,6 +92,19 @@ def serf_event_handler_error_file
now.hour, now.min, now.sec, now.nsec)
serf_event_handler_errors + name
end

def unique_file_path(directory, basename, suffix)
directory = Pathname(directory)
basename = basename.sub(/\.\z/, "")
suffix = suffix.sub(/\A\./, "")
uniqueness_count = 0
path = nil
begin
path = directory + "#{basename}.#{uniqueness_count}.#{suffix}"
uniqueness_count += 1
end while path.exist?
path
end
end
end
end

0 comments on commit d293182

Please sign in to comment.