KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > store > RAMDirectory


1 package org.apache.lucene.store;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.IOException JavaDoc;
20 import java.io.File JavaDoc;
21 import java.util.Hashtable JavaDoc;
22 import java.util.Enumeration JavaDoc;
23
24 import org.apache.lucene.store.Directory;
25 import org.apache.lucene.store.IndexInput;
26 import org.apache.lucene.store.IndexOutput;
27
28 /**
29  * A memory-resident {@link Directory} implementation.
30  *
31  * @version $Id: RAMDirectory.java 351779 2005-12-02 17:37:50Z bmesser $
32  */

33 public final class RAMDirectory extends Directory {
34   Hashtable JavaDoc files = new Hashtable JavaDoc();
35
36   /** Constructs an empty {@link Directory}. */
37   public RAMDirectory() {
38   }
39
40   /**
41    * Creates a new <code>RAMDirectory</code> instance from a different
42    * <code>Directory</code> implementation. This can be used to load
43    * a disk-based index into memory.
44    * <P>
45    * This should be used only with indices that can fit into memory.
46    *
47    * @param dir a <code>Directory</code> value
48    * @exception IOException if an error occurs
49    */

50   public RAMDirectory(Directory dir) throws IOException JavaDoc {
51     this(dir, false);
52   }
53   
54   private RAMDirectory(Directory dir, boolean closeDir) throws IOException JavaDoc {
55     final String JavaDoc[] files = dir.list();
56     byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE];
57     for (int i = 0; i < files.length; i++) {
58       // make place on ram disk
59
IndexOutput os = createOutput(files[i]);
60       // read current file
61
IndexInput is = dir.openInput(files[i]);
62       // and copy to ram disk
63
int len = (int) is.length();
64       int readCount = 0;
65       while (readCount < len) {
66         int toRead = readCount + BufferedIndexOutput.BUFFER_SIZE > len ? len - readCount : BufferedIndexOutput.BUFFER_SIZE;
67         is.readBytes(buf, 0, toRead);
68         os.writeBytes(buf, toRead);
69         readCount += toRead;
70       }
71
72       // graceful cleanup
73
is.close();
74       os.close();
75     }
76     if(closeDir)
77       dir.close();
78   }
79
80   /**
81    * Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
82    *
83    * @param dir a <code>File</code> specifying the index directory
84    */

85   public RAMDirectory(File JavaDoc dir) throws IOException JavaDoc {
86     this(FSDirectory.getDirectory(dir, false), true);
87   }
88
89   /**
90    * Creates a new <code>RAMDirectory</code> instance from the {@link FSDirectory}.
91    *
92    * @param dir a <code>String</code> specifying the full index directory path
93    */

94   public RAMDirectory(String JavaDoc dir) throws IOException JavaDoc {
95     this(FSDirectory.getDirectory(dir, false), true);
96   }
97
98   /** Returns an array of strings, one for each file in the directory. */
99   public final String JavaDoc[] list() {
100     String JavaDoc[] result = new String JavaDoc[files.size()];
101     int i = 0;
102     Enumeration JavaDoc names = files.keys();
103     while (names.hasMoreElements())
104       result[i++] = (String JavaDoc)names.nextElement();
105     return result;
106   }
107
108   /** Returns true iff the named file exists in this directory. */
109   public final boolean fileExists(String JavaDoc name) {
110     RAMFile file = (RAMFile)files.get(name);
111     return file != null;
112   }
113
114   /** Returns the time the named file was last modified. */
115   public final long fileModified(String JavaDoc name) {
116     RAMFile file = (RAMFile)files.get(name);
117     return file.lastModified;
118   }
119
120   /** Set the modified time of an existing file to now. */
121   public void touchFile(String JavaDoc name) {
122 // final boolean MONITOR = false;
123

124     RAMFile file = (RAMFile)files.get(name);
125     long ts2, ts1 = System.currentTimeMillis();
126     do {
127       try {
128         Thread.sleep(0, 1);
129       } catch (InterruptedException JavaDoc e) {}
130       ts2 = System.currentTimeMillis();
131 // if (MONITOR) {
132
// count++;
133
// }
134
} while(ts1 == ts2);
135
136     file.lastModified = ts2;
137
138 // if (MONITOR)
139
// System.out.println("SLEEP COUNT: " + count);
140
}
141
142   /** Returns the length in bytes of a file in the directory. */
143   public final long fileLength(String JavaDoc name) {
144     RAMFile file = (RAMFile)files.get(name);
145     return file.length;
146   }
147
148   /** Removes an existing file in the directory. */
149   public final void deleteFile(String JavaDoc name) {
150     files.remove(name);
151   }
152
153   /** Removes an existing file in the directory. */
154   public final void renameFile(String JavaDoc from, String JavaDoc to) {
155     RAMFile file = (RAMFile)files.get(from);
156     files.remove(from);
157     files.put(to, file);
158   }
159
160   /** Creates a new, empty file in the directory with the given name.
161       Returns a stream writing this file. */

162   public final IndexOutput createOutput(String JavaDoc name) {
163     RAMFile file = new RAMFile();
164     files.put(name, file);
165     return new RAMOutputStream(file);
166   }
167
168   /** Returns a stream reading an existing file. */
169   public final IndexInput openInput(String JavaDoc name) {
170     RAMFile file = (RAMFile)files.get(name);
171     return new RAMInputStream(file);
172   }
173
174   /** Construct a {@link Lock}.
175    * @param name the name of the lock file
176    */

177   public final Lock makeLock(final String JavaDoc name) {
178     return new Lock() {
179       public boolean obtain() throws IOException JavaDoc {
180         synchronized (files) {
181           if (!fileExists(name)) {
182             createOutput(name).close();
183             return true;
184           }
185           return false;
186         }
187       }
188       public void release() {
189         deleteFile(name);
190       }
191       public boolean isLocked() {
192         return fileExists(name);
193       }
194     };
195   }
196
197   /** Closes the store to future operations. */
198   public final void close() {
199   }
200 }
201
Popular Tags