Fixed #374055:Only the first "tag" is detected in digikam.
[beagle.git] / beagled / Lucene.Net / Index / SegmentInfos.cs
bloba63961c80e337af177861681f8b826396fbbd8bc
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.
17 using System;
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
26 [Serializable]
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.
37 /// </summary>
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);
49 try
51 int format = input.ReadInt();
52 if (format < 0)
54 // file contains explicit format info
55 // check that it is a format we can understand
56 if (format < FORMAT)
57 throw new System.IO.IOException("Unknown format version: " + format);
58 version = input.ReadLong(); // read version
59 counter = input.ReadInt(); // read counter
61 else
63 // file is in old format without explicit format info
64 counter = format;
67 for (int i = input.ReadInt(); i > 0; i--)
69 // read segmentInfos
70 SegmentInfo si = new SegmentInfo(input.ReadString(), input.ReadInt(), directory);
71 Add(si);
74 if (format >= 0)
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
80 else
81 version = input.ReadLong(); // read version
84 finally
86 input.Close();
90 public void Write(Directory directory)
92 IndexOutput output = directory.CreateOutput("segments.new");
93 try
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);
106 finally
108 output.Close();
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()
118 return version;
121 /// <summary> Current version number from segments file.</summary>
122 public static long ReadCurrentVersion(Directory directory)
125 IndexInput input = directory.OpenInput(IndexFileNames.SEGMENTS);
126 int format = 0;
127 long version = 0;
130 format = input.ReadInt();
131 if (format < 0)
133 if (format < FORMAT)
134 throw new System.IO.IOException("Unknown format version: " + format);
135 version = input.ReadLong(); // read version
138 finally
140 input.Close();
143 if (format < 0)
144 return 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();
150 sis.Read(directory);
151 return sis.GetVersion();