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.3 2005/06/24 18:52:03 joeshaw 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
.CreateFile(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 OutputStream os
= CreateFile(files
[i
]);
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
);
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 // 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;
182 file
.lastModified
= ts2
;
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
];
195 /// <summary>Removes an existing file in the directory. </summary>
196 public override void DeleteFile(System
.String 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];
209 /// <summary>Creates a new, empty file in the directory with the given name.
210 /// Returns a stream writing this file.
212 public override OutputStream
CreateFile(System
.String name
)
214 RAMFile file
= new RAMFile();
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
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()