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 InputStream
= Lucene
.Net
.Store
.InputStream
;
19 using OutputStream
= Lucene
.Net
.Store
.OutputStream
;
20 namespace Lucene
.Net
.Index
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");
44 int format
= input
.ReadInt();
47 // file contains explicit format info
48 // check that it is a format we can understand
50 throw new System
.IO
.IOException("Unknown format version: " + format
);
51 version
= input
.ReadLong(); // read version
52 counter
= input
.ReadInt(); // read counter
56 // file is in old format without explicit format info
60 for (int i
= input
.ReadInt(); i
> 0; i
--)
63 SegmentInfo si
= new SegmentInfo(input
.ReadString(), input
.ReadInt(), directory
);
69 // in old format the version number may be at the end of the file
70 if (input
.GetFilePointer() >= input
.Length())
72 // old file format without version number
74 version
= input
.ReadLong(); // read version
83 public void Write(Directory directory
)
85 OutputStream output
= directory
.CreateFile("segments.new");
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
);
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()
114 /// <summary> Current version number from segments file.</summary>
115 public static long ReadCurrentVersion(Directory directory
)
118 InputStream input
= directory
.OpenFile("segments");
123 format
= input
.ReadInt();
127 throw new System
.IO
.IOException("Unknown format version: " + format
);
128 version
= input
.ReadLong(); // read 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();
144 return sis
.GetVersion();