Skip to content

Commit

Permalink
simplify initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
xhad1234 committed Jun 18, 2016
1 parent f2d1ad4 commit c6781fc
Showing 1 changed file with 9 additions and 15 deletions.
24 changes: 9 additions & 15 deletions core/common/src/main/java/alluxio/collections/IndexedSet.java
Expand Up @@ -12,8 +12,10 @@
package alluxio.collections; package alluxio.collections;


import com.google.common.base.Preconditions; import com.google.common.base.Preconditions;
import com.google.common.collect.Iterables;


import java.util.AbstractSet; import java.util.AbstractSet;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
Expand Down Expand Up @@ -137,7 +139,7 @@ public interface FieldIndex<T> {
} }


/** /**
* An interface extending {@link FieldIndex}, represents a unique index. A Unique index is an * An interface extending {@link FieldIndex}, represents a unique index. A unique index is an
* index where each index value only maps to one object. * index where each index value only maps to one object.
* *
* @param <T> type of objects in this {@link IndexedSet} * @param <T> type of objects in this {@link IndexedSet}
Expand Down Expand Up @@ -166,13 +168,10 @@ public IndexedSet(FieldIndex<T> field, FieldIndex<T>... otherFields) {
int uniqueIndexLength = 0; int uniqueIndexLength = 0;
int nonUniqueIndexLength = 0; int nonUniqueIndexLength = 0;


if (field instanceof UniqueFieldIndex) { Iterable<FieldIndex<T>> fields =
uniqueIndexLength = 1; Iterables.concat(Arrays.asList(field), Arrays.asList(otherFields));
} else {
nonUniqueIndexLength = 1;
}


for (FieldIndex<T> fieldIndex : otherFields) { for (FieldIndex<T> fieldIndex : fields) {
if (fieldIndex instanceof UniqueFieldIndex) { if (fieldIndex instanceof UniqueFieldIndex) {
uniqueIndexLength++; uniqueIndexLength++;
} else { } else {
Expand All @@ -184,21 +183,15 @@ public IndexedSet(FieldIndex<T> field, FieldIndex<T>... otherFields) {
Map<NonUniqueFieldIndex<T>, ConcurrentHashMap<Object, ConcurrentHashSet<T>>> indexMapNonUnique = Map<NonUniqueFieldIndex<T>, ConcurrentHashMap<Object, ConcurrentHashSet<T>>> indexMapNonUnique =
null; null;
Map<UniqueFieldIndex<T>, ConcurrentHashMap<Object, T>> indexMapUnique = null; Map<UniqueFieldIndex<T>, ConcurrentHashMap<Object, T>> indexMapUnique = null;

if (uniqueIndexLength > 0) { if (uniqueIndexLength > 0) {
indexMapUnique = new HashMap<>(uniqueIndexLength); indexMapUnique = new HashMap<>(uniqueIndexLength);
} }
if (nonUniqueIndexLength > 0) { if (nonUniqueIndexLength > 0) {
indexMapNonUnique = new HashMap<>(nonUniqueIndexLength); indexMapNonUnique = new HashMap<>(nonUniqueIndexLength);
} }


if (field instanceof UniqueFieldIndex) { for (FieldIndex<T> fieldIndex : fields) {
indexMapUnique.put((UniqueFieldIndex) field, new ConcurrentHashMap<Object, T>(8, 0.95f, 8));
} else {
indexMapNonUnique.put((NonUniqueFieldIndex<T>) field,
new ConcurrentHashMap<Object, ConcurrentHashSet<T>>(8, 0.95f, 8));
}

for (FieldIndex<T> fieldIndex : otherFields) {
if (fieldIndex instanceof UniqueFieldIndex) { if (fieldIndex instanceof UniqueFieldIndex) {
indexMapUnique.put( indexMapUnique.put(
(UniqueFieldIndex<T>) fieldIndex, new ConcurrentHashMap<Object, T>(8, 0.95f, 8)); (UniqueFieldIndex<T>) fieldIndex, new ConcurrentHashMap<Object, T>(8, 0.95f, 8));
Expand All @@ -208,6 +201,7 @@ public IndexedSet(FieldIndex<T> field, FieldIndex<T>... otherFields) {
new ConcurrentHashMap<Object, ConcurrentHashSet<T>>(8, 0.95f, 8)); new ConcurrentHashMap<Object, ConcurrentHashSet<T>>(8, 0.95f, 8));
} }
} }

// read only, so it is thread safe and allows concurrent access. // read only, so it is thread safe and allows concurrent access.
if (uniqueIndexLength > 0) { if (uniqueIndexLength > 0) {
mIndexMapUnique = Collections.unmodifiableMap(indexMapUnique); mIndexMapUnique = Collections.unmodifiableMap(indexMapUnique);
Expand Down

0 comments on commit c6781fc

Please sign in to comment.