Use FilterDirectory from lucene instead of maintaining a copy

FilterDirectory has been ported to Lucene in LUCENE-5204 which makes
the class in elasticsearch obsolet. This commit removes the class and
moves the static utils that are not in lucene to `DirectoryUtils`
This commit is contained in:
Simon Willnauer 2013-11-28 14:35:39 +01:00
parent 11de330246
commit 06f520ebeb
8 changed files with 81 additions and 175 deletions

View File

@ -1,155 +0,0 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.lucene.store;
import java.io.IOException;
import java.util.Collection;
public class FilterDirectory extends Directory {
protected final Directory in;
public FilterDirectory(Directory in) {
this.in = in;
}
@Override
public String[] listAll() throws IOException {
ensureOpen();
return in.listAll();
}
@Override
public boolean fileExists(String name) throws IOException {
ensureOpen();
return in.fileExists(name);
}
@Override
public void deleteFile(String name) throws IOException {
ensureOpen();
in.deleteFile(name);
}
@Override
public long fileLength(String name) throws IOException {
ensureOpen();
return in.fileLength(name);
}
@Override
public IndexOutput createOutput(String name, IOContext context) throws IOException {
ensureOpen();
return in.createOutput(name, context);
}
@Override
public void sync(Collection<String> names) throws IOException {
ensureOpen();
in.sync(names);
}
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
ensureOpen();
return in.openInput(name, context);
}
@Override
public Lock makeLock(String name) {
ensureOpen();
return in.makeLock(name);
}
@Override
public void clearLock(String name) throws IOException {
ensureOpen();
in.clearLock(name);
}
@Override
public void close() throws IOException {
in.close();
}
@Override
public void setLockFactory(LockFactory lockFactory) throws IOException {
ensureOpen();
in.setLockFactory(lockFactory);
}
@Override
public LockFactory getLockFactory() {
ensureOpen();
return in.getLockFactory();
}
@Override
public String getLockID() {
ensureOpen();
return in.getLockID();
}
@Override
public void copy(Directory to, String src, String dest, IOContext context) throws IOException {
ensureOpen();
in.copy(to, src, dest, context);
}
@Override
public Directory.IndexInputSlicer createSlicer(final String name, final IOContext context) throws IOException {
ensureOpen();
return in.createSlicer(name, context);
}
public Directory getDelegate() {
ensureOpen();
return in;
}
@SuppressWarnings("resource")
final Directory getLeafDirectory() {
Directory current = getDelegate();
while ((current instanceof FilterDirectory)) {
current = ((FilterDirectory) current).getDelegate();
}
return current;
}
public static <T extends Directory> T getLeaf(Directory dir, Class<T> targetClass) {
return getLeaf(dir, targetClass, null);
}
public static <T extends Directory> T getLeaf(Directory dir, Class<T> targetClass, T defaultValue) {
Directory d = dir;
if (dir instanceof FilterDirectory) {
d = ((FilterDirectory) dir).getLeafDirectory();
}
if (targetClass.isAssignableFrom(d.getClass())) {
return targetClass.cast(d);
} else {
return defaultValue;
}
}
@Override
public String toString() {
return in.toString();
}
}

View File

