2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
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");
45 int format
= input
.ReadInt();
48 // file contains explicit format info
49 // check that it is a format we can understand
51 throw new System
.IO
.IOException("Unknown format version: " + format
);
52 version
= input
.ReadLong(); // read version
53 counter
= input
.ReadInt(); // read counter
57 // file is in old format without explicit format info
61 for (int i
= input
.ReadInt(); i
> 0; i
--)
64 SegmentInfo si
= new SegmentInfo(input
.ReadString(), input
.ReadInt(), directory
);
70 // in old format the version number may be at the end of the file
71 if (input
.GetFilePointer() >= input
.Length())
73 // old file format without version number
75 version
= input
.ReadLong(); // read version
84 public void Write(Directory directory
)
86 IndexOutput output
= directory
.CreateOutput("segments.new");
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
);
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()
115 /// <summary> Current version number from segments file.</summary>
116 public static long ReadCurrentVersion(Directory directory
)
119 IndexInput input
= directory
.OpenInput("segments");
124 format
= input
.ReadInt();
128 throw new System
.IO
.IOException("Unknown format version: " + format
);
129 version
= input
.ReadLong(); // read 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();
145 return sis
.GetVersion();