Skip to content

Instantly share code, notes, and snippets.

@floere
Created October 30, 2011 23:46
Show Gist options
  • Select an option

  • Save floere/1326598 to your computer and use it in GitHub Desktop.

Select an option

Save floere/1326598 to your computer and use it in GitHub Desktop.
require '/Users/admin/temp/picky/server/lib/picky'
t0 = Time.now
class Tweet
attr_reader :id, :user, :text
def initialize id, user, text
@id, @user, @text = id, user, text
end
def to_s
"#{id} #{user}: #{text}"
end
def self.index
@index ||= Picky::Index.new :tweets do
category :user, partial: Picky::Partial::None.new
category :text, partial: Picky::Partial::None.new
end
end
def self.count
@index && @index[:user].exact.inverted.size
end
def self.search query
@search ||= Picky::Search.new index
@search.search(query).ids
end
end
class RandomString
@@chars = ('a'..'z').to_a
def give_me_one_with characters
Array.new(characters).collect! do
@@chars[rand(@@chars.size)]
end.join
end
end
random = Random.new
random_string = RandomString.new
# Rotating insert in Array.
#
inserted = 0
size = 100_000
last_ids = Array[size]
t1 = Thread.new do
loop do
sleep 0.0001
user = random_string.give_me_one_with 10
text = random_string.give_me_one_with 100
Tweet.index.add Tweet.new(inserted, user, text)
last_ids[inserted % size] = inserted
inserted += 1 # random.rand 100_000_000
Tweet.index.remove last_ids[inserted % size]
end
end
t2 = Thread.new do
begin
loop do
puts "#{Time.now - t0} ===== #{last_100_ids.count} in index (#{inserted} inserted) =============="
query = $stdin.gets.strip
puts Tweet.search(query).join("\n")
end
rescue StandardError => e
p [e.class, e.message, e.backtrace]
end
end
t1.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment