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.
18 using Directory
= Lucene
.Net
.Store
.Directory
;
19 using IndexInput
= Lucene
.Net
.Store
.IndexInput
;
20 using IndexOutput
= Lucene
.Net
.Store
.IndexOutput
;
21 using Constants
= Lucene
.Net
.Util
.Constants
;
23 namespace Lucene
.Net
.Index
27 public sealed class SegmentInfos
: System
.Collections
.ArrayList
30 /// <summary>The file format version, a negative number. </summary>
31 /* Works since counter, the old 1st entry, is always >= 0 */
32 public const int FORMAT
= - 1;
34 public int counter
= 0; // used to name new segments
35 /// <summary> counts how often the index has been changed by adding or deleting docs.
36 /// starting with the current time in milliseconds forces to create unique version numbers.
38 private long version
= System
.DateTime
.Now
.Ticks
;
40 public SegmentInfo
Info(int i
)
42 return (SegmentInfo
) this[i
];
45 public void Read(Directory directory
)
48 IndexInput input
= directory
.OpenInput(IndexFileNames
.SEGMENTS
);
51 int format
= input
.ReadInt();
54 // file contains explicit format info
55 // check that it is a format we can understand
57 throw new System
.IO
.IOException("Unknown format version: " + format
);
58 version
= input
.ReadLong(); // read version
59 counter
= input
.ReadInt(); // read counter
63 // file is in old format without explicit format info
67 for (int i
= input
.ReadInt(); i
> 0; i
--)
70 SegmentInfo si
= new SegmentInfo(input
.ReadString(), input
.ReadInt(), directory
);
76 // in old format the version number may be at the end of the file
77 if (input
.GetFilePointer() >= input
.Length())
78 version
= (System
.DateTime
.Now
.Ticks
- 621355968000000000) / 10000;
79 // old file format without version number
81 version
= input
.ReadLong(); // read version
90 public void Write(Directory directory
)
92 IndexOutput output
= directory
.CreateOutput("segments.new");
95 output
.WriteInt(FORMAT
); // write FORMAT
96 output
.WriteLong(++version
); // every write changes the index
97 output
.WriteInt(counter
); // write counter
98 output
.WriteInt(Count
); // write infos
99 for (int i
= 0; i
< Count
; i
++)
101 SegmentInfo si
= Info(i
);
102 output
.WriteString(si
.name
);
103 output
.WriteInt(si
.docCount
);
111 // install new segment info
112 directory
.RenameFile("segments.new", IndexFileNames
.SEGMENTS
);
115 /// <summary> version number when this SegmentInfos was generated.</summary>
116 public long GetVersion()
121 /// <summary> Current version number from segments file.</summary>
122 public static long ReadCurrentVersion(Directory directory
)
125 IndexInput input
= directory
.OpenInput(IndexFileNames
.SEGMENTS
);
130 format
= input
.ReadInt();
134 throw new System
.IO
.IOException("Unknown format version: " + format
);
135 version
= input
.ReadLong(); // read version
146 // We cannot be sure about the format of the file.
147 // Therefore we have to read the whole file and cannot simply seek to the version entry.
149 SegmentInfos sis
= new SegmentInfos();
151 return sis
.GetVersion();