@ -22,7 +22,7 @@ import org.apache.lucene.store.IOContext.Context;
import java.io.IOException;
public final class RateLimitedFSDirectory extends FilterDirectory {
public final class RateLimitedFSDirectory extends FilterDirectory{
private final StoreRateLimiting.Provider rateLimitingProvider;

View File

@ -0,0 +1,69 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.index.store;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import java.io.IOException;
import java.util.Collection;
/**
* Utils for working with {@link Directory} classes.
*/
public final class DirectoryUtils {
private DirectoryUtils() {} // no instance
private static final Directory getLeafDirectory(FilterDirectory dir) {
Directory current = dir.getDelegate();
while ((current instanceof FilterDirectory)) {
current = ((FilterDirectory) current).getDelegate();
}
return current;
}
/**
* Tries to extract the leaf of the {@link Directory} if the directory is a {@link FilterDirectory} and cast
* it to the given target class or returns <code>null</code> if the leaf is not assignable to the target class.
* If the given {@link Directory} is a concrete directory it will treated as a leaf and the above applies.
*/
public static <T extends Directory> T getLeaf(Directory dir, Class<T> targetClass) {
return getLeaf(dir, targetClass, null);
}
/**
* Tries to extract the leaf of the {@link Directory} if the directory is a {@link FilterDirectory} and cast
* it to the given target class or returns the given default value, if the leaf is not assignable to the target class.
* If the given {@link Directory} is a concrete directory it will treated as a leaf and the above applies.
*/
public static <T extends Directory> T getLeaf(Directory dir, Class<T> targetClass, T defaultValue) {
Directory d = dir;
if (dir instanceof FilterDirectory) {
d = getLeafDirectory((FilterDirectory) dir);
}
if (targetClass.isAssignableFrom(d.getClass())) {
return targetClass.cast(d);
} else {
return defaultValue;
}
}
}

View File

@ -21,7 +21,7 @@ package org.elasticsearch.index.store.distributor;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.FilterDirectory;
import org.elasticsearch.index.store.DirectoryUtils;
import org.elasticsearch.index.store.DirectoryService;
import java.io.IOException;
@ -55,7 +55,7 @@ public abstract class AbstractDistributor implements Distributor {
@SuppressWarnings("unchecked")
protected long getUsableSpace(Directory directory) {
final FSDirectory leaf = FilterDirectory.getLeaf(directory, FSDirectory.class);
final FSDirectory leaf = DirectoryUtils.getLeaf(directory, FSDirectory.class);
if (leaf != null) {
return leaf.getDirectory().getUsableSpace();
} else {

View File

@ -28,6 +28,7 @@ import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.shard.AbstractIndexShardComponent;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.store.DirectoryService;
import org.elasticsearch.index.store.DirectoryUtils;
import org.elasticsearch.index.store.IndexStore;
import java.io.File;
@ -74,7 +75,7 @@ public abstract class FsDirectoryService extends AbstractIndexShardComponent imp
@Override
public final void renameFile(Directory dir, String from, String to) throws IOException {
final FSDirectory fsDirectory = FilterDirectory.getLeaf(dir, FSDirectory.class);
final FSDirectory fsDirectory = DirectoryUtils.getLeaf(dir, FSDirectory.class);
if (fsDirectory == null) {
throw new ElasticSearchIllegalArgumentException("Can not rename file on non-filesystem based directory ");
}
@ -108,7 +109,7 @@ public abstract class FsDirectoryService extends AbstractIndexShardComponent imp
@Override
public final void fullDelete(Directory dir) throws IOException {
final FSDirectory fsDirectory = FilterDirectory.getLeaf(dir, FSDirectory.class);
final FSDirectory fsDirectory = DirectoryUtils.getLeaf(dir, FSDirectory.class);
if (fsDirectory == null) {
throw new ElasticSearchIllegalArgumentException("Can not fully delete on non-filesystem based directory");
}

View File

@ -20,7 +20,7 @@
package org.elasticsearch.index.store.memory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import org.elasticsearch.index.store.DirectoryUtils;
import org.apache.lucene.store.bytebuffer.ByteBufferAllocator;
import org.apache.lucene.store.bytebuffer.ByteBufferDirectory;
import org.apache.lucene.store.bytebuffer.ByteBufferFile;
@ -59,7 +59,7 @@ public final class ByteBufferDirectoryService extends AbstractIndexShardComponen
@Override
public void renameFile(Directory dir, String from, String to) throws IOException {
CustomByteBufferDirectory leaf = FilterDirectory.getLeaf(dir, CustomByteBufferDirectory.class);
CustomByteBufferDirectory leaf = DirectoryUtils.getLeaf(dir, CustomByteBufferDirectory.class);
assert leaf != null;
leaf.renameTo(from, to);
}

View File

@ -19,16 +19,14 @@
package org.elasticsearch.index.store.ram;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.store.RAMFile;
import org.apache.lucene.store.*;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.settings.IndexSettings;
import org.elasticsearch.index.shard.AbstractIndexShardComponent;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.index.store.DirectoryService;
import org.elasticsearch.index.store.DirectoryUtils;
import java.io.FileNotFoundException;
import java.io.IOException;
@ -54,7 +52,7 @@ public final class RamDirectoryService extends AbstractIndexShardComponent imple
@Override
public void renameFile(Directory dir, String from, String to) throws IOException {
CustomRAMDirectory leaf = FilterDirectory.getLeaf(dir, CustomRAMDirectory.class);
CustomRAMDirectory leaf = DirectoryUtils.getLeaf(dir, CustomRAMDirectory.class);
assert leaf != null;
leaf.renameTo(from, to);
}

View File

@ -95,13 +95,7 @@ public class MockDirectoryHelper {
w.setPreventDoubleWrite(preventDoubleWrite);
w.setNoDeleteOpenFile(noDeleteOpenFile);
wrappers.add(w);
return new FilterDirectory(w) {
@Override
public Directory getDelegate() {
// TODO we should port this FilterDirectory to Lucene
return w.getDelegate();
}
};
return w;
}
public Directory[] wrapAllInplace(Directory[] dirs) {
@ -167,5 +161,4 @@ public class MockDirectoryHelper {
super.close(); // force fail if open files etc. called in tear down of ElasticsearchIntegrationTest
}
}
}