Class: Rod::Index::FlatIndex

Inherits:
Base
  • Object
show all
Defined in:
lib/rod/index/flat_index.rb

Overview

Class implementing segmented index, i.e. an index which allows for lazy loading of its pieces.

Instance Attribute Summary

Attributes inherited from Base

#path

Instance Method Summary collapse

Methods inherited from Base

#[], #copy, create, #key_persisted, #to_s

Methods included from Utils

#remove_file, #remove_files, #remove_files_but, #report_progress

Constructor Details

#initialize(path, klass, options = {}) ⇒ FlatIndex

Creats the index with given path for given klass. Options are not used in the case of FlatIndex.



11
12
13
14
15
# File 'lib/rod/index/flat_index.rb', line 11

def initialize(path,klass,options={})
  super(klass)
  @path = path + ".idx"
  @index = nil
end

Instance Method Details

#destroyObject

Destroys the index (removes it from the disk completely).



35
36
37
# File 'lib/rod/index/flat_index.rb', line 35

def destroy
  remove_file(@path)
end

#eachObject



39
40
41
42
43
44
45
46
47
48
# File 'lib/rod/index/flat_index.rb', line 39

def each
  load_index unless loaded?
  if block_given?
    @index.each_key do |key|
      yield key, self[key]
    end
  else
    enum_for(:each)
  end
end

#saveObject

Stores the index on disk.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/rod/index/flat_index.rb', line 18

def save
  # The index was not loaded nor modified.
  return if @index.nil?
  File.open(@path,"w") do |out|
    proxy_index = {}
    #load_index unless loaded?
    @index.each_key do |key|
      col = self[key]
      col.save
      proxy_index[key] = [col.offset,col.size]
    end
    out.puts(Marshal.dump(proxy_index))
    @index = nil
  end
end