Class: Omnizip::Parallel::ParallelCompressor
- Inherits:
-
Object
- Object
- Omnizip::Parallel::ParallelCompressor
- Defined in:
- lib/omnizip/parallel/parallel_compressor.rb
Overview
Parallel compression coordinator using Fractor
Manages parallel compression of files in a directory. Distributes compression work across multiple workers and writes results to archive in a thread-safe manner.
Defined Under Namespace
Classes: CompressionWork, CompressionWorker
Instance Attribute Summary collapse
-
#options ⇒ Omnizip::Models::ParallelOptions
readonly
Parallel options.
-
#stats ⇒ Hash
readonly
Compression statistics.
Instance Method Summary collapse
-
#compress(dir, output, **options) ⇒ String
Compress directory to archive in parallel.
-
#initialize(options = nil, threads: nil) ⇒ ParallelCompressor
constructor
Initialize parallel compressor.
-
#statistics ⇒ Hash
Get compression statistics.
Constructor Details
#initialize(options = nil, threads: nil) ⇒ ParallelCompressor
Initialize parallel compressor
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/omnizip/parallel/parallel_compressor.rb', line 129 def initialize( = nil, threads: nil) @options = case when Omnizip::Models::ParallelOptions .dup when Hash Omnizip::Models::ParallelOptions.new.apply() else Omnizip::Models::ParallelOptions.new end @options.threads = threads if threads @options.validate! @stats = { files_processed: 0, bytes_processed: 0, bytes_compressed: 0, start_time: nil, end_time: nil, } end |
Instance Attribute Details
#options ⇒ Omnizip::Models::ParallelOptions (readonly)
Returns parallel options.
120 121 122 |
# File 'lib/omnizip/parallel/parallel_compressor.rb', line 120 def @options end |
#stats ⇒ Hash (readonly)
Returns compression statistics.
123 124 125 |
# File 'lib/omnizip/parallel/parallel_compressor.rb', line 123 def stats @stats end |
Instance Method Details
#compress(dir, output, **options) ⇒ String
Compress directory to archive in parallel
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/omnizip/parallel/parallel_compressor.rb', line 161 def compress(dir, output, **) unless ::File.exist?(dir) raise Errno::ENOENT, "Directory not found: #{dir}" end unless ::File.directory?(dir) raise ArgumentError, "Not a directory: #{dir}" end compression = [:compression] || :deflate level = [:level] || 6 recursive = .fetch(:recursive, true) [:progress] @stats[:start_time] = Time.now # Scan directory for files files = scan_directory(dir, recursive: recursive) # Create job queue job_queue = JobQueue.new(max_size: @options.queue_size) # Schedule jobs JobScheduler.new(strategy: @options.strategy) files.each do |file_path| archive_path = file_path.sub("#{dir}/", "") file_size = ::File.size(file_path) job_queue.push_with_size( file: file_path, size: file_size, data: { archive_path: archive_path, compression: compression, level: level, }, ) end # Create work items from jobs work_items = [] until job_queue.empty? job = job_queue.pop(timeout: 0.1) break unless job work_items << CompressionWork.new( file_path: job.file, archive_path: job.data[:archive_path], compression: job.data[:compression], level: job.data[:level], ) end # Create worker pool pool = WorkerPool.new( worker_class: CompressionWorker, num_workers: @options.threads, continuous: false, ) pool.start pool.submit_batch(work_items) pool.run # Collect results results = pool.successful_results errors = pool.failed_results # Handle errors unless errors.empty? error_msgs = errors.map do |e| "#{e.work&.file_path}: #{e.error}" end.join("\n") raise Omnizip::CompressionError, "Compression errors:\n#{error_msgs}" end # Write archive sequentially (thread-safe) write_archive(output, results, compression: compression) pool.shutdown @stats[:end_time] = Time.now @stats[:files_processed] = results.size output end |
#statistics ⇒ Hash
Get compression statistics
252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/omnizip/parallel/parallel_compressor.rb', line 252 def statistics duration = if @stats[:start_time] && @stats[:end_time] @stats[:end_time] - @stats[:start_time] else 0 end @stats.merge( duration: duration, compression_ratio: calculate_compression_ratio, throughput_mbps: calculate_throughput(duration), ) end |