cvsimport
[beagle.git] / beagled / Lucene.Net / Store / RAMDirectory.cs
blob21cc70d3a77e9cd7b2d25ddb05104c6d3f21bc19
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.
17 using System;
19 namespace Lucene.Net.Store
22 /// <summary> A memory-resident {@link Directory} implementation.
23 ///
24 /// </summary>
25 /// <version> $Id: RAMDirectory.cs,v 1.6 2006/10/02 17:11:11 joeshaw Exp $
26 /// </version>
27 public sealed class RAMDirectory : Directory
29 private class AnonymousClassLock : Lock
31 public AnonymousClassLock(System.String name, RAMDirectory enclosingInstance)
33 InitBlock(name, enclosingInstance);
35 private void InitBlock(System.String name, RAMDirectory enclosingInstance)
37 this.name = name;
38 this.enclosingInstance = enclosingInstance;
40 private System.String name;
41 private RAMDirectory enclosingInstance;
42 public RAMDirectory Enclosing_Instance
44 get
46 return enclosingInstance;
50 public override bool Obtain()
52 lock (Enclosing_Instance.files.SyncRoot)
54 if (!Enclosing_Instance.FileExists(name))
56 Enclosing_Instance.CreateOutput(name).Close();
57 return true;
59 return false;
62 public override void Release()
64 Enclosing_Instance.DeleteFile(name);
66 public override bool IsLocked()
68 return Enclosing_Instance.FileExists(name);
71 internal System.Collections.Hashtable files = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
73 /// <summary>Constructs an empty {@link Directory}. </summary>
74 public RAMDirectory()
78 /// <summary> Creates a new <code>RAMDirectory</code> instance from a different
79 /// <code>Directory</code> implementation. This can be used to load
80 /// a disk-based index into memory.
81 /// <P>
82 /// This should be used only with indices that can fit into memory.
83 ///
84 /// </summary>
85 /// <param name="dir">a <code>Directory</code> value
86 /// </param>
87 /// <exception cref="IOException">if an error occurs
88 /// </exception>
89 public RAMDirectory(Directory dir):this(dir, false)
93 private RAMDirectory(Directory dir, bool closeDir)
95 System.String[] files = dir.List();
96 byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
97 for (int i = 0; i < files.Length; i++)
99 // make place on ram disk
100 IndexOutput os = CreateOutput(System.IO.Path.GetFileName(files[i]));
101 // read current file
102 IndexInput is_Renamed = dir.OpenInput(files[i]);
103 // and copy to ram disk
104 int len = (int) is_Renamed.Length();
105 int readCount = 0;
106 while (readCount < len)
108 int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len?len - readCount : BufferedIndexOutput.BUFFER_SIZE;
109 is_Renamed.ReadBytes(buf, 0, toRead);
110 os.WriteBytes(buf, toRead);
111 readCount += toRead;
114 // graceful cleanup
115 is_Renamed.Close();
116 os.Close();
118 if (closeDir)
119 dir.Close();
122 /// <summary> Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
123 ///
124 /// </summary>
125 /// <param name="dir">a <code>File</code> specifying the index directory
126 /// </param>
127 public RAMDirectory(System.IO.FileInfo dir) : this(FSDirectory.GetDirectory(dir, false), true)
131 /// <summary> Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
132 ///
133 /// </summary>
134 /// <param name="dir">a <code>String</code> specifying the full index directory path
135 /// </param>
136 public RAMDirectory(System.String dir) : this(FSDirectory.GetDirectory(dir, false), true)
140 /// <summary>Returns an array of strings, one for each file in the directory. </summary>
141 public override System.String[] List()
143 System.String[] result = new System.String[files.Count];
144 int i = 0;
145 System.Collections.IEnumerator names = files.Keys.GetEnumerator();
146 while (names.MoveNext())
148 result[i++] = ((System.String) names.Current);
150 return result;
153 /// <summary>Returns true iff the named file exists in this directory. </summary>
154 public override bool FileExists(System.String name)
156 RAMFile file = (RAMFile) files[name];
157 return file != null;
160 /// <summary>Returns the time the named file was last modified. </summary>
161 public override long FileModified(System.String name)
163 RAMFile file = (RAMFile) files[name];
164 return file.lastModified;
167 /// <summary>Set the modified time of an existing file to now. </summary>
168 public override void TouchFile(System.String name)
170 // final boolean MONITOR = false;
172 RAMFile file = (RAMFile) files[name];
173 long ts2, ts1 = System.DateTime.UtcNow.Ticks;
178 System.Threading.Thread.Sleep(new System.TimeSpan((System.Int64) 10000 * 0 + 100 * 1));
180 catch (System.Threading.ThreadInterruptedException)
183 ts2 = System.DateTime.UtcNow.Ticks;
184 // if (MONITOR) {
185 // count++;
186 // }
188 while (ts1 == ts2);
190 file.lastModified = ts2;
192 // if (MONITOR)
193 // System.out.println("SLEEP COUNT: " + count);
196 /// <summary>Returns the length in bytes of a file in the directory. </summary>
197 public override long FileLength(System.String name)
199 RAMFile file = (RAMFile) files[name];
200 return file.length;
203 /// <summary>Removes an existing file in the directory. </summary>
204 public override void DeleteFile(System.String name)
206 files.Remove(name);
209 /// <summary>Removes an existing file in the directory. </summary>
210 public override void RenameFile(System.String from, System.String to)
212 RAMFile file = (RAMFile) files[from];
213 files.Remove(from);
214 files[to] = file;
217 /// <summary>Creates a new, empty file in the directory with the given name.
218 /// Returns a stream writing this file.
219 /// </summary>
220 public override IndexOutput CreateOutput(System.String name)
222 RAMFile file = new RAMFile();
223 files[name] = file;
224 return new RAMOutputStream(file);
227 /// <summary>Returns a stream reading an existing file. </summary>
228 public override IndexInput OpenInput(System.String name)
230 RAMFile file = (RAMFile) files[name];
231 return new RAMInputStream(file);
234 /// <summary>Construct a {@link Lock}.</summary>
235 /// <param name="name">the name of the lock file
236 /// </param>
237 public override Lock MakeLock(System.String name)
239 return new AnonymousClassLock(name, this);
242 /// <summary>Closes the store to future operations. </summary>
243 public override void Close()