Class: RubyRoutes::RadixTree

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_routes/radix_tree.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRadixTree

Returns a new instance of RadixTree.



16
17
18
19
20
21
22
# File 'lib/ruby_routes/radix_tree.rb', line 16

def initialize
  @root = Node.new
  @split_cache = {}
  @split_cache_order = []
  @split_cache_max = 2048      # larger cache for better hit rates
  @empty_segments = [].freeze  # reuse for root path
end

Class Method Details

.new(*args, &block) ⇒ Object

Allow RadixTree.new(path, options…) to act as a convenience factory



7
8
9
10
11
12
13
# File 'lib/ruby_routes/radix_tree.rb', line 7

def new(*args, &block)
  if args.any?
    RubyRoutes::Route.new(*args, &block)
  else
    super()
  end
end

Instance Method Details

#add(path, methods, handler) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ruby_routes/radix_tree.rb', line 24

def add(path, methods, handler)
  current = @root
  segments = split_path_raw(path)

  segments.each do |raw_seg|
    seg = RubyRoutes::Segment.for(raw_seg)
    current = seg.ensure_child(current)
    break if seg.wildcard?
  end

  # Normalize methods once during registration
  Array(methods).each { |method| current.add_handler(method.to_s.upcase, handler) }
end

#find(path, method, params_out = nil) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
# File 'lib/ruby_routes/radix_tree.rb', line 38

def find(path, method, params_out = nil)
  # Handle nil path and method cases
  path ||= ''
  method = method.to_s.upcase if method
  # Strip query string before matching
  clean_path = path.split('?', 2).first || ''
  # Fast path: root route
  if clean_path == '/' || clean_path.empty?
    handler = @root.get_handler(method)
    if @root.is_endpoint && handler
      return [handler, params_out || {}]
    else
      return [nil, {}]
    end
  end

  segments = split_path_cached(clean_path)
  current = @root
  params = params_out || {}
  params.clear if params_out

  # Unrolled traversal for common case (1-3 segments)
  case segments.size
  when 1
    next_node, _ = current.traverse_for(segments[0], 0, segments, params)
    current = next_node
  when 2
    next_node, should_break = current.traverse_for(segments[0], 0, segments, params)
    return [nil, {}] unless next_node
    current = next_node
    unless should_break
      next_node, _ = current.traverse_for(segments[1], 1, segments, params)
      current = next_node
    end
  when 3
    next_node, should_break = current.traverse_for(segments[0], 0, segments, params)
    return [nil, {}] unless next_node
    current = next_node
    unless should_break
      next_node, should_break = current.traverse_for(segments[1], 1, segments, params)
      return [nil, {}] unless next_node
      current = next_node
      unless should_break
        next_node, _ = current.traverse_for(segments[2], 2, segments, params)
        current = next_node
      end
    end
  else
    # General case for longer paths
    segments.each_with_index do |text, idx|
      next_node, should_break = current.traverse_for(text, idx, segments, params)
      return [nil, {}] unless next_node
      current = next_node
      break if should_break
    end
  end

  return [nil, {}] unless current
  handler = current.get_handler(method)
  return [nil, {}] unless current.is_endpoint && handler

  # Fast constraint check
  if handler.respond_to?(:constraints) && !handler.constraints.empty?
    return [nil, {}] unless constraints_match_fast(handler.constraints, params)
  end

  [handler, params]
end