lib/massive_sitemap/writer/file.rb in massive_sitemap-2.0.0.rc5 vs lib/massive_sitemap/writer/file.rb in massive_sitemap-2.0.0.rc6
- old
+ new
@@ -15,11 +15,11 @@
:filename => "sitemap.xml",
)
def initialize(options = {})
super
- @files = Dir[::File.join(@options[:root], "*#{::File.extname(filename)}")]
+ init_stream_ids
end
protected
def open_stream
#create dir if not exists
@@ -33,29 +33,75 @@
def close_stream(file)
file.close
# Move from tmp_file into acutal file
::File.delete(filename) if ::File.exists?(filename)
::File.rename(tmp_filename, filename)
- @files << filename
+ add_stream_id(filename)
rotate
end
def init?
- if !@options[:force_overwrite] && @files.include?(filename)
+ if !@options[:force_overwrite] && find_stream_id(filename)
error_message = "Can not create file: #{filename} exits"
rotate #push next possible filename
raise FileExistsException, error_message
end
true
end
- def stream_ids
- @files.map do |path|
- [::File.basename(path), ::File.stat(path).mtime]
+ # Keep state of Files
+ def init_stream_ids
+ @stream_ids = {}
+ load_stream_ids
+
+ if @options[:partial_update]
+ delete_stream_ids upper_stream_ids(@stream_ids.keys)
+ end
+
+ if (keep_file_for = @options[:keep_file_for].to_i) > 0
+ delete_stream_ids chaos_monkey_stream_ids(@stream_ids.keys.sort, keep_file_for)
+ end
+ end
+
+ def load_stream_ids
+ Dir[::File.join(@options[:root], "*#{::File.extname(filename)}")].each do |path|
+ add_stream_id(path, ::File.stat(path).mtime)
+ end
+ end
+
+ def upper_stream_ids(stream_id_keys)
+ {}.tap do |cluster|
+ stream_id_keys.each do |path|
+ filename, rotation, ext = split_filename(path)
+ _, rotation2, _ = split_filename(cluster[filename])
+ if rotation.to_i >= rotation2.to_i
+ cluster[filename] = path
+ end
+ end
+ end.values
+ end
+
+ def chaos_monkey_stream_ids(stream_id_keys, days)
+ return [] if days < 1
+ offset = Time.now.to_i / (24 * 60 * 60)
+ (0...stream_id_keys.size).step(days).map do |index|
+ stream_id_keys.at((offset % days) + index)
end.compact
end
+ def delete_stream_ids(to_delete)
+ @stream_ids.delete_if { |key, value| to_delete.include?(key) }
+ end
+
+ def find_stream_id(path)
+ @stream_ids.keys.include?(::File.basename(path))
+ end
+
+ def add_stream_id(path, last_modified = Time.now)
+ @stream_ids[::File.basename(path)] = last_modified
+ end
+
def stream_id
@stream_id && ::File.basename(@stream_id)
end
private
@@ -77,9 +123,10 @@
end
def split_filename(filename)
filename.to_s.scan(/^([^.]*?)(?:-([0-9]+))?(\..+)?$/).flatten
end
+
end
end
end