Created
January 27, 2014 17:57
-
-
Save worace/8653905 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
profiler on index: