Skip to content

Commit

Permalink
replace the isUnique function with two interfaces extending the Field…
Browse files Browse the repository at this point in the history
…Index
  • Loading branch information
xhad1234 committed Jun 17, 2016
1 parent 61c31f9 commit e189e52
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 211 deletions.
39 changes: 24 additions & 15 deletions core/common/src/main/java/alluxio/collections/IndexedSet.java
Expand Up @@ -128,11 +128,6 @@ public class IndexedSet<T> extends AbstractSet<T> {
* @param <T> type of objects in this {@link IndexedSet} * @param <T> type of objects in this {@link IndexedSet}
*/ */
public interface FieldIndex<T> { public interface FieldIndex<T> {
/**
* @return whether this index entries are unique or repeatable
*/
Boolean isUnique();

/** /**
* Gets the value of the field that serves as index. * Gets the value of the field that serves as index.
* *
Expand All @@ -142,6 +137,20 @@ public interface FieldIndex<T> {
Object getFieldValue(T o); Object getFieldValue(T o);
} }


/**
* An interface extending{@link FieldIndex}, represents a unique index.
*
* @param <T> type of objects in this {@link IndexedSet}
*/
public interface UniqueFieldIndex<T> extends FieldIndex<T> {}

/**
* An interface extending{@link FieldIndex}, represents a nonunique index.
*
* @param <T> type of objects in this {@link IndexedSet}
*/
public interface NonUniqueFieldIndex<T> extends FieldIndex<T> {}

/** /**
* Constructs a new {@link IndexedSet} instance with at least one field as the index. * Constructs a new {@link IndexedSet} instance with at least one field as the index.
* *
Expand All @@ -154,14 +163,14 @@ public IndexedSet(FieldIndex<T> field, FieldIndex<T>... otherFields) {
int uniqueIndexLength = 0; int uniqueIndexLength = 0;
int nonUniqueIndexLength = 0; int nonUniqueIndexLength = 0;


if (field.isUnique()) { if (field instanceof UniqueFieldIndex) {
uniqueIndexLength = 1; uniqueIndexLength = 1;
} else { } else {
nonUniqueIndexLength = 1; nonUniqueIndexLength = 1;
} }


for (FieldIndex<T> fieldIndex : otherFields) { for (FieldIndex<T> fieldIndex : otherFields) {
if (fieldIndex.isUnique()) { if (fieldIndex instanceof UniqueFieldIndex) {
uniqueIndexLength++; uniqueIndexLength++;
} else { } else {
nonUniqueIndexLength++; nonUniqueIndexLength++;
Expand All @@ -178,15 +187,15 @@ public IndexedSet(FieldIndex<T> field, FieldIndex<T>... otherFields) {
indexMapNonUnique = new HashMap<>(nonUniqueIndexLength); indexMapNonUnique = new HashMap<>(nonUniqueIndexLength);
} }


if (field.isUnique()) { if (field instanceof UniqueFieldIndex) {
indexMapUnique.put(field, new ConcurrentHashMap<Object, T>(8, 0.95f, 8)); indexMapUnique.put(field, new ConcurrentHashMap<Object, T>(8, 0.95f, 8));
} else { } else {
indexMapNonUnique.put(field, indexMapNonUnique.put(field,
new ConcurrentHashMap<Object, ConcurrentHashSet<T>>(8, 0.95f, 8)); new ConcurrentHashMap<Object, ConcurrentHashSet<T>>(8, 0.95f, 8));
} }


for (FieldIndex<T> fieldIndex : otherFields) { for (FieldIndex<T> fieldIndex : otherFields) {
if (fieldIndex.isUnique()) { if (fieldIndex instanceof UniqueFieldIndex) {
indexMapUnique.put(fieldIndex, new ConcurrentHashMap<Object, T>(8, 0.95f, 8)); indexMapUnique.put(fieldIndex, new ConcurrentHashMap<Object, T>(8, 0.95f, 8));
} else { } else {
indexMapNonUnique.put(fieldIndex, indexMapNonUnique.put(fieldIndex,
Expand Down Expand Up @@ -339,7 +348,7 @@ public void remove() {
* @return true if there is one such object, otherwise false * @return true if there is one such object, otherwise false
*/ */
public boolean contains(FieldIndex<T> index, Object value) { public boolean contains(FieldIndex<T> index, Object value) {
if (index.isUnique()) { if (index instanceof UniqueFieldIndex) {
T res = getByFieldInternalUnique(index, value); T res = getByFieldInternalUnique(index, value);
return res != null; return res != null;
} }
Expand All @@ -359,7 +368,7 @@ public boolean contains(FieldIndex<T> index, Object value) {
// TODO(gpang): Remove this method, if it is not being used. // TODO(gpang): Remove this method, if it is not being used.
public Set<T> getByField(FieldIndex<T> index, Object value) { public Set<T> getByField(FieldIndex<T> index, Object value) {
Set<T> set; Set<T> set;
if (index.isUnique()) { if (index instanceof UniqueFieldIndex) {
set = new HashSet<T>(); set = new HashSet<T>();
T res = getByFieldInternalUnique(index, value); T res = getByFieldInternalUnique(index, value);
if (res != null) { if (res != null) {
Expand All @@ -379,7 +388,7 @@ public Set<T> getByField(FieldIndex<T> index, Object value) {
* @return the object or null if there is no such object * @return the object or null if there is no such object
*/ */
public T getFirstByField(FieldIndex<T> index, Object value) { public T getFirstByField(FieldIndex<T> index, Object value) {
if (index.isUnique()) { if (index instanceof UniqueFieldIndex) {
T res = getByFieldInternalUnique(index, value); T res = getByFieldInternalUnique(index, value);
return res; return res;
} }
Expand Down Expand Up @@ -456,7 +465,7 @@ private void removeFromIndices(T object) {
*/ */
public int removeByField(FieldIndex<T> index, Object value) { public int removeByField(FieldIndex<T> index, Object value) {
int removed = 0; int removed = 0;
if (index.isUnique()) { if (index instanceof UniqueFieldIndex) {
T toRemove = getByFieldInternalUnique(index, value); T toRemove = getByFieldInternalUnique(index, value);
if (toRemove == null) { if (toRemove == null) {
return 0; return 0;
Expand Down Expand Up @@ -495,7 +504,7 @@ public int size() {
* @return the set of objects with the specified field value * @return the set of objects with the specified field value
*/ */
private T getByFieldInternalUnique(FieldIndex<T> index, Object value) { private T getByFieldInternalUnique(FieldIndex<T> index, Object value) {
Preconditions.checkState(index.isUnique(), Preconditions.checkState(index instanceof UniqueFieldIndex,
"Using getByFieldInternalUnique for repeatable index"); "Using getByFieldInternalUnique for repeatable index");
if (mIndexMapUnique == null) { if (mIndexMapUnique == null) {
return null; return null;
Expand All @@ -504,7 +513,7 @@ private T getByFieldInternalUnique(FieldIndex<T> index, Object value) {
} }


private ConcurrentHashSet<T> getByFieldInternalNonUnique(FieldIndex<T> index, Object value) { private ConcurrentHashSet<T> getByFieldInternalNonUnique(FieldIndex<T> index, Object value) {
Preconditions.checkState(!index.isUnique(), Preconditions.checkState(index instanceof NonUniqueFieldIndex,
"Using getByFieldInternalNonUnique for unique index"); "Using getByFieldInternalNonUnique for unique index");
if (mIndexMapNonUnique == null) { if (mIndexMapNonUnique == null) {
return null; return null;
Expand Down
16 changes: 4 additions & 12 deletions core/common/src/test/java/alluxio/collections/IndexedSetTest.java
Expand Up @@ -44,30 +44,22 @@ public long longValue() {
} }


private IndexedSet<Pair> mSet; private IndexedSet<Pair> mSet;
private IndexedSet.FieldIndex<Pair> mIntIndex; private IndexedSet.NonUniqueFieldIndex<Pair> mIntIndex;
private IndexedSet.FieldIndex<Pair> mLongIndex; private IndexedSet.NonUniqueFieldIndex<Pair> mLongIndex;


/** /**
* Sets up the fields before running a test. * Sets up the fields before running a test.
*/ */
@Before @Before
public void before() { public void before() {
mIntIndex = new IndexedSet.FieldIndex<Pair>() { mIntIndex = new IndexedSet.NonUniqueFieldIndex<Pair>() {
@Override @Override
public Boolean isUnique() {
return false;
}

public Object getFieldValue(Pair o) { public Object getFieldValue(Pair o) {
return o.intValue(); return o.intValue();
} }
}; };
mLongIndex = new IndexedSet.FieldIndex<Pair>() { mLongIndex = new IndexedSet.NonUniqueFieldIndex<Pair>() {
@Override @Override
public Boolean isUnique() {
return false;
}

public Object getFieldValue(Pair o) { public Object getFieldValue(Pair o) {
return o.longValue(); return o.longValue();
} }
Expand Down
18 changes: 4 additions & 14 deletions core/server/src/main/java/alluxio/master/block/BlockMaster.java
Expand Up @@ -114,26 +114,16 @@ public final class BlockMaster extends AbstractMaster implements ContainerIdGene
new BlockContainerIdGenerator(); new BlockContainerIdGenerator();


// Worker metadata management. // Worker metadata management.
private final IndexedSet.FieldIndex<MasterWorkerInfo> mIdIndex = private final IndexedSet.UniqueFieldIndex<MasterWorkerInfo> mIdIndex =
new IndexedSet.FieldIndex<MasterWorkerInfo>() { new IndexedSet.UniqueFieldIndex<MasterWorkerInfo>() {
@Override
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(MasterWorkerInfo o) { public Object getFieldValue(MasterWorkerInfo o) {
return o.getId(); return o.getId();
} }
}; };


private final IndexedSet.FieldIndex<MasterWorkerInfo> mAddressIndex = private final IndexedSet.UniqueFieldIndex<MasterWorkerInfo> mAddressIndex =
new IndexedSet.FieldIndex<MasterWorkerInfo>() { new IndexedSet.UniqueFieldIndex<MasterWorkerInfo>() {
@Override
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(MasterWorkerInfo o) { public Object getFieldValue(MasterWorkerInfo o) {
return o.getWorkerAddress(); return o.getWorkerAddress();
Expand Down
Expand Up @@ -33,24 +33,16 @@
*/ */
@NotThreadSafe @NotThreadSafe
public final class InodeDirectory extends Inode<InodeDirectory> { public final class InodeDirectory extends Inode<InodeDirectory> {
private IndexedSet.FieldIndex<Inode<?>> mIdIndex = new IndexedSet.FieldIndex<Inode<?>>() { private IndexedSet.UniqueFieldIndex<Inode<?>> mIdIndex =
@Override new IndexedSet.UniqueFieldIndex<Inode<?>>() {
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(Inode<?> o) { public Object getFieldValue(Inode<?> o) {
return o.getId(); return o.getId();
} }
}; };


private IndexedSet.FieldIndex<Inode<?>> mNameIndex = new IndexedSet.FieldIndex<Inode<?>>() { private IndexedSet.UniqueFieldIndex<Inode<?>> mNameIndex =
@Override new IndexedSet.UniqueFieldIndex<Inode<?>>() {
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(Inode<?> o) { public Object getFieldValue(Inode<?> o) {
return o.getName(); return o.getName();
Expand Down
Expand Up @@ -93,12 +93,8 @@ public enum LockMode {
/** Mount table manages the file system mount points. */ /** Mount table manages the file system mount points. */
private final MountTable mMountTable; private final MountTable mMountTable;


private final IndexedSet.FieldIndex<Inode<?>> mIdIndex = new IndexedSet.FieldIndex<Inode<?>>() { private final IndexedSet.UniqueFieldIndex<Inode<?>> mIdIndex =
@Override new IndexedSet.UniqueFieldIndex<Inode<?>>() {
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(Inode<?> o) { public Object getFieldValue(Inode<?> o) {
return o.getId(); return o.getId();
Expand Down
Expand Up @@ -51,55 +51,35 @@ public final class UnderFileSystemManager {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE); private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);


// Input stream agent session index // Input stream agent session index
private final IndexedSet.FieldIndex<InputStreamAgent> mInputStreamAgentSessionIdIndex = private final IndexedSet.UniqueFieldIndex<InputStreamAgent> mInputStreamAgentSessionIdIndex =
new IndexedSet.FieldIndex<InputStreamAgent>() { new IndexedSet.UniqueFieldIndex<InputStreamAgent>() {
@Override
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(InputStreamAgent o) { public Object getFieldValue(InputStreamAgent o) {
return o.mSessionId; return o.mSessionId;
} }
}; };


// Input stream agent id index // Input stream agent id index
private final IndexedSet.FieldIndex<InputStreamAgent> mInputStreamAgentIdIndex = private final IndexedSet.UniqueFieldIndex<InputStreamAgent> mInputStreamAgentIdIndex =
new IndexedSet.FieldIndex<InputStreamAgent>() { new IndexedSet.UniqueFieldIndex<InputStreamAgent>() {
@Override
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(InputStreamAgent o) { public Object getFieldValue(InputStreamAgent o) {
return o.mAgentId; return o.mAgentId;
} }
}; };


// Output stream agent session index // Output stream agent session index
private final IndexedSet.FieldIndex<OutputStreamAgent> mOuputStreamAgentSessionIdIndex = private final IndexedSet.UniqueFieldIndex<OutputStreamAgent> mOuputStreamAgentSessionIdIndex =
new IndexedSet.FieldIndex<OutputStreamAgent>() { new IndexedSet.UniqueFieldIndex<OutputStreamAgent>() {
@Override
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(OutputStreamAgent o) { public Object getFieldValue(OutputStreamAgent o) {
return o.mSessionId; return o.mSessionId;
} }
}; };


// Output stream agent id index // Output stream agent id index
private final IndexedSet.FieldIndex<OutputStreamAgent> mOutputStreamAgentIdIndex = private final IndexedSet.UniqueFieldIndex<OutputStreamAgent> mOutputStreamAgentIdIndex =
new IndexedSet.FieldIndex<OutputStreamAgent>() { new IndexedSet.UniqueFieldIndex<OutputStreamAgent>() {
@Override
public Boolean isUnique() {
return true;
}

@Override @Override
public Object getFieldValue(OutputStreamAgent o) { public Object getFieldValue(OutputStreamAgent o) {
return o.mAgentId; return o.mAgentId;
Expand Down
Expand Up @@ -381,7 +381,7 @@ private void addWorker(BlockMaster master, long workerId, List<String> storageTi
/** Private access to {@link BlockMaster} internals. */ /** Private access to {@link BlockMaster} internals. */
private class PrivateAccess { private class PrivateAccess {
private final Map<Long, MasterBlockInfo> mBlocks; private final Map<Long, MasterBlockInfo> mBlocks;
private final IndexedSet.FieldIndex<MasterWorkerInfo> mIdIndex; private final IndexedSet.UniqueFieldIndex<MasterWorkerInfo> mIdIndex;
private final IndexedSet<MasterWorkerInfo> mLostWorkers; private final IndexedSet<MasterWorkerInfo> mLostWorkers;
private final IndexedSet<MasterWorkerInfo> mWorkers; private final IndexedSet<MasterWorkerInfo> mWorkers;


Expand Down

0 comments on commit e189e52

Please sign in to comment.