2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / beagled / Lucene.Net / Index / SegmentInfos.cs
blob314837a2288c3d66838b379c3cdf04913c43b62f
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 using Directory = Lucene.Net.Store.Directory;
18 using IndexInput = Lucene.Net.Store.IndexInput;
19 using IndexOutput = Lucene.Net.Store.IndexOutput;
20 namespace Lucene.Net.Index
23 [Serializable]
24 sealed public class SegmentInfos : System.Collections.ArrayList
27 /// <summary>The file format version, a negative number. </summary>
28 /* Works since counter, the old 1st entry, is always >= 0 */
29 public const int FORMAT = - 1;
31 public int counter = 0; // used to name new segments
32 private long version = 0; //counts how often the index has been changed by adding or deleting docs
34 public SegmentInfo Info(int i)
36 return (SegmentInfo) this[i];
39 public void Read(Directory directory)
42 IndexInput input = directory.OpenInput("segments");
43 try
45 int format = input.ReadInt();
46 if (format < 0)
48 // file contains explicit format info
49 // check that it is a format we can understand
50 if (format < FORMAT)
51 throw new System.IO.IOException("Unknown format version: " + format);
52 version = input.ReadLong(); // read version
53 counter = input.ReadInt(); // read counter
55 else
57 // file is in old format without explicit format info
58 counter = format;
61 for (int i = input.ReadInt(); i > 0; i--)
63 // read segmentInfos
64 SegmentInfo si = new SegmentInfo(input.ReadString(), input.ReadInt(), directory);
65 Add(si);
68 if (format >= 0)
70 // in old format the version number may be at the end of the file
71 if (input.GetFilePointer() >= input.Length())
72 version = 0;
73 // old file format without version number
74 else
75 version = input.ReadLong(); // read version
78 finally
80 input.Close();
84 public void Write(Directory directory)
86 IndexOutput output = directory.CreateOutput("segments.new");
87 try
89 output.WriteInt(FORMAT); // write FORMAT
90 output.WriteLong(++version); // every write changes the index
91 output.WriteInt(counter); // write counter
92 output.WriteInt(Count); // write infos
93 for (int i = 0; i < Count; i++)
95 SegmentInfo si = Info(i);
96 output.WriteString(si.name);
97 output.WriteInt(si.docCount);
100 finally
102 output.Close();
105 // install new segment info
106 directory.RenameFile("segments.new", "segments");
109 /// <summary> version number when this SegmentInfos was generated.</summary>
110 public long GetVersion()
112 return version;
115 /// <summary> Current version number from segments file.</summary>
116 public static long ReadCurrentVersion(Directory directory)
119 IndexInput input = directory.OpenInput("segments");
120 int format = 0;
121 long version = 0;
124 format = input.ReadInt();
125 if (format < 0)
127 if (format < FORMAT)
128 throw new System.IO.IOException("Unknown format version: " + format);
129 version = input.ReadLong(); // read version
132 finally
134 input.Close();
137 if (format < 0)
138 return version;
140 // We cannot be sure about the format of the file.
141 // Therefore we have to read the whole file and cannot simply seek to the version entry.
143 SegmentInfos sis = new SegmentInfos();
144 sis.Read(directory);
145 return sis.GetVersion();