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! ⇒ Object
Removes any files from the index that are no longer present locally.
-
#initialize(sync_path, connection_params) ⇒ Index
constructor
Returns a databse object used to connect to the indexing database.
- #last_indexed_at ⇒ Object
-
#orphans ⇒ Mir::Models::Resource
Returns any files not present since the last re-indexing.
-
#setup(options = {}) ⇒ Object
Creates necessary database and tables if this is the first time connecting.
-
#update ⇒ Object
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) ⇒ Index
Returns a databse object used to connect to the indexing database
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_path ⇒ Object (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_at ⇒ Object
78 79 80 |
# File 'lib/mir/index.rb', line 78 def last_indexed_at @last_indexed_at ||= Models::AppSetting.last_indexed_at end |
#orphans ⇒ 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.
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
30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/mir/index.rb', line 30 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 ⇒ Object
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 |