Lucene.Net  3.0.3
Lucene.Net is a .NET port of the Java Lucene Indexing Library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties
MergePolicy.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 Directory = Lucene.Net.Store.Directory;
21 
22 namespace Lucene.Net.Index
23 {
24 
60 
61  public abstract class MergePolicy : IDisposable
62  {
63 
70 
71  public class OneMerge
72  {
73 
74  internal SegmentInfo info; // used by IndexWriter
75  internal bool mergeDocStores; // used by IndexWriter
76  internal bool optimize; // used by IndexWriter
77  internal bool registerDone; // used by IndexWriter
78  internal long mergeGen; // used by IndexWriter
79  internal bool isExternal; // used by IndexWriter
80  internal int maxNumSegmentsOptimize; // used by IndexWriter
81  internal SegmentReader[] readers; // used by IndexWriter
82  internal SegmentReader[] readersClone; // used by IndexWriter
83  internal SegmentInfos segments;
84  internal bool useCompoundFile;
85  internal bool aborted;
86  internal System.Exception error;
87 
88  public OneMerge(SegmentInfos segments, bool useCompoundFile)
89  {
90  if (0 == segments.Count)
91  throw new ArgumentException("segments must include at least one segment", "segments");
92  this.segments = segments;
93  this.useCompoundFile = useCompoundFile;
94  }
95 
99  internal virtual void SetException(System.Exception error)
100  {
101  lock (this)
102  {
103  this.error = error;
104  }
105  }
106 
110  internal virtual System.Exception GetException()
111  {
112  lock (this)
113  {
114  return error;
115  }
116  }
117 
122  internal virtual void Abort()
123  {
124  lock (this)
125  {
126  aborted = true;
127  }
128  }
129 
131  internal virtual bool IsAborted()
132  {
133  lock (this)
134  {
135  return aborted;
136  }
137  }
138 
139  internal virtual void CheckAborted(Directory dir)
140  {
141  lock (this)
142  {
143  if (aborted)
144  throw new MergeAbortedException("merge is aborted: " + SegString(dir));
145  }
146  }
147 
148  internal virtual String SegString(Directory dir)
149  {
150  var b = new System.Text.StringBuilder();
151  int numSegments = segments.Count;
152  for (int i = 0; i < numSegments; i++)
153  {
154  if (i > 0)
155  b.Append(' ');
156  b.Append(segments.Info(i).SegString(dir));
157  }
158  if (info != null)
159  b.Append(" into ").Append(info.name);
160  if (optimize)
161  b.Append(" [optimize]");
162  if (mergeDocStores)
163  {
164  b.Append(" [mergeDocStores]");
165  }
166  return b.ToString();
167  }
168 
169  public SegmentInfos segments_ForNUnit
170  {
171  get { return segments; }
172  }
173  }
174 
179 
180  public class MergeSpecification
181  {
182 
184 
185  public IList<OneMerge> merges = new List<OneMerge>();
186 
187  public virtual void Add(OneMerge merge)
188  {
189  merges.Add(merge);
190  }
191 
192  public virtual String SegString(Directory dir)
193  {
194  var b = new System.Text.StringBuilder();
195  b.Append("MergeSpec:\n");
196  int count = merges.Count;
197  for (int i = 0; i < count; i++)
198  b.Append(" ").Append(1 + i).Append(": ").Append(merges[i].SegString(dir));
199  return b.ToString();
200  }
201  }
202 
206  [Serializable]
207  public class MergeException:System.SystemException
208  {
209  private readonly Directory dir;
210 
211  public MergeException(System.String message, Directory dir):base(message)
212  {
213  this.dir = dir;
214  }
215 
216  public MergeException(System.Exception exc, Directory dir):base(null, exc)
217  {
218  this.dir = dir;
219  }
220 
224  public virtual Directory Directory
225  {
226  get { return dir; }
227  }
228  }
229 
230  [Serializable]
231  public class MergeAbortedException:System.IO.IOException
232  {
233  public MergeAbortedException():base("merge is aborted")
234  {
235  }
236  public MergeAbortedException(System.String message):base(message)
237  {
238  }
239  }
240 
241  protected internal IndexWriter writer;
242 
243  protected MergePolicy(IndexWriter writer)
244  {
245  this.writer = writer;
246  }
247 
256  public abstract MergeSpecification FindMerges(SegmentInfos segmentInfos);
257 
273  public abstract MergeSpecification FindMergesForOptimize(SegmentInfos segmentInfos, int maxSegmentCount,
274  ISet<SegmentInfo> segmentsToOptimize);
275 
282  public abstract MergeSpecification FindMergesToExpungeDeletes(SegmentInfos segmentInfos);
283 
285  [Obsolete("Use Dispose() instead")]
286  public void Close()
287  {
288  Dispose();
289  }
290 
292  public void Dispose()
293  {
294  Dispose(true);
295  }
296 
297  protected abstract void Dispose(bool disposing);
298 
302  public abstract bool UseCompoundFile(SegmentInfos segments, SegmentInfo newSegment);
303 
307  public abstract bool UseCompoundDocStore(SegmentInfos segments);
308  }
309 }