Skip to content

Commit d62a95a

Browse files
thedarkonerafaelfranca
authored andcommitted
Make GTG::TransTable thread safe.
From now on only the `[]=` method is allowed to modify the internal states hashes.
1 parent cf53a32 commit d62a95a

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

actionpack/lib/action_dispatch/journey/gtg/transition_table.rb

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ class TransitionTable # :nodoc:
99
attr_reader :memos
1010

1111
def initialize
12-
@regexp_states = Hash.new { |h,k| h[k] = {} }
13-
@string_states = Hash.new { |h,k| h[k] = {} }
12+
@regexp_states = {}
13+
@string_states = {}
1414
@accepting = {}
1515
@memos = Hash.new { |h,k| h[k] = [] }
1616
end
@@ -111,14 +111,8 @@ def visualizer(paths, title = 'FSM')
111111
end
112112

113113
def []=(from, to, sym)
114-
case sym
115-
when String
116-
@string_states[from][sym] = to
117-
when Regexp
118-
@regexp_states[from][sym] = to
119-
else
120-
raise ArgumentError, 'unknown symbol: %s' % sym.class
121-
end
114+
to_mappings = states_hash_for(sym)[from] ||= {}
115+
to_mappings[sym] = to
122116
end
123117

124118
def states
@@ -137,18 +131,35 @@ def transitions
137131

138132
private
139133

134+
def states_hash_for(sym)
135+
case sym
136+
when String
137+
@string_states
138+
when Regexp
139+
@regexp_states
140+
else
141+
raise ArgumentError, 'unknown symbol: %s' % sym.class
142+
end
143+
end
144+
140145
def move_regexp(t, a)
141146
return [] if t.empty?
142147

143148
t.map { |s|
144-
@regexp_states[s].map { |re, v| re === a ? v : nil }
149+
if states = @regexp_states[s]
150+
states.map { |re, v| re === a ? v : nil }
151+
end
145152
}.flatten.compact.uniq
146153
end
147154

148155
def move_string(t, a)
149156
return [] if t.empty?
150157

151-
t.map { |s| @string_states[s][a] }.compact
158+
t.map do |s|
159+
if states = @string_states[s]
160+
states[a]
161+
end
162+
end.compact
152163
end
153164
end
154165
end

0 commit comments

Comments
 (0)