Class: SchemaSherlock::FileCache

Inherits:
Object
  • Object
show all
Defined in:
lib/schema_sherlock/file_cache.rb

Class Method Summary collapse

Class Method Details

.cache_statsObject



47
48
49
50
51
52
# File 'lib/schema_sherlock/file_cache.rb', line 47

def cache_stats
  {
    cached_files: @file_contents_cache&.size || 0,
    scan_stats: @scan_stats || { files_scanned: 0, files_failed: 0, total_size: 0 }
  }
end

.cached_files_countObject



43
44
45
# File 'lib/schema_sherlock/file_cache.rb', line 43

def cached_files_count
  @file_contents_cache&.size || 0
end

.clear_cacheObject



11
12
13
14
# File 'lib/schema_sherlock/file_cache.rb', line 11

def clear_cache
  @file_contents_cache&.clear
  @scan_stats = { files_scanned: 0, files_failed: 0, total_size: 0 }
end

.get_file_content(file_path) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/schema_sherlock/file_cache.rb', line 31

def get_file_content(file_path)
  initialize_cache if @file_contents_cache.nil?

  # Return cached content if available
  return @file_contents_cache[file_path] if @file_contents_cache.key?(file_path)

  # If not cached, read and cache it
  content = read_file_safely(file_path)
  @file_contents_cache[file_path] = content if content
  content
end

.initialize_cacheObject



6
7
8
9
# File 'lib/schema_sherlock/file_cache.rb', line 6

def initialize_cache
  @file_contents_cache = {}
  @scan_stats = { files_scanned: 0, files_failed: 0, total_size: 0 }
end

.preload_all_files(max_threads: 4) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/schema_sherlock/file_cache.rb', line 16

def preload_all_files(max_threads: 4)
  initialize_cache if @file_contents_cache.nil?

  files_to_scan = gather_all_files
  return { files_scanned: 0, files_failed: 0, total_size: 0 } if files_to_scan.empty?

  if max_threads > 1
    preload_files_parallel(files_to_scan, max_threads)
  else
    preload_files_sequential(files_to_scan)
  end

  @scan_stats.dup
end

.scan_for_pattern_in_all_files(pattern, table_name: nil, column_name: nil) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/schema_sherlock/file_cache.rb', line 54

def scan_for_pattern_in_all_files(pattern, table_name: nil, column_name: nil)
  initialize_cache if @file_contents_cache.nil?

  total_matches = 0

  @file_contents_cache.each do |file_path, content|
    next unless content

    if block_given?
      # Allow custom matching logic
      matches = yield(content, file_path, table_name, column_name)
    else
      # Default regex matching
      matches = content.scan(pattern).length
    end

    total_matches += matches
  end

  total_matches
end