massive_sitemap 2.0.0.rc5 → 2.0.0.rc6

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -29,4 +29,4 @@
29
29
  * updated/fixed Ping
30
30
  * move LockingFile into MassiveSitemap scope
31
31
  * FileManifest -> read all files into streams take that as reference
32
-
32
+ * add test for stream_id delete
data/VERSION CHANGED
@@ -1 +1 @@
1
- 2.0.0.rc5
1
+ 2.0.0.rc6
@@ -17,7 +17,7 @@ module MassiveSitemap
17
17
 
18
18
  def initialize(options = {})
19
19
  super
20
- @files = Dir[::File.join(@options[:root], "*#{::File.extname(filename)}")]
20
+ init_stream_ids
21
21
  end
22
22
 
23
23
  protected
@@ -35,12 +35,12 @@ module MassiveSitemap
35
35
  # Move from tmp_file into acutal file
36
36
  ::File.delete(filename) if ::File.exists?(filename)
37
37
  ::File.rename(tmp_filename, filename)
38
- @files << filename
38
+ add_stream_id(filename)
39
39
  rotate
40
40
  end
41
41
 
42
42
  def init?
43
- if !@options[:force_overwrite] && @files.include?(filename)
43
+ if !@options[:force_overwrite] && find_stream_id(filename)
44
44
  error_message = "Can not create file: #{filename} exits"
45
45
  rotate #push next possible filename
46
46
  raise FileExistsException, error_message
@@ -48,12 +48,58 @@ module MassiveSitemap
48
48
  true
49
49
  end
50
50
 
51
- def stream_ids
52
- @files.map do |path|
53
- [::File.basename(path), ::File.stat(path).mtime]
51
+ # Keep state of Files
52
+ def init_stream_ids
53
+ @stream_ids = {}
54
+ load_stream_ids
55
+
56
+ if @options[:partial_update]
57
+ delete_stream_ids upper_stream_ids(@stream_ids.keys)
58
+ end
59
+
60
+ if (keep_file_for = @options[:keep_file_for].to_i) > 0
61
+ delete_stream_ids chaos_monkey_stream_ids(@stream_ids.keys.sort, keep_file_for)
62
+ end
63
+ end
64
+
65
+ def load_stream_ids
66
+ Dir[::File.join(@options[:root], "*#{::File.extname(filename)}")].each do |path|
67
+ add_stream_id(path, ::File.stat(path).mtime)
68
+ end
69
+ end
70
+
71
+ def upper_stream_ids(stream_id_keys)
72
+ {}.tap do |cluster|
73
+ stream_id_keys.each do |path|
74
+ filename, rotation, ext = split_filename(path)
75
+ _, rotation2, _ = split_filename(cluster[filename])
76
+ if rotation.to_i >= rotation2.to_i
77
+ cluster[filename] = path
78
+ end
79
+ end
80
+ end.values
81
+ end
82
+
83
+ def chaos_monkey_stream_ids(stream_id_keys, days)
84
+ return [] if days < 1
85
+ offset = Time.now.to_i / (24 * 60 * 60)
86
+ (0...stream_id_keys.size).step(days).map do |index|
87
+ stream_id_keys.at((offset % days) + index)
54
88
  end.compact
55
89
  end
56
90
 
91
+ def delete_stream_ids(to_delete)
92
+ @stream_ids.delete_if { |key, value| to_delete.include?(key) }
93
+ end
94
+
95
+ def find_stream_id(path)
96
+ @stream_ids.keys.include?(::File.basename(path))
97
+ end
98
+
99
+ def add_stream_id(path, last_modified = Time.now)
100
+ @stream_ids[::File.basename(path)] = last_modified
101
+ end
102
+
57
103
  def stream_id
58
104
  @stream_id && ::File.basename(@stream_id)
59
105
  end
@@ -79,6 +125,7 @@ module MassiveSitemap
79
125
  def split_filename(filename)
80
126
  filename.to_s.scan(/^([^.]*?)(?:-([0-9]+))?(\..+)?$/).flatten
81
127
  end
128
+
82
129
  end
83
130
 
84
131
  end
