Skip to content

Commit

Permalink
Let IndexedSet implement Iterable.
Browse files Browse the repository at this point in the history
  • Loading branch information
cc committed Aug 21, 2015
1 parent 9551cde commit 5e853d6
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 21 deletions.
22 changes: 11 additions & 11 deletions servers/src/main/java/tachyon/master/next/IndexedSet.java
Expand Up @@ -16,12 +16,12 @@
package tachyon.master.next;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.Set;
import java.util.HashSet;
import java.lang.reflect.Field;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -39,7 +39,7 @@
*
* This class is thread safe.
*/
public class IndexedSet<T> {
public class IndexedSet<T> implements Iterable<T> {
private static final Logger LOG = LoggerFactory.getLogger(Constants.LOGGER_TYPE);

/** All objects in the set */
Expand Down Expand Up @@ -112,16 +112,16 @@ public boolean add(T object) {
}

/**
* Return the set of all objects, the returned set is backed up by the internal set, any changes
* to the internal set are reflected in the returned set, and vice-versa.
* Returns an iterator over the elements in this set. The elements are returned in no particular
* order. It is to implement {@link Iterable} so that users can foreach the IndexedSet directly.
* The traversal may reflect any modifications subsequent to the construction of the iterator.
*
* @return a set of all objects
* @return an iterator over the elements in this IndexedSet
*/
// TODO(cc) Let this class implement Iterable to avoid this method, all use cases are to foreach
// this set.
public Set<T> all() {
@Override
public Iterator<T> iterator() {
synchronized (mLock) {
return mObjects;
return mObjects.iterator();
}
}

Expand Down
Expand Up @@ -96,7 +96,7 @@ public List<PeriodicTask> getPeriodicTaskList() {
public List<WorkerInfo> getWorkerInfoList() {
List<WorkerInfo> workerInfoList = new ArrayList<WorkerInfo>(mWorkers.size());
synchronized (mWorkers) {
for (MasterWorkerInfo masterWorkerInfo : mWorkers.all()) {
for (MasterWorkerInfo masterWorkerInfo : mWorkers) {
workerInfoList.add(masterWorkerInfo.generateClientWorkerInfo());
}
}
Expand All @@ -106,7 +106,7 @@ public List<WorkerInfo> getWorkerInfoList() {
public long getCapacityBytes() {
long ret = 0;
synchronized (mWorkers) {
for (MasterWorkerInfo worker : mWorkers.all()) {
for (MasterWorkerInfo worker : mWorkers) {
ret += worker.getCapacityBytes();
}
}
Expand All @@ -116,7 +116,7 @@ public long getCapacityBytes() {
public long getUsedBytes() {
long ret = 0;
synchronized (mWorkers) {
for (MasterWorkerInfo worker : mWorkers.all()) {
for (MasterWorkerInfo worker : mWorkers) {
ret += worker.getUsedBytes();
}
}
Expand Down
Expand Up @@ -133,7 +133,7 @@ public synchronized Inode getChild(String name) {
* @return an unmodifiable set of the children inodes.
*/
public synchronized Set<Inode> getChildren() {
return ImmutableSet.copyOf(mChildren.all());
return ImmutableSet.copyOf(mChildren.iterator());
}

/**
Expand All @@ -142,10 +142,9 @@ public synchronized Set<Inode> getChildren() {
* @return the ids of the children
*/
public synchronized List<Long> getChildrenIds() {
Set<Inode> children = mChildren.all();
List<Long> ret = new ArrayList<Long>(children.size());
for (Inode inode : children) {
ret.add(inode.getId());
List<Long> ret = new ArrayList<Long>(mChildren.size());
for (Inode child : mChildren) {
ret.add(child.getId());
}
return ret;
}
Expand Down Expand Up @@ -182,7 +181,7 @@ public synchronized boolean removeChild(String name) {
@Override
public String toString() {
StringBuilder sb = new StringBuilder("InodeFolder(");
sb.append(super.toString()).append(",").append(mChildren.all()).append(")");
sb.append(super.toString()).append(",").append(getChildren()).append(")");
return sb.toString();
}
}
Expand Up @@ -110,7 +110,7 @@ public void getTest() {

@Test
public void removeTest() {
Pair toRemove = mSet.all().iterator().next();
Pair toRemove = mSet.getFirstByField(mLongIndex, 1L);
Assert.assertEquals(3, mSet.getByField(mLongIndex, toRemove.longValue()).size());
Assert.assertEquals(9, mSet.size());
Assert.assertTrue(mSet.remove(toRemove));
Expand Down

0 comments on commit 5e853d6

Please sign in to comment.