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
FileSwitchDirectory.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 namespace Lucene.Net.Store
21 {
22 
23  /// <summary> Expert: A Directory instance that switches files between
24  /// two other Directory instances.
25  /// <p/>Files with the specified extensions are placed in the
26  /// primary directory; others are placed in the secondary
27  /// directory. The provided Set must not change once passed
28  /// to this class, and must allow multiple threads to call
29  /// contains at once.<p/>
30  ///
31  /// <p/><b>NOTE</b>: this API is new and experimental and is
32  /// subject to suddenly change in the next release.
33  /// </summary>
34 
36  {
37  private Directory secondaryDir;
38  private Directory primaryDir;
39  private System.Collections.Generic.HashSet<string> primaryExtensions;
40  private bool doClose;
41  private bool isDisposed;
42 
43  public FileSwitchDirectory(System.Collections.Generic.HashSet<string> primaryExtensions,
44  Directory primaryDir,
45  Directory secondaryDir,
46  bool doClose)
47  {
48  this.primaryExtensions = primaryExtensions;
49  this.primaryDir = primaryDir;
50  this.secondaryDir = secondaryDir;
51  this.doClose = doClose;
52  this.interalLockFactory = primaryDir.LockFactory;
53  }
54 
55  /// <summary>Return the primary directory </summary>
56  public virtual Directory PrimaryDir
57  {
58  get { return primaryDir; }
59  }
60 
61  /// <summary>Return the secondary directory </summary>
62  public virtual Directory SecondaryDir
63  {
64  get { return secondaryDir; }
65  }
66 
67  protected override void Dispose(bool disposing)
68  {
69  if (isDisposed) return;
70 
71  if (doClose)
72  {
73  try
74  {
75  if (secondaryDir != null)
76  {
77  secondaryDir.Close();
78  }
79  }
80  finally
81  {
82  if (primaryDir != null)
83  {
84  primaryDir.Close();
85  }
86  }
87  doClose = false;
88  }
89 
90  secondaryDir = null;
91  primaryDir = null;
92  isDisposed = true;
93  }
94 
95  public override System.String[] ListAll()
96  {
97  var files = new System.Collections.Generic.List<string>();
98  files.AddRange(primaryDir.ListAll());
99  files.AddRange(secondaryDir.ListAll());
100  return files.ToArray();
101  }
102 
103  /// <summary>Utility method to return a file's extension. </summary>
104  public static System.String GetExtension(System.String name)
105  {
106  int i = name.LastIndexOf('.');
107  if (i == - 1)
108  {
109  return "";
110  }
111  return name.Substring(i + 1, (name.Length) - (i + 1));
112  }
113 
114  private Directory GetDirectory(System.String name)
115  {
116  System.String ext = GetExtension(name);
117  if (primaryExtensions.Contains(ext))
118  {
119  return primaryDir;
120  }
121  else
122  {
123  return secondaryDir;
124  }
125  }
126 
127  public override bool FileExists(System.String name)
128  {
129  return GetDirectory(name).FileExists(name);
130  }
131 
132  public override long FileModified(System.String name)
133  {
134  return GetDirectory(name).FileModified(name);
135  }
136 
137  public override void TouchFile(System.String name)
138  {
139  GetDirectory(name).TouchFile(name);
140  }
141 
142  public override void DeleteFile(System.String name)
143  {
144  GetDirectory(name).DeleteFile(name);
145  }
146 
147  public override long FileLength(System.String name)
148  {
149  return GetDirectory(name).FileLength(name);
150  }
151 
152  public override IndexOutput CreateOutput(System.String name)
153  {
154  return GetDirectory(name).CreateOutput(name);
155  }
156 
157  public override void Sync(System.String name)
158  {
159  GetDirectory(name).Sync(name);
160  }
161 
162  public override IndexInput OpenInput(System.String name)
163  {
164  return GetDirectory(name).OpenInput(name);
165  }
166  }
167 }