Class: Rod::Index::SegmentedIndex

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

Overview

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

Constant Summary collapse

BUCKETS_COUNT =

Default number of buckats.

1001

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 = {:buckets_count => BUCKETS_COUNT}) ⇒ SegmentedIndex

Creats the index with given path, with the previous index instance and the following options:

  • :buckets_count - the number of buckets.



14
15
16
17
18
19
20
# File 'lib/rod/index/segmented_index.rb', line 14

def initialize(path,klass,options={:buckets_count => BUCKETS_COUNT})
  super(klass)
  @path = path + "_idx/"
  @buckets_count = options[:buckets_count] || BUCKETS_COUNT
  @buckets_ceil = Math::log2(@buckets_count).ceil
  @buckets = {}
end

Instance Method Details

#destroyObject

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



41
42
43
44
# File 'lib/rod/index/segmented_index.rb', line 41

def destroy
  remove_files(@path + "*")
  @buckets = {}
end

#eachObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/rod/index/segmented_index.rb', line 46

def each
  if block_given?
    @buckets_count.times do |bucket_number|
      load_bucket(bucket_number) unless @buckets[bucket_number]
    end
    @buckets.each do |bucket_number,hash|
      hash.each_key do |key|
        yield key, self[key]
      end
    end
  else
    enum_for(:each)
  end
end

#saveObject

Stores the index at @path. Assumes the path exists.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/rod/index/segmented_index.rb', line 23

def save
  unless File.exist?(@path)
    Dir.mkdir(@path)
  end
  @buckets.each do |bucket_number,hash|
    File.open(path_for(bucket_number),"w") do |out|
      proxy_index = {}
      hash.each_key do |key|
        col = self[key]
        col.save
        proxy_index[key] = [col.offset,col.size]
      end
      out.puts(Marshal.dump(proxy_index))
    end
  end
end