Class: TraceVisualization::Mapping

Inherits:
Object
  • Object
show all
Defined in:
lib/trace_visualization/mapping.rb

Constant Summary collapse

TOKEN_REGEXP =
/\{TOKEN;(?<name>[a-zA-Z0-9]+);(?<source>[^;]+);(?<value>[0-9]+);(?<kind>[0-9]+)\}/

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMapping

Returns a new instance of Mapping.



14
15
16
17
18
# File 'lib/trace_visualization/mapping.rb', line 14

def initialize
  @tokens_map = {}
  @data       = []
  @lines      = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



171
172
173
# File 'lib/trace_visualization/mapping.rb', line 171

def method_missing(name, *args, &blk)
  raise "Missing method #{name}" 
end

Class Method Details

.init(&block) ⇒ Object



20
21
22
23
24
# File 'lib/trace_visualization/mapping.rb', line 20

def self.init(&block)
  mapping = Mapping.new
  mapping.instance_eval(&block)
  mapping
end

Instance Method Details

#<<(object) ⇒ Object



137
138
139
140
141
# File 'lib/trace_visualization/mapping.rb', line 137

def <<(object)
  lexeme = install_token('UNKNOWN', object, object.to_i, object.to_s.length)
  lexeme.ord = 0 # fake ord because Reorder already processed
  @data << lexeme
end

#[](index) ⇒ Object



125
126
127
# File 'lib/trace_visualization/mapping.rb', line 125

def [](index)
  @data[index]
end

#find_allObject



151
152
153
# File 'lib/trace_visualization/mapping.rb', line 151

def find_all
  @data.find_all { |item| yield(item) }
end

#from_file(path, offset = 0, limit = 2**32, use_lexeme_table = true) ⇒ Object

Load data from preprocessed file. File is read line by line



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/trace_visualization/mapping.rb', line 38

def from_file(path, offset = 0, limit = 2**32, use_lexeme_table = true)
  validate_file_name_argument(path)

  idx = 0
  open(path) do |fd|
    while (line = fd.gets)
      line.chomp!
      if idx >= offset && idx < offset + limit
        process_line(line, use_lexeme_table) 
      elsif idx >= offset + limit
        break
      end

      idx += 1
    end
  end
  
  @data.pop
end

#from_string(str, use_lexeme_table = true) ⇒ Object

Load data from string



59
60
61
62
63
64
65
66
67
# File 'lib/trace_visualization/mapping.rb', line 59

def from_string(str, use_lexeme_table = true)
  validate_string_argument(str)

  str.split("\n").each do |line|
    process_line(line)
  end
  
  @data.pop
end

#install_lf_tokenObject

Install token with LF (line feed or end of line)



121
122
123
# File 'lib/trace_visualization/mapping.rb', line 121

def install_lf_token
  install_token('CHAR', "\n", 0x0A, 1)
end

#install_token(name, token_string, int_value, token_length, use_token_table = true) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/trace_visualization/mapping.rb', line 101

def install_token(name, token_string, int_value, token_length, use_token_table = true)
  name = name.intern
  
  token = use_token_table ? @tokens_map[token_string] : nil
  
  if token.nil?
    token = Data::Token.new(name, token_string, int_value)
    token.token_length = token_length

    @tokens_map[token_string] = token if use_token_table
  end
  
  token
end

#install_token_m(m, use_lexeme_table = true) ⇒ Object



116
117
118
# File 'lib/trace_visualization/mapping.rb', line 116

def install_token_m(m, use_lexeme_table = true)
  install_token(m[:name], m[:source], m[:value].to_i, m.to_s.length, use_lexeme_table)
end

#lengthObject



129
130
131
# File 'lib/trace_visualization/mapping.rb', line 129

def length
  @data.length
end

#linesObject

Returns array of lines positions.

Returns:

  • array of lines positions



167
168
169
# File 'lib/trace_visualization/mapping.rb', line 167

def lines
  @lines
end

#maxObject



147
148
149
# File 'lib/trace_visualization/mapping.rb', line 147

def max
  @max_value
end

#popObject



143
144
145
# File 'lib/trace_visualization/mapping.rb', line 143

def pop
  @data.pop
end

#process(&block) ⇒ Object

Process input data with reorder



27
28
29
30
# File 'lib/trace_visualization/mapping.rb', line 27

def process(&block)
  instance_eval(&block)
  @max_value = Reorder.process(@data)
end

#process_line(line, use_lexeme_table = true) ⇒ Object

Process line



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
# File 'lib/trace_visualization/mapping.rb', line 70

def process_line(line, use_lexeme_table = true)
  @lines << @data.length
  
  token_positions = []
  pos = 0
  while (m = TOKEN_REGEXP.match(line, pos))
    pos = m.begin(0)

    token = install_token_m(m, use_lexeme_table)
    token_positions << Data::TokenPosition.new(token, pos)

    pos += token.token_length
  end


  pos, idx = 0, 0
  while pos < line.length
    token = nil
    if idx < token_positions.size && token_positions[idx].pos == pos
      token = token_positions[idx].token
      idx += 1
    else
      token = install_token('CHAR', line[pos], line[pos].ord, 1, use_lexeme_table)
    end
    pos += token.token_length
    @data << token
  end
  
  @data << install_lf_token
end

#process_without_reorder(&block) ⇒ Object

Process input data without reorder



33
34
35
# File 'lib/trace_visualization/mapping.rb', line 33

def process_without_reorder(&block)
  instance_eval(&block)
end

#restore(pos = 0, length = @data.length) ⇒ Object

Restore source data

Parameters:

  • pos (Integer) (defaults to: 0)

    begin position

  • length (Integer) (defaults to: @data.length)

    the length of restore part



158
159
160
# File 'lib/trace_visualization/mapping.rb', line 158

def restore(pos = 0, length = @data.length)
  @data[pos ... pos + length].inject("") { |res, c| res += c.value }
end

#sizeObject



133
134
135
# File 'lib/trace_visualization/mapping.rb', line 133

def size
  length
end

#to_aryObject



162
163
164
# File 'lib/trace_visualization/mapping.rb', line 162

def to_ary
  @data.collect { |lexeme| lexeme.value }
end