Class: Jason::GraphHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/jason/graph_helper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, includes_helper) ⇒ GraphHelper

Returns a new instance of GraphHelper.



4
5
6
7
# File 'lib/jason/graph_helper.rb', line 4

def initialize(id, includes_helper)
  @id = id
  @includes_helper = includes_helper
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/jason/graph_helper.rb', line 2

def id
  @id
end

#includes_helperObject (readonly)

Returns the value of attribute includes_helper.



2
3
4
# File 'lib/jason/graph_helper.rb', line 2

def includes_helper
  @includes_helper
end

Instance Method Details

#add_edge(parent_model, parent_id, child_model, child_id) ⇒ Object



9
10
11
12
# File 'lib/jason/graph_helper.rb', line 9

def add_edge(parent_model, parent_id, child_model, child_id)
  edge = "#{parent_model}:#{parent_id}/#{child_model}:#{child_id}"
  $redis_jason.sadd("jason:subscriptions:#{id}:graph", edge)
end

#add_edges(all_models, instance_ids) ⇒ Object



19
20
21
22
# File 'lib/jason/graph_helper.rb', line 19

def add_edges(all_models, instance_ids)
  edges = build_edges(all_models, instance_ids)
  $redis_jason.sadd("jason:subscriptions:#{id}:graph", edges)
end

#apply_add_node_at_root(node) ⇒ Object



29
30
31
# File 'lib/jason/graph_helper.rb', line 29

def apply_add_node_at_root(node)
  diff_edges_from_graph(add_edges: ["root/#{node}"])
end

#apply_remove_node(node) ⇒ Object



33
34
35
36
37
# File 'lib/jason/graph_helper.rb', line 33

def apply_remove_node(node)
  edges = $redis_jason.smembers("jason:subscriptions:#{id}:graph")
  edges = find_edges_with_node(edges, node)
  diff_edges_from_graph(remove_edges: edges)
end

#apply_update(add: nil, remove: nil, enforce: false) ⇒ Object

Add and remove edges, return graph before and after Enforce means make the graph contain only the add_edges



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/jason/graph_helper.rb', line 41

def apply_update(add: nil, remove: nil, enforce: false)
  add_edges = []
  remove_edges = []

  if add.present?
    add.each do |edge_set|
      add_edges += build_edges(edge_set[:model_names], edge_set[:instance_ids])
    end
  end

  if remove.present?
    remove.each do |edge_set|
      remove_edges += build_edges(edge_set[:model_names], edge_set[:instance_ids], include_root: false)
    end
  end

  diff_edges_from_graph(add_edges: add_edges, remove_edges: remove_edges, enforce: enforce)
end

#build_graph_from_edges(edges) ⇒ Object



135
136
137
138
139
140
141
142
143
# File 'lib/jason/graph_helper.rb', line 135

def build_graph_from_edges(edges)
  graph = {}
  edges.each do |edge|
    parent, child = edge.split('/')
    graph[parent] ||= []
    graph[parent].push(child)
  end
  graph
end

#diff_edges_from_graph(add_edges: [], remove_edges: [], enforce: false) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
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
103
104
105
106
# File 'lib/jason/graph_helper.rb', line 60

def diff_edges_from_graph(add_edges: [], remove_edges: [], enforce: false)
  if enforce
    old_edges = $redis_jason.multi do |r|
      r.smembers("jason:subscriptions:#{id}:graph")
      r.del("jason:subscriptions:#{id}:graph")
      r.sadd("jason:subscriptions:#{id}:graph", add_edges) if add_edges.present?
    end[0]
    new_edges = add_edges
  else
    old_edges, new_edges = Jason::LuaGenerator.new.update_set_with_diff("jason:subscriptions:#{id}:graph", add_edges.flatten, remove_edges.flatten)
  end

  old_graph = build_graph_from_edges(old_edges)
  new_graph = build_graph_from_edges(new_edges)

  old_nodes = (old_graph.values + old_graph.keys).flatten.uniq - ['root']
  new_nodes = (new_graph.values + new_graph.keys).flatten.uniq - ['root']
  orphan_nodes = find_orphans_in_graph(new_graph)

  added_nodes = new_nodes - old_nodes - orphan_nodes
  removed_nodes = old_nodes - new_nodes + orphan_nodes

  orphaned_edges = orphan_nodes.map do |node|
    find_edges_with_node(new_edges, node)
  end.flatten

  if orphaned_edges.present?
    $redis_jason.srem("jason:subscriptions:#{id}:graph", orphaned_edges)
  end

  ids_to_add = {}
  ids_to_remove = {}

  added_nodes.each do |node|
    model_name, instance_id = node.split(':')
    ids_to_add[model_name] ||= []
    ids_to_add[model_name].push(instance_id)
  end

  removed_nodes.each do |node|
    model_name, instance_id = node.split(':')
    ids_to_remove[model_name] ||= []
    ids_to_remove[model_name].push(instance_id)
  end

  { ids_to_remove: ids_to_remove, ids_to_add: ids_to_add }
end

#find_edges_with_node(edges, node) ⇒ Object



108
109
110
111
112
113
# File 'lib/jason/graph_helper.rb', line 108

def find_edges_with_node(edges, node)
  edges.select do |edge|
    parent, child = edge.split('/')
    parent == node || child == node
  end
end

#find_orphansObject



115
116
117
118
119
# File 'lib/jason/graph_helper.rb', line 115

def find_orphans
  edges = $redis_jason.smembers("jason:subscriptions:#{id}:graph")
  graph = build_graph_from_edges(edges)
  find_orphans_in_graph(graph)
end

#find_orphans_in_graph(graph) ⇒ Object



121
122
123
124
125
# File 'lib/jason/graph_helper.rb', line 121

def find_orphans_in_graph(graph)
  reachable_nodes = get_reachable_nodes(graph)
  all_nodes = (graph.values + graph.keys).flatten.uniq - ['root']
  all_nodes - reachable_nodes
end

#get_reachable_nodes(graph, parent = 'root') ⇒ Object



127
128
129
130
131
132
133
# File 'lib/jason/graph_helper.rb', line 127

def get_reachable_nodes(graph, parent = 'root')
  reached_nodes = graph[parent] || []
  reached_nodes.each do |child|
    reached_nodes += get_reachable_nodes(graph, child)
  end
  reached_nodes
end

#remove_edge(parent_model, parent_id, child_model, child_id) ⇒ Object



14
15
16
17
# File 'lib/jason/graph_helper.rb', line 14

def remove_edge(parent_model, parent_id, child_model, child_id)
  edge = "#{parent_model}:#{parent_id}/#{child_model}:#{child_id}"
  $redis_jason.srem("jason:subscriptions:#{id}:graph", edge)
end

#remove_edges(all_models, instance_ids) ⇒ Object



24
25
26
27
# File 'lib/jason/graph_helper.rb', line 24

def remove_edges(all_models, instance_ids)
  edges = build_edges(all_models, instance_ids)
  $redis_jason.srem("jason:subscriptions:#{id}:graph", edges)
end