try and extract store directory also if its wrapped in a compound dir

This commit is contained in:
Shay Banon 2013-12-19 14:20:33 +01:00
parent 8c1073bb6e
commit 46d191c8d4
2 changed files with 27 additions and 5 deletions

View File

@ -22,6 +22,7 @@ package org.elasticsearch.index.shard;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.SegmentReader;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.index.store.DirectoryUtils;
import org.elasticsearch.index.store.Store;
/**
@ -38,8 +39,9 @@ public class ShardUtils {
public static ShardId extractShardId(IndexReader reader) {
if (reader instanceof SegmentReader) {
SegmentReader sReader = (SegmentReader) reader;
if (sReader.directory() instanceof Store.StoreDirectory) {
return ((Store.StoreDirectory) sReader.directory()).shardId();
Store.StoreDirectory storeDir = DirectoryUtils.getStoreDirectory(sReader.directory());
if (storeDir != null) {
return storeDir.shardId();
}
}
return null;

View File

@ -19,11 +19,10 @@
package org.elasticsearch.index.store;
import org.apache.lucene.store.CompoundFileDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FilterDirectory;
import java.io.IOException;
import java.util.Collection;
import org.elasticsearch.common.Nullable;
/**
* Utils for working with {@link Directory} classes.
@ -32,6 +31,27 @@ public final class DirectoryUtils {
private DirectoryUtils() {} // no instance
/**
* Try and extract a store directory out of a directory, tries to take into
* account the fact that a directory is a filter directory, and/or a compound dir.
*/
@Nullable
public static Store.StoreDirectory getStoreDirectory(Directory dir) {
Directory current = dir;
while (true) {
if (current instanceof Store.StoreDirectory) {
return (Store.StoreDirectory) current;
}
if (current instanceof FilterDirectory) {
current = ((FilterDirectory) current).getDelegate();
} else if (current instanceof CompoundFileDirectory) {
current = ((CompoundFileDirectory) current).getDirectory();
} else {
return null;
}
}
}
private static final Directory getLeafDirectory(FilterDirectory dir) {
Directory current = dir.getDelegate();
while ((current instanceof FilterDirectory)) {