Method: Ferret::Index::IndexWriter#initialize

Defined in:
lib/ferret/index/index_writer.rb

#initialize(dir = nil, options = {}) ⇒ IndexWriter

Constructs an IndexWriter for the index in dir. Text will be analyzed with analyzer. If create is true, then a new, empty index will be created in dir, replacing the index already there, if any.

NOTE

all options are passed in a hash.

dir

the index directory

Options

analyzer

the analyzer to use. Defaults to StandardAnalyzer.

create

true to create the index or overwrite the existing one false to append to the existing index

create_if_missing

true to create the index if it’s missing false to throw an IOError if it’s missing

close_dir

This specifies whether you would this class to close the index directory when this class is closed. The default is false.

use_compound_file

Use a compound file to store the index. This is slower than using multiple files but it prevents the too many files open error. This defaults to true.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/ferret/index/index_writer.rb', line 71

def initialize(dir = nil, options = {})
  super()
  create = options[:create] || false
  create_if_missing = options[:create_if_missing] || false

  if dir.nil?
    @directory = Ferret::Store::RAMDirectory.new
  elsif dir.is_a?(String)
    @directory = Ferret::Store::FSDirectory.new(dir, create)
  else
    @directory = dir
  end
  @close_dir = options[:close_dir] || false
  @use_compound_file = (options[:use_compound_file] != false) # ie default true
  @analyzer = options[:analyzer] || Ferret::Analysis::StandardAnalyzer.new
  @merge_factor = DEFAULT_MERGE_FACTOR
  @min_merge_docs = DEFAULT_MIN_MERGE_DOCS
  @max_merge_docs = DEFAULT_MAX_MERGE_DOCS
  @max_field_length = DEFAULT_MAX_FIELD_LENGTH
  @term_index_interval = DEFAULT_TERM_INDEX_INTERVAL

  @similarity = Search::Similarity.default
  @segment_infos = SegmentInfos.new()
  @ram_directory = Ferret::Store::RAMDirectory.new()

  # Make sure that the lock is released when this object is destroyed
  define_finalizer(self, proc { |id| @write_lock.release() if @write_lock})
   
  @write_lock = @directory.make_lock(WRITE_LOCK_NAME)
  @write_lock.obtain(WRITE_LOCK_TIMEOUT) # obtain write lock

  @directory.synchronize() do # in- & inter-process sync
    @directory.make_lock(COMMIT_LOCK_NAME).while_locked(COMMIT_LOCK_TIMEOUT) do
      if (create)
        @segment_infos.write(@directory)
      else
        begin
          @segment_infos.read(@directory)
        rescue Exception => e
          if options[:create_if_missing]
            @segment_infos.write(@directory)
          else
            @write_lock.release() # obtain write lock
            raise e
          end
        end
      end 
    end
  end

  @info_stream = nil
end