Class: Mir::Index
- Inherits:
-
Object
- Object
- Mir::Index
- Defined in:
- lib/mir/index.rb
Constant Summary collapse
- MIGRATIONS_PATH =
File.join(File.dirname(__FILE__), "..", "..", "db", "migrate")
Instance Attribute Summary collapse
-
#sync_path ⇒ Object
readonly
Returns the value of attribute sync_path.
Instance Method Summary collapse
-
#clean! ⇒ void
Removes any files from the index that are no longer present locally.
-
#initialize(sync_path, connection_params) ⇒ Mir::Index
constructor
Returns a databse object used to connect to the indexing database.
-
#last_indexed_at ⇒ DateTime
The date at whish the backup path was last indexed.
-
#orphans ⇒ Array, Mir::Models::Resource
Returns any files not present since the last re-indexing.
-
#setup(options = {}) ⇒ void
Creates necessary database and tables if this is the first time connecting.
-
#update ⇒ void
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.
Constructor Details
#initialize(sync_path, connection_params) ⇒ Mir::Index
Returns a databse object used to connect to the indexing database
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_path ⇒ Object (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_at ⇒ DateTime
The date at whish the backup path was last indexed
84 85 86 |
# File 'lib/mir/index.rb', line 84 def last_indexed_at @last_indexed_at ||= Models::AppSetting.last_indexed_at end |
#orphans ⇒ Array, 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.
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
32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/mir/index.rb', line 32 def setup( = {}) [:force_flush] ||= false [:verbose] ||= false @connection = ActiveRecord::Base.establish_connection(@connection_params).connection ActiveRecord::Base. = false if [:verbose] ActiveRecord::Base.logger = Mir.logger ActiveRecord::Migration.verbose = true end load_tables rebuild if !tables_created? or [:force_flush] end |
#update ⇒ void
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 |