Class: Ferret::Index::SegmentInfos

Inherits:
Array
  • Object
show all
Defined in:
lib/ferret/index/segment_infos.rb

Constant Summary collapse

FORMAT =

for compatability with Java Ferret files

-1
SEGMENT_FILENAME =
"segments"
TEMPORARY_SEGMENT_FILENAME =
"segments.new"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSegmentInfos

Returns a new instance of SegmentInfos.



59
60
61
62
# File 'lib/ferret/index/segment_infos.rb', line 59

def initialize()
  @version = Time.now.to_i * 1000
  @counter = 0
end

Instance Attribute Details

#counterObject

counts how often the index has been modified by adding or deleting docs



28
29
30
# File 'lib/ferret/index/segment_infos.rb', line 28

def counter
  @counter
end

#versionObject (readonly)

Returns the value of attribute version.



26
27
28
# File 'lib/ferret/index/segment_infos.rb', line 26

def version
  @version
end

Class Method Details

.read_current_version(directory) ⇒ Object

Current version number from segments file.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/ferret/index/segment_infos.rb', line 31

def SegmentInfos.read_current_version(directory)
  return 0 if not directory.exists?(SEGMENT_FILENAME)
  input = directory.open_input(SEGMENT_FILENAME)
  @format = 0
  @version = 0
  begin
    @format = input.read_int()
    if(@format < 0)
      if (@format < FORMAT) then raise "Unknown format version: " + @format end
      @version = input.read_long() # read version
    end
  ensure
    input.close()
  end
   
  if(@format < 0)
    return @version
  end

  # We cannot be sure about the format of the file.
  # Therefore we have to read the whole file and cannot simply
  # seek to the version entry.

  sis = SegmentInfos.new()
  sis.read(directory)
  return sis.version()
end

Instance Method Details

#initialize_copy(o) ⇒ Object



64
65
66
67
# File 'lib/ferret/index/segment_infos.rb', line 64

def initialize_copy(o)
  super
  o.each_index {|i| self[i] = o[i].clone}
end

#read(directory) ⇒ Object



69
70
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
# File 'lib/ferret/index/segment_infos.rb', line 69

def read(directory)
  input = directory.open_input(SEGMENT_FILENAME)
  begin
    @format = input.read_int()
    if(@format < 0) # file contains explicit format info
      # check that it is a format we can understand
      if (@format < FORMAT) then raise "Unknown format version: " + @format end
      @version = input.read_long()
      @counter = input.read_int()
    else  # file is in old format without explicit format info
      @counter = @format
    end
    
    seg_count = input.read_int()
    seg_count.times do
      self << SegmentInfo.new(input.read_string(),
                              input.read_int(),
                              directory)
    end
   
    if(@format >= 0)   
      # in old format the version number may be at the end of the file
      if (input.pos() >= input.length())
        @version = 0 # old file format without version number
      else
        @version = input.read_long() # read version
      end
    end
  ensure
    input.close()
  end
end

#to_sObject



122
123
124
125
126
127
# File 'lib/ferret/index/segment_infos.rb', line 122

def to_s()
  str = "\nSegmentInfos: <"
  each() { |si| str << "#{si.name}:#{si.doc_count}," }
  str[-1] = ">"
  str
end

#write(directory) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ferret/index/segment_infos.rb', line 102

def write(directory)
  output = directory.create_output(TEMPORARY_SEGMENT_FILENAME)
  begin
    output.write_int(FORMAT) # write FORMAT
    output.write_long(@version += 1) # every write changes the index
    output.write_int(@counter) # write counter
    output.write_int(size()) # write infos
    each() do |si|
      output.write_string(si.name)
      output.write_int(si.doc_count)
    end         
  
  ensure
    output.close()
  end

  # install new segment info
  directory.rename(TEMPORARY_SEGMENT_FILENAME, SEGMENT_FILENAME)
end