Thumbnail file hits. Based on a patch from D Bera
[beagle.git] / beagled / Lucene.Net / Store / RAMDirectory.cs
blobc5fdd3668c9b1ff80f553a4f8354ac89d932b73b
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.3 2005/06/24 18:52:03 joeshaw 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.CreateFile(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 OutputStream os = CreateFile(files[i]);
98 // read current file
99 InputStream is_Renamed = dir.OpenFile(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 // FIXED joeshaw@novell.com 24 Jun 2005 - Use UTC
164 RAMFile file = (RAMFile) files[name];
165 long ts2, ts1 = (System.DateTime.UtcNow.Ticks - 621355968000000000) / 10000;
170 System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 0 + 100 * 1));
172 catch (System.Threading.ThreadInterruptedException)
175 ts2 = (System.DateTime.UtcNow.Ticks - 621355968000000000) / 10000;
176 // if (MONITOR) {
177 // count++;
178 // }
180 while (ts1 == ts2);
182 file.lastModified = ts2;
184 // if (MONITOR)
185 // System.out.println("SLEEP COUNT: " + count);
188 /// <summary>Returns the length in bytes of a file in the directory. </summary>
189 public override long FileLength(System.String name)
191 RAMFile file = (RAMFile) files[name];
192 return file.length;
195 /// <summary>Removes an existing file in the directory. </summary>
196 public override void DeleteFile(System.String name)
198 files.Remove(name);
201 /// <summary>Removes an existing file in the directory. </summary>
202 public override void RenameFile(System.String from, System.String to)
204 RAMFile file = (RAMFile) files[from];
205 files.Remove(from);
206 files[to] = file;
209 /// <summary>Creates a new, empty file in the directory with the given name.
210 /// Returns a stream writing this file.
211 /// </summary>
212 public override OutputStream CreateFile(System.String name)
214 RAMFile file = new RAMFile();
215 files[name] = file;
216 return new RAMOutputStream(file);
219 /// <summary>Returns a stream reading an existing file. </summary>
220 public override InputStream OpenFile(System.String name)
222 RAMFile file = (RAMFile) files[name];
223 return new RAMInputStream(file);
226 /// <summary>Construct a {@link Lock}.</summary>
227 /// <param name="name">the name of the lock file
228 /// </param>
229 public override Lock MakeLock(System.String name)
231 return new AnonymousClassLock(name, this);
234 /// <summary>Closes the store to future operations. </summary>
235 public override void Close()