Class: Kotoshu::Spellchecker::ParallelChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/kotoshu/spellchecker/parallel_checker.rb

Overview

Parallel file checker for concurrent spellchecking.

Uses a thread pool to check multiple files simultaneously, providing significant speedup on multi-core systems.

Examples:

Check files in parallel

checker = ParallelChecker.new(spellchecker: spellchecker, worker_count: 4)
results = checker.check_files_parallel(["file1.txt", "file2.txt"])

Constant Summary collapse

DEFAULT_WORKER_COUNT =

Default number of worker threads

4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(spellchecker:, worker_count: DEFAULT_WORKER_COUNT) ⇒ ParallelChecker

Create a new parallel checker.

Parameters:

  • spellchecker (Spellchecker)

    The spellchecker to use

  • worker_count (Integer) (defaults to: DEFAULT_WORKER_COUNT)

    Number of worker threads (default: 4)



27
28
29
30
31
32
33
# File 'lib/kotoshu/spellchecker/parallel_checker.rb', line 27

def initialize(spellchecker:, worker_count: DEFAULT_WORKER_COUNT)
  @spellchecker = spellchecker
  @worker_count = worker_count
  @queue = Queue.new
  @results = []
  @mutex = Mutex.new
end

Instance Attribute Details

#spellcheckerSpellchecker (readonly)

Returns The underlying spellchecker.

Returns:



18
19
20
# File 'lib/kotoshu/spellchecker/parallel_checker.rb', line 18

def spellchecker
  @spellchecker
end

#worker_countInteger (readonly)

Returns Number of worker threads.

Returns:

  • (Integer)

    Number of worker threads



21
22
23
# File 'lib/kotoshu/spellchecker/parallel_checker.rb', line 21

def worker_count
  @worker_count
end

Instance Method Details

#check_file(file_path) ⇒ Core::Models::Result::DocumentResult

Check a single file (convenience method).

Parameters:

  • file_path (String)

    Path to file

Returns:

  • (Core::Models::Result::DocumentResult)

    Check result



64
65
66
# File 'lib/kotoshu/spellchecker/parallel_checker.rb', line 64

def check_file(file_path)
  @spellchecker.check_file(file_path)
end

#check_files_parallel(file_paths) ⇒ Array<Core::Models::Result::DocumentResult>

Check multiple files in parallel.

Parameters:

  • file_paths (Array<String>)

    Paths to files to check

Returns:

  • (Array<Core::Models::Result::DocumentResult>)

    Results for each file



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/kotoshu/spellchecker/parallel_checker.rb', line 39

def check_files_parallel(file_paths)
  return [] if file_paths.empty?

  # Add all files to the queue
  file_paths.each { |path| @queue << path }

  # Add poison pills to signal workers to stop
  @worker_count.times { @queue << :done }

  # Create and start workers
  workers = @worker_count.times.map { create_worker }

  # Wait for all workers to complete
  workers.each(&:join)

  # Clear queue for reuse
  @queue.clear while @queue.empty? == false

  @results
end