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



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

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

Instance Attribute Details

#changesObject (readonly)

Returns the value of attribute changes.



9
10
11
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 9

def changes
  @changes
end

#directory_pathObject (readonly)

Returns the value of attribute directory_path.



9
10
11
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 9

def directory_path
  @directory_path
end

Instance Method Details

#added_filesObject

Get list of added files



40
41
42
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 40

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

#changes?Boolean

Check if any changes were detected

Returns:

  • (Boolean)


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

def changes?
  @changes.any?
end

#modified_filesObject

Get list of modified files



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

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

#removed_filesObject

Get list of removed files



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

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

#statsObject

Get scan statistics



60
61
62
63
64
65
66
67
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 60

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



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/fontist/indexes/incremental_index_updater.rb', line 19

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