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
Directory.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 
20 using IndexFileNameFilter = Lucene.Net.Index.IndexFileNameFilter;
21 
22 namespace Lucene.Net.Store
23 {
24 
41  [Serializable]
42  public abstract class Directory : System.IDisposable
43  {
44  protected internal volatile bool isOpen = true;
45 
49  [NonSerialized]
50  protected internal LockFactory interalLockFactory;
51 
54  public abstract System.String[] ListAll();
55 
57  public abstract bool FileExists(System.String name);
58 
60  public abstract long FileModified(System.String name);
61 
63  public abstract void TouchFile(System.String name);
64 
66  public abstract void DeleteFile(System.String name);
67 
69  public abstract long FileLength(System.String name);
70 
71 
75  public abstract IndexOutput CreateOutput(System.String name);
76 
82  public virtual void Sync(System.String name)
83  {
84  }
85 
87  public abstract IndexInput OpenInput(System.String name);
88 
96  public virtual IndexInput OpenInput(System.String name, int bufferSize)
97  {
98  return OpenInput(name);
99  }
100 
104  public virtual Lock MakeLock(System.String name)
105  {
106  return interalLockFactory.MakeLock(name);
107  }
114  public virtual void ClearLock(System.String name)
115  {
116  if (interalLockFactory != null)
117  {
118  interalLockFactory.ClearLock(name);
119  }
120  }
121 
122  [Obsolete("Use Dispose() instead")]
123  public void Close()
124  {
125  Dispose();
126  }
127 
129  public void Dispose()
130  {
131  Dispose(true);
132  }
133 
134  protected abstract void Dispose(bool disposing);
135 
145  public virtual void SetLockFactory(LockFactory lockFactory)
146  {
147  System.Diagnostics.Debug.Assert(lockFactory != null);
148  this.interalLockFactory = lockFactory;
149  lockFactory.LockPrefix = this.GetLockId();
150  }
151 
157  public virtual LockFactory LockFactory
158  {
159  get { return this.interalLockFactory; }
160  }
161 
169  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
170  public virtual string GetLockId()
171  {
172  return ToString();
173  }
174 
175  public override string ToString()
176  {
177  return base.ToString() + " lockFactory=" + LockFactory;
178  }
179 
201  public static void Copy(Directory src, Directory dest, bool closeDirSrc)
202  {
203  System.String[] files = src.ListAll();
204 
206 
207  byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
208  for (int i = 0; i < files.Length; i++)
209  {
210 
211  if (!filter.Accept(null, files[i]))
212  continue;
213 
214  IndexOutput os = null;
215  IndexInput is_Renamed = null;
216  try
217  {
218  // create file in dest directory
219  os = dest.CreateOutput(files[i]);
220  // read current file
221  is_Renamed = src.OpenInput(files[i]);
222  // and copy to dest directory
223  long len = is_Renamed.Length();
224  long readCount = 0;
225  while (readCount < len)
226  {
227  int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len?(int) (len - readCount):BufferedIndexOutput.BUFFER_SIZE;
228  is_Renamed.ReadBytes(buf, 0, toRead);
229  os.WriteBytes(buf, toRead);
230  readCount += toRead;
231  }
232  }
233  finally
234  {
235  // graceful cleanup
236  try
237  {
238  if (os != null)
239  os.Close();
240  }
241  finally
242  {
243  if (is_Renamed != null)
244  is_Renamed.Close();
245  }
246  }
247  }
248  if (closeDirSrc)
249  src.Close();
250  }
251 
253  public /*protected internal*/ void EnsureOpen()
254  {
255  if (!isOpen)
256  throw new AlreadyClosedException("this Directory is closed");
257  }
258 
259  public bool isOpen_ForNUnit
260  {
261  get { return isOpen; }
262  }
263  }
264 }