Class: Stellr::Collections::WriteableCollection

Inherits:
SearchableCollection show all
Defined in:
lib/stellr/collections/writeable_collection.rb

Overview

Base class for collection implementations that allow index updates

Direct Known Subclasses

Static

Instance Attribute Summary

Attributes inherited from Base

#logger, #name

Instance Method Summary collapse

Methods inherited from SearchableCollection

#highlight, #on_shutdown, #search, #size

Methods inherited from Base

create, #on_shutdown

Methods included from Utils::Observable

#add_listener, #listeners, #notify_listeners

Methods included from Utils::Shutdown

#shutdown, #shutting_down?

Constructor Details

#initialize(name, options) ⇒ WriteableCollection

Returns a new instance of WriteableCollection.



7
8
9
10
11
12
# File 'lib/stellr/collections/writeable_collection.rb', line 7

def initialize( name, options )
  super
  @writer_monitor = Monitor.new
  @processed_records = 0
  @writer = nil
end

Instance Method Details

#add_record(record, boost = nil) ⇒ Object Also known as: <<

Adds the given record to the index.

Record may be a hash, or a Ferret::Document instance



17
18
19
# File 'lib/stellr/collections/writeable_collection.rb', line 17

def add_record( record, boost = nil )
  add_records [ [ record, boost ] ]
end

#add_records(records) ⇒ Object

adds multiple records at once records should be an array of hashes or of two-element arrays consisting of a hash and the record-specific boost value



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/stellr/collections/writeable_collection.rb', line 25

def add_records(records)
  return unless records.any?
  records = if Hash === records.first
    records.map{ |r| prepare_document r }
  else
    records.map{ |r, boost| prepare_document(r, boost) }
  end
  @writer_monitor.synchronize do
    w = writer
    records.each do |record|
      @processed_records += 1
      w.delete :id, record[:id].to_s # ensure uniqueness by :id field
      w << record
    end
    w.commit
  end
  true
end

#batch_finishedObject

called whenever the strategy thinks it’s a good time do do something timeconsuming (like switching indexes, optimizing, flushing, …)



66
67
# File 'lib/stellr/collections/writeable_collection.rb', line 66

def batch_finished
end

#clear!Object



60
61
62
# File 'lib/stellr/collections/writeable_collection.rb', line 60

def clear!
  @processed_records = 0
end

#closeObject

close this collection



70
71
72
73
# File 'lib/stellr/collections/writeable_collection.rb', line 70

def close
  close_writer
  super
end

#delete_record(record) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
# File 'lib/stellr/collections/writeable_collection.rb', line 44

def delete_record( record )
  raise ArgumentError.new("record must contain :id field") if record[:id].nil?
  @writer_monitor.synchronize do
    w = writer
    @processed_records += 1
    w.delete :id, record[:id].to_s
    w.commit
  end
  true
end

#dirty?Boolean

true if records have been processed since the last call to clear!

Returns:

  • (Boolean)


56
57
58
# File 'lib/stellr/collections/writeable_collection.rb', line 56

def dirty?
  @processed_records > 0
end

#flushObject

flush any unwritten changes to the index



76
77
78
79
80
# File 'lib/stellr/collections/writeable_collection.rb', line 76

def flush
  @writer_monitor.synchronize do
    writer.commit
  end
end

#optimizeObject

optimize the index



83
84
85
86
87
# File 'lib/stellr/collections/writeable_collection.rb', line 83

def optimize
  @writer_monitor.synchronize do
    writer.optimize
  end
end