LUCENE-5810: NRTCachingDirectory now implements FilterDirectory

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1609200 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Simon Willnauer 2014-07-09 15:12:42 +00:00
parent 4007157b14
commit 130a9ea87d
1 changed files with 17 additions and 22 deletions

View File

@ -63,11 +63,10 @@ import org.apache.lucene.util.IOUtils;
* @lucene.experimental
*/
public class NRTCachingDirectory extends Directory {
public class NRTCachingDirectory extends FilterDirectory {
private final RAMDirectory cache = new RAMDirectory();
private final Directory delegate;
private final long maxMergeSizeBytes;
private final long maxCachedBytes;
@ -80,43 +79,39 @@ public class NRTCachingDirectory extends Directory {
* maxMergeSizeMB, and 2) the total cached bytes is <=
* maxCachedMB */
public NRTCachingDirectory(Directory delegate, double maxMergeSizeMB, double maxCachedMB) {
this.delegate = delegate;
super(delegate);
maxMergeSizeBytes = (long) (maxMergeSizeMB*1024*1024);
maxCachedBytes = (long) (maxCachedMB*1024*1024);
}
public Directory getDelegate() {
return delegate;
}
@Override
public LockFactory getLockFactory() {
return delegate.getLockFactory();
return in.getLockFactory();
}
@Override
public void setLockFactory(LockFactory lf) throws IOException {
delegate.setLockFactory(lf);
in.setLockFactory(lf);
}
@Override
public String getLockID() {
return delegate.getLockID();
return in.getLockID();
}
@Override
public Lock makeLock(String name) {
return delegate.makeLock(name);
return in.makeLock(name);
}
@Override
public void clearLock(String name) throws IOException {
delegate.clearLock(name);
in.clearLock(name);
}
@Override
public String toString() {
return "NRTCachingDirectory(" + delegate + "; maxCacheMB=" + (maxCachedBytes/1024/1024.) + " maxMergeSizeMB=" + (maxMergeSizeBytes/1024/1024.) + ")";
return "NRTCachingDirectory(" + in + "; maxCacheMB=" + (maxCachedBytes/1024/1024.) + " maxMergeSizeMB=" + (maxMergeSizeBytes/1024/1024.) + ")";
}
@Override
@ -130,7 +125,7 @@ public class NRTCachingDirectory extends Directory {
// yet been called, because so far everything is a cached write,
// in this case, we don't want to throw a NoSuchDirectoryException
try {
for(String f : delegate.listAll()) {
for(String f : in.listAll()) {
// Cannot do this -- if lucene calls createOutput but
// file already exists then this falsely trips:
//assert !files.contains(f): "file \"" + f + "\" is in both dirs";
@ -160,7 +155,7 @@ public class NRTCachingDirectory extends Directory {
if (cache.fileNameExists(name)) {
cache.deleteFile(name);
} else {
delegate.deleteFile(name);
in.deleteFile(name);
}
}
@ -169,7 +164,7 @@ public class NRTCachingDirectory extends Directory {
if (cache.fileNameExists(name)) {
return cache.fileLength(name);
} else {
return delegate.fileLength(name);
return in.fileLength(name);
}
}
@ -187,7 +182,7 @@ public class NRTCachingDirectory extends Directory {
System.out.println(" to cache");
}
try {
delegate.deleteFile(name);
in.deleteFile(name);
} catch (IOException ioe) {
// This is fine: file may not exist
}
@ -198,7 +193,7 @@ public class NRTCachingDirectory extends Directory {
} catch (IOException ioe) {
// This is fine: file may not exist
}
return delegate.createOutput(name, context);
return in.createOutput(name, context);
}
}
@ -210,7 +205,7 @@ public class NRTCachingDirectory extends Directory {
for(String fileName : fileNames) {
unCache(fileName);
}
delegate.sync(fileNames);
in.sync(fileNames);
}
@Override
@ -224,7 +219,7 @@ public class NRTCachingDirectory extends Directory {
}
return cache.openInput(name, context);
} else {
return delegate.openInput(name, context);
return in.openInput(name, context);
}
}
@ -241,7 +236,7 @@ public class NRTCachingDirectory extends Directory {
unCache(fileName);
}
cache.close();
delegate.close();
in.close();
}
/** Subclass can override this to customize logic; return
@ -273,7 +268,7 @@ public class NRTCachingDirectory extends Directory {
return;
}
final IOContext context = IOContext.DEFAULT;
final IndexOutput out = delegate.createOutput(fileName, context);
final IndexOutput out = in.createOutput(fileName, context);
IndexInput in = null;
try {
in = cache.openInput(fileName, context);