/** * Return total size in bytes of all files in this directory. This is * currently quantized to RAMOutputStream.BUFFER_SIZE. */ @Override public final long ramBytesUsed() { ensureOpen(); return sizeInBytes.get(); }
public final boolean fileNameExists(String name) { ensureOpen(); return fileMap.containsKey(name); }
@Override public void deleteFile(String name) throws IOException { ensureOpen(); RAMFile file = fileMap.remove(name); if (file != null) { file.directory = null; sizeInBytes.addAndGet(-file.sizeInBytes); } else { throw new FileNotFoundException(name); } }
/** Returns a stream reading an existing file. */ @Override public IndexInput openInput(String name, IOContext context) throws IOException { ensureOpen(); RAMFile file = fileMap.get(name); if (file == null) { throw new FileNotFoundException(name); } return new RAMInputStream(name, file); }
/** Returns the length in bytes of a file in the directory. * @throws IOException if the file does not exist */ @Override public final long fileLength(String name) throws IOException { ensureOpen(); RAMFile file = fileMap.get(name); if (file == null) { throw new FileNotFoundException(name); } return file.getLength(); }
@Override public void rename(String source, String dest) throws IOException { ensureOpen(); RAMFile file = fileMap.get(source); if (file == null) { throw new FileNotFoundException(source); } if (fileMap.putIfAbsent(dest, file) != null) { throw new FileAlreadyExistsException(dest); } if (!fileMap.remove(source, file)) { throw new IllegalStateException("file was unexpectedly replaced: " + source); } fileMap.remove(source); }
@Override public final String[] listAll() { ensureOpen(); // NOTE: this returns a "weakly consistent view". Unless we change Dir API, keep this, // and do not synchronize or anything stronger. it's great for testing! // NOTE: fileMap.keySet().toArray(new String[0]) is broken in non Sun JDKs, // and the code below is resilient to map changes during the array population. // NOTE: don't replace this with return names.toArray(new String[names.size()]); // or some files could be null at the end of the array if files are being deleted // concurrently Set<String> fileNames = fileMap.keySet(); List<String> names = new ArrayList<>(fileNames.size()); for (String name : fileNames) { names.add(name); } String[] namesArray = names.toArray(new String[names.size()]); Arrays.sort(namesArray); return namesArray; }
@Override public IndexOutput createTempOutput(String prefix, String suffix, IOContext context) throws IOException { ensureOpen(); // Make the file first... RAMFile file = newRAMFile(); // ... then try to find a unique name for it: while (true) { String name = IndexFileNames.segmentFileName(prefix, suffix + "_" + Long.toString(nextTempFileCounter.getAndIncrement(), Character.MAX_RADIX), "tmp"); if (fileMap.putIfAbsent(name, file) == null) { return new RAMOutputStream(name, file, true); } } }
@Override public IndexOutput createOutput(String name, IOContext context) throws IOException { ensureOpen(); RAMFile file = newRAMFile(); if (fileMap.putIfAbsent(name, file) != null) { throw new FileAlreadyExistsException(name); } return new RAMOutputStream(name, file, true); }
/** Return total size in bytes of all files in this * directory. This is currently quantized to * RAMOutputStream.BUFFER_SIZE. */ public synchronized final long sizeInBytes() { ensureOpen(); return sizeInBytes; }
/** * Return total size in bytes of all files in this directory. This is * currently quantized to RAMOutputStream.BUFFER_SIZE. */ @Override public final long ramBytesUsed() { ensureOpen(); return sizeInBytes.get(); }
/** Returns true iff the named file exists in this directory. */ public final boolean fileExists(String name) { ensureOpen(); RAMFile file; synchronized (this) { file = (RAMFile)fileMap.get(name); } return file != null; }
/** * Return total size in bytes of all files in this directory. This is * currently quantized to RAMOutputStream.BUFFER_SIZE. */ @Override public final long ramBytesUsed() { ensureOpen(); return sizeInBytes.get(); }
@Override public void renameFile(String source, String dest) throws IOException { ensureOpen(); RAMFile file = fileMap.get(source); if (file == null) { throw new FileNotFoundException(source); } fileMap.put(dest, file); fileMap.remove(source); }
/** Returns an array of strings, one for each file in the directory. */ public synchronized final String[] list() { ensureOpen(); Set fileNames = fileMap.keySet(); String[] result = new String[fileNames.size()]; int i = 0; Iterator it = fileNames.iterator(); while (it.hasNext()) result[i++] = (String)it.next(); return result; }
/** Returns an array of strings, one for each file in the directory. */ public synchronized final String[] list() { ensureOpen(); Set fileNames = fileMap.keySet(); String[] result = new String[fileNames.size()]; int i = 0; Iterator it = fileNames.iterator(); while (it.hasNext()) result[i++] = (String)it.next(); return result; }
/** Returns the length in bytes of a file in the directory. * @throws IOException if the file does not exist */ @Override public final long fileLength(String name) throws IOException { ensureOpen(); RAMFile file = fileMap.get(name); if (file == null) { throw new FileNotFoundException(name); } return file.getLength(); }
/** Returns a stream reading an existing file. */ public IndexInput openInput(String name) throws IOException { ensureOpen(); RAMFile file; synchronized (this) { file = (RAMFile)fileMap.get(name); } if (file == null) throw new FileNotFoundException(name); return new RAMInputStream(file); }
/** Returns the length in bytes of a file in the directory. * @throws IOException if the file does not exist */ @Override public final long fileLength(String name) throws IOException { ensureOpen(); RAMFile file = fileMap.get(name); if (file == null) { throw new FileNotFoundException(name); } return file.getLength(); }
/** Returns a stream reading an existing file. */ @Override public IndexInput openInput(String name, IOContext context) throws IOException { ensureOpen(); RAMFile file = fileMap.get(name); if (file == null) { throw new FileNotFoundException(name); } return new RAMInputStream(name, file); }