Skip to content

Instantly share code, notes, and snippets.

@wjessop
Last active September 21, 2017 18:41
Show Gist options
  • Save wjessop/43c32520cee6d4dd5c88ad73a4e4aad4 to your computer and use it in GitHub Desktop.
Save wjessop/43c32520cee6d4dd5c88ad73a4e4aad4 to your computer and use it in GitHub Desktop.
# This code has edge cases on it's edge cases. If you use it in production, or even at all, it may eat your cat/dog/hamster
require 'time'
def time_from_string(line)
m = /\A\[(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} \+\d{4})\]/.match(line)
if m
return Time.parse(m[:time])
end
end
def read_until_time(io)
loop do
c = io.getc
if c == "\n"
return time_from_string(io.readline)
end
raise EOFError if io.size == io.pos
io.seek(io.pos - c.bytesize - 1)
end
end
def within_window(time, window)
time >= window[0] && time <= window[1]
end
# Expects io to be seeked to before the start of the window
def read_data_from_pos(io, window)
begin
io.each_line {|ln|
t = time_from_string(ln)
if within_window(t, window)
puts ln
elsif t > window[1]
return
end
}
rescue EOFError
end
end
def search_for_date(filename, window_of_interest)
io = File.open filename
window_end = io.size
window_start = 0
begin
loop do
new_seek = ((window_end - window_start) / 2) + window_start
io.seek(new_seek)
# Search back for date if it's not found at current position
time = read_until_time(io)
if within_window(time, window_of_interest)
before_window = nil
seek_to = io.pos - 1024
loop do
io.seek(seek_to)
time = read_until_time(io)
if within_window(time, window_of_interest)
seek_to = seek_to - 1024
else
read_data_from_pos(io, window_of_interest)
return
end
end
elsif time < window_of_interest[0]
# seek forwards
window_start = io.pos
else
# seek backwards
window_end = io.pos
end
end
rescue EOFError
puts "Read to the end of the file but no matching entries"
end
end
window_of_interest = [Time.parse("2043-01-25 12:35:00"), Time.parse("2043-01-25 12:40:00")]
search_for_date('log', window_of_interest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment