Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
SegmentsGenCommit.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 using System.Collections.Generic;
20 using System.Linq;
21 using System.Text;
22 
23 using Lucene.Net.Store;
24 
25 namespace Lucene.Net.Index
26 {
27  /// <summary>
28  /// Class that will force an index writer to open an index based
29  /// on the generation in the segments.gen file as opposed to the
30  /// highest generation found in a directory listing.
31  ///
32  /// A use case for using this IndexCommit when opening an IndexWriter
33  /// would be if index snapshots (source) are being copied over an
34  /// existing index (target) and the source now has a lower generation
35  /// than the target due to initiating a rebuild of the index.
36  /// </summary>
38  {
39  /// <summary>
40  /// The index.
41  /// </summary>
42  private Directory directory;
43 
44  /// <summary>
45  /// The generation to use.
46  /// </summary>
47  private long generation = long.MinValue;
48 
49  /// <summary>
50  /// Ctor.
51  /// </summary>
52  /// <param name="d">The index directory.</param>
54  {
55  this.directory = d;
56  this.ReadDirectory();
57  }
58 
59  /// <summary>
60  /// Get the segments_n file for the generation found in the
61  /// segments.gen file.
62  /// </summary>
63  /// <returns>The segments_n file to use.</returns>
64  public override string SegmentsFileName
65  {
66  get
67  {
68  return IndexFileNames.FileNameFromGeneration(IndexFileNames.SEGMENTS, string.Empty, this.generation);
69  }
70  }
71 
72  public override long Generation
73  {
74  get
75  {
76  return this.generation;
77  }
78  }
79 
80  public override ICollection<string> FileNames
81  {
82  get
83  {
84  throw new NotImplementedException();
85  }
86  }
87 
88  public override Directory Directory
89  {
90  get
91  {
92  return this.directory;
93  }
94  }
95 
96  /// <summary>
97  /// Read the segments.gen file to get the generation number.
98  /// </summary>
99  private void ReadDirectory()
100  {
101  IndexInput genInput = null;
102  try
103  {
104  genInput = directory.OpenInput(IndexFileNames.SEGMENTS_GEN);
105 
106  if (genInput != null)
107  {
108  int version = genInput.ReadInt();
109  if (version == Lucene.Net.Index.SegmentInfos.FORMAT_LOCKLESS)
110  {
111  long gen0 = genInput.ReadLong();
112  long gen1 = genInput.ReadLong();
113  if (gen0 == gen1)
114  {
115  // The file is consistent, use the generation
116  this.generation = gen0;
117  }
118  }
119  }
120  }
121  finally
122  {
123  genInput.Close();
124  }
125  }
126 
127  // TODO: implement these new API functions -thoward
128  public override void Delete()
129  {
130  throw new NotImplementedException();
131  }
132 
133  public override bool IsDeleted
134  {
135  get
136  {
137  throw new NotImplementedException();
138  }
139  }
140 
141  public override bool IsOptimized
142  {
143  get { throw new NotImplementedException(); }
144  }
145 
146  public override long Version
147  {
148  get
149  {
150  throw new NotImplementedException();
151  }
152  }
153 
154  public override IDictionary<string, string> UserData
155  {
156  get
157  {
158  throw new NotImplementedException();
159  }
160  }
161  }
162 }