Class: Fontist::Indexes::IncrementalIndexUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/fontist/indexes/incremental_index_updater.rb

Overview

Service for performing incremental index updates Only scans changed files/directories instead of full scans

Constant Summary collapse

SNAPSHOT_TTL =

5 minutes

300
CHANGE_DETECTION_TTL =

1 minute

60

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory_path) ⇒ IncrementalIndexUpdater

Initialize updater for a directory



16
17
18
19
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 16

def initialize(directory_path)
  @directory_path = directory_path
  @changes = []
end

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



13
14
15
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 13

def changes
  @changes
end

#directory_pathObject (readonly)

Returns the value of attribute directory_path.



13
14
15
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 13

def directory_path
  @directory_path
end

Instance Method Details

#added_filesObject

Get list of added files



44
45
46
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 44

def added_files
  @changes.select(&:added?)
end

#changes?Boolean

Check if any changes were detected

Returns:

  • (Boolean)


59
60
61
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 59

def changes?
  @changes.any?
end

#modified_filesObject

Get list of modified files



49
50
51
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 49

def modified_files
  @changes.select(&:modified?)
end

#removed_filesObject

Get list of removed files



54
55
56
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 54

def removed_files
  @changes.select(&:removed?)
end

#statsObject

Get scan statistics



64
65
66
67
68
69
70
71
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 64

def stats
  {
    total_changes: @changes.size,
    added: added_files.size,
    modified: modified_files.size,
    removed: removed_files.size,
  }
end

#updateObject

Perform incremental update Returns: Array of DirectoryChange objects



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 23

def update
  old_snapshot = load_snapshot
  new_snapshot = create_snapshot

  @changes = if old_snapshot.nil?
               # First scan - all files are new
               new_snapshot.files.map do |file|
                 DirectoryChange.added(file[:filename], file)
               end
             else
               # Detect changes
               DirectoryChange.diff(old_snapshot, new_snapshot)
             end

  # Save new snapshot for next time
  save_snapshot(new_snapshot)

  @changes
end