Class: IndexState

Inherits:
Object
  • Object
show all
Defined in:
indexer/app/lib/index_state.rb

Overview

we store the state of uri’s index in the indexer_state directory

Instance Method Summary collapse

Constructor Details

#initialize(state_dir = "indexer_state") ⇒ IndexState

Returns a new instance of IndexState.



4
5
6
# File 'indexer/app/lib/index_state.rb', line 4

def initialize(state_dir = "indexer_state")
  @state_dir = File.join(AppConfig[:data_directory], state_dir)
end

Instance Method Details

#get_last_mtime(repository_id, record_type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'indexer/app/lib/index_state.rb', line 26

def get_last_mtime(repository_id, record_type)
  path = path_for(repository_id, record_type)

  begin
    File.open("#{path}.dat", "r") do |fh|
      fh.readline.to_i
    end
  rescue Errno::ENOENT
    # If we've never run against this repository_id/type before, just index
    # everything.
    0
  end
end

#path_for(repository_id, record_type) ⇒ Object



9
10
11
12
# File 'indexer/app/lib/index_state.rb', line 9

def path_for(repository_id, record_type)
  FileUtils.mkdir_p(@state_dir)
  File.join(@state_dir, "#{repository_id}_#{record_type}")
end

#set_last_mtime(repository_id, record_type, time) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'indexer/app/lib/index_state.rb', line 15

def set_last_mtime(repository_id, record_type, time)
  path = path_for(repository_id, record_type)

  File.open("#{path}.tmp", "w") do |fh|
    fh.puts(time.to_i)
  end

  File.rename("#{path}.tmp", "#{path}.dat")
end