data/spec/lock_spec.rb CHANGED
@@ -37,9 +37,7 @@ describe MassiveSitemap do
37
37
  it 'fails if lockfile exists' do
38
38
  ::File.open(lock_file, 'w') {}
39
39
  expect do
40
- MassiveSitemap.lock! do
41
- puts "Hi"
42
- end
40
+ MassiveSitemap.lock! {}
43
41
  end.to raise_error
44
42
  end
45
43
  end
@@ -1,4 +1,5 @@
1
1
  require "spec_helper"
2
+ require "time"
2
3
 
3
4
  require "massive_sitemap/writer/file"
4
5
 
@@ -168,4 +169,49 @@ describe MassiveSitemap::Writer::File do
168
169
  end
169
170
  end
170
171
  end
172
+
173
+ describe "upper_stream_ids" do
174
+ let(:writer) { MassiveSitemap::Writer::File.new }
175
+
176
+ it { writer.send(:upper_stream_ids, %w(sitemap-1.xml)).should == %w(sitemap-1.xml) }
177
+ it { writer.send(:upper_stream_ids, %w(sitemap-2.xml sitemap-1.xml)).should == %w(sitemap-2.xml) }
178
+ it { writer.send(:upper_stream_ids, %w(sitemap.xml sitemap_user-1.xml)).should == %w(sitemap.xml sitemap_user-1.xml) }
179
+ end
180
+
181
+ describe "chaos_monkey_stream_ids" do
182
+ let(:writer) { MassiveSitemap::Writer::File.new }
183
+
184
+ context "one file" do
185
+ it { writer.send(:chaos_monkey_stream_ids, %w(sitemap-1.xml), 0).should == [] }
186
+ it { writer.send(:chaos_monkey_stream_ids, %w(sitemap-1.xml), 1).should == %w(sitemap-1.xml) }
187
+
188
+ it "keeps file for 2 days" do
189
+ Time.stub!(:now).and_return(Time.parse("1-1-2012"))
190
+ writer.send(:chaos_monkey_stream_ids, %w(sitemap-1.xml), 2).should == []
191
+ end
192
+
193
+ it "deletes file on snd day" do
194
+ Time.stub!(:now).and_return(Time.parse("2-1-2012"))
195
+ writer.send(:chaos_monkey_stream_ids, %w(sitemap-1.xml), 2).should == %w(sitemap-1.xml)
196
+ end
197
+ end
198
+
199
+ context "many files" do
200
+ it "keeps file for 2 days" do
201
+ Time.stub!(:now).and_return(Time.parse("1-1-2012"))
202
+ writer.send(:chaos_monkey_stream_ids, %w(sitemap-1.xml sitemap-2.xml sitemap-3.xml), 2).should == %w(sitemap-2.xml)
203
+ end
204
+
205
+ it "deletes file on 2nd day" do
206
+ Time.stub!(:now).and_return(Time.parse("2-1-2012"))
207
+ writer.send(:chaos_monkey_stream_ids, %w(sitemap-1.xml sitemap-2.xml sitemap-3.xml), 2).should == %w(sitemap-1.xml sitemap-3.xml)
208
+ end
209
+
210
+ it "deletes file on 3rd day" do
211
+ Time.stub!(:now).and_return(Time.parse("3-1-2012"))
212
+ writer.send(:chaos_monkey_stream_ids, %w(sitemap-1.xml sitemap-2.xml sitemap-3.xml), 2).should == %w(sitemap-2.xml)
213
+ end
214
+ end
215
+ end
216
+
171
217
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: massive_sitemap
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0.rc5
4
+ version: 2.0.0.rc6
5
5
  prerelease: 6
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-02-15 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
16
- requirement: &70096501425420 !ruby/object:Gem::Requirement
16
+ requirement: &70204488347100 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70096501425420
24
+ version_requirements: *70204488347100
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: rspec
27
- requirement: &70096501424960 !ruby/object:Gem::Requirement
27
+ requirement: &70204488346640 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70096501424960
35
+ version_requirements: *70204488346640
36
36
  description: MassiveSitemap - build huge sitemaps painfree. Differential updates keeps
37
37
  generation time short and reduces load on DB. It's heavealy inspired by BigSitemaps
38
38
  and offers compatiable API