Class: Mir::Index

Inherits:
Object
  • Object
show all
Defined in:
lib/mir/index.rb

Constant Summary collapse

MIGRATIONS_PATH =
File.join(File.dirname(__FILE__), "..", "..", "db", "migrate")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sync_path, connection_params) ⇒ Index

Returns a databse object used to connect to the indexing database

Parameters:

  • the (String)

    absolute path of the directory to be synchronized

  • database (Hash)

    configuration settings. See ActiveRecord#Base::establish_connection



18
19
20
21
# File 'lib/mir/index.rb', line 18

def initialize(sync_path, connection_params)
  @sync_path = sync_path
  @connection_params = connection_params
end

Instance Attribute Details

#sync_pathObject (readonly)

Returns the value of attribute sync_path.



23
24
25
# File 'lib/mir/index.rb', line 23

def sync_path
  @sync_path
end

Instance Method Details

#clean!Object

Removes any files from the index that are no longer present locally



85
86
87
# File 'lib/mir/index.rb', line 85

def clean!
  Models::Resource.delete_all_except(last_indexed_at)
end

#last_indexed_atObject



78
79
80
# File 'lib/mir/index.rb', line 78

def last_indexed_at
  @last_indexed_at ||= Models::AppSetting.last_indexed_at
end

#orphansMir::Models::Resource

Returns any files not present since the last re-indexing. This is useful for finding files that have been deleted post-index.



74
75
76
# File 'lib/mir/index.rb', line 74

def orphans
  Models::Resource.not_indexed_on(last_indexed_at)
end

#setup(options = {}) ⇒ Object

Creates necessary database and tables if this is the first time connecting

Parameters:

  • opts (Hash)

    a customizable set of options



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/mir/index.rb', line 30

def setup(options = {})
  options[:force_flush] ||= false
  options[:verbose] ||= false
  @connection = ActiveRecord::Base.establish_connection(@connection_params).connection
  ActiveRecord::Base.timestamped_migrations = false
  
  if options[:verbose]
    ActiveRecord::Base.logger = Mir.logger
    ActiveRecord::Migration.verbose = true
  end
  
  load_tables
  rebuild if !tables_created? or options[:force_flush]
end

#updateObject

Scans the synchronization path and evaluates whether a resource has changed since the last index or is new and needs to be added to the index.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mir/index.rb', line 48

def update
  Mir.logger.info "Updating backup index for '#{sync_path}'"
  Models::AppSetting.last_indexed_at = @last_indexed_at = DateTime.now
  
  Dir.glob(File.join(sync_path, "**", "*")) do |f|
    fname = relative_path(f)
    file = File.new(f)
    resource = Models::Resource.find_by_filename(fname)

    if resource.nil?
      Mir.logger.debug "Adding file to index #{fname}"
      resource = Models::Resource.create_from_file_and_name(file, fname)
    elsif !resource.synchronized?(file)
      resource.flag_for_update
    end
    resource.update_attribute(:last_indexed_at, last_indexed_at)
  end
  
  Mir.logger.info "Index updated"
end