Skip to content

Instantly share code, notes, and snippets.

@worace
Created January 27, 2014 17:57
Show Gist options
  • Save worace/8653905 to your computer and use it in GitHub Desktop.
Save worace/8653905 to your computer and use it in GitHub Desktop.
require 'sinatra'
require 'json'
require 'ruby-prof'
class Index
attr_reader :base_path, :index
def initialize(base_path)
@base_path = base_path
end
def inspect
to_s
end
def to_s
"Index with #{index.keys.count} indexed keys"
end
def build_index
start_time = Time.now
puts "BUILDING INDEX"
walk base_path
puts "FINISHED INDEXING; Took: #{Time.now - start_time} seconds"
self
end
def index
@index ||= Hash.new()
end
def walk(start)
Dir.foreach(start) do |x|
if x == "." or x == ".."
next
elsif File.directory?(File.join(start, x))
walk(File.join(start, x))
else
index_file(File.join(start, x))
end
end
end
def indexed_words
index.keys
end
def index_file(path)
File.readlines(path).each_with_index do |line, line_no|
line.split.each do |word|
#{success: true, results: ["path/to/file1:5", "path/to/another/file:33"]}.to_json
index[word] ||= []
index[word] << "#{File.expand_path(path).gsub(File.expand_path(base_path), "")[1..-1]}:#{line_no+1}"
end
end
end
def search(word)
start=Time.now
matching_keys = indexed_words.select { |w| w.include?(word) }
result = matching_keys.map { |key| index.fetch(key, []) }.flatten
puts "search took: #{Time.now - start} seconds"
result
end
end
DEFAULT_PORT = 9090
set :port, DEFAULT_PORT
get '/healthcheck' do
"true"
end
get '/index' do
puts params.inspect
puts "YR WEBAPP: #{self}"
@@index = Index.new(params[:path]).build_index
#{"path"=>"/Users/horacewilliams/Documents/misc_code/stripe_ctf_14/level3/test/data/input"}
"true"
end
get '/isIndexed' do
puts "is indexed??"
if defined?(@@index)
puts "INDEX IS: #{@@index}"
"true"
else
puts "no index available"
"false"
end
end
get '/' do
content_type :json
{success: true, results: @@index.search(params[:q])}.to_json
end
@worace
Copy link
Author

worace commented Jan 27, 2014

profiler on index:


  %total   %self      total       self       wait      child            calls    Name
--------------------------------------------------------------------------------
 100.00%   0.00%      3.478      0.000      0.000      3.478                1      Index#build_index
                      3.478      0.000      0.000      3.478             1/37      Index#walk
                      0.000      0.000      0.000      0.000              2/2      Kernel#puts
                      0.000      0.000      0.000      0.000              2/2      <Class::Time>#now
                      0.000      0.000      0.000      0.000              1/1      Time#-
                      0.000      0.000      0.000      0.000              1/1      Float#to_s
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000            36/37      <Class::Dir>#foreach
                      3.478      0.000      0.000      3.478             1/37      Index#build_index
  99.99%   0.00%      3.478      0.000      0.000      3.478               37     *Index#walk
                      3.478      0.000      0.000      3.478            37/37      <Class::Dir>#foreach
--------------------------------------------------------------------------------
                      3.478      0.000      0.000      3.478            37/37      Index#walk
  99.99%   0.00%      3.478      0.000      0.000      3.478               37     *<Class::Dir>#foreach
                      3.472      0.000      0.000      3.472            82/82      Index#index_file
                      0.002      0.002      0.000      0.000          118/118      <Class::File>#directory?
                      0.001      0.001      0.000      0.000          236/236      <Class::File>#join
                      0.001      0.001      0.000      0.000            37/37      <Class::Dir>#open
                      0.000      0.000      0.000      0.000            36/37      Index#walk
--------------------------------------------------------------------------------
                      3.472      0.000      0.000      3.472            82/82      <Class::Dir>#foreach
  99.83%   0.01%      3.472      0.000      0.000      3.472               82      Index#index_file
                      3.463      0.000      0.000      3.463            82/82      Enumerable#each_with_index
                      0.009      0.009      0.000      0.000            82/82      <Class::IO>#readlines
--------------------------------------------------------------------------------
                      3.463      0.000      0.000      3.463            82/82      Index#index_file
  99.56%   0.01%      3.463      0.000      0.000      3.463               82      Enumerable#each_with_index
                      3.463      0.016      0.000      3.447          82/7105      Array#each
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000        7023/7105      Array#each
                      3.463      0.016      0.000      3.447          82/7105      Enumerable#each_with_index
  99.56%   0.46%      3.463      0.016      0.000      3.447             7105     *Array#each
                      0.795      0.795      0.000      0.000    277946/277946      <Class::File>#expand_path
                      0.631      0.631      0.000      0.000    138973/138973      String#gsub
                      0.345      0.345      0.000      0.000    277946/277946      Index#index
                      0.147      0.147      0.000      0.000    138973/138973      String#[]
                      0.127      0.127      0.000      0.000    110258/110258      Hash#[]=
                      0.115      0.115      0.000      0.000    138973/138973      Fixnum#to_s
                      0.044      0.044      0.000      0.000        7023/7023      String#split
                      0.000      0.000      0.000      0.000        7023/7105      Array#each
