Merge pull request #409 from Alkarex/patch-1
[mono.git] / mcs / tools / monkeydoc / Lucene.Net / Lucene.Net / Index / DirectoryOwningReader.cs
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
20 namespace Mono.Lucene.Net.Index
21 {
22         
23         /// <summary> This class keeps track of closing the underlying directory. It is used to wrap
24         /// DirectoryReaders, that are created using a String/File parameter
25         /// in IndexReader.open() with FSDirectory.getDirectory().
26         /// </summary>
27         /// <deprecated> This helper class is removed with all String/File
28         /// IndexReader.open() methods in Lucene 3.0
29         /// </deprecated>
30     [Obsolete("This helper class is removed with all String/File IndexReader.open() methods in Lucene 3.0")]
31         sealed class DirectoryOwningReader:FilterIndexReader, System.ICloneable
32         {
33                 
34                 internal DirectoryOwningReader(IndexReader in_Renamed):base(in_Renamed)
35                 {
36                         this.ref_Renamed = new SegmentReader.Ref();
37                         System.Diagnostics.Debug.Assert(this.ref_Renamed.RefCount() == 1);
38                 }
39                 
40                 private DirectoryOwningReader(IndexReader in_Renamed, SegmentReader.Ref ref_Renamed):base(in_Renamed)
41                 {
42                         this.ref_Renamed = ref_Renamed;
43                         ref_Renamed.IncRef();
44                 }
45                 
46                 public override IndexReader Reopen()
47                 {
48                         EnsureOpen();
49                         IndexReader r = in_Renamed.Reopen();
50                         if (r != in_Renamed)
51                                 return new DirectoryOwningReader(r, ref_Renamed);
52                         return this;
53                 }
54                 
55                 public override IndexReader Reopen(bool openReadOnly)
56                 {
57                         EnsureOpen();
58                         IndexReader r = in_Renamed.Reopen(openReadOnly);
59                         if (r != in_Renamed)
60                                 return new DirectoryOwningReader(r, ref_Renamed);
61                         return this;
62                 }
63                 
64                 public override IndexReader Reopen(IndexCommit commit)
65                 {
66                         EnsureOpen();
67                         IndexReader r = in_Renamed.Reopen(commit);
68                         if (r != in_Renamed)
69                                 return new DirectoryOwningReader(r, ref_Renamed);
70                         return this;
71                 }
72                 
73                 public override System.Object Clone()
74                 {
75                         EnsureOpen();
76                         return new DirectoryOwningReader((IndexReader) in_Renamed.Clone(), ref_Renamed);
77                 }
78                 
79                 public override IndexReader Clone(bool openReadOnly)
80                 {
81                         EnsureOpen();
82                         return new DirectoryOwningReader(in_Renamed.Clone(openReadOnly), ref_Renamed);
83                 }
84                 
85                 protected internal override void  DoClose()
86                 {
87                         System.IO.IOException ioe = null;
88                         // close the reader, record exception
89                         try
90                         {
91                                 base.DoClose();
92                         }
93                         catch (System.IO.IOException e)
94                         {
95                                 ioe = e;
96                         }
97                         // close the directory, record exception
98                         if (ref_Renamed.DecRef() == 0)
99                         {
100                                 try
101                                 {
102                                         in_Renamed.Directory().Close();
103                                 }
104                                 catch (System.IO.IOException e)
105                                 {
106                                         if (ioe == null)
107                                                 ioe = e;
108                                 }
109                         }
110                         // throw the first exception
111                         if (ioe != null)
112                                 throw ioe;
113                 }
114                 
115                 /// <summary> This member contains the ref counter, that is passed to each instance after cloning/reopening,
116                 /// and is global to all DirectoryOwningReader derived from the original one.
117                 /// This reuses the class {@link SegmentReader.Ref}
118                 /// </summary>
119                 private SegmentReader.Ref ref_Renamed;
120         }
121 }