public
Last active — forked from nkallen/MODULARITY_OLYMPICS.markdown

  • Download Gist
a_program.rb
Ruby
1 2
query_factory = MemoizingQueryFactory.new(TimingOutQueryFactory.new(Query, 1.second))
QueryEvaluator.new(ConnectionPool.new(20), query_factory)
memoizing_query_factory.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11
class MemoizingQueryFactory
def initialize(query_factory)
@memo = Hash.new do |h, k|
h = query_factory.new(*k)
end
end
 
def new(connection, query_string, *args)
@memo[[connection, query_string, args]]
end
end
query_evaluator.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12
class QueryEvaluator
def initialize(connection_pool, query_factory = Query)
@query_factory = query_factory
@connection_pool = connection_pool
end
 
def select(query_string, *args)
@connection_pool.with_connection do |connection|
@query_factory.new(connection, query_string, *args).select
end
end
end
timing_out_query_proxy.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
class TimingOutQueryProxyFactory
def initialize(query_factory, timeout)
@query_factory = query_factory
@timeout = timeout
end
 
def new(connection, query_string, *args)
@query_factory.new(connection, query_string, *args)
end
end
 
class TimingOutQueryProxy
def initialize(query, timeout)
@query = query
@timeout = timeout
end
 
def select
Timeout.timeout(@timeout) do
@query.select
end
end
 
# ...
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.