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) ⇒ Mir::Index

Returns a databse object used to connect to the indexing database

Parameters:

  • sync_path (String)

    the absolute path of the directory to be synchronized

  • connection_params (Hash)

    database configuration settings. See ActiveRecord#Base::establish_connection



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

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.



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

def sync_path
  @sync_path
end

Instance Method Details

#clean!void

This method returns an undefined value.

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



92
93
94
# File 'lib/mir/index.rb', line 92

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

#last_indexed_atDateTime

The date at whish the backup path was last indexed

Returns:

  • (DateTime)


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

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

#orphansArray, Mir::Models::Resource

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

Returns:



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

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

#setup(options = {}) ⇒ void

This method returns an undefined value.

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

Parameters:

  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :verbose (Boolean)

    Enable on ActiveRecord reporting

  • :force_flush (Boolean)

    Rebuild index no matter what



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

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

#updatevoid

This method returns an undefined value.

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.



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

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