--------------------------------------------------------------------------------
                      0.795      0.795      0.000      0.000    277946/277946      Array#each
  22.87%  22.87%      0.795      0.795      0.000      0.000           277946      <Class::File>#expand_path
--------------------------------------------------------------------------------
                      0.631      0.631      0.000      0.000    138973/138973      Array#each
  18.14%  18.14%      0.631      0.631      0.000      0.000           138973      String#gsub
--------------------------------------------------------------------------------
                      0.345      0.345      0.000      0.000    277946/277946      Array#each
   9.91%   9.91%      0.345      0.345      0.000      0.000           277946      Index#index
                      0.000      0.000      0.000      0.000              1/1      Class#new
--------------------------------------------------------------------------------
                      0.147      0.147      0.000      0.000    138973/138973      Array#each
   4.22%   4.22%      0.147      0.147      0.000      0.000           138973      String#[]
--------------------------------------------------------------------------------
                      0.127      0.127      0.000      0.000    110258/110258      Array#each
   3.67%   3.67%      0.127      0.127      0.000      0.000           110258      Hash#[]=
--------------------------------------------------------------------------------
                      0.115      0.115      0.000      0.000    138973/138973      Array#each
   3.30%   3.30%      0.115      0.115      0.000      0.000           138973      Fixnum#to_s
--------------------------------------------------------------------------------
                      0.044      0.044      0.000      0.000        7023/7023      Array#each
   1.28%   1.28%      0.044      0.044      0.000      0.000             7023      String#split
--------------------------------------------------------------------------------
                      0.009      0.009      0.000      0.000            82/82      Index#index_file
   0.26%   0.25%      0.009      0.009      0.000      0.000               82      <Class::IO>#readlines
                      0.000      0.000      0.000      0.000            82/82      Fixnum#<
--------------------------------------------------------------------------------
                      0.002      0.002      0.000      0.000          118/118      <Class::Dir>#foreach
   0.05%   0.05%      0.002      0.002      0.000      0.000              118      <Class::File>#directory?
--------------------------------------------------------------------------------
                      0.001      0.001      0.000      0.000          236/236      <Class::Dir>#foreach
   0.02%   0.02%      0.001      0.001      0.000      0.000              236      <Class::File>#join
--------------------------------------------------------------------------------
                      0.001      0.001      0.000      0.000            37/37      <Class::Dir>#foreach
   0.02%   0.02%      0.001      0.001      0.000      0.000               37      <Class::Dir>#open
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000            82/82      <Class::IO>#readlines
   0.00%   0.00%      0.000      0.000      0.000      0.000               82      Fixnum#<
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              2/2      Index#build_index
   0.00%   0.00%      0.000      0.000      0.000      0.000                2      Kernel#puts
                      0.000      0.000      0.000      0.000              2/2      IO#puts
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              2/2      Kernel#puts
   0.00%   0.00%      0.000      0.000      0.000      0.000                2      IO#puts
                      0.000      0.000      0.000      0.000              4/4      IO#write
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              2/2      Index#build_index
   0.00%   0.00%      0.000      0.000      0.000      0.000                2      <Class::Time>#now
                      0.000      0.000      0.000      0.000              2/2      Time#initialize
                      0.000      0.000      0.000      0.000              2/2      <Class::Time>#allocate
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              4/4      IO#puts
   0.00%   0.00%      0.000      0.000      0.000      0.000                4      IO#write
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Index#build_index
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Time#-
                      0.000      0.000      0.000      0.000              1/1      Numeric#quo
                      0.000      0.000      0.000      0.000              1/1      Rational#to_f
                      0.000      0.000      0.000      0.000              1/1      Fixnum#-
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              2/2      <Class::Time>#now
   0.00%   0.00%      0.000      0.000      0.000      0.000                2      Time#initialize
                      0.000      0.000      0.000      0.000              2/2      Fixnum#+
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              2/2      Time#initialize
   0.00%   0.00%      0.000      0.000      0.000      0.000                2      Fixnum#+
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Time#-
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Numeric#quo
                      0.000      0.000      0.000      0.000              1/1      Rational#/
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Index#build_index
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Float#to_s
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Index#index
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Class#new
                      0.000      0.000      0.000      0.000              1/1      Hash#initialize
                      0.000      0.000      0.000      0.000              1/1      <Class::Hash>#allocate
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Time#-
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Rational#to_f
                      0.000      0.000      0.000      0.000              1/1      Fixnum#fdiv
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Time#-
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Fixnum#-
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Numeric#quo
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Rational#/
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              2/2      <Class::Time>#now
   0.00%   0.00%      0.000      0.000      0.000      0.000                2      <Class::Time>#allocate
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Class#new
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Hash#initialize
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Rational#to_f
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      Fixnum#fdiv
--------------------------------------------------------------------------------
                      0.000      0.000      0.000      0.000              1/1      Class#new
   0.00%   0.00%      0.000      0.000      0.000      0.000                1      <Class::Hash>#allocate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment