Skip to content

Instantly share code, notes, and snippets.

@jcalvert
Created March 19, 2012 15:23
Show Gist options
  • Save jcalvert/2116098 to your computer and use it in GitHub Desktop.
Save jcalvert/2116098 to your computer and use it in GitHub Desktop.
candidate's sample code. ouch. sql injection
require 'rubygems'
require 'sequel'
require 'sqlite3'
def readFile(filePath)
#Creating file handler
STDOUT.sync = true
fileObj = File.open(filePath)
begin
#Making use of in-memory database for faster and better performace
db = Sequel.sqlite
#Creating inmemory structure of Table
# create an items table
db.create_table :items do
primary_key :id
String :key_name #For storing the String
Integer :count #For storing it's occurrences
Integer :foundat #Found at column
Float :updated #Storing epoch hour format for millisecond , that helps order what recently found
end
items = db[:items] # Getting Table in normal objects
while(ln= fileObj.gets("\n")) # Insted of reading total file gets with \n helps reading single line. Easy to oprate on large files
splitted = ln.split(',') # Now get the ',' values into Array
splitted.each_with_index do |k,index|
rows = db.fetch("SELECT id,count FROM items where key_name='#{k}'") # Chek for the exixting occurrence
if rows.count > 0
#if found update time, column and Time
rows.each do |rs|
db << "update items set updated = #{Time.new.to_f} , count = #{rs[:count]+1} , foundat = #{index+1} where id = '#{rs[:id]}'"
print "."
end
else
#If not then insert a new record
items.insert(:key_name => k , :count => 1, :foundat => index+1,:updated => Time.new.to_f)
p "creating "
end
#
#
end
end
# After parsing cunnect back to in-memory Database get the results that were found recently
db.fetch("SELECT * FROM items order by updated desc") do |row|
puts "#{row[:key_name]} - column #{row[:foundat]} - occurrences: #{row[:count]}"
end
rescue Exception => e
p e
end
end
time = Time.now.to_i
readFile(ARGV[0])
p "Total time taken #{Time.now.to_i - time}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment