2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 namespace Lucene
.Net
.Store
20 /// <summary> A memory-resident {@link Directory} implementation.
23 /// <version> $Id: RAMDirectory.cs,v 1.4 2005/10/06 19:29:58 dsd Exp $
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
)
36 this.enclosingInstance
= enclosingInstance
;
38 private System
.String name
;
39 private RAMDirectory enclosingInstance
;
40 public RAMDirectory Enclosing_Instance
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();
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>
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.
80 /// This should be used only with indices that can fit into memory.
83 /// <param name="dir">a <code>Directory</code> value
85 /// <exception cref=""> IOException if an error occurs
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
]);
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
);
113 /// <summary> Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
116 /// <param name="dir">a <code>File</code> specifying the index directory
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}.
125 /// <param name="dir">a <code>String</code> specifying the full index directory path
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
];
136 System
.Collections
.IEnumerator names
= files
.Keys
.GetEnumerator();
137 while (names
.MoveNext())
139 result
[i
++] = ((System
.String
) names
.Current
);
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
];
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
;
181 file
.lastModified
= ts2
;
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
];
194 /// <summary>Removes an existing file in the directory. </summary>
195 public override void DeleteFile(System
.String 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];
208 /// <summary>Creates a new, empty file in the directory with the given name.
209 /// Returns a stream writing this file.
211 public override IndexOutput
CreateOutput(System
.String name
)
213 RAMFile file
= new RAMFile();
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
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()