Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ public ContainerAttribute() {
}

/**
* Insert or update the value in the Attribute map.
* Insert the value in the Attribute map, keep the original value if it exists
* already.
*
* @param key - The key to the set where the ContainerID should exist.
* @param value - Actual Container ID.
Expand All @@ -93,30 +94,8 @@ public ContainerAttribute() {
public boolean insert(T key, ContainerID value) throws SCMException {
Preconditions.checkNotNull(key);
Preconditions.checkNotNull(value);

if (attributeMap.containsKey(key)) {
if (attributeMap.get(key).add(value)) {
return true; //we inserted the value as it doesn’t exist in the set.
} else { // Failure indicates that this ContainerID exists in the Set
if (!attributeMap.get(key).remove(value)) {
LOG.error("Failure to remove the object from the Map.Key:{}, " +
"ContainerID: {}", key, value);
throw new SCMException("Failure to remove the object from the Map",
FAILED_TO_CHANGE_CONTAINER_STATE);
}
attributeMap.get(key).add(value);
return true;
}
} else {
// This key does not exist, we need to allocate this key in the map.
// TODO: Replace TreeSet with FoldedTreeSet from HDFS Utils.
// Skipping for now, since FoldedTreeSet does not have implementations
// for headSet and TailSet. We need those calls.
this.attributeMap.put(key, new TreeSet<>());
// This should not fail, we just allocated this object.
attributeMap.get(key).add(value);
return true;
}
attributeMap.computeIfAbsent(key, any -> new TreeSet<>()).add(value);
return true;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testInsert() throws SCMException {
containerAttribute.getCollection(1).size());
Assert.assertTrue(containerAttribute.getCollection(1).contains(id));

// Insert again and verify that it overwrites an existing value.
// Insert again and verify that the new ContainerId is inserted.
ContainerID newId =
new ContainerID(42);
containerAttribute.insert(1, newId);
Expand Down