Class: Kotoshu::Spellchecker::ParallelChecker
- Inherits:
-
Object
- Object
- Kotoshu::Spellchecker::ParallelChecker
- 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.
Constant Summary collapse
- DEFAULT_WORKER_COUNT =
Default number of worker threads
4
Instance Attribute Summary collapse
-
#spellchecker ⇒ Spellchecker
readonly
The underlying spellchecker.
-
#worker_count ⇒ Integer
readonly
Number of worker threads.
Instance Method Summary collapse
-
#check_file(file_path) ⇒ Core::Models::Result::DocumentResult
Check a single file (convenience method).
-
#check_files_parallel(file_paths) ⇒ Array<Core::Models::Result::DocumentResult>
Check multiple files in parallel.
-
#initialize(spellchecker:, worker_count: DEFAULT_WORKER_COUNT) ⇒ ParallelChecker
constructor
Create a new parallel checker.
Constructor Details
#initialize(spellchecker:, worker_count: DEFAULT_WORKER_COUNT) ⇒ ParallelChecker
Create a new parallel checker.
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
#spellchecker ⇒ Spellchecker (readonly)
Returns The underlying spellchecker.
18 19 20 |
# File 'lib/kotoshu/spellchecker/parallel_checker.rb', line 18 def spellchecker @spellchecker end |
#worker_count ⇒ Integer (readonly)
Returns 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).
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.
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 |