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.
19 namespace Lucene
.Net
.Store
22 /// <summary> A memory-resident {@link Directory} implementation.
25 /// <version> $Id: RAMDirectory.cs,v 1.6 2006/10/02 17:11:11 joeshaw Exp $
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
)
38 this.enclosingInstance
= enclosingInstance
;
40 private System
.String name
;
41 private RAMDirectory enclosingInstance
;
42 public RAMDirectory Enclosing_Instance
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();
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>
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.
82 /// This should be used only with indices that can fit into memory.
85 /// <param name="dir">a <code>Directory</code> value
87 /// <exception cref="IOException">if an error occurs
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
]));
102 IndexInput is_Renamed
= dir
.OpenInput(files
[i
]);
103 // and copy to ram disk
104 int len
= (int) is_Renamed
.Length();
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
);
122 /// <summary> Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
125 /// <param name="dir">a <code>File</code> specifying the index directory
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}.
134 /// <param name="dir">a <code>String</code> specifying the full index directory path
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
];
145 System
.Collections
.IEnumerator names
= files
.Keys
.GetEnumerator();
146 while (names
.MoveNext())
148 result
[i
++] = ((System
.String
) names
.Current
);
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
];
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
;
190 file
.lastModified
= ts2
;
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
];
203 /// <summary>Removes an existing file in the directory. </summary>
204 public override void DeleteFile(System
.String 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];
217 /// <summary>Creates a new, empty file in the directory with the given name.
218 /// Returns a stream writing this file.
220 public override IndexOutput
CreateOutput(System
.String name
)
222 RAMFile file
= new RAMFile();
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
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()