2006-09-10 Francisco Javier F. Serrador <serrador@openshine.com>
[beagle.git] / beagled / Lucene.Net / Store / RAMDirectory.cs
blob4b3253148941adec511077d5a199044d3270a0da
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 using System;
17 namespace Lucene.Net.Store
20 /// <summary> A memory-resident {@link Directory} implementation.
21 ///
22 /// </summary>
23 /// <version> $Id: RAMDirectory.cs,v 1.4 2005/10/06 19:29:58 dsd Exp $
24 /// </version>
25 public sealed class RAMDirectory : Directory
27 private class AnonymousClassLock:Lock
29 public AnonymousClassLock(System.String name, RAMDirectory enclosingInstance)
31 InitBlock(name, enclosingInstance);
33 private void InitBlock(System.String name, RAMDirectory enclosingInstance)
35 this.name = name;
36 this.enclosingInstance = enclosingInstance;
38 private System.String name;
39 private RAMDirectory enclosingInstance;
40 public RAMDirectory Enclosing_Instance
42 get
44 return enclosingInstance;
48 public override bool Obtain()
50 lock (Enclosing_Instance.files.SyncRoot)
52 if (!Enclosing_Instance.FileExists(name))
54 Enclosing_Instance.CreateOutput(name).Close();
55 return true;
57 return false;
60 public override void Release()
62 Enclosing_Instance.DeleteFile(name);
64 public override bool IsLocked()
66 return Enclosing_Instance.FileExists(name);
69 internal System.Collections.Hashtable files = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
71 /// <summary>Constructs an empty {@link Directory}. </summary>
72 public RAMDirectory()
76 /// <summary> Creates a new <code>RAMDirectory</code> instance from a different
77 /// <code>Directory</code> implementation. This can be used to load
78 /// a disk-based index into memory.
79 /// <P>
80 /// This should be used only with indices that can fit into memory.
81 ///
82 /// </summary>
83 /// <param name="dir">a <code>Directory</code> value
84 /// </param>
85 /// <exception cref=""> IOException if an error occurs
86 /// </exception>
87 public RAMDirectory(Directory dir) : this(dir, false)
91 private RAMDirectory(Directory dir, bool closeDir)
93 System.String[] files = dir.List();
94 for (int i = 0; i < files.Length; i++)
96 // make place on ram disk
97 IndexOutput os = CreateOutput(files[i]);
98 // read current file
99 IndexInput is_Renamed = dir.OpenInput(files[i]);
100 // and copy to ram disk
101 int len = (int) is_Renamed.Length();
102 byte[] buf = new byte[len];
103 is_Renamed.ReadBytes(buf, 0, len);
104 os.WriteBytes(buf, len);
105 // graceful cleanup
106 is_Renamed.Close();
107 os.Close();
109 if (closeDir)
110 dir.Close();
113 /// <summary> Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
114 ///
115 /// </summary>
116 /// <param name="dir">a <code>File</code> specifying the index directory
117 /// </param>
118 public RAMDirectory(System.IO.FileInfo dir) : this(FSDirectory.GetDirectory(dir, false), true)
122 /// <summary> Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
123 ///
124 /// </summary>
125 /// <param name="dir">a <code>String</code> specifying the full index directory path
126 /// </param>
127 public RAMDirectory(System.String dir) : this(FSDirectory.GetDirectory(dir, false), true)
131 /// <summary>Returns an array of strings, one for each file in the directory. </summary>
132 public override System.String[] List()
134 System.String[] result = new System.String[files.Count];
135 int i = 0;
136 System.Collections.IEnumerator names = files.Keys.GetEnumerator();
137 while (names.MoveNext())
139 result[i++] = ((System.String) names.Current);
141 return result;
144 /// <summary>Returns true iff the named file exists in this directory. </summary>
145 public override bool FileExists(System.String name)
147 RAMFile file = (RAMFile) files[name];
148 return file != null;
151 /// <summary>Returns the time the named file was last modified. </summary>
152 public override long FileModified(System.String name)
154 RAMFile file = (RAMFile) files[name];
155 return file.lastModified;
158 /// <summary>Set the modified time of an existing file to now. </summary>
159 public override void TouchFile(System.String name)
161 // final boolean MONITOR = false;
163 RAMFile file = (RAMFile) files[name];
164 long ts2, ts1 = System.DateTime.UtcNow.Ticks;
169 System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 0 + 100 * 1));
171 catch (System.Threading.ThreadInterruptedException)
174 ts2 = System.DateTime.UtcNow.Ticks;
175 // if (MONITOR) {
176 // count++;
177 // }
179 while (ts1 == ts2);
181 file.lastModified = ts2;
183 // if (MONITOR)
184 // System.out.println("SLEEP COUNT: " + count);
187 /// <summary>Returns the length in bytes of a file in the directory. </summary>
188 public override long FileLength(System.String name)
190 RAMFile file = (RAMFile) files[name];
191 return file.length;
194 /// <summary>Removes an existing file in the directory. </summary>
195 public override void DeleteFile(System.String name)
197 files.Remove(name);
200 /// <summary>Removes an existing file in the directory. </summary>
201 public override void RenameFile(System.String from, System.String to)
203 RAMFile file = (RAMFile) files[from];
204 files.Remove(from);
205 files[to] = file;
208 /// <summary>Creates a new, empty file in the directory with the given name.
209 /// Returns a stream writing this file.
210 /// </summary>
211 public override IndexOutput CreateOutput(System.String name)
213 RAMFile file = new RAMFile();
214 files[name] = file;
215 return new RAMOutputStream(file);
218 /// <summary>Returns a stream reading an existing file. </summary>
219 public override IndexInput OpenInput(System.String name)
221 RAMFile file = (RAMFile) files[name];
222 return new RAMInputStream(file);
225 /// <summary>Construct a {@link Lock}.</summary>
226 /// <param name="name">the name of the lock file
227 /// </param>
228 public override Lock MakeLock(System.String name)
230 return new AnonymousClassLock(name, this);
233 /// <summary>Closes the store to future operations. </summary>
234 public override void Close()