1 package org.apache.lucene.store; 2 3 18 19 import java.io.IOException ; 20 import java.io.File ; 21 import java.util.Hashtable ; 22 import java.util.Enumeration ; 23 24 import org.apache.lucene.store.Directory; 25 import org.apache.lucene.store.IndexInput; 26 import org.apache.lucene.store.IndexOutput; 27 28 33 public final class RAMDirectory extends Directory { 34 Hashtable files = new Hashtable (); 35 36 37 public RAMDirectory() { 38 } 39 40 50 public RAMDirectory(Directory dir) throws IOException { 51 this(dir, false); 52 } 53 54 private RAMDirectory(Directory dir, boolean closeDir) throws IOException { 55 final String [] files = dir.list(); 56 byte[] buf = new byte[BufferedIndexOutput.BUFFER_SIZE]; 57 for (int i = 0; i < files.length; i++) { 58 IndexOutput os = createOutput(files[i]); 60 IndexInput is = dir.openInput(files[i]); 62 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 is.close(); 74 os.close(); 75 } 76 if(closeDir) 77 dir.close(); 78 } 79 80 85 public RAMDirectory(File dir) throws IOException { 86 this(FSDirectory.getDirectory(dir, false), true); 87 } 88 89 94 public RAMDirectory(String dir) throws IOException { 95 this(FSDirectory.getDirectory(dir, false), true); 96 } 97 98 99 public final String [] list() { 100 String [] result = new String [files.size()]; 101 int i = 0; 102 Enumeration names = files.keys(); 103 while (names.hasMoreElements()) 104 result[i++] = (String )names.nextElement(); 105 return result; 106 } 107 108 109 public final boolean fileExists(String name) { 110 RAMFile file = (RAMFile)files.get(name); 111 return file != null; 112 } 113 114 115 public final long fileModified(String name) { 116 RAMFile file = (RAMFile)files.get(name); 117 return file.lastModified; 118 } 119 120 121 public void touchFile(String name) { 122 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 e) {} 130 ts2 = System.currentTimeMillis(); 131 } while(ts1 == ts2); 135 136 file.lastModified = ts2; 137 138 } 141 142 143 public final long fileLength(String name) { 144 RAMFile file = (RAMFile)files.get(name); 145 return file.length; 146 } 147 148 149 public final void deleteFile(String name) { 150 files.remove(name); 151 } 152 153 154 public final void renameFile(String from, String to) { 155 RAMFile file = (RAMFile)files.get(from); 156 files.remove(from); 157 files.put(to, file); 158 } 159 160 162 public final IndexOutput createOutput(String name) { 163 RAMFile file = new RAMFile(); 164 files.put(name, file); 165 return new RAMOutputStream(file); 166 } 167 168 169 public final IndexInput openInput(String name) { 170 RAMFile file = (RAMFile)files.get(name); 171 return new RAMInputStream(file); 172 } 173 174 177 public final Lock makeLock(final String name) { 178 return new Lock() { 179 public boolean obtain() throws IOException { 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 198 public final void close() { 199 } 200 } 201 | Popular Tags |