Oops ... My cut & paste buffer was old!

The key difference between this script and the old script is that the
writer thread, t1, replaces the searcher after each index update, and
each reader thread, t2 and t3, grab a new copy of the searcher, which
they use for the duration of a search.

So the old searchers are GC'd when no longer required.


===================

require 'rubygems'
require 'ferret'

p Ferret::VERSION

@dir = Ferret::Store::RAMDirectory.new
@writer = Ferret::Index::IndexWriter.new(:dir => @dir)
@searcher = Ferret::Search::Searcher.new(@dir) 
@parser = Ferret::QueryParser.new

@docs = []
@docs << {:id =>  1, :name => 'Fred',       :occupation => 'Toon'}
@docs << {:id =>  2, :name => 'Barney',     :occupation => 'Toon'}
@docs << {:id =>  3, :name => 'Wilma',      :occupation => 'Toon'}
@docs << {:id =>  4, :name => 'Betty',      :occupation => 'Toon'}
@docs << {:id =>  5, :name => 'Pebbles',    :occupation => 'Toon'}

@docs << {:id =>  6, :name => 'Superman',   :occupation => 'Hero'}
@docs << {:id =>  7, :name => 'Batman',     :occupation => 'Hero'}
@docs << {:id =>  8, :name => 'Spiderman',  :occupation => 'Hero'}
@docs << {:id =>  9, :name => 'Green Lantern', :occupation => 'Hero'}
@docs << {:id => 10, :name => 'Dr Strange', :occupation => 'Hero'}

@docs << {:id => 11, :name => 'Phantom',    :occupation => 'Hero'}

[EMAIL PROTECTED] {|doc| @writer << doc}
[EMAIL PROTECTED]
[EMAIL PROTECTED] = Ferret::Search::Searcher.new(@dir) 

#populate index over time
t1 = Thread.new do  
  @docs.each do |doc|    
    p "t1: adding #{doc[:id]} to index"
    @writer << doc       
    @writer.commit
    
    #new searcher   
    @searcher = Ferret::Search::Searcher.new(@dir) 
    sleep(10)
  end
end   

#search for heroes over time
t2 = Thread.new do  
  query_txt = 'occupation:hero'
  query = @parser.parse(query_txt)
  while true do
    mysearcher = @searcher
    hits = mysearcher.search(query)
    p "t2: searching for #{query_txt} found #{hits.total_hits}"    
    break if hits.total_hits == 6
    
    sleep(5)
  end
end

#search for toons over time
t3 = Thread.new do  
  query_txt = 'occupation:toon'
  query = @parser.parse(query_txt)
  while true do
    mysearcher = @searcher
    hits = mysearcher.search(query)
    p "t3: searching for #{query_txt} found #{hits.total_hits}"    
    break if hits.total_hits == 5
    
    sleep(5)
  end
end   
   
t1.join; t2.join; t3.join
_______________________________________________
Ferret-talk mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/ferret-talk

Reply via email to