~ubuntu-branches/ubuntu/wily/ruby-ferret/wily

« back to all changes in this revision

Viewing changes to test/threading/thread_safety_index_test.rb

  • Committer: Bazaar Package Importer
  • Author(s): Antonio Terceiro
  • Date: 2011-07-28 00:02:49 UTC
  • Revision ID: james.westby@ubuntu.com-20110728000249-v0443y69ftcpxwi6
Tags: upstream-0.11.6
ImportĀ upstreamĀ versionĀ 0.11.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
require File.dirname(__FILE__) + "/../test_helper"
 
2
require File.dirname(__FILE__) + "/number_to_spoken.rb"
 
3
require 'thread'
 
4
 
 
5
class IndexThreadSafetyTest < Test::Unit::TestCase
 
6
  include Ferret::Index
 
7
 
 
8
  INDEX_DIR = File.expand_path(File.join(File.dirname(__FILE__), "index"))
 
9
  ITERATIONS = 100
 
10
  NUM_THREADS = 3
 
11
  ANALYZER = Ferret::Analysis::StandardAnalyzer.new()
 
12
 
 
13
  def setup
 
14
    index = Index.new(:path => INDEX_DIR,
 
15
                      :create => true,
 
16
                      :analyzer => ANALYZER,
 
17
                      :default_field => :content)
 
18
    index.close
 
19
  end
 
20
 
 
21
  def indexing_thread()
 
22
    index = Index.new(:path => INDEX_DIR,
 
23
                      :analyzer => ANALYZER,
 
24
                      :default_field => :content)
 
25
 
 
26
    ITERATIONS.times do
 
27
      choice = rand()
 
28
 
 
29
      if choice > 0.98
 
30
        do_optimize(index)
 
31
      elsif choice > 0.7
 
32
        do_delete_doc(index)
 
33
      elsif choice > 0.5
 
34
        do_search(index)
 
35
      else
 
36
        do_add_doc(index)
 
37
      end
 
38
      index.commit
 
39
    end
 
40
  end 
 
41
 
 
42
  def do_optimize(index)
 
43
    puts "Optimizing the index"
 
44
    index.optimize
 
45
  end
 
46
 
 
47
  def do_delete_doc(index)
 
48
    return if index.size == 0
 
49
    doc_num = rand(index.size)
 
50
    puts "Deleting #{doc_num} from index which has#{index.has_deletions? ? "" : " no"} deletions"
 
51
    puts "document was already deleted" if (index.deleted?(doc_num))
 
52
    index.delete(doc_num)
 
53
  end
 
54
 
 
55
  def do_add_doc(index)
 
56
    n = rand(0xFFFFFFFF)
 
57
    d = {:id => n, :content => n.to_spoken}
 
58
    puts("Adding #{n}")
 
59
    index << d
 
60
  end
 
61
  
 
62
  def do_search(index)
 
63
    n = rand(0xFFFFFFFF)
 
64
    puts("Searching for #{n}")
 
65
    hits = index.search_each(n.to_spoken, :num_docs => 3) do |d, s|
 
66
      puts "Hit for #{n}: #{index[d][:id]} - #{s}"
 
67
    end
 
68
    puts("Searched for #{n}: total = #{hits}")
 
69
  end
 
70
 
 
71
  def test_threading
 
72
    threads = []
 
73
    NUM_THREADS.times do
 
74
      threads << Thread.new { indexing_thread }
 
75
    end
 
76
 
 
77
    threads.each {|t| t.join}
 
78
  end
 
79
end