Class: Fbe::Iterate

Inherits:
Object
  • Object
show all
Defined in:
lib/fbe/iterate.rb

Overview

Iterate.

Author

Yegor Bugayenko (yegor256@gmail.com)

Copyright

Copyright © 2024 Zerocracy

License

MIT

Instance Method Summary collapse

Constructor Details

#initialize(fb, loog) ⇒ Iterate

Returns a new instance of Iterate.

[View source]

41
42
43
44
45
46
47
48
49
# File 'lib/fbe/iterate.rb', line 41

def initialize(fb, loog)
  @fb = fb
  @loog = loog
  @label = nil
  @since = 0
  @query = nil
  @limit = 100
  @quota_aware = false
end

Instance Method Details

#as(label) ⇒ Object

[View source]

66
67
68
69
70
# File 'lib/fbe/iterate.rb', line 66

def as(label)
  raise 'Label is already set' unless @label.nil?
  raise 'Cannot set "label" to nil' if label.nil?
  @label = label
end

#by(query) ⇒ Object

[View source]

60
61
62
63
64
# File 'lib/fbe/iterate.rb', line 60

def by(query)
  raise 'Query is already set' unless @query.nil?
  raise 'Cannot set query to nil' if query.nil?
  @query = query
end

#limit(limit) ⇒ Object

[View source]

55
56
57
58
# File 'lib/fbe/iterate.rb', line 55

def limit(limit)
  raise 'Cannot set "limit" to nil' if limit.nil?
  @limit = limit
end

#overObject

[View source]

72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/fbe/iterate.rb', line 72

def over(&)
  raise 'Use "as" first' if @label.nil?
  raise 'Use "by" first' if @query.nil?
  seen = {}
  oct = Fbe.octo(loog: @loog)
  repos = Fbe.unmask_repos(loog: @loog)
  loop do
    repos.each do |repo|
      seen[repo] = 0 if seen[repo].nil?
      next if seen[repo] > @limit
      rid = oct.repo_id_by_name(repo)
      before = Fbe.fb.query("(agg (and (eq what '#{@label}') (eq repository #{rid})) (first latest))").one
      Fbe.fb.query("(and (eq what '#{@label}') (eq repository #{rid}))").delete!
      before = before.nil? ? @since : before[0]
      nxt = Fbe.fb.query(@query).one(before:, repository: rid)
      after = nxt.nil? ? @since : yield(rid, nxt)
      raise "Iterator must return an Integer, while #{after.class} returned" unless after.is_a?(Integer)
      f = Fbe.fb.insert
      f.repository = rid
      f.latest = after.nil? ? nxt : after
      f.what = @label
      seen[repo] += 1
      break if oct.off_quota
    end
    break if oct.off_quota
    unless seen.values.any? { |v| v < @limit }
      @loog.debug("Finished scanning #{repos.size} repos: #{seen.map { |k, v| "#{k}:#{v}" }.join(', ')}")
      break
    end
  end
end

#quota_awareObject

[View source]

51
52
53
# File 'lib/fbe/iterate.rb', line 51

def quota_aware
  @quota_aware = true
end