Module: RecordSearch

Defined in:
lib/recordsearch/search.rb,
lib/recordsearch/indexer.rb,
lib/recordsearch/version.rb,
lib/recordsearch/data_source.rb

Defined Under Namespace

Modules: VERSION Classes: DataSource, FileDataSource, Search

Class Method Summary collapse

Class Method Details

.index(data_source, db, db_idx = nil) ⇒ Object

Builds an index file using as source data data_source, which can be a subclass of RecordSearch::DataSource or an object with a next method.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/recordsearch/indexer.rb', line 4

def self.index(data_source, db, db_idx=nil)
   db_idx = db + '.idx' if db_idx.nil?
   records = 0

   File.open(db, 'w') do |fdb|
     File.open(db_idx, 'w') do |fidx|
       last = nil
       while record = data_source.next
         if !last.nil? && last >= record
           raise "data is not ordered! #{last} >= #{record}"
         end

         fidx.write([fdb.pos].pack('L'))
         fdb.write(record)

         last = record
         records += 1
       end
     end
   end

   records
end