Module: FastFind

Defined in:
lib/fast_find.rb,
lib/fast_find/version.rb

Overview

A Find workalike optimized for multithreaded operation on supporting Rubies

Constant Summary collapse

VERSION =
'0.2.1'

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_executorObject

Returns the value of attribute default_executor.



12
13
14
# File 'lib/fast_find.rb', line 12

def default_executor
  @default_executor
end

Class Method Details

.find(*paths, ignore_error: true, executor: default_executor, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/fast_find.rb', line 18

def find(*paths, ignore_error: true, executor: default_executor, &block)
  block or return enum_for(__method__, *paths, ignore_error: ignore_error, executor: executor)

  results = SizedQueue.new(1024)
  pending = Set.new

  paths.map!(&:dup).each do |path|
    path = path.to_path if path.respond_to? :to_path
    results << [path, safe_stat(path)]
  end
  results << %i[initial finished]
  pending << path_signature(:initial)

  while (result = results.deq)
    path, stat = result

    if stat == :finished
      break if pending.delete(path_signature(path)).empty?

      next
    end

    catch(:prune) do
      yield_entry(result, block) if path.is_a? String

      if stat.is_a?(File::Stat) && stat.directory? && pending.add?(path_signature(path))
        executor.post(path, results) do |path_, results_|
          walk(path_, results_)
        end
      end
    end

    raise stat if stat.is_a?(Exception) && !ignore_error
  end
ensure
  results.close if results
end

.pruneObject



14
15
16
# File 'lib/fast_find.rb', line 14

def prune
  throw :prune
end