Class: Solrizer::Rabbit::BufferedIndexer

Inherits:
Object
  • Object
show all
Includes:
ActiveSupport::Benchmarkable
Defined in:
lib/solrizer-rabbit/buffered_indexer.rb

Constant Summary collapse

BUFFER_SIZE =
1000
COMMIT_EVERY =
0

Instance Method Summary collapse

Constructor Details

#initialize(conn) ⇒ BufferedIndexer

Returns a new instance of BufferedIndexer.



8
9
10
11
12
13
14
# File 'lib/solrizer-rabbit/buffered_indexer.rb', line 8

def initialize(conn)
  @count = 0
  @batch_count = 0
  @add_buffer = []
  @delete_buffer = []
  @solr = conn
end

Instance Method Details

#add(doc) ⇒ Object



68
69
70
71
# File 'lib/solrizer-rabbit/buffered_indexer.rb', line 68

def add(doc)
  @add_buffer << doc
  increment
end

#delete_by_id(doc) ⇒ Object



72
73
74
75
# File 'lib/solrizer-rabbit/buffered_indexer.rb', line 72

def delete_by_id(doc)
  @delete_buffer << doc
  increment
end

#flush(commit = false) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/solrizer-rabbit/buffered_indexer.rb', line 16

def flush(commit = false)
  try_to_add unless @add_buffer.empty?
  @add_buffer = []
  try_to_delete unless @delete_buffer.empty?
  @delete_buffer = []
  @count = 0
  maybe_commit()
end

#maybe_commit(force = false) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/solrizer-rabbit/buffered_indexer.rb', line 59

def maybe_commit(force=false)
  return if COMMIT_EVERY == 0 && !force
  @batch_count += 1
  if force || @batch_count > COMMIT_EVERY
    solr.commit
    @batch_count =0
  end
end

#try_to_addObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/solrizer-rabbit/buffered_indexer.rb', line 25

def try_to_add
  tries = 0
  begin 
    benchmark "#{$$} -- #{Rails.env} solr add" do
      solr.add @add_buffer
    end
  rescue TimeoutError 
    ## The timeout is set in this parameter.  It is 60 seconds by default.
    # rsolr.connection.connection.read_timeout = 60
    tries += 1 
    puts "Timeout #{tries}" 
    sleep(10 * tries) # wait a little longer each time through
    retry if tries < 5 
    raise "Adding docs timed out #{tries} times. Qutting." 
  end
end

#try_to_deleteObject



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/solrizer-rabbit/buffered_indexer.rb', line 42

def try_to_delete
  tries = 0
  begin 
    benchmark "#{$$} -- #{Rails.env} solr delete" do
      solr.delete_by_id @delete_buffer
    end
  rescue TimeoutError 
    ## The timeout is set in this parameter.  It is 60 seconds by default.
    # rsolr.connection.connection.read_timeout = 60
    tries += 1 
    puts "Timeout #{tries}" 
    sleep(10 * tries) # wait a little longer each time through
    retry if tries < 5 
    raise "Adding docs timed out #{tries} times. Qutting." 
  end
end