diff --git a/.gitignore b/.gitignore index d1c551bb120c..edc6298d34cb 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,5 @@ /underFSStorage/ dependency-reduced-pom.xml target/ +examples/src/main/java/alluxio/examples/LoadGenerator.java tests.log* diff --git a/core/common/src/main/java/alluxio/collections/FieldIndex.java b/core/common/src/main/java/alluxio/collections/FieldIndex.java index f078de64eb8b..dd69c5da0afc 100644 --- a/core/common/src/main/java/alluxio/collections/FieldIndex.java +++ b/core/common/src/main/java/alluxio/collections/FieldIndex.java @@ -22,20 +22,20 @@ * @param type of objects in this {@link IndexedSet} */ interface FieldIndex { - /** - * Gets the value of the field that serves as index. - * - * @param o the instance to get the field value from - * @return the field value, which is just an Object - */ - Object getFieldValue(T o); +// /** +// * Gets the value of the field that serves as index. +// * +// * @param o the instance to get the field value from +// * @return the field value, which is just an Object +// */ +// Object getFieldValue(T o); /** * Puts the an object o to the index. * * @param o the instance to get the field value from */ - void put(T o); + void add(T o); /** * Remove the object o from the index. @@ -55,9 +55,7 @@ interface FieldIndex { /** * Gets a subset of objects with the specified field value. If there is no object with - * the specified field value, a newly created empty set is returned. For non unique index, the - * returned set is backed up by an internal set, so changes in internal set will be reflected - * in returned set. + * the specified field value, a newly created empty set is returned. * * @param value the field value to be satisfied * @return the set of objects or an empty set if no such object exists @@ -65,7 +63,7 @@ interface FieldIndex { Set getByField(Object value); /** - * Gets the object from the set of objects with the specified unique field value. + * Gets an object from the set of objects with the specified unique field value. * * @param value the field value * @return the object or null if there is no such object diff --git a/core/common/src/main/java/alluxio/collections/IndexDefinition.java b/core/common/src/main/java/alluxio/collections/IndexDefinition.java index 15d6474a738a..19aa6a1850ee 100644 --- a/core/common/src/main/java/alluxio/collections/IndexDefinition.java +++ b/core/common/src/main/java/alluxio/collections/IndexDefinition.java @@ -19,46 +19,20 @@ * * @param type of objects in this {@link IndexedSet} */ -public class IndexDefinition { - - /** - * An interface abstracting the value of the field that serves as index. - * - * @param type of objects in this {@link Abstracter} - */ - public interface Abstracter { - /** - * Gets the value of the field that serves as index. - * - * @param o the instance to get the field value from - * @return the field value, which is just an Object - */ - Object getFieldValue(T o); - } - - /** The index name, representing the index in methods of {@link IndexedSet}. */ - private final String mName; - +public abstract class IndexDefinition { /** Whether it is a unique index. */ //TODO(lei): change the mIsUnique to mIndexType enum private final boolean mIsUnique; - /** Abstracts the data field of this index. */ - private final Abstracter mAbstracter; - /** * Constructs a new {@link IndexDefinition} instance. * - * @param name the name of the field index, used as index id * @param isUnique if the index is unique. A unique index is an index where each index value only * maps toone object; A non-unique index is an index where an index value can map * to one or more objects. - * @param abstracter the interface to abstract the value of the field that serves as index */ - public IndexDefinition(String name, boolean isUnique, Abstracter abstracter) { + public IndexDefinition(boolean isUnique) { mIsUnique = isUnique; - mAbstracter = abstracter; - mName = name; } /** @@ -69,16 +43,10 @@ public boolean isUnique() { } /** - * @return the abstracter of the index - */ - public Abstracter getAbstracter() { - return mAbstracter; - } - - /** - * @return the name of the index + * Gets the value of the field that serves as index. + * + * @param o the instance to get the field value from + * @return the field value, which is just an Object */ - public String getName() { - return mName; - } + public abstract Object getFieldValue(T o); } diff --git a/core/common/src/main/java/alluxio/collections/IndexedSet.java b/core/common/src/main/java/alluxio/collections/IndexedSet.java index 85d9af58d2d5..4ce0b5f328bf 100644 --- a/core/common/src/main/java/alluxio/collections/IndexedSet.java +++ b/core/common/src/main/java/alluxio/collections/IndexedSet.java @@ -67,14 +67,14 @@ * First, define the fields to be indexed: * *
- *  FieldIndex idIndex = new FieldIndex {
+ *  IndexDefinition idIndex = new IndexDefinition(true) {
  *    {@literal @Override}
  *    Object getFieldValue(Puppy o) {
  *      return o.id();
  *    }
  *  }
  *
- *  FieldIndex nameIndex = new FieldIndex {
+ *  IndexDefinition nameIndex = new IndexDefinition(true) {
  *    {@literal @Override}
  *    Object getFieldValue(Puppy o) {
  *      return o.name();
@@ -111,37 +111,36 @@ public class IndexedSet extends AbstractSet {
   private final ConcurrentHashSet mObjects = new ConcurrentHashSet<>(8, 0.95f, 8);
 
   /**
-   * Map from {@link FieldIndex} to the index. An index is a map from index value to one or a set of
+   * Map from index name to the index. An index is a map from index value to one or a set of
    * objects with that index value. A unique index is an index where each index value only maps to
    * one object. A non-unique index is an index where an index value can map to one or more objects.
    */
-
-  private final Map> mIndices;
+  private final Map, FieldIndex> mIndices;
 
   /**
    * Constructs a new {@link IndexedSet} instance with at least one field as the index.
    *
-   * @param field at least one field is needed to index the set of objects
-   * @param otherFields other fields to index the set
+   * @param firstIndexDefinition at least one field is needed to index the set of objects
+   * @param otherIndexDefinitions other index definitions to index the set
    */
   @SafeVarargs
-  public IndexedSet(IndexDefinition field, IndexDefinition... otherFields) {
-    // count the numbers of two index types
-    Iterable> fields =
-        Iterables.concat(Arrays.asList(field), Arrays.asList(otherFields));
+  public IndexedSet(IndexDefinition firstIndexDefinition, IndexDefinition...
+      otherIndexDefinitions) {
+    Iterable> indexDefinitions =
+        Iterables.concat(Arrays.asList(firstIndexDefinition), Arrays.asList(otherIndexDefinitions));
 
     // initialization
-    Map> indices = new HashMap>();
+    Map, FieldIndex> indices = new HashMap<>();
 
-    for (IndexDefinition indexDefinition : fields) {
+    for (IndexDefinition indexDefinition : indexDefinitions) {
       FieldIndex index;
       if (indexDefinition.isUnique()) {
-        index = new UniqueFieldIndex(indexDefinition.getAbstracter());
+        index = new UniqueFieldIndex(indexDefinition);
       } else {
-        index = new NonUniqueFieldIndex(indexDefinition.getAbstracter());
+        index = new NonUniqueFieldIndex(indexDefinition);
       }
 
-      if (indices.put(indexDefinition.getName(), index) != null) {
+      if (indices.put(indexDefinition, index) != null) {
         throw new IllegalStateException("Adding two indices to indexedSet using same name.");
       }
     }
@@ -180,8 +179,8 @@ public boolean add(T object) {
         return false;
       }
 
-      for (Map.Entry> fieldInfo : mIndices.entrySet()) {
-        fieldInfo.getValue().put(object);
+      for (Map.Entry, FieldIndex> fieldInfo : mIndices.entrySet()) {
+        fieldInfo.getValue().add(object);
       }
     }
     return true;
@@ -242,38 +241,37 @@ public void remove() {
   /**
    * Whether there is an object with the specified unique index field value in the set.
    *
-   * @param indexName the field index name
+   * @param indexDefinition the field index definition
    * @param value the field value
    * @return true if there is one such object, otherwise false
    */
-  public boolean contains(String indexName, Object value) {
-    FieldIndex index = mIndices.get(indexName);
+  public boolean contains(IndexDefinition indexDefinition, Object value) {
+    FieldIndex index = mIndices.get(indexDefinition);
     return index != null && index.contains(value);
   }
 
   /**
    * Gets a subset of objects with the specified field value. If there is no object with the
-   * specified field value, a newly created empty set is returned. Otherwise, the returned set is
-   * backed up by an internal set, so changes in internal set will be reflected in returned set.
+   * specified field value, a newly created empty set is returned.
    *
-   * @param indexName the field index name
+   * @param indexDefinition the field index definition
    * @param value the field value to be satisfied
    * @return the set of objects or an empty set if no such object exists
    */
-  public Set getByField(String indexName, Object value) {
-    FieldIndex index = mIndices.get(indexName);
+  public Set getByField(IndexDefinition indexDefinition, Object value) {
+    FieldIndex index = mIndices.get(indexDefinition);
     return index == null ? new HashSet() : index.getByField(value);
   }
 
   /**
    * Gets the object from the set of objects with the specified non-unique field value.
    *
-   * @param indexName the field index name
+   * @param indexDefinition the field index definition
    * @param value the field value
    * @return the object or null if there is no such object
    */
-  public T getFirstByField(String indexName, Object value) {
-    FieldIndex index = mIndices.get(indexName);
+  public T getFirstByField(IndexDefinition indexDefinition, Object value) {
+    FieldIndex index = mIndices.get(indexDefinition);
     return index == null ? null : index.getFirst(value);
   }
 
@@ -311,7 +309,7 @@ public boolean remove(Object object) {
    * @param object the object to be removed
    */
   private void removeFromIndices(T object) {
-    for (Map.Entry> fieldInfo : mIndices.entrySet()) {
+    for (Map.Entry, FieldIndex> fieldInfo : mIndices.entrySet()) {
       fieldInfo.getValue().remove(object);
     }
   }
@@ -319,34 +317,27 @@ private void removeFromIndices(T object) {
   /**
    * Removes the object with the specified unique index field value.
    *
-   * @param indexName the field index
+   * @param indexDefinition the field index
    * @param value the field value
    * @return the number of objects removed
    */
-  public int removeByField(String indexName, Object value) {
-    int removed = 0;
-    FieldIndex index = mIndices.get(indexName);
+  public int removeByField(IndexDefinition indexDefinition, Object value) {
+    FieldIndex index = mIndices.get(indexDefinition);
 
     if (index == null) {
       return 0;
-    } else if (index instanceof UniqueFieldIndex) {
-      T toRemove = ((UniqueFieldIndex) index).get(value);
-      if (remove(toRemove)) {
-        removed++;
-      }
-    } else if (index instanceof NonUniqueFieldIndex) {
-      ConcurrentHashSet toRemove = ((NonUniqueFieldIndex) index).get(value);
+    }
 
-      if (toRemove == null) {
-        return 0;
-      }
-      for (T o : toRemove) {
-        if (remove(o)) {
-          removed++;
-        }
+    Set toRemove = index.getByField(value);
+    if (toRemove == null) {
+      return 0;
+    }
+    int removed = 0;
+    for (T o : toRemove) {
+      if (remove(o)) {
+        removed++;
       }
     }
-
     return removed;
   }
 
diff --git a/core/common/src/main/java/alluxio/collections/NonUniqueFieldIndex.java b/core/common/src/main/java/alluxio/collections/NonUniqueFieldIndex.java
index 7d91a65658e7..6f784b7618e5 100644
--- a/core/common/src/main/java/alluxio/collections/NonUniqueFieldIndex.java
+++ b/core/common/src/main/java/alluxio/collections/NonUniqueFieldIndex.java
@@ -23,35 +23,20 @@
  * @param  type of objects in this {@link IndexedSet}
  */
 class NonUniqueFieldIndex implements FieldIndex {
-  private final IndexDefinition.Abstracter mAbstracter;
+  private final IndexDefinition mIndexDefinition;
   private ConcurrentHashMap> mIndexMap;
 
   /**
    * Constructs a new {@link NonUniqueFieldIndex} instance.
    */
-  public NonUniqueFieldIndex(IndexDefinition.Abstracter abstracter) {
+  public NonUniqueFieldIndex(IndexDefinition indexDefinition) {
     mIndexMap = new ConcurrentHashMap>(8, 0.95f, 8);
-    mAbstracter = abstracter;
-  }
-
-  /**
-   * Gets the set of objects with the specified field value - internal function.
-   *
-   * @param value the field value
-   * @return the set of objects with the specified field value
-   */
-  public ConcurrentHashSet get(Object value) {
-    return mIndexMap.get(value);
+    mIndexDefinition = indexDefinition;
   }
 
   @Override
-  public Object getFieldValue(T o) {
-    return mAbstracter.getFieldValue(o);
-  }
-
-  @Override
-  public void put(T object) {
-    Object fieldValue = getFieldValue(object);
+  public void add(T object) {
+    Object fieldValue = mIndexDefinition.getFieldValue(object);
 
     ConcurrentHashSet objSet = mIndexMap.get(fieldValue);
 
@@ -61,20 +46,12 @@ public void put(T object) {
       objSet = mIndexMap.get(fieldValue);
     }
 
-    if (!objSet.addIfAbsent(object)) {
-      // this call can never return false because:
-      // a. the second-level sets in the indices are all
-      // {@link java.util.Set} instances of unbounded space
-      // b. We have already successfully added object on mObjects,
-      // meaning that it cannot be already in any of the sets.
-      // (mObjects is exactly the set-union of all the other second-level sets)
-      throw new IllegalStateException("Indexed Set is in an illegal state");
-    }
+    objSet.add(object);
   }
 
   @Override
   public boolean remove(T object) {
-    Object fieldValue = getFieldValue(object);
+    Object fieldValue = mIndexDefinition.getFieldValue(object);
     ConcurrentHashSet objSet = mIndexMap.get(fieldValue);
     if (objSet != null) {
       return objSet.remove(object);
diff --git a/core/common/src/main/java/alluxio/collections/UniqueFieldIndex.java b/core/common/src/main/java/alluxio/collections/UniqueFieldIndex.java
index b9451428fe10..64c1c95498b9 100644
--- a/core/common/src/main/java/alluxio/collections/UniqueFieldIndex.java
+++ b/core/common/src/main/java/alluxio/collections/UniqueFieldIndex.java
@@ -23,35 +23,20 @@
  * @param  type of objects in this {@link IndexedSet}
  */
 class UniqueFieldIndex implements FieldIndex {
-  private final IndexDefinition.Abstracter mAbstracter;
+  private final IndexDefinition mIndexDefinition;
   private ConcurrentHashMap mIndexMap;
 
   /**
    * Constructs a new {@link UniqueFieldIndex} instance.
    */
-  public UniqueFieldIndex(IndexDefinition.Abstracter abstracter) {
+  public UniqueFieldIndex(IndexDefinition indexDefinition) {
     mIndexMap = new ConcurrentHashMap(8, 0.95f, 8);
-    mAbstracter = abstracter;
-  }
-
-  /**
-   * Gets the object with the specified field value - internal function.
-   *
-   * @param value the field value
-   * @return the object with the specified field value
-   */
-  public T get(Object value) {
-    return mIndexMap.get(value);
-  }
-
-  @Override
-  public Object getFieldValue(T o) {
-    return mAbstracter.getFieldValue(o);
+    mIndexDefinition = indexDefinition;
   }
 
   @Override
-  public void put(T object) {
-    Object fieldValue = getFieldValue(object);
+  public void add(T object) {
+    Object fieldValue = mIndexDefinition.getFieldValue(object);
 
     if (mIndexMap.putIfAbsent(fieldValue, object) != null) {
       throw new IllegalStateException("Adding more than one value to a unique index.");
@@ -60,7 +45,7 @@ public void put(T object) {
 
   @Override
   public boolean remove(T object) {
-    Object fieldValue = getFieldValue(object);
+    Object fieldValue = mIndexDefinition.getFieldValue(object);
     return mIndexMap.remove(fieldValue, object);
   }
 
diff --git a/core/common/src/test/java/alluxio/collections/IndexedSetTest.java b/core/common/src/test/java/alluxio/collections/IndexedSetTest.java
index f065856cb94a..a972e5532b4e 100644
--- a/core/common/src/test/java/alluxio/collections/IndexedSetTest.java
+++ b/core/common/src/test/java/alluxio/collections/IndexedSetTest.java
@@ -45,35 +45,29 @@ public long longValue() {
   }
 
   private IndexedSet mSet;
-  private static final String INT_INDEX_NAME = "NonUniqueIntIndex";
-  private static final String LONG_INDEX_NAME = "UniqueLongIndex";
+  private IndexDefinition mNonUniqueIntIndex;
+  private IndexDefinition mUniqueLongIndex;
 
   /**
    * Sets up the fields before running a test.
    */
   @Before
   public void before() {
-    IndexDefinition nonUniqueIntIndex = new IndexDefinition<>(
-        INT_INDEX_NAME,
-        false,
-        new IndexDefinition.Abstracter() {
-          @Override
-          public Object getFieldValue(Pair o) {
-            return o.intValue();
-          }
-        }
-    );
-    IndexDefinition uniqueLongIndex = new IndexDefinition<>(
-        LONG_INDEX_NAME,
-        true,
-        new IndexDefinition.Abstracter() {
-          @Override
-          public Object getFieldValue(Pair o) {
-            return o.longValue();
-          }
-        }
-    );
-    mSet = new IndexedSet<>(nonUniqueIntIndex, uniqueLongIndex);
+    mNonUniqueIntIndex = new IndexDefinition(false) {
+      @Override
+      public Object getFieldValue(Pair o) {
+        return o.intValue();
+      }
+    };
+
+    mUniqueLongIndex = new IndexDefinition(true) {
+      @Override
+      public Object getFieldValue(Pair o) {
+        return o.longValue();
+      }
+    };
+
+    mSet = new IndexedSet<>(mNonUniqueIntIndex, mUniqueLongIndex);
     long l = 0;
     for (int i = 0; i < 3; i++) {
       for (int k = 0; k < 3; k++) {
@@ -83,34 +77,34 @@ public Object getFieldValue(Pair o) {
   }
 
   /**
-   * Tests the {@link IndexedSet#contains(String, Object) } method.
+   * Tests the {@link IndexedSet#contains(IndexDefinition, Object) } method.
    */
   @Test
   public void UniqueContainsTest() {
     for (int i = 0; i < 3; i++) {
-      Assert.assertTrue(mSet.contains(INT_INDEX_NAME, i));
+      Assert.assertTrue(mSet.contains(mNonUniqueIntIndex, i));
     }
-    Assert.assertFalse(mSet.contains(INT_INDEX_NAME, 4));
+    Assert.assertFalse(mSet.contains(mNonUniqueIntIndex, 4));
   }
 
   /**
-   * Tests the {@link IndexedSet#contains(String, Object)} method.
+   * Tests the {@link IndexedSet#contains(IndexDefinition, Object)} method.
    */
   @Test
   public void NonUniqueContainsTest() {
     for (long l = 0; l < 9; l++) {
-      Assert.assertTrue(mSet.contains(LONG_INDEX_NAME, l));
+      Assert.assertTrue(mSet.contains(mUniqueLongIndex, l));
     }
-    Assert.assertFalse(mSet.contains(LONG_INDEX_NAME, 9L));
+    Assert.assertFalse(mSet.contains(mUniqueLongIndex, 9L));
   }
 
   /**
-   * Tests the {@link IndexedSet#getByField(String, Object)} method.
+   * Tests the {@link IndexedSet#getByField(IndexDefinition, Object)} method.
    */
   @Test
   public void nonUniqueGetTest() {
     for (int i = 0; i < 3; i++) {
-      Set set = mSet.getByField(INT_INDEX_NAME, i);
+      Set set = mSet.getByField(mNonUniqueIntIndex, i);
       Assert.assertEquals(3, set.size());
       List longs = new ArrayList<>(set.size());
       for (Pair o : set) {
@@ -124,14 +118,14 @@ public void nonUniqueGetTest() {
   }
 
   /**
-   * Tests the {@link IndexedSet#getByField(String, Object)} method.
+   * Tests the {@link IndexedSet#getByField(IndexDefinition, Object)} method.
    */
   @Test
   public void uniqueGetTest() {
     for (int i = 0; i < 9; i++) {
-      Set set = mSet.getByField(LONG_INDEX_NAME, i);
+      Set set = mSet.getByField(mUniqueLongIndex, i);
       Assert.assertEquals(0, set.size()); // i is integer, must be in the same type
-      set = mSet.getByField(LONG_INDEX_NAME, (long) i);
+      set = mSet.getByField(mUniqueLongIndex, (long) i);
       Assert.assertEquals(1, set.size());
       Assert.assertEquals(i / 3, set.iterator().next().intValue());
     }
@@ -142,13 +136,13 @@ public void uniqueGetTest() {
    */
   @Test
   public void removeTest() {
-    Pair toRemove = mSet.getFirstByField(LONG_INDEX_NAME, 1L);
-    Assert.assertEquals(1, mSet.getByField(LONG_INDEX_NAME, toRemove.longValue()).size());
+    Pair toRemove = mSet.getFirstByField(mUniqueLongIndex, 1L);
+    Assert.assertEquals(1, mSet.getByField(mUniqueLongIndex, toRemove.longValue()).size());
     Assert.assertEquals(9, mSet.size());
     Assert.assertTrue(mSet.remove(toRemove));
     Assert.assertEquals(8, mSet.size());
-    Assert.assertEquals(2, mSet.getByField(INT_INDEX_NAME, toRemove.intValue()).size());
-    Assert.assertEquals(0, mSet.getByField(LONG_INDEX_NAME, toRemove.longValue()).size());
+    Assert.assertEquals(2, mSet.getByField(mNonUniqueIntIndex, toRemove.intValue()).size());
+    Assert.assertEquals(0, mSet.getByField(mUniqueLongIndex, toRemove.longValue()).size());
   }
 
   /**
@@ -158,41 +152,41 @@ public void removeTest() {
   @Test
   public void removeNonExistTest() {
     Assert.assertFalse(mSet.remove(new Pair(-1, -1)));
-    Assert.assertEquals(0, mSet.removeByField(INT_INDEX_NAME, -1));
-    Assert.assertEquals(0, mSet.removeByField(LONG_INDEX_NAME, -1L));
+    Assert.assertEquals(0, mSet.removeByField(mNonUniqueIntIndex, -1));
+    Assert.assertEquals(0, mSet.removeByField(mUniqueLongIndex, -1L));
   }
 
   /**
-   * Tests the {@link IndexedSet#removeByField(String, Object)} method.
+   * Tests the {@link IndexedSet#removeByField(IndexDefinition, Object)} method.
    */
   @Test
   public void nonUniqueRemoveByFieldTest() {
-    Assert.assertEquals(3, mSet.getByField(INT_INDEX_NAME, 1).size());
+    Assert.assertEquals(3, mSet.getByField(mNonUniqueIntIndex, 1).size());
     Assert.assertEquals(9, mSet.size());
-    Assert.assertEquals(3, mSet.removeByField(INT_INDEX_NAME, 1));
+    Assert.assertEquals(3, mSet.removeByField(mNonUniqueIntIndex, 1));
     Assert.assertEquals(6, mSet.size());
-    Assert.assertEquals(0, mSet.getByField(INT_INDEX_NAME, 1).size());
-    Assert.assertEquals(3, mSet.getByField(INT_INDEX_NAME, 0).size());
-    Assert.assertEquals(3, mSet.getByField(INT_INDEX_NAME, 2).size());
+    Assert.assertEquals(0, mSet.getByField(mNonUniqueIntIndex, 1).size());
+    Assert.assertEquals(3, mSet.getByField(mNonUniqueIntIndex, 0).size());
+    Assert.assertEquals(3, mSet.getByField(mNonUniqueIntIndex, 2).size());
     for (long l = 3; l < 6; l++) {
-      Assert.assertEquals(0, mSet.getByField(LONG_INDEX_NAME, l).size());
+      Assert.assertEquals(0, mSet.getByField(mUniqueLongIndex, l).size());
     }
   }
 
   /**
-   * Tests the {@link IndexedSet#removeByField(String, Object)} method.
+   * Tests the {@link IndexedSet#removeByField(IndexDefinition, Object)} method.
    */
   @Test
   public void uniqueRemoveByFieldTest() {
     Assert.assertEquals(9, mSet.size());
-    Assert.assertEquals(1, mSet.removeByField(LONG_INDEX_NAME, 1L));
+    Assert.assertEquals(1, mSet.removeByField(mUniqueLongIndex, 1L));
     Assert.assertEquals(8, mSet.size());
-    Assert.assertEquals(0, mSet.removeByField(LONG_INDEX_NAME, 1L));
+    Assert.assertEquals(0, mSet.removeByField(mUniqueLongIndex, 1L));
     Assert.assertEquals(8, mSet.size());
-    Assert.assertEquals(0, mSet.getByField(LONG_INDEX_NAME, 1L).size());
-    Assert.assertEquals(1, mSet.getByField(LONG_INDEX_NAME, 0L).size());
-    Assert.assertEquals(1, mSet.getByField(LONG_INDEX_NAME, 2L).size());
-    Assert.assertEquals(2, mSet.getByField(INT_INDEX_NAME, 0).size());
+    Assert.assertEquals(0, mSet.getByField(mUniqueLongIndex, 1L).size());
+    Assert.assertEquals(1, mSet.getByField(mUniqueLongIndex, 0L).size());
+    Assert.assertEquals(1, mSet.getByField(mUniqueLongIndex, 2L).size());
+    Assert.assertEquals(2, mSet.getByField(mNonUniqueIntIndex, 0).size());
   }
 
   /**
@@ -203,14 +197,14 @@ public void addTheSameObjectMultipleTimesTest() {
     final ExpectedException exception = ExpectedException.none();
     for (int i = 0; i < 3; i++) {
       Assert.assertEquals(9, mSet.size());
-      Assert.assertEquals(3, mSet.getByField(INT_INDEX_NAME, i).size());
-      for (Pair p : mSet.getByField(INT_INDEX_NAME, i)) {
+      Assert.assertEquals(3, mSet.getByField(mNonUniqueIntIndex, i).size());
+      for (Pair p : mSet.getByField(mNonUniqueIntIndex, i)) {
         exception.expect(IllegalStateException.class);
         exception.expectMessage("Adding more than one value to a unique index.");
         mSet.add(p);
       }
       Assert.assertEquals(9, mSet.size());
-      Assert.assertEquals(3, mSet.getByField(INT_INDEX_NAME, i).size());
+      Assert.assertEquals(3, mSet.getByField(mNonUniqueIntIndex, i).size());
     }
     try {
       mSet.add(new Pair(1 , 9L));
@@ -218,7 +212,7 @@ public void addTheSameObjectMultipleTimesTest() {
       Assert.assertTrue(true);
     }
     Assert.assertEquals(10, mSet.size());
-    Assert.assertEquals(4, mSet.getByField(INT_INDEX_NAME, 1).size());
+    Assert.assertEquals(4, mSet.getByField(mNonUniqueIntIndex, 1).size());
   }
 
   /**
@@ -230,10 +224,10 @@ public void iteratorRemoveTest() {
     Iterator it =  mSet.iterator();
     Assert.assertTrue(it.hasNext());
     final Pair first = it.next();
-    Set allWithSameIntValue = mSet.getByField(INT_INDEX_NAME, first.intValue());
+    Set allWithSameIntValue = mSet.getByField(mNonUniqueIntIndex, first.intValue());
     Assert.assertTrue("Element should be in the set", allWithSameIntValue.contains(first));
     it.remove();
-    allWithSameIntValue = mSet.getByField(INT_INDEX_NAME, first.intValue());
+    allWithSameIntValue = mSet.getByField(mNonUniqueIntIndex, first.intValue());
     Assert.assertFalse("Element should not be in the set", allWithSameIntValue.contains(first));
   }
 }
diff --git a/core/server/src/main/java/alluxio/master/block/BlockMaster.java b/core/server/src/main/java/alluxio/master/block/BlockMaster.java
index 9a8b29638228..4578c8bb595d 100644
--- a/core/server/src/main/java/alluxio/master/block/BlockMaster.java
+++ b/core/server/src/main/java/alluxio/master/block/BlockMaster.java
@@ -84,9 +84,6 @@ public final class BlockMaster extends AbstractMaster implements ContainerIdGene
    */
   private static final long CONTAINER_ID_RESERVATION_SIZE = 1000;
 
-  static final String ID_INDEX_NAME = "IdIndex";
-  static final String ADDRESS_INDEX_NAME = "AddressIndex";
-
   /**
    * Concurrency and locking in the BlockMaster
    *
@@ -118,21 +115,21 @@ public final class BlockMaster extends AbstractMaster implements ContainerIdGene
       new BlockContainerIdGenerator();
 
   // Worker metadata management.
-  private final IndexDefinition mIdIndex = new IndexDefinition<>(ID_INDEX_NAME,
-      true, new IndexDefinition.Abstracter() {
+  private final IndexDefinition mIdIndex = new
+      IndexDefinition(true) {
         @Override
         public Object getFieldValue(MasterWorkerInfo o) {
           return o.getId();
         }
-      });
+      };
 
-  private final IndexDefinition mAddressIndex = new IndexDefinition<>(
-      ADDRESS_INDEX_NAME, true, new IndexDefinition.Abstracter() {
+  private final IndexDefinition mAddressIndex = new
+      IndexDefinition(true) {
         @Override
         public Object getFieldValue(MasterWorkerInfo o) {
           return o.getWorkerAddress();
         }
-      });
+      };
 
   /**
    * Mapping between all possible storage level aliases and their ordinal position. This mapping
@@ -347,7 +344,7 @@ public void removeBlocks(List blockIds, boolean delete) {
       // Outside of locking the block. This does not have to be synchronized with the block
       // metadata, since it is essentially an asynchronous signal to the worker to remove the block.
       for (long workerId : workerIds) {
-        MasterWorkerInfo worker = mWorkers.getFirstByField(ID_INDEX_NAME, workerId);
+        MasterWorkerInfo worker = mWorkers.getFirstByField(mIdIndex, workerId);
         if (worker != null) {
           synchronized (worker) {
             worker.updateToRemovedBlock(true, blockId);
@@ -416,7 +413,7 @@ public void commitBlock(long workerId, long usedBytesOnTier, String tierAlias, l
 
     long counter = AsyncJournalWriter.INVALID_FLUSH_COUNTER;
 
-    MasterWorkerInfo worker = mWorkers.getFirstByField(ID_INDEX_NAME, workerId);
+    MasterWorkerInfo worker = mWorkers.getFirstByField(mIdIndex, workerId);
     // TODO(peis): Check lost workers as well.
     if (worker == null) {
       throw new NoWorkerException(ExceptionMessage.NO_WORKER_FOUND.getMessage(workerId));
@@ -582,7 +579,7 @@ public Map getUsedBytesOnTiers() {
   public long getWorkerId(WorkerNetAddress workerNetAddress) {
     // TODO(gpang): This NetAddress cloned in case thrift re-uses the object. Does thrift re-use it?
     MasterWorkerInfo existingWorker =
-        mWorkers.getFirstByField(ADDRESS_INDEX_NAME, workerNetAddress);
+        mWorkers.getFirstByField(mAddressIndex, workerNetAddress);
     if (existingWorker != null) {
       // This worker address is already mapped to a worker id.
       long oldWorkerId = existingWorker.getId();
@@ -591,7 +588,7 @@ public long getWorkerId(WorkerNetAddress workerNetAddress) {
     }
 
     MasterWorkerInfo lostWorker =
-        mLostWorkers.getFirstByField(ADDRESS_INDEX_NAME, workerNetAddress);
+        mLostWorkers.getFirstByField(mAddressIndex, workerNetAddress);
     if (lostWorker != null) {
       // this is one of the lost workers
       synchronized (lostWorker) {
@@ -628,7 +625,7 @@ public long getWorkerId(WorkerNetAddress workerNetAddress) {
   public void workerRegister(long workerId, List storageTiers,
       Map totalBytesOnTiers, Map usedBytesOnTiers,
       Map> currentBlocksOnTiers) throws NoWorkerException {
-    MasterWorkerInfo worker = mWorkers.getFirstByField(ID_INDEX_NAME, workerId);
+    MasterWorkerInfo worker = mWorkers.getFirstByField(mIdIndex, workerId);
     if (worker == null) {
       throw new NoWorkerException(ExceptionMessage.NO_WORKER_FOUND.getMessage(workerId));
     }
@@ -662,7 +659,7 @@ public void workerRegister(long workerId, List storageTiers,
    */
   public Command workerHeartbeat(long workerId, Map usedBytesOnTiers,
       List removedBlockIds, Map> addedBlocksOnTiers) {
-    MasterWorkerInfo worker = mWorkers.getFirstByField(ID_INDEX_NAME, workerId);
+    MasterWorkerInfo worker = mWorkers.getFirstByField(mIdIndex, workerId);
     if (worker == null) {
       LOG.warn("Could not find worker id: {} for heartbeat.", workerId);
       return new Command(CommandType.Register, new ArrayList());
@@ -773,7 +770,7 @@ public int compare(MasterBlockLocation o1, MasterBlockLocation o2) {
     });
     for (MasterBlockLocation masterBlockLocation : blockLocations) {
       MasterWorkerInfo workerInfo =
-          mWorkers.getFirstByField(ID_INDEX_NAME, masterBlockLocation.getWorkerId());
+          mWorkers.getFirstByField(mIdIndex, masterBlockLocation.getWorkerId());
       if (workerInfo != null) {
         // worker metadata is intentionally not locked here because:
         // - it would be an incorrect order (correct order is lock worker first, then block)
diff --git a/core/server/src/main/java/alluxio/master/file/meta/InodeDirectory.java b/core/server/src/main/java/alluxio/master/file/meta/InodeDirectory.java
index de883aa0de61..04692bd8ff51 100644
--- a/core/server/src/main/java/alluxio/master/file/meta/InodeDirectory.java
+++ b/core/server/src/main/java/alluxio/master/file/meta/InodeDirectory.java
@@ -34,27 +34,19 @@
  */
 @NotThreadSafe
 public final class InodeDirectory extends Inode {
-  /** Name of Id Index. */
-  private static final String ID_INDEX_NAME = "IdIndex";
-
-  /** Name of Name Index. */
-  private static final String NAME_INDEX_NAME = "NameIndex";
-
-  private final IndexDefinition> mIdIndex =
-      new IndexDefinition<>(ID_INDEX_NAME, true, new IndexDefinition.Abstracter>() {
-        @Override
-        public Object getFieldValue(Inode o) {
-          return o.getId();
-        }
-      });
+  private final IndexDefinition> mIdIndex = new IndexDefinition>(true) {
+    @Override
+    public Object getFieldValue(Inode o) {
+      return o.getId();
+    }
+  };
 
-  private final IndexDefinition> mNameIndex =
-      new IndexDefinition<>(NAME_INDEX_NAME, true, new IndexDefinition.Abstracter>() {
-        @Override
-        public Object getFieldValue(Inode o) {
-          return o.getName();
-        }
-      });
+  private final IndexDefinition> mNameIndex = new IndexDefinition>(true) {
+    @Override
+    public Object getFieldValue(Inode o) {
+      return o.getName();
+    }
+  };
 
   @SuppressWarnings("unchecked")
   private IndexedSet> mChildren = new IndexedSet<>(mIdIndex, mNameIndex);
@@ -100,7 +92,7 @@ public void addChild(Inode child) {
    * @return the inode with the given id, or null if there is no child with that id
    */
   public Inode getChild(long id) {
-    return mChildren.getFirstByField(ID_INDEX_NAME, id);
+    return mChildren.getFirstByField(mIdIndex, id);
   }
 
   /**
@@ -108,7 +100,7 @@ public Inode getChild(long id) {
    * @return the inode with the given name, or null if there is no child with that name
    */
   public Inode getChild(String name) {
-    return mChildren.getFirstByField(NAME_INDEX_NAME, name);
+    return mChildren.getFirstByField(mNameIndex, name);
   }
 
   /**
@@ -167,7 +159,7 @@ public boolean removeChild(Inode child) {
    * @return true if the inode was removed, false otherwise
    */
   public boolean removeChild(String name) {
-    return mChildren.removeByField(NAME_INDEX_NAME, name) == 0;
+    return mChildren.removeByField(mNameIndex, name) == 0;
   }
 
   /**
diff --git a/core/server/src/main/java/alluxio/master/file/meta/InodeTree.java b/core/server/src/main/java/alluxio/master/file/meta/InodeTree.java
index 4fa0aea8c38d..22e20e2bc79d 100644
--- a/core/server/src/main/java/alluxio/master/file/meta/InodeTree.java
+++ b/core/server/src/main/java/alluxio/master/file/meta/InodeTree.java
@@ -87,22 +87,18 @@ public enum LockMode {
   private static final String ROOT_INODE_NAME = "";
   /** Number of retries when trying to lock a path, from a given id. */
   private static final int PATH_TRAVERSAL_RETRIES = 1000;
-  /** Name of IdIndex. */
-  private static final String ID_INDEX_NAME = "IdIndex";
-
   /** The root of the entire file system. */
   private InodeDirectory mRoot = null;
 
   /** Mount table manages the file system mount points. */
   private final MountTable mMountTable;
 
-  private final IndexDefinition> mIdIndex =
-      new IndexDefinition<>(ID_INDEX_NAME, true, new IndexDefinition.Abstracter>() {
-        @Override
-        public Object getFieldValue(Inode o) {
-          return o.getId();
-        }
-      });
+  private final IndexDefinition> mIdIndex = new IndexDefinition>(true) {
+    @Override
+    public Object getFieldValue(Inode o) {
+      return o.getId();
+    }
+  };
 
   @SuppressWarnings("unchecked")
   private final IndexedSet> mInodes = new IndexedSet<>(mIdIndex);
@@ -184,7 +180,7 @@ public int getPinnedSize() {
    * @return whether the inode exists
    */
   public boolean inodeIdExists(long id) {
-    return mInodes.getFirstByField(ID_INDEX_NAME, id) != null;
+    return mInodes.getFirstByField(mIdIndex, id) != null;
   }
 
   /**
@@ -350,7 +346,7 @@ public LockedInodePath lockFullInodePath(long id, LockMode lockMode)
       throws FileDoesNotExistException {
     int count = 0;
     while (true) {
-      Inode inode = mInodes.getFirstByField(ID_INDEX_NAME, id);
+      Inode inode = mInodes.getFirstByField(mIdIndex, id);
       if (inode == null) {
         throw new FileDoesNotExistException(ExceptionMessage.INODE_DOES_NOT_EXIST.getMessage(id));
       }
@@ -427,7 +423,7 @@ private void computePathForInode(Inode inode, StringBuilder builder)
       builder.append(AlluxioURI.SEPARATOR);
       builder.append(name);
     } else {
-      Inode parentInode = mInodes.getFirstByField(ID_INDEX_NAME, parentId);
+      Inode parentInode = mInodes.getFirstByField(mIdIndex, parentId);
       if (parentInode == null) {
         throw new FileDoesNotExistException(
             ExceptionMessage.INODE_DOES_NOT_EXIST.getMessage(parentId));
@@ -708,7 +704,7 @@ public void deleteInode(LockedInodePath inodePath, long opTimeMs)
       throws FileDoesNotExistException {
     Inode inode = inodePath.getInode();
     InodeDirectory parent =
-        (InodeDirectory) mInodes.getFirstByField(ID_INDEX_NAME, inode.getParentId());
+        (InodeDirectory) mInodes.getFirstByField(mIdIndex, inode.getParentId());
     if (parent == null) {
       throw new FileDoesNotExistException(
           ExceptionMessage.INODE_DOES_NOT_EXIST.getMessage(inode.getParentId()));
@@ -858,7 +854,7 @@ private void addInodeFromJournalInternal(Inode inode) {
     InodeDirectory parentDirectory = mCachedInode;
     if (inode.getParentId() != mCachedInode.getId()) {
       parentDirectory =
-          (InodeDirectory) mInodes.getFirstByField(ID_INDEX_NAME, inode.getParentId());
+          (InodeDirectory) mInodes.getFirstByField(mIdIndex, inode.getParentId());
       mCachedInode = parentDirectory;
     }
     parentDirectory.addChild(inode);
diff --git a/core/server/src/main/java/alluxio/worker/file/UnderFileSystemManager.java b/core/server/src/main/java/alluxio/worker/file/UnderFileSystemManager.java
index 2a620df23c86..bf80154e7172 100644
--- a/core/server/src/main/java/alluxio/worker/file/UnderFileSystemManager.java
+++ b/core/server/src/main/java/alluxio/worker/file/UnderFileSystemManager.java
@@ -54,17 +54,41 @@
 public final class UnderFileSystemManager {
   private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);
 
-  private static final String INPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME =
-      "InputStreamAgentSessionIdIndex";
-
-  private static final String INPUT_STREAM_AGENT_ID_INDEX_NAME =
-      "inputStreamAgentIdIndex";
-
-  private static final String OUTPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME =
-      "outputStreamAgentSessionIdIndex";
-
-  private static final String OUTPUT_STREAM_AGENT_ID_INDEX_NAME =
-      "outputStreamAgentIdIndex";
+  // Input stream agent session index
+  private final IndexDefinition mInputStreamAgentSessionIdIndex =
+      new IndexDefinition(false) {
+        @Override
+        public Object getFieldValue(InputStreamAgent o) {
+          return o.mSessionId;
+        }
+      };
+
+  // Input stream agent id index
+  private final IndexDefinition mInputStreamAgentIdIndex =
+      new IndexDefinition(true) {
+        @Override
+        public Object getFieldValue(InputStreamAgent o) {
+          return o.mAgentId;
+        }
+      };
+
+  // Output stream agent session index
+  private final IndexDefinition mOutputStreamAgentSessionIdIndex =
+      new IndexDefinition(false) {
+        @Override
+        public Object getFieldValue(OutputStreamAgent o) {
+          return o.mSessionId;
+        }
+      };
+
+  // Output stream agent id index
+  private final IndexDefinition mOutputStreamAgentIdIndex =
+      new IndexDefinition(true) {
+        @Override
+        public Object getFieldValue(OutputStreamAgent o) {
+          return o.mAgentId;
+        }
+      };
 
   /** A random id generator for worker file ids. */
   private final AtomicLong mIdGenerator;
@@ -80,50 +104,11 @@ public final class UnderFileSystemManager {
    * manager.
    */
   public UnderFileSystemManager() {
-
-    // Input stream agent session index
-    IndexDefinition inputStreamAgentSessionIdIndex =
-        new IndexDefinition<>(INPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME, false,
-            new IndexDefinition.Abstracter() {
-            @Override
-            public Object getFieldValue(InputStreamAgent o) {
-              return o.mSessionId;
-            }
-          });
-
-    // Input stream agent id index
-    IndexDefinition inputStreamAgentIdIndex = new IndexDefinition<>(
-        INPUT_STREAM_AGENT_ID_INDEX_NAME, true, new IndexDefinition.Abstracter() {
-          @Override
-          public Object getFieldValue(InputStreamAgent o) {
-            return o.mAgentId;
-          }
-        });
-
-    // Output stream agent session index
-    IndexDefinition outputStreamAgentSessionIdIndex =
-        new IndexDefinition<>(OUTPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME, false,
-            new IndexDefinition.Abstracter() {
-            @Override
-            public Object getFieldValue(OutputStreamAgent o) {
-              return o.mSessionId;
-            }
-          });
-
-    // Output stream agent id index
-    IndexDefinition outputStreamAgentIdIndex =
-        new IndexDefinition<>(OUTPUT_STREAM_AGENT_ID_INDEX_NAME, true,
-            new IndexDefinition.Abstracter() {
-            @Override
-            public Object getFieldValue(OutputStreamAgent o) {
-              return o.mAgentId;
-            }
-          });
     mIdGenerator = new AtomicLong(IdUtils.getRandomNonNegativeLong());
     mInputStreamAgents =
-        new IndexedSet<>(inputStreamAgentSessionIdIndex, inputStreamAgentIdIndex);
+        new IndexedSet<>(mInputStreamAgentSessionIdIndex, mInputStreamAgentIdIndex);
     mOutputStreamAgents =
-        new IndexedSet<>(outputStreamAgentSessionIdIndex, outputStreamAgentIdIndex);
+        new IndexedSet<>(mOutputStreamAgentSessionIdIndex, mOutputStreamAgentIdIndex);
   }
 
   /**
@@ -374,7 +359,7 @@ public void cancelFile(long sessionId, long tempUfsFileId)
       throws FileDoesNotExistException, IOException {
     OutputStreamAgent agent;
     synchronized (mOutputStreamAgents) {
-      agent = mOutputStreamAgents.getFirstByField(OUTPUT_STREAM_AGENT_ID_INDEX_NAME, tempUfsFileId);
+      agent = mOutputStreamAgents.getFirstByField(mOutputStreamAgentIdIndex, tempUfsFileId);
       if (agent == null) {
         throw new FileDoesNotExistException(
             ExceptionMessage.BAD_WORKER_FILE_ID.getMessage(tempUfsFileId));
@@ -397,8 +382,8 @@ public void cleanupSession(long sessionId) {
     synchronized (mInputStreamAgents) {
       toClose =
           new HashSet<>(
-              mInputStreamAgents.getByField(INPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME, sessionId));
-      mInputStreamAgents.removeByField(INPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME, sessionId);
+              mInputStreamAgents.getByField(mInputStreamAgentSessionIdIndex, sessionId));
+      mInputStreamAgents.removeByField(mInputStreamAgentSessionIdIndex, sessionId);
     }
     // close is done outside of the synchronized block since it may be expensive
     for (InputStreamAgent agent : toClose) {
@@ -412,8 +397,8 @@ public void cleanupSession(long sessionId) {
     Set toCancel;
     synchronized (mOutputStreamAgents) {
       toCancel = new HashSet<>(
-          mOutputStreamAgents.getByField(OUTPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME, sessionId));
-      mOutputStreamAgents.removeByField(OUTPUT_STREAM_AGENT_SESSION_ID_INDEX_NAME, sessionId);
+          mOutputStreamAgents.getByField(mOutputStreamAgentSessionIdIndex, sessionId));
+      mOutputStreamAgents.removeByField(mOutputStreamAgentSessionIdIndex, sessionId);
     }
     // cancel is done outside of the synchronized block since it may be expensive
     for (OutputStreamAgent agent : toCancel) {
@@ -438,7 +423,7 @@ public void closeFile(long sessionId, long tempUfsFileId)
       throws FileDoesNotExistException, IOException {
     InputStreamAgent agent;
     synchronized (mInputStreamAgents) {
-      agent = mInputStreamAgents.getFirstByField(INPUT_STREAM_AGENT_ID_INDEX_NAME, tempUfsFileId);
+      agent = mInputStreamAgents.getFirstByField(mInputStreamAgentIdIndex, tempUfsFileId);
       if (agent == null) {
         throw new FileDoesNotExistException(
             ExceptionMessage.BAD_WORKER_FILE_ID.getMessage(tempUfsFileId));
@@ -468,7 +453,7 @@ public long completeFile(long sessionId, long tempUfsFileId, String user, String
       throws FileDoesNotExistException, IOException {
     OutputStreamAgent agent;
     synchronized (mOutputStreamAgents) {
-      agent = mOutputStreamAgents.getFirstByField(OUTPUT_STREAM_AGENT_ID_INDEX_NAME, tempUfsFileId);
+      agent = mOutputStreamAgents.getFirstByField(mOutputStreamAgentIdIndex, tempUfsFileId);
       if (agent == null) {
         throw new FileDoesNotExistException(
             ExceptionMessage.BAD_WORKER_FILE_ID.getMessage(tempUfsFileId));
@@ -493,7 +478,7 @@ public InputStream getInputStreamAtPosition(long tempUfsFileId, long position)
       throws FileDoesNotExistException, IOException {
     InputStreamAgent agent;
     synchronized (mInputStreamAgents) {
-      agent = mInputStreamAgents.getFirstByField(INPUT_STREAM_AGENT_ID_INDEX_NAME, tempUfsFileId);
+      agent = mInputStreamAgents.getFirstByField(mInputStreamAgentIdIndex, tempUfsFileId);
     }
     if (agent == null) {
       throw new FileDoesNotExistException(
@@ -510,7 +495,7 @@ public InputStream getInputStreamAtPosition(long tempUfsFileId, long position)
   public OutputStream getOutputStream(long tempUfsFileId) throws FileDoesNotExistException {
     OutputStreamAgent agent;
     synchronized (mOutputStreamAgents) {
-      agent = mOutputStreamAgents.getFirstByField(OUTPUT_STREAM_AGENT_ID_INDEX_NAME, tempUfsFileId);
+      agent = mOutputStreamAgents.getFirstByField(mOutputStreamAgentIdIndex, tempUfsFileId);
     }
     if (agent == null) {
       throw new FileDoesNotExistException(
diff --git a/core/server/src/test/java/alluxio/master/block/BlockMasterPrivateAccess.java b/core/server/src/test/java/alluxio/master/block/BlockMasterPrivateAccess.java
index ccee6ab7ee2d..3c3b0d04980a 100644
--- a/core/server/src/test/java/alluxio/master/block/BlockMasterPrivateAccess.java
+++ b/core/server/src/test/java/alluxio/master/block/BlockMasterPrivateAccess.java
@@ -11,6 +11,7 @@
 
 package alluxio.master.block;
 
+import alluxio.collections.IndexDefinition;
 import alluxio.collections.IndexedSet;
 import alluxio.master.block.meta.MasterWorkerInfo;
 
@@ -29,8 +30,9 @@ public final class BlockMasterPrivateAccess {
    */
   public static boolean isWorkerRegistered(BlockMaster master, long workerId) {
     IndexedSet workers = Whitebox.getInternalState(master, "mWorkers");
+    IndexDefinition idIndex = Whitebox.getInternalState(master, "mIdIndex");
     synchronized (workers) {
-      MasterWorkerInfo workerInfo = workers.getFirstByField(master.ID_INDEX_NAME, workerId);
+      MasterWorkerInfo workerInfo = workers.getFirstByField(idIndex, workerId);
       return workerInfo != null && workerInfo.isRegistered();
     }
   }
diff --git a/core/server/src/test/java/alluxio/master/block/BlockMasterTest.java b/core/server/src/test/java/alluxio/master/block/BlockMasterTest.java
index ee216bfc5bca..9ce2f3500dc2 100644
--- a/core/server/src/test/java/alluxio/master/block/BlockMasterTest.java
+++ b/core/server/src/test/java/alluxio/master/block/BlockMasterTest.java
@@ -11,6 +11,7 @@
 
 package alluxio.master.block;
 
+import alluxio.collections.IndexDefinition;
 import alluxio.collections.IndexedSet;
 import alluxio.exception.AlluxioException;
 import alluxio.exception.BlockInfoException;
@@ -381,13 +382,13 @@ private void addWorker(BlockMaster master, long workerId, List storageTi
   /** Private access to {@link BlockMaster} internals. */
   private class PrivateAccess {
     private final Map mBlocks;
-    private final String mIdIndexName;
+    private final IndexDefinition mIdIndex;
     private final IndexedSet mLostWorkers;
     private final IndexedSet mWorkers;
 
     PrivateAccess(BlockMaster blockMaster) {
-      mIdIndexName = mMaster.ID_INDEX_NAME;
       mBlocks = Whitebox.getInternalState(mMaster, "mBlocks");
+      mIdIndex = Whitebox.getInternalState(mMaster, "mIdIndex");
       mLostWorkers = Whitebox.getInternalState(mMaster, "mLostWorkers");
       mWorkers = Whitebox.getInternalState(mMaster, "mWorkers");
     }
@@ -409,7 +410,7 @@ private void addLostWorker(MasterWorkerInfo worker) {
      */
     private MasterWorkerInfo getWorkerById(long workerId) {
       synchronized (mWorkers) {
-        return mWorkers.getFirstByField(mIdIndexName, workerId);
+        return mWorkers.getFirstByField(mIdIndex, workerId);
       }
     }
 
diff --git a/tests/src/test/java/alluxio/collections/IndexedSetConcurrencyTest.java b/tests/src/test/java/alluxio/collections/IndexedSetConcurrencyTest.java
index 806865df28f1..cd27731f015b 100644
--- a/tests/src/test/java/alluxio/collections/IndexedSetConcurrencyTest.java
+++ b/tests/src/test/java/alluxio/collections/IndexedSetConcurrencyTest.java
@@ -80,7 +80,7 @@ private class ConcurrentRemove extends ConcurrentTask {
     @Override
     public long runSingleTask() {
       TestInfo info =
-          mIndexedSet.getFirstByField(SIZE_INDEX_NAME,
+          mIndexedSet.getFirstByField(mSizeIndex,
               ThreadLocalRandom.current().nextInt(0, MAX_SIZE));
       if (info != null) {
         return mIndexedSet.remove(info) ? 1 : 0;
@@ -93,7 +93,7 @@ private class ConcurrentRemoveByField extends ConcurrentTask {
     @Override
     public long runSingleTask() {
       return mIndexedSet
-          .removeByField(SIZE_INDEX_NAME, ThreadLocalRandom.current().nextInt(0, MAX_SIZE));
+          .removeByField(mSizeIndex, ThreadLocalRandom.current().nextInt(0, MAX_SIZE));
     }
   }
 
@@ -137,24 +137,19 @@ public int getSize() {
     }
   }
 
-  private static final String ID_INDEX_NAME = "IdIndex";
-  private static final String SIZE_INDEX_NAME = "SizeIndex";
-
-  private final IndexDefinition mIdIndex =
-      new IndexDefinition<>(ID_INDEX_NAME, true, new IndexDefinition.Abstracter() {
-        @Override
-        public Object getFieldValue(TestInfo o) {
-          return o.getId();
-        }
-      });
-
-  private final IndexDefinition mSizeIndex =
-      new IndexDefinition<>(SIZE_INDEX_NAME, false, new IndexDefinition.Abstracter() {
-        @Override
-        public Object getFieldValue(TestInfo o) {
-          return o.getSize();
-        }
-      });
+  private final IndexDefinition mIdIndex = new IndexDefinition(true) {
+    @Override
+    public Object getFieldValue(TestInfo o) {
+      return o.getId();
+    }
+  };
+
+  private final IndexDefinition mSizeIndex = new IndexDefinition(false) {
+    @Override
+    public Object getFieldValue(TestInfo o) {
+      return o.getSize();
+    }
+  };
 
   @Before
   public void before() throws Exception {
@@ -189,7 +184,7 @@ private void verifySet() {
     // Verify the size according to the id index.
     int count = 0;
     for (Long id : ids) {
-      Set elements = mIndexedSet.getByField(ID_INDEX_NAME, id);
+      Set elements = mIndexedSet.getByField(mIdIndex, id);
       count += elements.size();
     }
     Assert.assertEquals(expectedCount, count);
@@ -197,7 +192,7 @@ private void verifySet() {
     // Verify the size according to the size index.
     count = 0;
     for (Integer size : sizes) {
-      Set elements = mIndexedSet.getByField(SIZE_INDEX_NAME, size);
+      Set elements = mIndexedSet.getByField(mSizeIndex, size);
       count += elements.size();
     }
     Assert.assertEquals(expectedCount, count);