Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Index / SegmentInfos.cs
blobf75ebe7885216cdd34238f334971d1ddf6912da1
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 InputStream = Lucene.Net.Store.InputStream;
19 using OutputStream = Lucene.Net.Store.OutputStream;
20 namespace Lucene.Net.Index
22 [Serializable]
23 sealed public class SegmentInfos : System.Collections.ArrayList
26 /// <summary>The file format version, a negative number. </summary>
27 /* Works since counter, the old 1st entry, is always >= 0 */
28 public const int FORMAT = - 1;
30 public int counter = 0; // used to name new segments
31 private long version = 0; //counts how often the index has been changed by adding or deleting docs
33 public SegmentInfo Info(int i)
35 return (SegmentInfo) this[i];
38 public void Read(Directory directory)
41 InputStream input = directory.OpenFile("segments");
42 try
44 int format = input.ReadInt();
45 if (format < 0)
47 // file contains explicit format info
48 // check that it is a format we can understand
49 if (format < FORMAT)
50 throw new System.IO.IOException("Unknown format version: " + format);
51 version = input.ReadLong(); // read version
52 counter = input.ReadInt(); // read counter
54 else
56 // file is in old format without explicit format info
57 counter = format;
60 for (int i = input.ReadInt(); i > 0; i--)
62 // read segmentInfos
63 SegmentInfo si = new SegmentInfo(input.ReadString(), input.ReadInt(), directory);
64 Add(si);
67 if (format >= 0)
69 // in old format the version number may be at the end of the file
70 if (input.GetFilePointer() >= input.Length())
71 version = 0;
72 // old file format without version number
73 else
74 version = input.ReadLong(); // read version
77 finally
79 input.Close();
83 public void Write(Directory directory)
85 OutputStream output = directory.CreateFile("segments.new");
86 try
88 output.WriteInt(FORMAT); // write FORMAT
89 output.WriteLong(++version); // every write changes the index
90 output.WriteInt(counter); // write counter
91 output.WriteInt(Count); // write infos
92 for (int i = 0; i < Count; i++)
94 SegmentInfo si = Info(i);
95 output.WriteString(si.name);
96 output.WriteInt(si.docCount);
99 finally
101 output.Close();
104 // install new segment info
105 directory.RenameFile("segments.new", "segments");
108 /// <summary> version number when this SegmentInfos was generated.</summary>
109 public long GetVersion()
111 return version;
114 /// <summary> Current version number from segments file.</summary>
115 public static long ReadCurrentVersion(Directory directory)
118 InputStream input = directory.OpenFile("segments");
119 int format = 0;
120 long version = 0;
123 format = input.ReadInt();
124 if (format < 0)
126 if (format < FORMAT)
127 throw new System.IO.IOException("Unknown format version: " + format);
128 version = input.ReadLong(); // read version
131 finally
133 input.Close();
136 if (format < 0)
137 return version;
139 // We cannot be sure about the format of the file.
140 // Therefore we have to read the whole file and cannot simply seek to the version entry.
142 SegmentInfos sis = new SegmentInfos();
143 sis.Read(directory);
144 return sis.GetVersion();