Class: Roby::DRoby::Logfile::Index
- Includes:
- Enumerable
- Defined in:
- lib/roby/droby/logfile/index.rb
Instance Attribute Summary collapse
-
#data ⇒ Array<Hash>
readonly
The index data.
-
#file_size ⇒ Object
readonly
The size in bytes of the file that has been indexed.
-
#file_time ⇒ Object
readonly
The modification time of the file that has been indexed.
Class Method Summary collapse
-
.read(filename) ⇒ Object
Read an index file.
-
.rebuild(event_io, index_io) ⇒ Object
Creates an index file for
event_loginindex_log.
Instance Method Summary collapse
- #[](*args) ⇒ Object
-
#cycle_count ⇒ Object
Returns the number of cycles in this index.
- #each(&block) ⇒ Object
-
#empty? ⇒ Boolean
Tests whether this index contains cycles.
-
#initialize(file_size, file_time, data) ⇒ Index
constructor
A new instance of Index.
-
#range ⇒ nil, (Time,Time)
The time range.
- #size ⇒ Object
-
#valid_for?(path) ⇒ Boolean
Tests whether this index is valid for a given file.
Constructor Details
#initialize(file_size, file_time, data) ⇒ Index
Returns a new instance of Index.
47 48 49 50 51 |
# File 'lib/roby/droby/logfile/index.rb', line 47 def initialize(file_size, file_time, data) @file_size = file_size @file_time = file_time @data = data end |
Instance Attribute Details
#data ⇒ Array<Hash> (readonly)
The index data
45 46 47 |
# File 'lib/roby/droby/logfile/index.rb', line 45 def data @data end |
#file_size ⇒ Object (readonly)
The size in bytes of the file that has been indexed
39 40 41 |
# File 'lib/roby/droby/logfile/index.rb', line 39 def file_size @file_size end |
#file_time ⇒ Object (readonly)
The modification time of the file that has been indexed
41 42 43 |
# File 'lib/roby/droby/logfile/index.rb', line 41 def file_time @file_time end |
Class Method Details
.read(filename) ⇒ Object
Read an index file
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/roby/droby/logfile/index.rb', line 99 def self.read(filename) io = File.open(filename) file_info = io.read(16) size, tv_sec, tv_nsec = file_info.unpack("Q<L<L<") data = Array.new begin while !io.eof? data << ::Marshal.load(Logfile.read_one_chunk(io)) end rescue EOFError end new(size, Time.at(tv_sec, Rational(tv_nsec, 1000)), data) end |
.rebuild(event_io, index_io) ⇒ Object
Creates an index file for event_log in index_log
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/roby/droby/logfile/index.rb', line 6 def self.rebuild(event_io, index_io) stat = File.stat(event_io.path) event_log = Reader.new(event_io) index_io.write [stat.size, stat.mtime.tv_sec, stat.mtime.tv_nsec].pack("Q<L<L<") dump_io = StringIO.new("", 'w') while !event_log.eof? current_pos = event_log.tell cycle = event_log.load_one_cycle info = cycle.last.last event_count = 0 cycle.each_slice(4) do |m, *| if m.to_s !~ /^timepoint/ event_count += 1 end end info[:event_count] = event_count info[:pos] = current_pos if block_given? yield(Float(event_io.tell) / end_pos) end info = ::Marshal.dump(info) index_io.write [info.size].pack("L<") index_io.write info end rescue EOFError ensure index_io.flush if index_io end |
Instance Method Details
#[](*args) ⇒ Object
57 58 59 |
# File 'lib/roby/droby/logfile/index.rb', line 57 def [](*args) data[*args] end |
#cycle_count ⇒ Object
Returns the number of cycles in this index
77 78 79 |
# File 'lib/roby/droby/logfile/index.rb', line 77 def cycle_count data.size end |
#each(&block) ⇒ Object
61 62 63 |
# File 'lib/roby/droby/logfile/index.rb', line 61 def each(&block) data.each(&block) end |
#empty? ⇒ Boolean
Tests whether this index contains cycles
82 83 84 |
# File 'lib/roby/droby/logfile/index.rb', line 82 def empty? data.empty? end |
#range ⇒ nil, (Time,Time)
The time range
89 90 91 92 93 94 |
# File 'lib/roby/droby/logfile/index.rb', line 89 def range if !data.empty? [Time.at(*data.first[:start]), Time.at(*data.last[:start]) + data.last[:end]] end end |
#size ⇒ Object
53 54 55 |
# File 'lib/roby/droby/logfile/index.rb', line 53 def size data.size end |
#valid_for?(path) ⇒ Boolean
Tests whether this index is valid for a given file
71 72 73 74 |
# File 'lib/roby/droby/logfile/index.rb', line 71 def valid_for?(path) stat = File.stat(path) stat.size == file_size && stat.mtime == file_time end |