Class: LaunchDarkly::Impl::DependencyTracker

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/impl/dependency_tracker.rb

Overview

Since:

  • 5.5.0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDependencyTracker

Returns a new instance of DependencyTracker.

Since:

  • 5.5.0



4
5
6
7
# File 'lib/ldclient-rb/impl/dependency_tracker.rb', line 4

def initialize
  @from = {}
  @to = {}
end

Class Method Details

.compute_dependencies_from(from_kind, from_item) ⇒ Set

Parameters:

Returns:

  • (Set)

Since:

  • 5.5.0



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ldclient-rb/impl/dependency_tracker.rb', line 54

def self.compute_dependencies_from(from_kind, from_item)
  return Set.new if from_item.nil?

  if from_kind == LaunchDarkly::FEATURES
    prereq_keys = from_item.prerequisites.map { |prereq| {kind: from_kind, key: prereq.key} }
    segment_keys = from_item.rules.flat_map { |rule| DependencyTracker.segment_keys_from_clauses(rule.clauses) }

    results = Set.new(prereq_keys)
    results.merge(segment_keys)
  elsif from_kind == LaunchDarkly::SEGMENTS
    kind_and_keys  = from_item.rules.flat_map do |rule|
      DependencyTracker.segment_keys_from_clauses(rule.clauses)
    end
    Set.new(kind_and_keys)
  else
    Set.new
  end
end

.segment_keys_from_clauses(clauses) ⇒ Object

Since:

  • 5.5.0



39
40
41
42
43
44
45
46
47
# File 'lib/ldclient-rb/impl/dependency_tracker.rb', line 39

def self.segment_keys_from_clauses(clauses)
  clauses.flat_map do |clause|
    if clause.op == :segmentMatch
      clause.values.map { |value| {kind: LaunchDarkly::SEGMENTS, key: value }}
    else
      []
    end
  end
end

Instance Method Details

#add_affected_items(items_out, initial_modified_item) ⇒ Object

Populates the given set with the union of the initial item and all items that directly or indirectly depend on it (based on the current state of the dependency graph).

Parameters:

  • items_out (Set)
  • initial_modified_item (Object)

Since:

  • 5.5.0



88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ldclient-rb/impl/dependency_tracker.rb', line 88

def add_affected_items(items_out, initial_modified_item)
  return if items_out.include? initial_modified_item

  items_out.add(initial_modified_item)
  affected_items = @to[initial_modified_item]

  return if affected_items.nil?

  affected_items.each do |affected_item|
    add_affected_items(items_out, affected_item)
  end
end

#resetObject

Clear any tracked dependencies and reset the tracking state to a clean slate.

Since:

  • 5.5.0



76
77
78
79
# File 'lib/ldclient-rb/impl/dependency_tracker.rb', line 76

def reset
  @from.clear
  @to.clear
end

#update_dependencies_from(from_kind, from_key, from_item) ⇒ Object

Updates the dependency graph when an item has changed.

Parameters:

  • from_kind (Object)

    the changed item’s kind

  • from_key (String)

    the changed item’s key

  • from_item (Object)

    the changed item

Since:

  • 5.5.0



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ldclient-rb/impl/dependency_tracker.rb', line 16

def update_dependencies_from(from_kind, from_key, from_item)
  from_what = { kind: from_kind, key: from_key }
  updated_dependencies = DependencyTracker.compute_dependencies_from(from_kind, from_item)

  old_dependency_set = @from[from_what]
  unless old_dependency_set.nil?
    old_dependency_set.each do |kind_and_key|
      deps_to_this_old_dep = @to[kind_and_key]
      deps_to_this_old_dep&.delete(from_what)
    end
  end

  @from[from_what] = updated_dependencies
  updated_dependencies.each do |kind_and_key|
    deps_to_this_new_dep = @to[kind_and_key]
    if deps_to_this_new_dep.nil?
      deps_to_this_new_dep = Set.new
      @to[kind_and_key] = deps_to_this_new_dep
    end
    deps_to_this_new_dep.add(from_what)
  end
end